Created
December 20, 2012 12:12
-
-
Save Ribesg/4344963 to your computer and use it in GitHub Desktop.
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.github.bobacadodl.AterHelp; | |
import org.bukkit.Bukkit; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.EventPriority; | |
import org.bukkit.event.HandlerList; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.inventory.InventoryClickEvent; | |
import org.bukkit.inventory.Inventory; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.plugin.Plugin; | |
public class IconMenu implements Listener { | |
private String name; | |
private int size; | |
private OptionClickEventHandler handler; | |
private AterHelp ah; | |
private String[] optionNames; | |
private ItemStack[] optionIcons; | |
public IconMenu(String name, int size, OptionClickEventHandler handler, AterHelp ah) { | |
this.name = name; | |
this.size = size; | |
this.handler = handler; | |
this.ah = ah; | |
this.optionNames = new String[size]; | |
this.optionIcons = new ItemStack[size]; | |
ah.getServer().getPluginManager().registerEvents(this, ah); | |
} | |
public IconMenu setOption(int position, ItemStack icon, String name, String... info) { | |
optionNames[position] = name; | |
optionIcons[position] = ah.setItemNameAndLore(icon, name, info); | |
return this; | |
} | |
public void open(Player player) { | |
Inventory inventory = Bukkit.createInventory(player, size, name); | |
for (int i = 0; i < optionIcons.length; i++) { | |
if (optionIcons[i] != null) { | |
inventory.setItem(i, optionIcons[i]); | |
} | |
} | |
player.openInventory(inventory); | |
} | |
public void destroy() { | |
HandlerList.unregisterAll(this); | |
handler = null; | |
ah = null; | |
optionNames = null; | |
optionIcons = null; | |
} | |
@EventHandler(priority=EventPriority.MONITOR) | |
void onInventoryClick(InventoryClickEvent event) { | |
if (event.getInventory().getTitle().equals(name)) { | |
event.setCancelled(true); | |
int slot = event.getRawSlot(); | |
if (slot >= 0 && slot < size && optionNames[slot] != null) { | |
Plugin plugin = this.ah; | |
OptionClickEvent e = new OptionClickEvent((Player)event.getWhoClicked(), slot, optionNames[slot]); | |
handler.onOptionClick(e); | |
if (e.willClose()) { | |
final Player p = (Player)event.getWhoClicked(); | |
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { | |
public void run() { | |
p.closeInventory(); | |
} | |
}, 1); | |
} | |
if (e.willDestroy()) { | |
destroy(); | |
} | |
} | |
} | |
} | |
public interface OptionClickEventHandler { | |
public void onOptionClick(OptionClickEvent event); | |
} | |
public class OptionClickEvent { | |
private Player player; | |
private int position; | |
private String name; | |
private boolean close; | |
private boolean destroy; | |
public OptionClickEvent(Player player, int position, String name) { | |
this.player = player; | |
this.position = position; | |
this.name = name; | |
this.close = true; | |
this.destroy = false; | |
} | |
public Player getPlayer() { | |
return player; | |
} | |
public int getPosition() { | |
return position; | |
} | |
public String getName() { | |
return name; | |
} | |
public boolean willClose() { | |
return close; | |
} | |
public boolean willDestroy() { | |
return destroy; | |
} | |
public void setWillClose(boolean close) { | |
this.close = close; | |
} | |
public void setWillDestroy(boolean destroy) { | |
this.destroy = destroy; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment