Advertisement
Evyatar12

Main.java

Jan 6th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 KB | None | 0 0
  1. package com.thebukkitarea.inventories;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.ChatColor;
  5. import org.bukkit.Material;
  6. import org.bukkit.command.Command;
  7. import org.bukkit.command.CommandSender;
  8. import org.bukkit.entity.Player;
  9. import org.bukkit.event.EventHandler;
  10. import org.bukkit.event.Listener;
  11. import org.bukkit.event.inventory.InventoryAction;
  12. import org.bukkit.event.inventory.InventoryClickEvent;
  13. import org.bukkit.event.inventory.InventoryCloseEvent;
  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. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.UUID;
  22.  
  23. /**
  24.  * Created by אביתר on 06/01/2015.
  25.  */
  26. public class Main extends JavaPlugin implements Listener {
  27.  
  28.     private List<UUID> users = new ArrayList<UUID>();
  29.     private  ItemStack _0 = createItemStack(Material.STAINED_GLASS_PANE, 1, (short) 15, ChatColor.GREEN + "It's good to be back."),
  30.             _8 = createItemStack(Material.BOOK, 1, (short) 0, ChatColor.YELLOW + "THE BUKKIT AREA - 2k15");
  31.  
  32.     @Override
  33.     public void onEnable() {
  34.         getServer().getPluginManager().registerEvents(this, this);
  35.     }
  36.  
  37.     private Inventory getInventory() {
  38.         Inventory inv = Bukkit.createInventory(null, 9, "Back to the channel");
  39.  
  40.         inv.addItem(_0);
  41.         inv.setItem(8, _8);
  42.  
  43.         // 0, 1, 2, 3, 4, 5, 6, 7, 8
  44.  
  45.         return inv;
  46.     }
  47.  
  48.     private ItemStack createItemStack(Material material, int amount, short durability, String name) {
  49.         ItemStack stack = new ItemStack(material, amount, durability);
  50.  
  51.         ItemMeta meta = stack.getItemMeta();
  52.         meta.setDisplayName(ChatColor.RESET + name);
  53.         stack.setItemMeta(meta);
  54.  
  55.         return stack;
  56.     }
  57.  
  58.     @Override
  59.     public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  60.         if (sender instanceof Player) {
  61.             Player player = (Player) sender;
  62.  
  63.             if (!users.contains(player.getUniqueId())) {
  64.                 users.add(player.getUniqueId());
  65.             }
  66.  
  67.             player.openInventory(getInventory());
  68.         }
  69.  
  70.         return true;
  71.     }
  72.  
  73.     @EventHandler
  74.     public void onInventoryClick(InventoryClickEvent event) {
  75.         if (event.getWhoClicked() instanceof Player) {
  76.             Player player = (Player) event.getWhoClicked();
  77.  
  78.             if (!users.contains(player.getUniqueId())) {
  79.                 return;
  80.             }
  81.  
  82.             if (event.getCurrentItem() != null) {
  83.                 ItemStack current = event.getCurrentItem();
  84.                 event.setCancelled(true);
  85.  
  86.                 if (current.equals(_0) && event.getSlot() == 0);
  87.                 else if (current.equals(_8) && event.getSlot() == 8);
  88.                 else {
  89.                     event.setCancelled(false);
  90.                 }
  91.             }
  92.         }
  93.     }
  94.  
  95.     @EventHandler
  96.     public void onInventoryClose(InventoryCloseEvent event) {
  97.         if (event.getPlayer() instanceof Player) {
  98.             Player player = (Player) event.getPlayer();
  99.  
  100.             if (users.contains(player.getUniqueId())) {
  101.                 users.remove(player.getUniqueId());
  102.             }
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement