Implement FakeItem

This commit is contained in:
Flowsqy 2022-12-31 03:58:14 +01:00
parent 0412eecec0
commit 9cde5e2e5d

View File

@ -1,6 +1,8 @@
package de.epiceric.shopchest.nms.v1_19_R2; package de.epiceric.shopchest.nms.v1_19_R2;
import de.epiceric.shopchest.nms.FakeItem; import de.epiceric.shopchest.nms.FakeItem;
import de.epiceric.shopchest.nms.PacketQueue;
import de.epiceric.shopchest.nms.ReflectionUtils;
import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket; import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket;
import net.minecraft.network.syncher.EntityDataAccessor; import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.network.syncher.SynchedEntityData;
@ -8,21 +10,18 @@ import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.Vec3;
import org.bukkit.craftbukkit.v1_19_R2.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_19_R2.inventory.CraftItemStack;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.lang.reflect.Field; import java.util.Collections;
import java.util.List; import java.util.List;
public class FakeItemImpl extends FakeEntityImpl<ItemStack> implements FakeItem { public class FakeItemImpl extends FakeEntityImpl implements FakeItem {
private final static EntityDataAccessor<net.minecraft.world.item.ItemStack> DATA_ITEM; private final static EntityDataAccessor<net.minecraft.world.item.ItemStack> DATA_ITEM;
static { static {
try { try {
final Field dataItemField = ItemEntity.class.getDeclaredField("c"); // DATA_ITEM DATA_ITEM = ReflectionUtils.forceCast(ReflectionUtils.getPrivateStaticFieldValue(ItemEntity.class, "c"));
dataItemField.setAccessible(true);
DATA_ITEM = forceCast(dataItemField.get(null));
} catch (ReflectiveOperationException e) { } catch (ReflectiveOperationException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -33,23 +32,21 @@ public class FakeItemImpl extends FakeEntityImpl<ItemStack> implements FakeItem
} }
@Override @Override
public void sendData(ItemStack item, Iterable<Player> receivers) { public void metadata(PacketQueue packetQueue, ItemStack item) {
sendData(receivers, item); final List<SynchedEntityData.DataValue<?>> addProperties = Collections.singletonList(
SynchedEntityData.DataValue.create(DATA_ITEM, CraftItemStack.asNMSCopy(item))
);
metadata(packetQueue, addProperties);
} }
@Override @Override
public void resetVelocity(Iterable<Player> receivers) { public void cancelVelocity(PacketQueue packetQueue) {
final ClientboundSetEntityMotionPacket velocityPacket = new ClientboundSetEntityMotionPacket(entityId, Vec3.ZERO); final ClientboundSetEntityMotionPacket velocityPacket = new ClientboundSetEntityMotionPacket(getEntityId(), Vec3.ZERO);
sendPacket(velocityPacket, receivers); ((PacketQueueImpl) packetQueue).register(velocityPacket);
} }
@Override @Override
protected EntityType<?> getEntityType() { protected EntityType<?> getEntityType() {
return EntityType.ITEM; return EntityType.ITEM;
} }
@Override
protected void addSpecificData(List<SynchedEntityData.DataValue<?>> packedItems, ItemStack data) {
packedItems.add(SynchedEntityData.DataValue.create(DATA_ITEM, CraftItemStack.asNMSCopy(data)));
}
} }