Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package net.minecraft.server;
2
3import java.io.DataInputStream;
4import java.io.DataOutputStream;
5import java.io.IOException;
6import java.util.*;
7
8public class DataWatcher {
9
10 private boolean d = true;
11 private static final HashMap a = new HashMap();
12 private final Map b = new HashMap();
13 private boolean c;
14
15 public DataWatcher() {
16 }
17
18 public void a(int i, Object object) {
19 Integer integer = (Integer) a.get(object.getClass());
20
21 if (integer == null) {
22 throw new IllegalArgumentException("Unknown data type: " + object.getClass());
23 } else if (i > 31) {
24 throw new IllegalArgumentException("Data value id is too big with " + i + "! (Max is " + 31 + ")");
25 } else if (this.b.containsKey(Integer.valueOf(i))) {
26 throw new IllegalArgumentException("Duplicate id value for " + i + "!");
27 } else {
28 WatchableObject watchableobject = new WatchableObject(integer.intValue(), i, object);
29
30 this.b.put(Integer.valueOf(i), watchableobject);
31 this.d = false;
32 }
33 }
34
35 public byte a(int i) {
36 return ((Byte) ((WatchableObject) this.b.get(Integer.valueOf(i))).b()).byteValue();
37 }
38
39 public int b(int i) {
40 return ((Integer) ((WatchableObject) this.b.get(Integer.valueOf(i))).b()).intValue();
41 }
42
43 public String c(int i) {
44 return (String) ((WatchableObject) this.b.get(Integer.valueOf(i))).b();
45 }
46
47 public void watch(int i, Object object) {
48 WatchableObject watchableobject = (WatchableObject) this.b.get(Integer.valueOf(i));
49
50 if (!object.equals(watchableobject.b())) {
51 watchableobject.a(object);
52 watchableobject.a(true);
53 this.c = true;
54 }
55 }
56
57 public boolean a() {
58 return this.c;
59 }
60
61 public static void a(List list, DataOutputStream dataoutputstream) throws IOException {
62 if (list != null) {
63 Iterator iterator = list.iterator();
64
65 while (iterator.hasNext()) {
66 WatchableObject watchableobject = (WatchableObject) iterator.next();
67
68 a(dataoutputstream, watchableobject);
69 }
70 }
71
72 dataoutputstream.writeByte(127);
73 }
74
75 public ArrayList b() {
76 ArrayList arraylist = null;
77
78 if (this.c) {
79 Iterator iterator = this.b.values().iterator();
80
81 while (iterator.hasNext()) {
82 WatchableObject watchableobject = (WatchableObject) iterator.next();
83
84 if (watchableobject.d()) {
85 watchableobject.a(false);
86 if (arraylist == null) {
87 arraylist = new ArrayList();
88 }
89
90 arraylist.add(watchableobject);
91 }
92 }
93 }
94
95 this.c = false;
96 return arraylist;
97 }
98
99 public void a(DataOutputStream dataoutputstream) throws IOException {
100 Iterator iterator = this.b.values().iterator();
101
102 while (iterator.hasNext()) {
103 WatchableObject watchableobject = (WatchableObject) iterator.next();
104
105 a(dataoutputstream, watchableobject);
106 }
107
108 dataoutputstream.writeByte(127);
109 }
110
111 private static void a(DataOutputStream dataoutputstream, WatchableObject watchableobject) throws IOException {
112 int i = (watchableobject.c() << 5 | watchableobject.a() & 31) & 255;
113
114 dataoutputstream.writeByte(i);
115 switch (watchableobject.c()) {
116 case 0:
117 dataoutputstream.writeByte(((Byte) watchableobject.b()).byteValue());
118 break;
119
120 case 1:
121 dataoutputstream.writeShort(((Short) watchableobject.b()).shortValue());
122 break;
123
124 case 2:
125 dataoutputstream.writeInt(((Integer) watchableobject.b()).intValue());
126 break;
127
128 case 3:
129 dataoutputstream.writeFloat(((Float) watchableobject.b()).floatValue());
130 break;
131
132 case 4:
133 Packet.a((String) watchableobject.b(), dataoutputstream);
134 break;
135
136 case 5:
137 ItemStack itemstack = (ItemStack) watchableobject.b();
138
139 dataoutputstream.writeShort(itemstack.getItem().id);
140 dataoutputstream.writeByte(itemstack.count);
141 dataoutputstream.writeShort(itemstack.getData());
142 break;
143
144 case 6:
145 ChunkCoordinates chunkcoordinates = (ChunkCoordinates) watchableobject.b();
146
147 dataoutputstream.writeInt(chunkcoordinates.x);
148 dataoutputstream.writeInt(chunkcoordinates.y);
149 dataoutputstream.writeInt(chunkcoordinates.z);
150 }
151 }
152
153 public static List a(DataInputStream datainputstream) throws IOException {
154 ArrayList arraylist = null;
155
156 for (byte b0 = datainputstream.readByte(); b0 != 127; b0 = datainputstream.readByte()) {
157 if (arraylist == null) {
158 arraylist = new ArrayList();
159 }
160
161 int i = (b0 & 224) >> 5;
162 int j = b0 & 31;
163 WatchableObject watchableobject = null;
164
165 switch (i) {
166 case 0:
167 watchableobject = new WatchableObject(i, j, Byte.valueOf(datainputstream.readByte()));
168 break;
169
170 case 1:
171 watchableobject = new WatchableObject(i, j, Short.valueOf(datainputstream.readShort()));
172 break;
173
174 case 2:
175 watchableobject = new WatchableObject(i, j, Integer.valueOf(datainputstream.readInt()));
176 break;
177
178 case 3:
179 watchableobject = new WatchableObject(i, j, Float.valueOf(datainputstream.readFloat()));
180 break;
181
182 case 4:
183 watchableobject = new WatchableObject(i, j, Packet.a(datainputstream, 64));
184 break;
185
186 case 5:
187 short short1 = datainputstream.readShort();
188 byte b1 = datainputstream.readByte();
189 short short2 = datainputstream.readShort();
190
191 watchableobject = new WatchableObject(i, j, new ItemStack(short1, b1, short2));
192 break;
193
194 case 6:
195 int k = datainputstream.readInt();
196 int l = datainputstream.readInt();
197 int i1 = datainputstream.readInt();
198
199 watchableobject = new WatchableObject(i, j, new ChunkCoordinates(k, l, i1));
200 }
201
202 arraylist.add(watchableobject);
203 }
204
205 return arraylist;
206 }
207
208 public boolean getD() {
209 return this.d;
210 }
211
212 static {
213 a.put(Byte.class, Integer.valueOf(0));
214 a.put(Short.class, Integer.valueOf(1));
215 a.put(Integer.class, Integer.valueOf(2));
216 a.put(Float.class, Integer.valueOf(3));
217 a.put(String.class, Integer.valueOf(4));
218 a.put(ItemStack.class, Integer.valueOf(5));
219 a.put(ChunkCoordinates.class, Integer.valueOf(6));
220 }
221}