Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import java.util.Arrays;
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.Plugin;
public enum ItemSet {
TEST(Material.QUARTZ, ChatColor.GREEN + "Test",
Arrays.asList(ChatColor.GRAY + "This is a test",
ChatColor.GRAY + "Lorem ipsum dolor sit amet, consectetuer adipiscing elit"),
false, new EventCallback() {
@Override
public void call(PlayerInteractEvent event, Plugin plugin) {
Player player = event.getPlayer();
}
});
private final ItemStack item;
private final boolean interactable;
private final EventCallback eventCallback;
private ItemSet(ItemStack item, boolean interactable, EventCallback eventCallback) {
this.item = item;
this.interactable = interactable;
this.eventCallback = eventCallback;
}
private ItemSet(Material material, String name, List<String> lore, boolean interactable, EventCallback eventCallback) {
item = new ItemStack(material);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(name);
meta.setLore(lore);
item.setItemMeta(meta);
this.interactable = interactable;
this.eventCallback = eventCallback;
}
public ItemStack getItemStack() {
return item;
}
public boolean isInteractable() {
return interactable;
}
public EventCallback getEventCallback() {
return eventCallback;
}
public static abstract class EventCallback {
public abstract void call(PlayerInteractEvent event, Plugin plugin);
}
private static ItemListener listener;
public static void startListening(Plugin plugin) {
listener = new ItemListener(plugin);
plugin.getServer().getPluginManager().registerEvents(listener, plugin);
}
public static class ItemListener implements Listener {
private final Plugin plugin;
public ItemListener(Plugin plugin) {
this.plugin = plugin;
}
@EventHandler
public void onInteract(PlayerInteractEvent event) {
ItemStack item = event.getItem();
for (ItemSet set : values()) {
if (set.getItemStack().equals(item)) {
if (set.getEventCallback() != null) {
set.getEventCallback().call(event, plugin);
}
if (set.isInteractable()) return;
event.setCancelled(true);
break;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.