Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Client-side item
public class ClientsideItem extends ClientsideEntity {
private final ClientsideHologram hitbox;
private final List<PacketContainer> packets = new ArrayList<>();
public ClientsideItem(Location location, ItemStack item) {
hitbox = new ClientsideHologram(location, "", true);
setup(item, location);
}
private void setup(ItemStack item, Location location) {
packets.add(createSpawnPacket(location));
packets.add(createMetadataPacket(item));
}
private PacketContainer createSpawnPacket(Location location) {
WrapperPlayServerSpawnEntity packet = new WrapperPlayServerSpawnEntity();
packet.setEntityID(getEntityId());
packet.setX(location.getX());
packet.setY(location.getY());
packet.setZ(location.getZ());
packet.setObjectData(0);
packet.setOptionalSpeedX(0);
packet.setOptionalSpeedY(0);
packet.setOptionalSpeedZ(0);
packet.setUniqueId(UUID.randomUUID());
packet.getHandle().getEntityTypeModifier().writeSafely(0, EntityType.DROPPED_ITEM);
return packet.getHandle();
}
private PacketContainer createMetadataPacket(ItemStack item) {
WrapperPlayServerEntityMetadata wrapper = new WrapperPlayServerEntityMetadata();
wrapper.setEntityID(getEntityId());
String name = item.getItemMeta().getDisplayName();
EasyMetadataPacket metadata = new EasyMetadataPacket(null);
metadata.write(0, (byte) 0x00);
metadata.write(1, 300);
if (name.isEmpty())
metadata.writeEmptyData(2, WrappedChatComponent.fromText(""));
else
metadata.writeOptional(2, WrappedChatComponent.fromText(name));
metadata.write(3, !name.isEmpty());
metadata.write(4, Boolean.FALSE);
metadata.write(5, Boolean.TRUE);
metadata.write(7, item);
wrapper.setMetadata(metadata.export());
return wrapper.getHandle();
}
@Override
public void onClick(Consumer<ClickData> consumer) {
hitbox.onClick(consumer);
}
public void show(Player player) {
hitbox.show(player);
for (PacketContainer packet : packets) {
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment