DataWatcher .java

net.minecraft.entity.DataWatcher (class): members: readWatchedListFromPacketBuffer, writeWatchedListToPacketBuffer, updateWatchedObjectsFromList, writeTo.

net.minecraft.entityclass451 linescommon14.2 KB

API Surface (26)

<T> void addObject(int id, T object)
void addObjectByDataType(int id, int type)
byte getWatchableObjectByte(int id)
short getWatchableObjectShort(int id)
int getWatchableObjectInt(int id)
float getWatchableObjectFloat(int id)
String getWatchableObjectString(int id)
ItemStack getWatchableObjectItemStack(int id)
Rotations getWatchableObjectRotations(int id)
<T> void updateObject(int id, T newData)
void setObjectWatched(int id)
boolean hasObjectChanged()
void writeWatchedListToPacketBuffer(List<DataWatcher.WatchableObject> objectsList, PacketBuffer buffer)
List<DataWatcher.WatchableObject> getChanged()
void writeTo(PacketBuffer buffer)
List<DataWatcher.WatchableObject> getAllWatched()
List<DataWatcher.WatchableObject> readWatchedListFromPacketBuffer(PacketBuffer buffer)
void updateWatchedObjectsFromList(List<DataWatcher.WatchableObject> p_75687_1_)
boolean getIsBlank()
void func_111144_e()
int getDataValueId()
void setObject(Object object)
Object getObject()
int getObjectType()
boolean isWatched()
void setWatched(boolean watched)

Fields & Constants (6)

dataTypes
owner
isBlank
watchedObjects
objectChanged
lock

String literals (7)

"Unknown data type: "
"Data value id is too big with "
"! (Max is "
"Duplicate id value for "
"Getting synched entity data"
"Synched entity data"
"Data ID"

Full Source

DataWatcher.java451 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.entity;
2
3
import com.google.common.collect.Lists;
4
import com.google.common.collect.Maps;
5
import java.io.IOException;
6
import java.util.List;
7
import java.util.Map;
8
import java.util.concurrent.locks.ReadWriteLock;
9
import java.util.concurrent.locks.ReentrantReadWriteLock;
10
import net.minecraft.crash.CrashReport;
11
import net.minecraft.crash.CrashReportCategory;
12
import net.minecraft.item.ItemStack;
13
import net.minecraft.network.PacketBuffer;
14
import net.minecraft.util.BlockPos;
15
import net.minecraft.util.ReportedException;
16
import net.minecraft.util.Rotations;
17
import org.apache.commons.lang3.ObjectUtils;
18
19
public class DataWatcher
20
{
21
    private final Entity owner;
22
23
    /** When isBlank is true the DataWatcher is not watching any objects */
24
    private boolean isBlank = true;
25
    private static final Map < Class<?>, Integer > dataTypes = Maps. < Class<?>, Integer > newHashMap();
26
    private final Map<Integer, DataWatcher.WatchableObject> watchedObjects = Maps.<Integer, DataWatcher.WatchableObject>newHashMap();
27
28
    /** true if one or more object was changed */
29
    private boolean objectChanged;
30
    private ReadWriteLock lock = new ReentrantReadWriteLock();
31
32
    public DataWatcher(Entity owner)
33
    {
34
        this.owner = owner;
35
    }
36
37
    public <T> void addObject(int id, T object)
38
    {
39
        Integer integer = (Integer)dataTypes.get(object.getClass());
40
41
        if (integer == null)
42
        {
43
            throw new IllegalArgumentException("Unknown data type: " + object.getClass());
44
        }
45
        else if (id > 31)
46
        {
47
            throw new IllegalArgumentException("Data value id is too big with " + id + "! (Max is " + 31 + ")");
48
        }
49
        else if (this.watchedObjects.containsKey(Integer.valueOf(id)))
50
        {
51
            throw new IllegalArgumentException("Duplicate id value for " + id + "!");
52
        }
53
        else
54
        {
55
            DataWatcher.WatchableObject datawatcher$watchableobject = new DataWatcher.WatchableObject(integer.intValue(), id, object);
56
            this.lock.writeLock().lock();
57
            this.watchedObjects.put(Integer.valueOf(id), datawatcher$watchableobject);
58
            this.lock.writeLock().unlock();
59
            this.isBlank = false;
60
        }
61
    }
62
63
    /**
64
     * Add a new object for the DataWatcher to watch, using the specified data type.
65
     */
66
    public void addObjectByDataType(int id, int type)
67
    {
68
        DataWatcher.WatchableObject datawatcher$watchableobject = new DataWatcher.WatchableObject(type, id, (Object)null);
69
        this.lock.writeLock().lock();
70
        this.watchedObjects.put(Integer.valueOf(id), datawatcher$watchableobject);
71
        this.lock.writeLock().unlock();
72
        this.isBlank = false;
73
    }
74
75
    /**
76
     * gets the bytevalue of a watchable object
77
     */
78
    public byte getWatchableObjectByte(int id)
79
    {
80
        return ((Byte)this.getWatchedObject(id).getObject()).byteValue();
81
    }
82
83
    public short getWatchableObjectShort(int id)
84
    {
85
        return ((Short)this.getWatchedObject(id).getObject()).shortValue();
86
    }
87
88
    /**
89
     * gets a watchable object and returns it as a Integer
90
     */
91
    public int getWatchableObjectInt(int id)
92
    {
93
        return ((Integer)this.getWatchedObject(id).getObject()).intValue();
94
    }
95
96
    public float getWatchableObjectFloat(int id)
97
    {
98
        return ((Float)this.getWatchedObject(id).getObject()).floatValue();
99
    }
100
101
    /**
102
     * gets a watchable object and returns it as a String
103
     */
104
    public String getWatchableObjectString(int id)
105
    {
106
        return (String)this.getWatchedObject(id).getObject();
107
    }
108
109
    /**
110
     * Get a watchable object as an ItemStack.
111
     */
112
    public ItemStack getWatchableObjectItemStack(int id)
113
    {
114
        return (ItemStack)this.getWatchedObject(id).getObject();
115
    }
116
117
    /**
118
     * is threadsafe, unless it throws an exception, then
119
     */
120
    private DataWatcher.WatchableObject getWatchedObject(int id)
121
    {
122
        this.lock.readLock().lock();
123
        DataWatcher.WatchableObject datawatcher$watchableobject;
124
125
        try
126
        {
127
            datawatcher$watchableobject = (DataWatcher.WatchableObject)this.watchedObjects.get(Integer.valueOf(id));
128
        }
129
        catch (Throwable throwable)
130
        {
131
            CrashReport crashreport = CrashReport.makeCrashReport(throwable, "Getting synched entity data");
132
            CrashReportCategory crashreportcategory = crashreport.makeCategory("Synched entity data");
133
            crashreportcategory.addCrashSection("Data ID", Integer.valueOf(id));
134
            throw new ReportedException(crashreport);
135
        }
136
137
        this.lock.readLock().unlock();
138
        return datawatcher$watchableobject;
139
    }
140
141
    public Rotations getWatchableObjectRotations(int id)
142
    {
143
        return (Rotations)this.getWatchedObject(id).getObject();
144
    }
145
146
    public <T> void updateObject(int id, T newData)
147
    {
148
        DataWatcher.WatchableObject datawatcher$watchableobject = this.getWatchedObject(id);
149
150
        if (ObjectUtils.notEqual(newData, datawatcher$watchableobject.getObject()))
151
        {
152
            datawatcher$watchableobject.setObject(newData);
153
            this.owner.onDataWatcherUpdate(id);
154
            datawatcher$watchableobject.setWatched(true);
155
            this.objectChanged = true;
156
        }
157
    }
158
159
    public void setObjectWatched(int id)
160
    {
161
        this.getWatchedObject(id).watched = true;
162
        this.objectChanged = true;
163
    }
164
165
    /**
166
     * true if one or more object was changed
167
     */
168
    public boolean hasObjectChanged()
169
    {
170
        return this.objectChanged;
171
    }
172
173
    /**
174
     * Writes the list of watched objects (entity attribute of type {byte, short, int, float, string, ItemStack,
175
     * ChunkCoordinates}) to the specified PacketBuffer
176
     */
177
    public static void writeWatchedListToPacketBuffer(List<DataWatcher.WatchableObject> objectsList, PacketBuffer buffer) throws IOException
178
    {
179
        if (objectsList != null)
180
        {
181
            for (DataWatcher.WatchableObject datawatcher$watchableobject : objectsList)
182
            {
183
                writeWatchableObjectToPacketBuffer(buffer, datawatcher$watchableobject);
184
            }
185
        }
186
187
        buffer.writeByte(127);
188
    }
189
190
    public List<DataWatcher.WatchableObject> getChanged()
191
    {
192
        List<DataWatcher.WatchableObject> list = null;
193
194
        if (this.objectChanged)
195
        {
196
            this.lock.readLock().lock();
197
198
            for (DataWatcher.WatchableObject datawatcher$watchableobject : this.watchedObjects.values())
199
            {
200
                if (datawatcher$watchableobject.isWatched())
201
                {
202
                    datawatcher$watchableobject.setWatched(false);
203
204
                    if (list == null)
205
                    {
206
                        list = Lists.<DataWatcher.WatchableObject>newArrayList();
207
                    }
208
209
                    list.add(datawatcher$watchableobject);
210
                }
211
            }
212
213
            this.lock.readLock().unlock();
214
        }
215
216
        this.objectChanged = false;
217
        return list;
218
    }
219
220
    public void writeTo(PacketBuffer buffer) throws IOException
221
    {
222
        this.lock.readLock().lock();
223
224
        for (DataWatcher.WatchableObject datawatcher$watchableobject : this.watchedObjects.values())
225
        {
226
            writeWatchableObjectToPacketBuffer(buffer, datawatcher$watchableobject);
227
        }
228
229
        this.lock.readLock().unlock();
230
        buffer.writeByte(127);
231
    }
232
233
    public List<DataWatcher.WatchableObject> getAllWatched()
234
    {
235
        List<DataWatcher.WatchableObject> list = null;
236
        this.lock.readLock().lock();
237
238
        for (DataWatcher.WatchableObject datawatcher$watchableobject : this.watchedObjects.values())
239
        {
240
            if (list == null)
241
            {
242
                list = Lists.<DataWatcher.WatchableObject>newArrayList();
243
            }
244
245
            list.add(datawatcher$watchableobject);
246
        }
247
248
        this.lock.readLock().unlock();
249
        return list;
250
    }
251
252
    /**
253
     * Writes a watchable object (entity attribute of type {byte, short, int, float, string, ItemStack,
254
     * ChunkCoordinates}) to the specified PacketBuffer
255
     */
256
    private static void writeWatchableObjectToPacketBuffer(PacketBuffer buffer, DataWatcher.WatchableObject object) throws IOException
257
    {
258
        int i = (object.getObjectType() << 5 | object.getDataValueId() & 31) & 255;
259
        buffer.writeByte(i);
260
261
        switch (object.getObjectType())
262
        {
263
            case 0:
264
                buffer.writeByte(((Byte)object.getObject()).byteValue());
265
                break;
266
267
            case 1:
268
                buffer.writeShort(((Short)object.getObject()).shortValue());
269
                break;
270
271
            case 2:
272
                buffer.writeInt(((Integer)object.getObject()).intValue());
273
                break;
274
275
            case 3:
276
                buffer.writeFloat(((Float)object.getObject()).floatValue());
277
                break;
278
279
            case 4:
280
                buffer.writeString((String)object.getObject());
281
                break;
282
283
            case 5:
284
                ItemStack itemstack = (ItemStack)object.getObject();
285
                buffer.writeItemStackToBuffer(itemstack);
286
                break;
287
288
            case 6:
289
                BlockPos blockpos = (BlockPos)object.getObject();
290
                buffer.writeInt(blockpos.getX());
291
                buffer.writeInt(blockpos.getY());
292
                buffer.writeInt(blockpos.getZ());
293
                break;
294
295
            case 7:
296
                Rotations rotations = (Rotations)object.getObject();
297
                buffer.writeFloat(rotations.getX());
298
                buffer.writeFloat(rotations.getY());
299
                buffer.writeFloat(rotations.getZ());
300
        }
301
    }
302
303
    public static List<DataWatcher.WatchableObject> readWatchedListFromPacketBuffer(PacketBuffer buffer) throws IOException
304
    {
305
        List<DataWatcher.WatchableObject> list = null;
306
307
        for (int i = buffer.readByte(); i != 127; i = buffer.readByte())
308
        {
309
            if (list == null)
310
            {
311
                list = Lists.<DataWatcher.WatchableObject>newArrayList();
312
            }
313
314
            int j = (i & 224) >> 5;
315
            int k = i & 31;
316
            DataWatcher.WatchableObject datawatcher$watchableobject = null;
317
318
            switch (j)
319
            {
320
                case 0:
321
                    datawatcher$watchableobject = new DataWatcher.WatchableObject(j, k, Byte.valueOf(buffer.readByte()));
322
                    break;
323
324
                case 1:
325
                    datawatcher$watchableobject = new DataWatcher.WatchableObject(j, k, Short.valueOf(buffer.readShort()));
326
                    break;
327
328
                case 2:
329
                    datawatcher$watchableobject = new DataWatcher.WatchableObject(j, k, Integer.valueOf(buffer.readInt()));
330
                    break;
331
332
                case 3:
333
                    datawatcher$watchableobject = new DataWatcher.WatchableObject(j, k, Float.valueOf(buffer.readFloat()));
334
                    break;
335
336
                case 4:
337
                    datawatcher$watchableobject = new DataWatcher.WatchableObject(j, k, buffer.readStringFromBuffer(32767));
338
                    break;
339
340
                case 5:
341
                    datawatcher$watchableobject = new DataWatcher.WatchableObject(j, k, buffer.readItemStackFromBuffer());
342
                    break;
343
344
                case 6:
345
                    int l = buffer.readInt();
346
                    int i1 = buffer.readInt();
347
                    int j1 = buffer.readInt();
348
                    datawatcher$watchableobject = new DataWatcher.WatchableObject(j, k, new BlockPos(l, i1, j1));
349
                    break;
350
351
                case 7:
352
                    float f = buffer.readFloat();
353
                    float f1 = buffer.readFloat();
354
                    float f2 = buffer.readFloat();
355
                    datawatcher$watchableobject = new DataWatcher.WatchableObject(j, k, new Rotations(f, f1, f2));
356
            }
357
358
            list.add(datawatcher$watchableobject);
359
        }
360
361
        return list;
362
    }
363
364
    public void updateWatchedObjectsFromList(List<DataWatcher.WatchableObject> p_75687_1_)
365
    {
366
        this.lock.writeLock().lock();
367
368
        for (DataWatcher.WatchableObject datawatcher$watchableobject : p_75687_1_)
369
        {
370
            DataWatcher.WatchableObject datawatcher$watchableobject1 = (DataWatcher.WatchableObject)this.watchedObjects.get(Integer.valueOf(datawatcher$watchableobject.getDataValueId()));
371
372
            if (datawatcher$watchableobject1 != null)
373
            {
374
                datawatcher$watchableobject1.setObject(datawatcher$watchableobject.getObject());
375
                this.owner.onDataWatcherUpdate(datawatcher$watchableobject.getDataValueId());
376
            }
377
        }
378
379
        this.lock.writeLock().unlock();
380
        this.objectChanged = true;
381
    }
382
383
    public boolean getIsBlank()
384
    {
385
        return this.isBlank;
386
    }
387
388
    public void func_111144_e()
389
    {
390
        this.objectChanged = false;
391
    }
392
393
    static
394
    {
395
        dataTypes.put(Byte.class, Integer.valueOf(0));
396
        dataTypes.put(Short.class, Integer.valueOf(1));
397
        dataTypes.put(Integer.class, Integer.valueOf(2));
398
        dataTypes.put(Float.class, Integer.valueOf(3));
399
        dataTypes.put(String.class, Integer.valueOf(4));
400
        dataTypes.put(ItemStack.class, Integer.valueOf(5));
401
        dataTypes.put(BlockPos.class, Integer.valueOf(6));
402
        dataTypes.put(Rotations.class, Integer.valueOf(7));
403
    }
404
405
    public static class WatchableObject
406
    {
407
        private final int objectType;
408
        private final int dataValueId;
409
        private Object watchedObject;
410
        private boolean watched;
411
412
        public WatchableObject(int type, int id, Object object)
413
        {
414
            this.dataValueId = id;
415
            this.watchedObject = object;
416
            this.objectType = type;
417
            this.watched = true;
418
        }
419
420
        public int getDataValueId()
421
        {
422
            return this.dataValueId;
423
        }
424
425
        public void setObject(Object object)
426
        {
427
            this.watchedObject = object;
428
        }
429
430
        public Object getObject()
431
        {
432
            return this.watchedObject;
433
        }
434
435
        public int getObjectType()
436
        {
437
            return this.objectType;
438
        }
439
440
        public boolean isWatched()
441
        {
442
            return this.watched;
443
        }
444
445
        public void setWatched(boolean watched)
446
        {
447
            this.watched = watched;
448
        }
449
    }
450
}
451

Deobfuscated MCP 1.8.9 source of DataWatcher from Marcelektro/MavenMCP-1.8.9 (net/minecraft/entity/DataWatcher.java), stored verbatim.