public
Created

Set a different armor color per observer.

  • Download Gist
ExampleMod.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
package com.comphenix.example;
 
import org.bukkit.Color;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.plugin.java.JavaPlugin;
 
import com.comphenix.protocol.Packets;
import com.comphenix.protocol.ProtocolLibrary;
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;
 
public class ExampleMod extends JavaPlugin implements Listener {
private ProtocolManager manager;
public void onLoad() {
manager = ProtocolLibrary.getProtocolManager();
}
@Override
public void onEnable() {
manager.addPacketListener(new PacketAdapter(this, ConnectionSide.SERVER_SIDE,
Packets.Server.ENTITY_EQUIPMENT) {
@Override
public void onPacketSending(PacketEvent event) {
PacketContainer packet = event.getPacket();
 
ItemStack stack = packet.getItemModifier().read(0);
 
// Only modify leather armor
if (stack != null && stack.getType().name().contains("LEATHER")) {
 
// The problem turned out to be that certain Minecraft functions update
// every player with the same packet for an equipment, whereas other
// methods update the equipment with a different packet per player.
 
// To fix this, we'll simply clone the packet before we modify it
packet = event.getPacket().deepClone();
event.setPacket(packet);
stack = packet.getItemModifier().read(0);
 
// Color that depends on the player's name
String recieverName = event.getPlayer().getName();
int color = recieverName.hashCode() & 0xFFFFFF;
 
// Update the color
LeatherArmorMeta meta = (LeatherArmorMeta) stack.getItemMeta();
meta.setColor(Color.fromBGR(color));
stack.setItemMeta(meta);
}
}
});
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.