Last active
November 7, 2015 17:31
-
-
Save dmulloy2/5d52ddbb89a1609dbea2 to your computer and use it in GitHub Desktop.
Code to remove enchantments sent to other clients
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
/** | |
* (c) 2015 dmulloy2 | |
*/ | |
package net.dmulloy2.enchanthider; | |
import java.util.Collections; | |
import java.util.Set; | |
import net.dmulloy2.enchanthider.packets.WrapperPlayServerEntityEquipment; | |
import org.bukkit.Material; | |
import org.bukkit.enchantments.Enchantment; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.inventory.meta.BookMeta; | |
import org.bukkit.inventory.meta.FireworkEffectMeta; | |
import org.bukkit.inventory.meta.FireworkMeta; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import com.comphenix.protocol.PacketType; | |
import com.comphenix.protocol.ProtocolLibrary; | |
import com.comphenix.protocol.ProtocolManager; | |
import com.comphenix.protocol.events.ListenerPriority; | |
import com.comphenix.protocol.events.PacketAdapter; | |
import com.comphenix.protocol.events.PacketEvent; | |
/** | |
* @author dmulloy2 | |
*/ | |
public class Protocol extends JavaPlugin { | |
private ProtocolManager manager; | |
@Override | |
public void onEnable() { | |
manager = ProtocolLibrary.getProtocolManager(); | |
manager.addPacketListener(new PacketAdapter(PacketAdapter.params() | |
.plugin(this).types(PacketType.Play.Server.ENTITY_EQUIPMENT) | |
.listenerPriority(ListenerPriority.NORMAL)) { | |
@Override | |
public void onPacketSending(PacketEvent event) { | |
try { | |
WrapperPlayServerEntityEquipment packet = new WrapperPlayServerEntityEquipment(event.getPacket()); | |
if (packet.getItem() != null && ! event.getPlayer().equals(packet.getEntity(event))) { // Make sure it's not the player | |
ItemStack item = packet.getItem(); | |
// Reset potion type | |
if (item.getType() == Material.POTION) { | |
item.setDurability((short) 0); | |
} | |
// Remove the enchantments | |
Set<Enchantment> enchants = item.getEnchantments().keySet(); | |
for (Enchantment ench : enchants.toArray(new Enchantment[enchants.size()])) { // Avoid CME's | |
item.removeEnchantment(ench); | |
} | |
// Remove meta | |
if (item.hasItemMeta()) { | |
ItemMeta meta = item.getItemMeta(); | |
meta.setLore(null); | |
meta.setDisplayName(null); | |
if (meta instanceof BookMeta) { | |
BookMeta bookMeta = (BookMeta) meta; | |
bookMeta.setTitle(null); | |
bookMeta.setPages(Collections.emptyList()); | |
bookMeta.setAuthor(null); | |
} else if (meta instanceof FireworkEffectMeta) { | |
((FireworkEffectMeta) meta).setEffect(null); | |
} else if (meta instanceof FireworkMeta) { | |
FireworkMeta fireworkMeta = (FireworkMeta) meta; | |
fireworkMeta.clearEffects(); | |
fireworkMeta.setPower(0); | |
} | |
item.setItemMeta(meta); | |
} | |
item.setAmount(1); | |
packet.setItem(item); | |
} | |
} catch (Throwable ex) { // Should catch any CME's | |
System.err.println("Encountered an exception onPacketSending for ENTITY_EQUIPMENT:"); | |
ex.printStackTrace(); | |
} | |
} | |
}); | |
} | |
@Override | |
public void onDisable() { | |
manager.removePacketListeners(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment