From 8bb397b90efe67b04676c0fc427f26f72aa6f1d2 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Thu, 13 Sep 2012 20:47:21 +0200 Subject: [PATCH] Adding the ItemDisguise example program. --- ItemDisguise/.classpath | 8 ++ ItemDisguise/.project | 17 +++ .../.settings/org.eclipse.jdt.core.prefs | 11 ++ .../HideEnchantmentsListener.java | 102 ++++++++++++++++++ .../itemdisguise/ItemDisguiseMod.java | 82 ++++++++++++++ ItemDisguise/src/plugin.yml | 14 +++ .../comphenix/protocol/ProtocolManager.java | 2 - .../protocol/reflect/VolatileField.java | 1 - 8 files changed, 234 insertions(+), 3 deletions(-) create mode 100644 ItemDisguise/.classpath create mode 100644 ItemDisguise/.project create mode 100644 ItemDisguise/.settings/org.eclipse.jdt.core.prefs create mode 100644 ItemDisguise/src/com/comphenix/itemdisguise/HideEnchantmentsListener.java create mode 100644 ItemDisguise/src/com/comphenix/itemdisguise/ItemDisguiseMod.java create mode 100644 ItemDisguise/src/plugin.yml diff --git a/ItemDisguise/.classpath b/ItemDisguise/.classpath new file mode 100644 index 00000000..b4cf4f7f --- /dev/null +++ b/ItemDisguise/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/ItemDisguise/.project b/ItemDisguise/.project new file mode 100644 index 00000000..513103b3 --- /dev/null +++ b/ItemDisguise/.project @@ -0,0 +1,17 @@ + + + ItemDisguise + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/ItemDisguise/.settings/org.eclipse.jdt.core.prefs b/ItemDisguise/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000..838bd9d6 --- /dev/null +++ b/ItemDisguise/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/ItemDisguise/src/com/comphenix/itemdisguise/HideEnchantmentsListener.java b/ItemDisguise/src/com/comphenix/itemdisguise/HideEnchantmentsListener.java new file mode 100644 index 00000000..fe8cd54b --- /dev/null +++ b/ItemDisguise/src/com/comphenix/itemdisguise/HideEnchantmentsListener.java @@ -0,0 +1,102 @@ +package com.comphenix.itemdisguise; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.bukkit.Server; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.java.JavaPlugin; + +import com.comphenix.protocol.ProtocolManager; +import com.comphenix.protocol.events.ConnectionSide; +import com.comphenix.protocol.events.PacketAdapter; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.events.PacketEvent; +import com.comphenix.protocol.reflect.FieldAccessException; + +public class HideEnchantmentsListener { + + private final Server server; + private final Logger logger; + + public HideEnchantmentsListener(Server server, Logger logger) { + this.server = server; + this.logger = logger; + } + + public void addListener(ProtocolManager protocolManager, JavaPlugin plugin) { + + // Hide all enchantments + protocolManager.addPacketListener(new PacketAdapter(plugin, ConnectionSide.SERVER_SIDE, 0x67, 0x68, 0x3E) { + @Override + public void onPacketSending(PacketEvent event) { + PacketContainer packet = event.getPacket(); + + try { + // Item packets + switch (event.getPacketID()) { + case 0x67: // Set slot + removeEnchantments(packet.getItemModifier().read(0)); + break; + + case 0x68: // Set Window Items + ItemStack[] elements = packet.getItemArrayModifier().read(0); + + for (int i = 0; i < elements.length; i++) { + if (elements[i] != null) { + removeEnchantments(elements[i]); + } + } + break; + + case 0x3E: // Sound effect + event.setCancelled(true); + break; + } + + } catch (FieldAccessException e) { + logger.log(Level.SEVERE, "Couldn't access field.", e); + } + } + }); + + // Censor + protocolManager.addPacketListener(new PacketAdapter(plugin, ConnectionSide.CLIENT_SIDE, 0x3) { + @Override + public void onPacketReceiving(PacketEvent event) { + if (event.getPacketID() == 0x3) { + try { + String message = event.getPacket(). + getSpecificModifier(String.class).read(0); + + if (message.contains("shit") || message.contains("fuck")) { + event.setCancelled(true); + event.getPlayer().sendMessage("Bad manners!"); + } + + } catch (FieldAccessException e) { + logger.log(Level.SEVERE, "Couldn't access field.", e); + } + } + } + }); + } + + public Server getServer() { + return server; + } + + public void removeListener(ProtocolManager protocolManager, JavaPlugin plugin) { + // Just remove every adapter with this plugin + protocolManager.removePacketListeners(plugin); + } + + private void removeEnchantments(ItemStack stack) { + Object[] copy = stack.getEnchantments().keySet().toArray(); + + for (Object enchantment : copy) { + stack.removeEnchantment((Enchantment) enchantment); + } + } +} diff --git a/ItemDisguise/src/com/comphenix/itemdisguise/ItemDisguiseMod.java b/ItemDisguise/src/com/comphenix/itemdisguise/ItemDisguiseMod.java new file mode 100644 index 00000000..3aee175b --- /dev/null +++ b/ItemDisguise/src/com/comphenix/itemdisguise/ItemDisguiseMod.java @@ -0,0 +1,82 @@ +package com.comphenix.itemdisguise; + +import java.util.logging.Logger; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; + +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.ProtocolManager; +import com.comphenix.protocol.events.PacketContainer; + +public class ItemDisguiseMod extends JavaPlugin { + + private ProtocolManager protocolManager; + private Logger logger; + + private HideEnchantmentsListener enchantmentsListener; + + @Override + public void onEnable() { + + logger = getLoggerSafely(); + protocolManager = ProtocolLibrary.getProtocolManager(); + + enchantmentsListener = new HideEnchantmentsListener(getServer(), logger); + enchantmentsListener.addListener(protocolManager, this); + } + + @Override + public void onDisable() { + enchantmentsListener.removeListener(protocolManager, this); + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (sender instanceof Player) { + + Player player = (Player) sender; + + if (label.equalsIgnoreCase("explosion")) { + PacketContainer fakeExplosion = protocolManager.createPacket(0x3C); + + // Set the coordinates + try { + fakeExplosion.getSpecificModifier(double.class). + write(0, player.getLocation().getX()). + write(1, player.getLocation().getY()). + write(2, player.getLocation().getZ()); + fakeExplosion.getSpecificModifier(float.class). + write(0, 3.0F); + + protocolManager.sendServerPacket(player, fakeExplosion); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + return true; + } + + return false; + } + + // Get the Bukkit logger first, before we try to create our own + private Logger getLoggerSafely() { + + Logger log = null; + + try { + log = getLogger(); + } catch (Throwable e) { + // We'll handle it + } + + if (log == null) + log = Logger.getLogger("Minecraft"); + return log; + } +} diff --git a/ItemDisguise/src/plugin.yml b/ItemDisguise/src/plugin.yml new file mode 100644 index 00000000..a91f9720 --- /dev/null +++ b/ItemDisguise/src/plugin.yml @@ -0,0 +1,14 @@ +name: ItemDisguiseMod +version: 1.0.0 +description: Change the appearance of inventory items. +author: Comphenix +website: http://www.comphenix.net/ItemDisguise + +main: com.comphenix.itemdisguise.ItemDisguiseMod +depends: [ProtocolLib] +database: false + +commands: + explosion: + description: Creates a fake explosion around the caller. + usage: / \ No newline at end of file diff --git a/ProtocolLib/src/com/comphenix/protocol/ProtocolManager.java b/ProtocolLib/src/com/comphenix/protocol/ProtocolManager.java index 0f3c6f41..2f134292 100644 --- a/ProtocolLib/src/com/comphenix/protocol/ProtocolManager.java +++ b/ProtocolLib/src/com/comphenix/protocol/ProtocolManager.java @@ -36,8 +36,6 @@ public interface ProtocolManager { /** * Removes every listener associated with the given plugin. - *

- * Note that this only works for listeners that derive from PacketAdapter. * @param plugin - the plugin to unload. */ public void removePacketListeners(Plugin plugin); diff --git a/ProtocolLib/src/com/comphenix/protocol/reflect/VolatileField.java b/ProtocolLib/src/com/comphenix/protocol/reflect/VolatileField.java index b6259332..6b131a74 100644 --- a/ProtocolLib/src/com/comphenix/protocol/reflect/VolatileField.java +++ b/ProtocolLib/src/com/comphenix/protocol/reflect/VolatileField.java @@ -2,7 +2,6 @@ package com.comphenix.protocol.reflect; import java.lang.reflect.Field; - /** * Represents a field that will revert to its original state when this class is garbaged collected. *