StarShadow

MC - Menu Maker API

Jun 12th, 2015 (edited)
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.84 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.regex.Pattern;
  6.  
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.Material;
  9. import org.bukkit.entity.Player;
  10. import org.bukkit.event.EventHandler;
  11. import org.bukkit.event.Listener;
  12. import org.bukkit.event.inventory.InventoryAction;
  13. import org.bukkit.event.inventory.InventoryClickEvent;
  14. import org.bukkit.inventory.Inventory;
  15. import org.bukkit.inventory.ItemStack;
  16. import org.bukkit.inventory.meta.ItemMeta;
  17. import org.bukkit.plugin.java.JavaPlugin;
  18.  
  19. public class Menu {
  20.        
  21.         public interface ItemAction {
  22.                 public void run(Player player, Inventory inv, ItemStack item, int slot, InventoryAction action);
  23.         }
  24.        
  25.         private String title = "";
  26.         private int rows = 3;
  27.         private HashMap<Integer, ItemStack> content = new HashMap<Integer, ItemStack>();
  28.         private HashMap<Integer, ItemAction> commands = new HashMap<Integer, ItemAction>();
  29.         private Inventory inventory;
  30.         private JavaPlugin plugin;
  31.         private ItemAction gaction;
  32.         private boolean runempty = true;
  33.        
  34.         public Menu(JavaPlugin plugin, String title, int rows) {
  35.                 this(title, rows);
  36.                 setListener(plugin);
  37.         }
  38.         public Menu(JavaPlugin plugin, String title, int rows, ItemStack[] contents) {
  39.                 this(plugin, title, rows);
  40.                 setContents(contents);
  41.         }
  42.         public Menu(String title, int rows) throws IndexOutOfBoundsException {
  43.                 if (rows < 1 || rows > 6) throw new IndexOutOfBoundsException("Menu can only have between 1 and 6 rows.");
  44.                 this.title = title;
  45.                 this.rows = rows;
  46.         }
  47.        
  48.         private void setListener(JavaPlugin pl) {
  49.                 pl.getServer().getPluginManager().registerEvents(new Listener() {
  50.                         @EventHandler
  51.                         public void onInvClick(InventoryClickEvent event) {
  52.                                 Player player = (Player) event.getWhoClicked();
  53.                                 Inventory inv = event.getInventory();
  54.                                 ItemStack item = event.getCurrentItem();
  55.                                 int slot = event.getRawSlot();
  56.                                 InventoryAction a = event.getAction();
  57.                                
  58.                                 if (item == null || item.getType() == Material.AIR) {
  59.                                         if (!runempty) return;
  60.                                 }
  61.                                
  62.                                 if (inv.getName().equals(title)) {
  63.                                         if (inv.equals(inventory)) {
  64.                                                 if (slot <= (rows*9)-1) {
  65.                                                         event.setCancelled(true);
  66.                                                         if (hasAction(slot)) commands.get(slot).run(player, inv, item, slot, a);
  67.                                                         if (gaction != null) gaction.run(player, inv, item, slot, a);
  68.                                                 }
  69.                                         }
  70.                                 }
  71.                         }
  72.                 }, pl);
  73.         }
  74.        
  75.         @Deprecated
  76.         public boolean hasAction(int slot) {
  77.                 return commands.containsKey(slot);
  78.         }
  79.        
  80.         @Deprecated
  81.         public void setAction(int slot, ItemAction action) {
  82.                 commands.put(slot, action);
  83.         }
  84.        
  85.         public void setGlobalAction(ItemAction action) {
  86.                 this.gaction = action;
  87.         }
  88.        
  89.         public void removeGlobalAction() {
  90.                 this.gaction = null;
  91.         }
  92.        
  93.         @Deprecated
  94.         public void removeAction(int slot) {
  95.                 if (commands.containsKey(slot)) {
  96.                         commands.remove(slot);
  97.                 }
  98.         }
  99.        
  100.         public void runWhenEmpty(boolean state) {
  101.                 this.runempty = state;
  102.         }
  103.        
  104.         public int nextOpenSlot() {
  105.                 int h = 0;
  106.                 for (Integer i : content.keySet()) {
  107.                         if (i > h) h = i;
  108.                 }
  109.                 for (int i = 0; i <= h; i++) {
  110.                         if (!content.containsKey(i)) return i;
  111.                 }
  112.                 return h+1;
  113.         }
  114.        
  115.         public void setContents(ItemStack[] contents) throws ArrayIndexOutOfBoundsException {
  116.                 if (contents.length > rows*9) throw new ArrayIndexOutOfBoundsException("setContents() : Contents are larger than inventory.");
  117.                 content.clear();
  118.                 for (int i = 0; i < contents.length; i++) {
  119.                         if (contents[i] != null && contents[i].getType() != Material.AIR) content.put(i, contents[i]);
  120.                 }
  121.         }
  122.        
  123.         public void addItem(ItemStack item) {
  124.                 if (nextOpenSlot() > (rows*9)-1) {
  125.                         plugin.getLogger().info("addItem() : Inventory is full.");
  126.                         return;
  127.                 }
  128.                 setItem(nextOpenSlot(), item);
  129.         }
  130.        
  131.         public void setItem(int slot, ItemStack item) throws IndexOutOfBoundsException {
  132.                 if (slot < 0 || slot > (rows*9)-1) throw new IndexOutOfBoundsException("setItem() : Slot is outside inventory.");
  133.                 if (item == null || item.getType() == Material.AIR) {
  134.                         removeItem(slot);
  135.                         return;
  136.                 }
  137.                 content.put(slot, item);
  138.         }
  139.        
  140.         public void fill(ItemStack item) {
  141.                 for (int i = 0; i < rows*9; i++) content.put(i, item);
  142.         }
  143.        
  144.         public void fillRange(int s, int e, ItemStack item) throws IndexOutOfBoundsException {
  145.                 if (e <= s) throw new IndexOutOfBoundsException("fillRange() : Ending index must be less than starting index.");
  146.                 if (s < 0 || s > (rows*9)-1) throw new IndexOutOfBoundsException("fillRange() : Starting index is outside inventory.");
  147.                 if (e < 0 || e > (rows*9)-1) throw new IndexOutOfBoundsException("fillRange() : Ending index is outside inventory.");
  148.                 for (int i = s; i <= e; i++) content.put(i, item);
  149.         }
  150.        
  151.         public void removeItem(int slot) {
  152.                 if (content.containsKey(slot)) content.remove(slot);
  153.         }
  154.        
  155.         public ItemStack getItem(int slot) {
  156.                 if (content.containsKey(slot)) return content.get(slot);
  157.                 return null;
  158.         }
  159.        
  160.         public void setTitle(String title) {
  161.                 this.title = title;
  162.         }
  163.        
  164.         public String getTitle() {
  165.                 return this.title;
  166.         }
  167.        
  168.         public int rows() {
  169.                 return this.rows;
  170.         }
  171.        
  172.         public void build() {
  173.                 this.inventory = Bukkit.createInventory(null, rows*9, this.title);
  174.                 inventory.clear();
  175.                 for (Map.Entry<Integer, ItemStack> entry : content.entrySet()) inventory.setItem(entry.getKey(), entry.getValue());
  176.         }
  177.        
  178.         public Inventory getMenu() {
  179.                 build();
  180.                 return inventory;
  181.         }
  182.        
  183.         public void showMenu(Player player) {
  184.                 player.openInventory(getMenu());
  185.         }
  186.        
  187.         public ItemStack[] getContents() {
  188.                 return getMenu().getContents();
  189.         }
  190.        
  191.         public ItemStack createItem(Material material, int amount, String name, String lore, short durability) throws IndexOutOfBoundsException {
  192.                 if (amount < 1 || amount > 64) {
  193.                         throw new IndexOutOfBoundsException("Amount should be between 1 and 64.");
  194.                 }
  195.                 ItemStack item = new ItemStack(material, amount);
  196.                 ItemMeta meta = item.getItemMeta();
  197.                
  198.                 if (name != null && name != "") {
  199.                         meta.setDisplayName(name);
  200.                 }
  201.                 if (lore != null && lore != "") {
  202.                         String[] lines = lore.split(Pattern.quote("^$"));
  203.                         List<String> newlore = new ArrayList<String>();
  204.                         for (String s : lines) {
  205.                                 newlore.add(s);
  206.                         }
  207.                         meta.setLore(newlore);
  208.                 }
  209.                 item.setDurability(durability);
  210.                 item.setItemMeta(meta);
  211.                
  212.                 return item;
  213.         }
  214.         public ItemStack createItem(Material material, int amount, String name, String lore, short durability, byte data) throws IndexOutOfBoundsException {
  215.                 if (amount < 1 || amount > 64) {
  216.                         throw new IndexOutOfBoundsException("Amount should be between 1 and 64.");
  217.                 }
  218.                 ItemStack item = new ItemStack(material, amount, data);
  219.                 ItemMeta meta = item.getItemMeta();
  220.                
  221.                 if (name != null && name != "") {
  222.                         meta.setDisplayName(name);
  223.                 }
  224.                 if (lore != null && lore != "") {
  225.                         String[] lines = lore.split(Pattern.quote("^$"));
  226.                         List<String> newlore = new ArrayList<String>();
  227.                         for (String s : lines) {
  228.                                 newlore.add(s);
  229.                         }
  230.                         meta.setLore(newlore);
  231.                 }
  232.                 item.setDurability(durability);
  233.                 item.setItemMeta(meta);
  234.                
  235.                 return item;
  236.         }
  237.  
  238. }
Add Comment
Please, Sign In to add comment