Skip to content

Instantly share code, notes, and snippets.

@aadnk
Created October 24, 2013 01:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aadnk/7129577 to your computer and use it in GitHub Desktop.
Save aadnk/7129577 to your computer and use it in GitHub Desktop.
Display a fake pumkin helmet for all users. DEPRECIATED: Use https://gist.github.com/aadnk/11323369 instead.
package com.comphenix.example;
import java.lang.reflect.InvocationTargetException;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
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;
public class FakePumpinHelmets extends JavaPlugin {
// http://wiki.vg/Protocol#Entity_Equipment_.280x05.29
public static final int SLOT_HELD = 0;
public static final int SLOT_BOOTS = 1;
public static final int SLOT_LEGGINGS = 2;
public static final int SLOT_CHESTPLATE = 3;
public static final int SLOT_HELMET = 4;
@Override
public void onEnable() {
ProtocolLibrary.getProtocolManager().addPacketListener(
new PacketAdapter(this, ConnectionSide.SERVER_SIDE,
Packets.Server.ENTITY_EQUIPMENT, Packets.Server.NAMED_ENTITY_SPAWN) {
@Override
public void onPacketSending(PacketEvent event) {
PacketContainer packet = event.getPacket();
// The entity that is being displayed on the player's screen
Entity visibleEntity = packet.getEntityModifier(event).read(0);
// The player that is receiving the armor information
Player observingPlayer = event.getPlayer();
switch (event.getPacketID()) {
case Packets.Server.ENTITY_EQUIPMENT :
// Determine if this is a helmet slot
if (packet.getIntegers().read(1) == SLOT_HELMET) {
packet.getItemModifier().write(0, new ItemStack(Material.PUMPKIN, 1));
}
break;
// Entity equipment is not sent when the helm slot is empty, so we have to help it along
case Packets.Server.NAMED_ENTITY_SPAWN:
// Update the head slot too
LivingEntity living = (LivingEntity) visibleEntity;
updateEquipmentSlot(observingPlayer, living, SLOT_HELMET, living.getEquipment().getHelmet());
break;
default:
throw new IllegalArgumentException("Unknown packet ID:" + event.getPacketID());
}
}
});
}
private void updateEquipmentSlot(final Player observer, LivingEntity entity, int slot, ItemStack content) {
final PacketContainer equipmentPacket = new PacketContainer(Packets.Server.ENTITY_EQUIPMENT);
equipmentPacket.getIntegers().
write(0, entity.getEntityId()).
write(1, slot);
equipmentPacket.getItemModifier().write(0, content);
// We have to send the packet AFTer named entity spawn has been sent
getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
@Override
public void run() {
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, equipmentPacket);
} catch (InvocationTargetException e) {
throw new RuntimeException("Unable to update slot.", e);
}
}
});
}
}
@SupaHam
Copy link

SupaHam commented Nov 25, 2013

How come this only works on entities that spawn with armor

@aadnk
Copy link
Author

aadnk commented Apr 26, 2014

@SupaHam: You're supposed to call updateEquipmentSlot() with the slot you want to modify.

But I think I'll just rewrite this example with a better API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment