Skip to content

Commit

Permalink
Adapt position packet to the new position packet architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
Flowsqy committed Oct 28, 2024
1 parent 94e548d commit 8da0491
Showing 1 changed file with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package de.epiceric.shopchest.nms.v1_21_R2;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Optional;

import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_21_R2.util.CraftChatMessage;
import org.bukkit.entity.Player;

import de.epiceric.shopchest.nms.FakeArmorStand;
import io.netty.buffer.Unpooled;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundTeleportEntityPacket;
import net.minecraft.network.protocol.game.ClientboundEntityPositionSyncPacket;
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.PositionMoveRotation;
import net.minecraft.world.entity.decoration.ArmorStand;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_21_R2.util.CraftChatMessage;
import org.bukkit.entity.Player;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Optional;
import net.minecraft.world.phys.Vec3;

public class FakeArmorStandImpl extends FakeEntityImpl<String> implements FakeArmorStand {

Expand All @@ -35,7 +36,8 @@ public class FakeArmorStandImpl extends FakeEntityImpl<String> implements FakeAr
final Field dataCustomNameField = Entity.class.getDeclaredField(ObfuscatedFieldNames.DATA_CUSTOM_NAME);
dataCustomNameField.setAccessible(true);
DATA_CUSTOM_NAME = forceCast(dataCustomNameField.get(null));
final Field dataCustomNameVisibleField = Entity.class.getDeclaredField(ObfuscatedFieldNames.DATA_CUSTOM_NAME_VISIBLE);
final Field dataCustomNameVisibleField = Entity.class
.getDeclaredField(ObfuscatedFieldNames.DATA_CUSTOM_NAME_VISIBLE);
dataCustomNameVisibleField.setAccessible(true);
DATA_CUSTOM_NAME_VISIBLE = forceCast(dataCustomNameVisibleField.get(null));
} catch (ReflectiveOperationException e) {
Expand Down Expand Up @@ -71,23 +73,17 @@ protected int getDataItemCount() {
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(
CraftChatMessage.fromStringOrNull(name)
)));
CraftChatMessage.fromStringOrNull(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 = ClientboundTeleportEntityPacket.STREAM_CODEC.decode(buffer);
final Vec3 pos = new Vec3(location.getX(), location.getY() + MARKER_ARMOR_STAND_OFFSET, location.getZ());
final PositionMoveRotation positionMoveRotation = new PositionMoveRotation(pos, Vec3.ZERO, 0f, 0f);
final ClientboundEntityPositionSyncPacket positionPacket = new ClientboundEntityPositionSyncPacket(entityId,
positionMoveRotation, false);
sendPacket(positionPacket, receivers);
}

Expand Down

0 comments on commit 8da0491

Please sign in to comment.