Skip to content

Instantly share code, notes, and snippets.

@aadnk
Created October 15, 2013 23:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aadnk/7000492 to your computer and use it in GitHub Desktop.
Save aadnk/7000492 to your computer and use it in GitHub Desktop.
Demonstrate the use of ItemRenamer's new API.
package com.comphenix.example;
import java.util.Arrays;
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.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.shininet.bukkit.itemrenamer.api.ItemsListener;
import org.shininet.bukkit.itemrenamer.api.RenamerAPI;
import org.shininet.bukkit.itemrenamer.api.RenamerPriority;
import org.shininet.bukkit.itemrenamer.api.RenamerSnapshot;
import com.comphenix.protocol.wrappers.nbt.NbtCompound;
import com.comphenix.protocol.wrappers.nbt.NbtFactory;
// This requires that your plugin depends on both ItemRenamer and ProtocolLib
public class GlowPlugin extends JavaPlugin {
private static final String EXAMPLE_MOD_GLOW = "[ExampleMod]glow";
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
// Create the specially marked axe
ItemStack stack = new ItemStack(Material.IRON_AXE, 1);
ItemMeta meta = stack.getItemMeta();
meta.setLore(Arrays.asList(EXAMPLE_MOD_GLOW));
stack.setItemMeta(meta);
player.getInventory().addItem(stack);
}
return true;
}
@Override
public void onEnable() {
RenamerAPI.getAPI().addListener(this, RenamerPriority.POST_NORMAL, new ItemsListener() {
@Override
public void onItemsSending(Player player, RenamerSnapshot snapshot) {
addGlow(snapshot);
}
});
}
private void addGlow(RenamerSnapshot stacks) {
for (ItemStack stack : stacks) {
// Only update those stacks that have our flag lore
if (stack != null && stack.hasItemMeta()) {
List<String> lore = stack.getItemMeta().getLore();
if (Arrays.asList(EXAMPLE_MOD_GLOW).equals(lore)) {
NbtCompound compound = (NbtCompound) NbtFactory.fromItemTag(stack);
compound.put(NbtFactory.ofList("ench"));
compound.getCompound("display").remove("Lore");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment