
net.minecraft.server.DataWatcher Maven / Gradle / Ivy
package net.minecraft.server;
import com.google.common.collect.Lists;
import gnu.trove.map.hash.TIntObjectHashMap;
import org.apache.commons.lang3.ObjectUtils;
import walkmc.annotation.FromWalk;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class DataWatcher {
// Spigot Start
public static final gnu.trove.map.TObjectIntMap> classToId = new gnu.trove.map.hash.TObjectIntHashMap<>(10, 0.5f, -1);
// These exist as an attempt at backwards compatability for (broken) NMS plugins
public static final Map, Integer> c = gnu.trove.TDecorators.wrap(classToId);
static {
// Spigot Start - remove valueOf
classToId.put(Byte.class, 0);
classToId.put(Short.class, 1);
classToId.put(Integer.class, 2);
classToId.put(Float.class, 3);
classToId.put(String.class, 4);
classToId.put(ItemStack.class, 5);
classToId.put(BlockPosition.class, 6);
classToId.put(Vector3f.class, 7);
// Spigot End
}
public final Entity entity;
public gnu.trove.map.TIntObjectMap dataValues = new gnu.trove.map.hash.TIntObjectHashMap<>(10, 0.5f, -1);
public Map d = gnu.trove.TDecorators.wrap(dataValues);
public boolean b = true;
// Spigot End
public boolean e;
public final ReadWriteLock f = new ReentrantReadWriteLock();
public DataWatcher(Entity entity) {
this.entity = entity;
}
@FromWalk
public DataWatcher copy() {
DataWatcher watcher = new DataWatcher(entity);
watcher.dataValues = new TIntObjectHashMap<>(dataValues);
watcher.d = new HashMap<>(d);
watcher.b = this.b;
return watcher;
}
public static void a(List list, PacketDataSerializer serializer) throws IOException {
if (list != null) {
for (WatchableObject object : list) {
a(serializer, object);
}
}
serializer.writeByte(127);
}
private static void a(PacketDataSerializer serializer, DataWatcher.WatchableObject object) {
int i = (object.c() << 5 | object.a() & 31) & 255;
serializer.writeByte(i);
switch (object.c()) {
case 0:
serializer.writeByte((Byte) object.b());
break;
case 1:
serializer.writeShort((Short) object.b());
break;
case 2:
serializer.writeInt((Integer) object.b());
break;
case 3:
serializer.writeFloat((Float) object.b());
break;
case 4:
serializer.a((String) object.b());
break;
case 5:
ItemStack itemstack = (ItemStack) object.b();
serializer.a(itemstack);
break;
case 6:
BlockPosition blockposition = (BlockPosition) object.b();
serializer.writeInt(blockposition.getX());
serializer.writeInt(blockposition.getY());
serializer.writeInt(blockposition.getZ());
break;
case 7:
Vector3f vector3f = (Vector3f) object.b();
serializer.writeFloat(vector3f.getX());
serializer.writeFloat(vector3f.getY());
serializer.writeFloat(vector3f.getZ());
}
}
public static List b(PacketDataSerializer serializer) throws IOException {
ArrayList arraylist = null;
for (byte b0 = serializer.readByte(); b0 != 127; b0 = serializer.readByte()) {
if (arraylist == null) {
arraylist = Lists.newArrayList();
}
int i = (b0 & 224) >> 5;
int j = b0 & 31;
DataWatcher.WatchableObject object = null;
switch (i) {
case 0:
object = new DataWatcher.WatchableObject(i, j, serializer.readByte());
break;
case 1:
object = new DataWatcher.WatchableObject(i, j, serializer.readShort());
break;
case 2:
object = new DataWatcher.WatchableObject(i, j, serializer.readInt());
break;
case 3:
object = new DataWatcher.WatchableObject(i, j, serializer.readFloat());
break;
case 4:
object = new DataWatcher.WatchableObject(i, j, serializer.c(32767));
break;
case 5:
object = new DataWatcher.WatchableObject(i, j, serializer.i());
break;
case 6:
int k = serializer.readInt();
int l = serializer.readInt();
int i1 = serializer.readInt();
object = new DataWatcher.WatchableObject(i, j, new BlockPosition(k, l, i1));
break;
case 7:
float f = serializer.readFloat();
float f1 = serializer.readFloat();
float f2 = serializer.readFloat();
object = new DataWatcher.WatchableObject(i, j, new Vector3f(f, f1, f2));
}
arraylist.add(object);
}
return arraylist;
}
public void a(int i, T t0) {
int integer = classToId.get(t0.getClass()); // Spigot
if (integer == -1) { // Spigot
throw new IllegalArgumentException("Unknown data type: " + t0.getClass());
} else if (i > 31) {
throw new IllegalArgumentException("Data value id is too big with " + i + "! (Max is " + 31 + ")");
} else if (this.dataValues.containsKey(i)) { // Spigot
throw new IllegalArgumentException("Duplicate id value for " + i + "!");
} else {
DataWatcher.WatchableObject object = new DataWatcher.WatchableObject(integer, i, t0); // Spigot
this.f.writeLock().lock();
this.dataValues.put(i, object); // Spigot
this.f.writeLock().unlock();
this.b = false;
}
}
public void add(int i, int j) {
DataWatcher.WatchableObject object = new DataWatcher.WatchableObject(j, i, null);
this.f.writeLock().lock();
this.dataValues.put(i, object); // Spigot
this.f.writeLock().unlock();
this.b = false;
}
public byte getByte(int i) {
return (byte) this.j(i).b();
}
public short getShort(int i) {
return (short) this.j(i).b();
}
public int getInt(int i) {
return (int) this.j(i).b();
}
public float getFloat(int i) {
return (float) this.j(i).b();
}
public String getString(int i) {
return (String) this.j(i).b();
}
public ItemStack getItemStack(int i) {
return (ItemStack) this.j(i).b();
}
public DataWatcher.WatchableObject get(int index) {
return j(index);
}
private DataWatcher.WatchableObject j(int i) {
this.f.readLock().lock();
DataWatcher.WatchableObject object;
try {
object = this.dataValues.get(i); // Spigot
} catch (Throwable throwable) {
CrashReport crashreport = CrashReport.a(throwable, "Getting synched entity data");
CrashReportSystemDetails crashreportsystemdetails = crashreport.a("Synched entity data");
crashreportsystemdetails.a("Data ID", i);
throw new ReportedException(crashreport);
}
this.f.readLock().unlock();
return object;
}
public Vector3f h(int i) {
return (Vector3f) this.j(i).b();
}
public void watch(int i, T t0) {
DataWatcher.WatchableObject object = this.j(i);
if (ObjectUtils.notEqual(t0, object.b())) {
object.a(t0);
this.entity.onDataWatcherUpdate(i);
object.a(true);
this.e = true;
}
}
public void update(int i) {
this.j(i).d = true;
this.e = true;
}
public boolean a() {
return this.e;
}
public List b() {
ArrayList arraylist = null;
if (this.e) {
this.f.readLock().lock();
// Spigot
for (WatchableObject o : this.dataValues.valueCollection()) {
WatchableObject object = o;
if (object.d()) {
object.a(false);
if (arraylist == null) {
arraylist = Lists.newArrayList();
}
// Spigot start - copy ItemStacks to prevent ConcurrentModificationExceptions
if (object.b() instanceof ItemStack) {
object = new WatchableObject(
object.c(),
object.a(),
((ItemStack) object.b()).cloneItemStack()
);
}
// Spigot end
arraylist.add(object);
}
}
this.f.readLock().unlock();
}
this.e = false;
return arraylist;
}
public void a(PacketDataSerializer packetdataserializer) throws IOException {
this.f.readLock().lock();
// Spigot
for (WatchableObject o : this.dataValues.valueCollection()) {
a(packetdataserializer, o);
}
this.f.readLock().unlock();
packetdataserializer.writeByte(127);
}
public List c() {
ArrayList arraylist = Lists.newArrayList(); // Spigot
this.f.readLock().lock();
arraylist.addAll(this.dataValues.valueCollection()); // Spigot
// Spigot start - copy ItemStacks to prevent ConcurrentModificationExceptions
for (int i = 0; i < arraylist.size(); i++) {
WatchableObject watchableobject = arraylist.get(i);
if (watchableobject.b() instanceof ItemStack) {
watchableobject = new WatchableObject(
watchableobject.c(),
watchableobject.a(),
((ItemStack) watchableobject.b()).cloneItemStack()
);
arraylist.set(i, watchableobject);
}
}
// Spigot end
this.f.readLock().unlock();
return arraylist;
}
public boolean d() {
return this.b;
}
public void e() {
this.e = false;
}
public static class WatchableObject {
private final int a;
private final int b;
private Object c;
private boolean d;
public WatchableObject(int i, int j, Object object) {
this.b = j;
this.c = object;
this.a = i;
this.d = true;
}
public WatchableObject(int id, Object value) {
this.b = id;
this.c = value;
this.a = classToId.get(value.getClass());
this.d = true;
}
public int a() {
return this.b;
}
public void a(Object object) {
this.c = object;
}
public Object b() {
return this.c;
}
public int c() {
return this.a;
}
public boolean d() {
return this.d;
}
public void a(boolean flag) {
this.d = flag;
}
public Object getValue() {
return c;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy