Created
April 10, 2014 01:54
-
-
Save aadnk/10336564 to your computer and use it in GitHub Desktop.
Demonstrates different armor color per player.
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
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.PacketType; | |
import com.comphenix.protocol.ProtocolLibrary; | |
import com.comphenix.protocol.events.PacketAdapter; | |
import com.comphenix.protocol.events.PacketContainer; | |
import com.comphenix.protocol.events.PacketEvent; | |
public class DifferentArmorColor extends JavaPlugin implements Listener { | |
@Override | |
public void onEnable() { | |
ProtocolLibrary.getProtocolManager().addPacketListener( | |
new PacketAdapter(this, | |
PacketType.Play.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 | |
event.setPacket(packet = packet.deepClone()); | |
stack = packet.getItemModifier().read(0); | |
// Color that depends on the player's name | |
String recieverName = event.getPlayer().getName(); | |
int color = recieverName.hashCode() & 0xFFFFFF; | |
// Change the color | |
LeatherArmorMeta meta = (LeatherArmorMeta) stack.getItemMeta(); | |
meta.setColor(Color.fromBGR(color)); | |
stack.setItemMeta(meta); | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How would you do something such as make a player helmet appear as an TNT block but they'll actual have an iron helmet for themselves, it's just displayed as an TNT block for others.