Implement FakeArmorStand

This commit is contained in:
Flowsqy 2022-12-31 03:52:39 +01:00
parent 9089988836
commit 0412eecec0

View File

@ -1,44 +1,33 @@
package de.epiceric.shopchest.nms.v1_19_R2;
import de.epiceric.shopchest.nms.FakeArmorStand;
import io.netty.buffer.Unpooled;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.chat.ComponentSerializer;
import net.minecraft.network.FriendlyByteBuf;
import de.epiceric.shopchest.nms.NMSComponent;
import de.epiceric.shopchest.nms.PacketQueue;
import de.epiceric.shopchest.nms.ReflectionUtils;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.decoration.ArmorStand;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class FakeArmorStandImpl extends FakeEntityImpl<String> implements FakeArmorStand {
public class FakeArmorStandImpl extends FakeEntityImpl implements FakeArmorStand {
private final static byte INVISIBLE_FLAG = 0b100000;
private final static byte MARKER_FLAG = 0b10000;
private final static EntityDataAccessor<Byte> DATA_SHARED_FLAGS_ID;
private final static EntityDataAccessor<Optional<Component>> DATA_CUSTOM_NAME;
private final static EntityDataAccessor<Boolean> DATA_CUSTOM_NAME_VISIBLE;
private final static float MARKER_ARMOR_STAND_OFFSET = 1.975f;
static {
try {
final Field dataSharedFlagsId = Entity.class.getDeclaredField("Z"); // DATA_SHARED_FLAGS_ID
dataSharedFlagsId.setAccessible(true);
DATA_SHARED_FLAGS_ID = forceCast(dataSharedFlagsId.get(null));
final Field dataCustomNameField = Entity.class.getDeclaredField("aM"); // DATA_CUSTOM_NAME
dataCustomNameField.setAccessible(true);
DATA_CUSTOM_NAME = forceCast(dataCustomNameField.get(null));
final Field dataCustomNameVisibleField = Entity.class.getDeclaredField("aN"); // DATA_CUSTOM_NAME_VISIBLE
dataCustomNameVisibleField.setAccessible(true);
DATA_CUSTOM_NAME_VISIBLE = forceCast(dataCustomNameVisibleField.get(null));
DATA_SHARED_FLAGS_ID = ReflectionUtils.forceCast(ReflectionUtils.getPrivateStaticFieldValue(Entity.class, "Z"));
DATA_CUSTOM_NAME = ReflectionUtils.forceCast(ReflectionUtils.getPrivateStaticFieldValue(Entity.class, "aM"));
DATA_CUSTOM_NAME_VISIBLE = ReflectionUtils.forceCast(ReflectionUtils.getPrivateStaticFieldValue(Entity.class, "aN"));
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
@ -49,46 +38,19 @@ public class FakeArmorStandImpl extends FakeEntityImpl<String> implements FakeAr
}
@Override
public void sendData(String name, Iterable<Player> receivers) {
sendData(receivers, name);
public void metadata(PacketQueue packetQueue, NMSComponent customName) {
final List<SynchedEntityData.DataValue<?>> addProperties = Arrays.asList(
SynchedEntityData.DataValue.create(DATA_SHARED_FLAGS_ID, INVISIBLE_FLAG),
// TODO Handle customName properly
SynchedEntityData.DataValue.create(DATA_CUSTOM_NAME, Optional.ofNullable(null)),
SynchedEntityData.DataValue.create(DATA_CUSTOM_NAME_VISIBLE, true),
SynchedEntityData.DataValue.create(ArmorStand.DATA_CLIENT_FLAGS, MARKER_FLAG)
);
super.metadata(packetQueue, addProperties);
}
@Override
protected EntityType<?> getEntityType() {
return EntityType.ARMOR_STAND;
}
@Override
protected float getSpawnOffSet() {
return MARKER_ARMOR_STAND_OFFSET;
}
@Override
protected void addSpecificData(List<SynchedEntityData.DataValue<?>> packedItems, String name) {
packedItems.add(SynchedEntityData.DataValue.create(DATA_SHARED_FLAGS_ID, INVISIBLE_FLAG));
packedItems.add(SynchedEntityData.DataValue.create(DATA_CUSTOM_NAME, Optional.ofNullable(
Component.Serializer.fromJson(
ComponentSerializer.toString(
TextComponent.fromLegacyText(name)
)
)
)));
packedItems.add(SynchedEntityData.DataValue.create(DATA_CUSTOM_NAME_VISIBLE, true));
packedItems.add(SynchedEntityData.DataValue.create(ArmorStand.DATA_CLIENT_FLAGS, MARKER_FLAG));
}
@Override
public void setLocation(Location location, Iterable<Player> receivers) {
final FriendlyByteBuf buffer = new FriendlyByteBuf(Unpooled.buffer());
buffer.writeVarInt(entityId);
buffer.writeDouble(location.getX());
buffer.writeDouble(location.getY() + MARKER_ARMOR_STAND_OFFSET);
buffer.writeDouble(location.getZ());
buffer.writeByte(0);
buffer.writeByte(0);
buffer.writeBoolean(false);
final ClientboundTeleportEntityPacket positionPacket = new ClientboundTeleportEntityPacket(buffer);
sendPacket(positionPacket, receivers);
}
}