|
| 1 | +package de.epiceric.shopchest.nms.v1_21_R1; |
| 2 | + |
| 3 | +import de.epiceric.shopchest.nms.FakeEntity; |
| 4 | +import net.minecraft.network.protocol.Packet; |
| 5 | +import net.minecraft.network.protocol.game.ClientboundAddEntityPacket; |
| 6 | +import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket; |
| 7 | +import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket; |
| 8 | +import net.minecraft.network.syncher.EntityDataAccessor; |
| 9 | +import net.minecraft.network.syncher.SynchedEntityData; |
| 10 | +import net.minecraft.world.entity.Entity; |
| 11 | +import net.minecraft.world.entity.EntityType; |
| 12 | +import net.minecraft.world.phys.Vec3; |
| 13 | +import org.bukkit.Location; |
| 14 | +import org.bukkit.craftbukkit.v1_21_R1.entity.CraftPlayer; |
| 15 | +import org.bukkit.entity.Player; |
| 16 | + |
| 17 | +import java.lang.reflect.Field; |
| 18 | +import java.util.LinkedList; |
| 19 | +import java.util.List; |
| 20 | +import java.util.UUID; |
| 21 | +import java.util.concurrent.atomic.AtomicInteger; |
| 22 | + |
| 23 | +public abstract class FakeEntityImpl<T> implements FakeEntity { |
| 24 | + |
| 25 | + private final static AtomicInteger ENTITY_COUNTER; |
| 26 | + private final static EntityDataAccessor<Boolean> DATA_NO_GRAVITY; |
| 27 | + private final static EntityDataAccessor<Boolean> DATA_SILENT; |
| 28 | + |
| 29 | + static { |
| 30 | + try { |
| 31 | + final Field entityCounterField = Entity.class.getDeclaredField(ObfuscatedFieldNames.ENTITY_COUNTER); |
| 32 | + entityCounterField.setAccessible(true); |
| 33 | + ENTITY_COUNTER = (AtomicInteger) entityCounterField.get(null); |
| 34 | + final Field dataNoGravityField = Entity.class.getDeclaredField(ObfuscatedFieldNames.DATA_NO_GRAVITY); |
| 35 | + dataNoGravityField.setAccessible(true); |
| 36 | + DATA_NO_GRAVITY = forceCast(dataNoGravityField.get(null)); |
| 37 | + final Field dataSilentField = Entity.class.getDeclaredField(ObfuscatedFieldNames.DATA_SILENT); |
| 38 | + dataSilentField.setAccessible(true); |
| 39 | + DATA_SILENT = forceCast(dataSilentField.get(null)); |
| 40 | + } catch (ReflectiveOperationException e) { |
| 41 | + throw new RuntimeException(e); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + protected final int entityId; |
| 46 | + |
| 47 | + public FakeEntityImpl() { |
| 48 | + entityId = ENTITY_COUNTER.incrementAndGet(); |
| 49 | + } |
| 50 | + |
| 51 | + @SuppressWarnings("unchecked") |
| 52 | + protected static <T> T forceCast(Object o) { |
| 53 | + return (T) o; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public int getEntityId() { |
| 58 | + return entityId; |
| 59 | + } |
| 60 | + |
| 61 | + protected void sendPacket(Packet<?> packet, Iterable<Player> receivers) { |
| 62 | + for (Player receiver : receivers) { |
| 63 | + ((CraftPlayer) receiver).getHandle().connection.send(packet); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void spawn(UUID uuid, Location location, Iterable<Player> receivers) { |
| 69 | + final ClientboundAddEntityPacket spawnPacket = new ClientboundAddEntityPacket( |
| 70 | + entityId, |
| 71 | + uuid, |
| 72 | + location.getX(), |
| 73 | + location.getY() + getSpawnOffSet(), |
| 74 | + location.getZ(), |
| 75 | + 0f, |
| 76 | + 0f, |
| 77 | + getEntityType(), |
| 78 | + 0, |
| 79 | + Vec3.ZERO, |
| 80 | + 0d |
| 81 | + ); |
| 82 | + sendPacket(spawnPacket, receivers); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void remove(Iterable<Player> receivers) { |
| 87 | + final ClientboundRemoveEntitiesPacket removePacket = new ClientboundRemoveEntitiesPacket(entityId); |
| 88 | + sendPacket(removePacket, receivers); |
| 89 | + } |
| 90 | + |
| 91 | + protected void sendData(Iterable<Player> receivers, T data) { |
| 92 | + // Create packet |
| 93 | + final List<SynchedEntityData.DataValue<?>> packedItems = new LinkedList<>(); |
| 94 | + final ClientboundSetEntityDataPacket dataPacket = new ClientboundSetEntityDataPacket(entityId, packedItems); |
| 95 | + |
| 96 | + // Setup data |
| 97 | + packedItems.add(SynchedEntityData.DataValue.create(DATA_NO_GRAVITY, true)); |
| 98 | + packedItems.add(SynchedEntityData.DataValue.create(DATA_SILENT, true)); |
| 99 | + addSpecificData(packedItems, data); |
| 100 | + |
| 101 | + // Send packet |
| 102 | + sendPacket(dataPacket, receivers); |
| 103 | + } |
| 104 | + |
| 105 | + protected abstract EntityType<?> getEntityType(); |
| 106 | + |
| 107 | + protected float getSpawnOffSet() { |
| 108 | + return 0f; |
| 109 | + } |
| 110 | + |
| 111 | + protected abstract int getDataItemCount(); |
| 112 | + |
| 113 | + protected abstract void addSpecificData(List<SynchedEntityData.DataValue<?>> packedItems, T data); |
| 114 | + |
| 115 | +} |
0 commit comments