Skip to content

Instantly share code, notes, and snippets.

@IllusionTheDev
Created July 5, 2021 23:02
Show Gist options
  • Save IllusionTheDev/5fc9a4e45d67c646081ca0af1ddc0e73 to your computer and use it in GitHub Desktop.
Save IllusionTheDev/5fc9a4e45d67c646081ca0af1ddc0e73 to your computer and use it in GitHub Desktop.
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