Skip to content

Instantly share code, notes, and snippets.

@aadnk
Created July 17, 2013 23:47
Show Gist options
  • Save aadnk/6025583 to your computer and use it in GitHub Desktop.
Save aadnk/6025583 to your computer and use it in GitHub Desktop.
Make attributes work in creative mode.
package com.comphenix.example;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.List;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCreativeEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import com.comphenix.example.Attributes.Attribute;
import com.comphenix.example.Attributes.AttributeType;
import com.comphenix.protocol.Packets;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.ConnectionSide;
import com.comphenix.protocol.events.ListenerOptions;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.utility.StreamSerializer;
import com.comphenix.protocol.wrappers.nbt.NbtCompound;
import com.comphenix.protocol.wrappers.nbt.NbtFactory;
public class AttributesExample extends JavaPlugin implements Listener {
private ItemStack expectedCursor;
private long expectedTime;
@Override
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
ProtocolLibrary.getProtocolManager().addPacketListener(
new PacketAdapter(
this, ConnectionSide.CLIENT_SIDE, new ListenerOptions[] { ListenerOptions.INTERCEPT_INPUT_BUFFER }, Packets.Client.SET_CREATIVE_SLOT) {
@Override
public void onPacketReceiving(PacketEvent event) {
DataInputStream input = event.getNetworkMarker().getInputStream();
try {
// Read slot
input.readShort();
ItemStack stack = readItemStack(input, new StreamSerializer());
// Undo the stupid "item meta" cleanup
event.getPacket().getItemModifier().write(0, stack);
// Update the next event handler
expectedTime = System.currentTimeMillis();
expectedCursor = stack;
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
@EventHandler(priority = EventPriority.LOWEST)
public void onCreativeInventory(InventoryCreativeEvent e) {
if ((System.currentTimeMillis() - expectedTime) < 50) {
e.setCursor(expectedCursor);
}
}
private ItemStack readItemStack(DataInputStream input, StreamSerializer serializer) throws IOException {
ItemStack result = null;
short type = input.readShort();
if (type >= 0) {
byte amount = input.readByte();
short damage = input.readShort();
result = new ItemStack(type, amount, damage);
NbtCompound tag = serializer.deserializeCompound(input);
if (tag != null) {
result = MinecraftReflection.getBukkitItemStack(result);
NbtFactory.setItemTag(result, tag);
}
}
return result;
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
ItemStack helmet = new ItemStack(Material.GOLD_HELMET);
Attributes attributes = new Attributes(helmet);
attributes.add(Attribute.newBuilder().name("Health")
.type(AttributeType.GENERIC_MAX_HEALTH).amount(20).build());
attributes.add(Attribute.newBuilder().name("Speed")
.type(AttributeType.GENERIC_MOVEMENT_SPEED).amount(2).build());
((Player) sender).getInventory().addItem(attributes.getStack());
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment