summaryrefslogtreecommitdiff
path: root/NET/worlds/network/ObjID.java
blob: d8ad62c15d023ab1f39b050e4f9a6cacdec08f0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package NET.worlds.network;

import java.io.IOException;

public class ObjID {
   private int _shortObjID;
   private String _longObjID;

   public ObjID(int id) {
      this._shortObjID = id;
      this._longObjID = null;
   }

   public ObjID(String id) {
      this._shortObjID = 0;
      if (id.startsWith("!")) {
         id = id.substring(1);
      }

      this._longObjID = id;
   }

   public ObjID() {
      this._shortObjID = 0;
      this._longObjID = null;
   }

   public int shortID() {
      return this._shortObjID;
   }

   public String longID() {
      return this._longObjID;
   }

   void parseNetData(ServerInputStream data) throws IOException {
      this._shortObjID = data.readUnsignedByte();
      if (this._shortObjID == 0) {
         this._longObjID = data.readUTF();
      }
   }

   int packetSize() {
      return this._longObjID != null ? 2 + ServerOutputStream.utfLength(this._longObjID) : 1;
   }

   void send(ServerOutputStream o) throws IOException {
      if (this._longObjID != null) {
         o.writeByte(0);
         o.writeUTF(this._longObjID);
      } else {
         assert this._shortObjID == 1 || this._shortObjID >= 253;

         o.writeByte(this._shortObjID);
      }
   }

   public String toString(WorldServer serv) {
      return this._longObjID != null ? this._longObjID : Integer.toString(this._shortObjID) + "[" + serv.getLongID(this) + "]";
   }

   @Override
   public String toString() {
      return this._longObjID != null ? this._longObjID : "[#" + Integer.toString(this._shortObjID) + "]";
   }
}