Created
November 11, 2013 21:38
-
-
Save aadnk/7420881 to your computer and use it in GitHub Desktop.
Modify the appearance of a dropped item stack on the client side.
This file contains hidden or 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
package com.comphenix.example; | |
import org.bukkit.Material; | |
import org.bukkit.entity.Item; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import com.comphenix.protocol.Packets; | |
import com.comphenix.protocol.ProtocolLibrary; | |
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.wrappers.WrappedDataWatcher; | |
public class ModifyDroppedItemStack extends JavaPlugin { | |
@Override | |
public void onEnable() { | |
ProtocolLibrary.getProtocolManager().addPacketListener( | |
new PacketAdapter(this, ConnectionSide.SERVER_SIDE, Packets.Server.ENTITY_METADATA) { | |
@Override | |
public void onPacketSending(PacketEvent event) { | |
PacketContainer packet = event.getPacket(); | |
// See if we are modifying an item stack | |
if (packet.getEntityModifier(event).read(0) instanceof Item) { | |
WrappedDataWatcher watcher = new WrappedDataWatcher(packet.getWatchableCollectionModifier().read(0)); | |
ItemStack stack = watcher.getItemStack(10); | |
if (stack != null) { | |
// You have to clone the watcher, unfortunately | |
watcher = watcher.deepClone(); | |
watcher.removeObject(10); // Not needed after build #148 | |
watcher.setObject(10, processItemStack(stack)); | |
// Save the modification | |
packet.getWatchableCollectionModifier().write(0, watcher.getWatchableObjects()); | |
} | |
} | |
} | |
}); | |
} | |
private ItemStack processItemStack(ItemStack stack) { | |
// Otherwise you'll modify the stack on the server | |
ItemStack cloned = stack.clone(); | |
cloned.setType(Material.STONE); | |
return cloned; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment