Created
August 24, 2021 13:50
-
-
Save WizardlyBump17/567c4acb0196dce197fc70975845a915 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ProtocolManager manager = ProtocolLibrary.getProtocolManager(); | |
manager.addPacketListener(new PacketAdapter(Bukkit.getPluginManager().getPlugin("WLib"), PacketType.fromClass(PacketPlayOutSetSlot.class), PacketType.fromClass(PacketPlayOutEntityMetadata.class), PacketType.fromClass(PacketPlayOutEntityEquipment.class)) { | |
@SneakyThrows | |
@Override | |
public void onPacketSending(PacketEvent event) { | |
final Object handle = event.getPacket().getHandle(); | |
if (handle instanceof PacketPlayOutSetSlot) | |
setSlot((PacketPlayOutSetSlot) handle); | |
if (handle instanceof PacketPlayOutEntityMetadata) | |
entityMetadata(event.getPacket(), (PacketPlayOutEntityMetadata) handle); | |
if (handle instanceof PacketPlayOutEntityEquipment) | |
entityEquipment((PacketPlayOutEntityEquipment) handle); | |
} | |
private boolean isValidItem(ItemStack itemStack) { | |
final net.minecraft.server.v1_16_R3.ItemStack copy = CraftItemStack.asNMSCopy(itemStack); | |
return copy.hasTag() && copy.getTag().hasKey("WLib-glowing"); | |
} | |
private ItemStack fixItem(ItemStack itemStack) { | |
final ItemMeta itemMeta = itemStack.getItemMeta(); | |
itemMeta.addEnchant(Enchantment.ARROW_INFINITE, 1, true); | |
itemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS); | |
itemStack.setItemMeta(itemMeta); | |
return itemStack; | |
} | |
@SneakyThrows | |
public void entityEquipment(PacketPlayOutEntityEquipment handle) { | |
final Field field = handle.getClass().getDeclaredField("b"); | |
field.setAccessible(true); | |
List<Pair<EquipmentSlot, net.minecraft.server.v1_16_R3.ItemStack>> items = (List<Pair<EquipmentSlot, net.minecraft.server.v1_16_R3.ItemStack>>) field.get(handle); | |
for (Pair<EquipmentSlot, net.minecraft.server.v1_16_R3.ItemStack> item : items) { | |
final CraftItemStack stack = CraftItemStack.asCraftMirror(item.getSecond()).clone(); | |
if (!isValidItem(stack)) | |
continue; | |
final Field second = item.getClass().getDeclaredField("second"); | |
second.setAccessible(true); | |
second.set(item, CraftItemStack.asNMSCopy(fixItem(stack))); | |
} | |
} | |
@SneakyThrows | |
private void entityMetadata(PacketContainer packet, PacketPlayOutEntityMetadata handle) { | |
final Entity entity = packet.getEntityModifier(player.getWorld()).read(0); | |
if (!(entity instanceof org.bukkit.entity.Item)) | |
return; | |
final ItemStack itemStack = ((org.bukkit.entity.Item) entity).getItemStack().clone(); | |
if (!isValidItem(itemStack)) | |
return; | |
final Field field = handle.getClass().getDeclaredField("b"); | |
field.setAccessible(true); | |
List<DataWatcher.Item<?>> items = new ArrayList<>((List<DataWatcher.Item<?>>) field.get(handle)); | |
final DataWatcher.Item<?> item; | |
int index; | |
if (items.size() == 8) | |
item = items.get(index = 6).d(); //new item | |
else | |
item = items.get(index = 0).d(); //item merge | |
final Field itemField = item.getClass().getDeclaredField("b"); | |
itemField.setAccessible(true); | |
itemField.set(item, CraftItemStack.asNMSCopy(fixItem(itemStack))); | |
items.set(index, item); | |
field.set(handle, items); | |
} | |
@SneakyThrows | |
private void setSlot(PacketPlayOutSetSlot packet) { | |
final Field field = packet.getClass().getDeclaredField("c"); | |
field.setAccessible(true); | |
final net.minecraft.server.v1_16_R3.ItemStack stack = ((net.minecraft.server.v1_16_R3.ItemStack) field.get(packet)).cloneItemStack(); | |
final CraftItemStack newItemStack = CraftItemStack.asCraftMirror(stack); | |
if (!isValidItem(newItemStack)) | |
return; | |
field.set(packet, CraftItemStack.asNMSCopy(fixItem(newItemStack))); | |
} | |
}); | |
@EventHandler | |
public void onClick(InventoryClickEvent event) { //for some reason that idk, when in creative and interacting with the fake item, it makes the fake item a real item in server-side, which we dont want | |
final ItemStack cursor = event.getCursor(); | |
if (cursor == null || cursor.getType() == Material.AIR) | |
return; | |
if (Item.fromItemStack(cursor).hasNbtTag("WLib-glowing")) { | |
cursor.removeEnchantment(Enchantment.ARROW_INFINITE); | |
final ItemMeta meta = cursor.getItemMeta(); | |
meta.removeItemFlags(ItemFlag.HIDE_ENCHANTS); | |
cursor.setItemMeta(meta); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment