Add FakeItem implementation

This commit is contained in:
Flowsqy 2021-12-29 16:34:05 +01:00
parent b37ae0fa9d
commit 278b4462f3
2 changed files with 28 additions and 2 deletions

View File

@ -7,4 +7,6 @@ public interface FakeItem extends FakeEntity{
void sendData(ItemStack item, Iterable<Player> receivers); void sendData(ItemStack item, Iterable<Player> receivers);
void resetVelocity(Iterable<Player> receivers);
} }

View File

@ -1,22 +1,46 @@
package de.epiceric.shopchest.nms.v1_17_R1; package de.epiceric.shopchest.nms.v1_17_R1;
import de.epiceric.shopchest.nms.FakeItem; import de.epiceric.shopchest.nms.FakeItem;
import net.minecraft.network.protocol.game.ClientboundSetEntityMotionPacket;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.phys.Vec3;
import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.lang.reflect.Field;
import java.util.List; import java.util.List;
public class FakeItemImpl extends FakeEntityImpl<ItemStack> implements FakeItem { public class FakeItemImpl extends FakeEntityImpl<ItemStack> implements FakeItem {
private final static EntityDataAccessor<net.minecraft.world.item.ItemStack> DATA_ITEM;
static {
try{
final Field dataItemField = ItemEntity.class.getDeclaredField("c"); // DATA_ITEM
dataItemField.setAccessible(true);
DATA_ITEM = forceCast(dataItemField.get(null));
}catch (ReflectiveOperationException e){
throw new RuntimeException(e);
}
}
public FakeItemImpl() { public FakeItemImpl() {
super(); super();
} }
@Override @Override
public void sendData(ItemStack item, Iterable<Player> receivers) { public void sendData(ItemStack item, Iterable<Player> receivers) {
sendData(receivers, item);
}
@Override
public void resetVelocity(Iterable<Player> receivers) {
final ClientboundSetEntityMotionPacket velocityPacket = new ClientboundSetEntityMotionPacket(entityId, Vec3.ZERO);
sendPacket(velocityPacket, receivers);
} }
@Override @Override
@ -26,11 +50,11 @@ public class FakeItemImpl extends FakeEntityImpl<ItemStack> implements FakeItem
@Override @Override
protected int getDataItemCount() { protected int getDataItemCount() {
return 0; return 1;
} }
@Override @Override
protected void addSpecificData(List<SynchedEntityData.DataItem<?>> packedItems, ItemStack data) { protected void addSpecificData(List<SynchedEntityData.DataItem<?>> packedItems, ItemStack data) {
packedItems.add(new SynchedEntityData.DataItem<>(DATA_ITEM, CraftItemStack.asNMSCopy(data)));
} }
} }