@EventHandler
  public void onInterfaceInteract(InventoryClickEvent event) {
    if (event.getView() != this) return;

    ItemStack itemClicked = event.getCurrentItem();
    if (itemClicked == null || itemClicked.getType() == Material.AIR) return;

    event.setCancelled(true);

    // If not right or left-click.
    if (!(event.getClick() == ClickType.RIGHT || event.getClick() == ClickType.LEFT)) return;

    if (this.ACCEPT_OBJECT.equals(itemClicked)) {
      onAcceptPressed();
      return;
    }

    if (this.BACK_OBJECT.equals(itemClicked)) {
      if (onBackPressed()) scheduleCloseOfInventory();
      return;
    }

    int rawSlot = event.getRawSlot();
    int slotNumber = event.getSlot();
    boolean usedTopInv = rawSlot < getTopInventory().getSize();

    if (usedTopInv) onSelectionItemPressed(slotNumber, itemClicked);
    else onControlItemPressed(slotNumber, itemClicked);
  }
 public static void debug(InventoryClickEvent event) {
   System.out.println("===== DEBUG START =====");
   System.out.println("event.getAction() " + event.getAction());
   System.out.println("event.isLeftClick() " + event.isLeftClick());
   System.out.println("event.isRightClick() " + event.isRightClick());
   System.out.println("event.isShiftClick() " + event.isShiftClick());
   System.out.println("event.getClick() " + event.getClick());
   System.out.println("event.getCurrentItem() " + event.getCurrentItem());
   System.out.println("event.getCursor() " + event.getCursor());
   System.out.println("event.getHotbarButton() " + event.getHotbarButton());
   System.out.println("getInventory().getType() " + event.getInventory().getType());
   System.out.println("event.getRawSlot() " + event.getRawSlot());
   System.out.println("event.getResult() " + event.getResult());
   System.out.println("event.getSlot() " + event.getSlot());
   System.out.println("event.getSlotType() " + event.getSlotType());
   System.out.println(
       "getView().getTopInventory().getType() " + event.getView().getTopInventory().getType());
   System.out.println("getView().getType() " + event.getView().getType());
   System.out.println(
       "getView().getBottomInventory().getType() "
           + event.getView().getBottomInventory().getType());
   System.out.println("event.getWhoClicked() " + event.getWhoClicked());
   System.out.println("-----");
   System.out.println("isOutside(event) " + isOutside(event));
   System.out.println("isTopInventory(event) " + isTopInventory(event));
   System.out.println("isBottomInventory(event) " + isBottomInventory(event));
   System.out.println("getAlter(event) " + getAlter(event));
   System.out.println("isAltering(event) " + isAltering(event));
   System.out.println("isEquipping(event) " + isEquipping(event));
   System.out.println("===== DEBUG END =====");
 }
 @EventHandler(ignoreCancelled = true)
 public void onPrefsMenuClick(InventoryClickEvent event) {
   Inventory inv = event.getInventory();
   String name = inv.getTitle();
   if (name.equals("§4TARDIS Key Prefs Menu")) {
     Player p = (Player) event.getWhoClicked();
     int slot = event.getRawSlot();
     if (slot >= 0 && slot < 27) {
       switch (slot) {
         case 0:
         case 1:
         case 2:
         case 3:
         case 4:
         case 5:
         case 6:
         case 7:
         case 8:
         case 10:
         case 12:
         case 14:
         case 16:
           event.setCancelled(true);
           // set display name of key in slot 18
           ItemStack key = inv.getItem(18);
           if (key == null || !key.getType().equals(Material.GOLD_NUGGET) || !key.hasItemMeta()) {
             return;
           }
           // get Display name of selected key
           ItemStack choice = inv.getItem(slot);
           ItemMeta choice_im = choice.getItemMeta();
           String choice_name = choice_im.getDisplayName();
           ItemMeta sonic_im = key.getItemMeta();
           sonic_im.setDisplayName(choice_name);
           key.setItemMeta(sonic_im);
           break;
         case 18:
           break;
         case 26:
           // close
           event.setCancelled(true);
           close(p);
           break;
         default:
           event.setCancelled(true);
           break;
       }
     } else {
       ClickType click = event.getClick();
       if (click.equals(ClickType.SHIFT_RIGHT) || click.equals(ClickType.SHIFT_LEFT)) {
         event.setCancelled(true);
       }
     }
   }
 }
Beispiel #4
0
 @Override
 public boolean onClick(InventoryClickEvent event) {
   if (event.getInventory() == null
       || event.getInventory().getTitle() == null
       || !stripFormatting(event.getInventory().getTitle())
           .contains(stripFormatting(getTitle()))) {
     return false;
   }
   if (event.getSlotType() != InventoryType.SlotType.CONTAINER) {
     return true;
   }
   Player player = (Player) event.getWhoClicked();
   Inventory menu = event.getInventory();
   ItemStack returnItem = menu.getItem(getIndex(5, 0));
   String configName = returnItem.getItemMeta().getLore().get(0);
   String path = returnItem.getItemMeta().getLore().get(1);
   int page = getPage(returnItem.getItemMeta().getLore().get(2));
   int slot = event.getSlot();
   int row = slot / 9;
   int col = slot % 9;
   ItemStack clickedItem = event.getCurrentItem();
   if (slot >= getIndex(3, 0) && slot <= getIndex(3, 8)) {
     // increment buttons
     YmlConfiguration config = FileUtil.getYmlConfiguration(configName);
     int value = config.getInt(path, 0);
     int increment = getDisplayNameAsInt(clickedItem);
     if (event.getClick() == ClickType.LEFT) {
       value += increment;
     } else if (event.getClick() == ClickType.RIGHT) {
       value = increment;
     }
     config.set(path, value);
     config.set("dirty", true);
   }
   if (slot != getIndex(5, 0)) {
     player.openInventory(createEditMenu(configName, path, page));
   } else {
     player.openInventory(parent.createEditMenu(configName, path, page));
   }
   return true;
 }
Beispiel #5
0
 /**
  * Cancel players from removing, picking the item in their inventory.
  *
  * @param event
  */
 @EventHandler(priority = EventPriority.LOWEST)
 public void cancelMove(InventoryClickEvent event) {
   Player player = (Player) event.getWhoClicked();
   if (player == getPlayer()) {
     if (event.getClick() == ClickType.SHIFT_LEFT
         || event.getClick() == ClickType.SHIFT_RIGHT
         || event.getClick() == ClickType.NUMBER_KEY
         || event.getClick() == ClickType.UNKNOWN) {
       event.setCancelled(true);
       player.updateInventory();
       return;
     }
     if (event.getCurrentItem() != null) {
       if (event.getCurrentItem().equals(itemStack)) {
         event.setCancelled(true);
         player.updateInventory();
         return;
       }
     }
   }
 }
Beispiel #6
0
  @EventHandler
  public void on(InventoryClickEvent event) {
    if (!(event.getWhoClicked() instanceof Player)) {
      return;
    }
    Player player = (Player) event.getWhoClicked();
    if (this.hasOpen(player)) {
      int slot = event.getSlot();
      if (slot >= this.getSize()) {
        return;
      }

      event.setCancelled(true);
      getComponent(slot)
          .ifPresent(
              component -> {
                Position pos = this.components.get(component);
                Position xy = Position.toPosition(this, slot);
                component.onClick(player, event.getClick(), xy.x - pos.x, xy.y - pos.y);
              });
    }
  }
Beispiel #7
0
    @EventHandler
    public void NextPageEvent(InventoryClickEvent event) {
      // Get next items when next page item is clicked
      if (event.getInventory().getTitle().equals("Auction House")) {
        if (event.getClick().equals(ClickType.SHIFT_LEFT)) {
          Plugin plugin = RPGMaker.getPlugin(RPGMaker.class);
          boolean flag = true;
          Inventory inv = null;

          if (event.getCurrentItem().equals(nextpage)) {
            // Get next page
            player.sendMessage("Getting next page");
            page++;
            ids.clear();
            inv = getInventory();
          } else if (event.getCurrentItem().equals(sortname)) {
            sortingType = 2;
            ids.clear();
            sortByName();
          } else if (event.getCurrentItem().equals(sortnew)) {
            sortingType = 0;
            ids.clear();
            inv = sortByNew();
          } else if (event.getCurrentItem().equals(sortold)) {
            sortingType = 1;
            ids.clear();
            inv = sortByOld();
          } else {
            flag = false;
            player.sendMessage("You are trying to buy something");
            // Real time updates? -> need to update every viewer of AH
            ItemStack item = event.getCurrentItem();
            if (!(item.getType().equals(Material.AIR))) {
              if (getCost(item) == 0) {
                event.setCancelled(true);
                return;
              }

              item = removeAuctionLore(item);
              int id = ids.get(event.getRawSlot());

              try {
                String Query = "delete from `auctionhouse` where `ID` = " + id + ";";
                database.Query = database.connection.prepareStatement(Query);
                database.Query.execute();
                player.getInventory().addItem(item);
                player.closeInventory(); // for now until I handle realtime updates
                // and economy
              } catch (SQLException e) {
                e.printStackTrace();
              }
            }
          }
          final Inventory savedInv = inv;
          final Inventory oldInv = event.getInventory();
          if (flag) {
            Bukkit.getServer()
                .getScheduler()
                .scheduleSyncDelayedTask(
                    plugin,
                    new Runnable() {
                      @Override
                      public void run() {
                        // player.closeInventory();
                        oldInv.setContents(savedInv.getContents());
                      }
                    },
                    1);
          }
        }
        event.setCancelled(true);
      }
    }
  @SuppressWarnings("deprecation")
  @EventHandler
  public void onInventoryClick(InventoryClickEvent event) {
    // controller.getLogger().info("CLICK: " + event.getAction() + ", " + event.getClick() + " on "
    // + event.getSlotType() + " in "+ event.getInventory().getType() + " slots: " + event.getSlot()
    // + ":" + event.getRawSlot());

    if (event.isCancelled()) return;
    if (!(event.getWhoClicked() instanceof Player)) return;

    Player player = (Player) event.getWhoClicked();
    Mage apiMage = controller.getMage(player);

    if (!(apiMage instanceof com.elmakers.mine.bukkit.magic.Mage)) return;
    final com.elmakers.mine.bukkit.magic.Mage mage = (com.elmakers.mine.bukkit.magic.Mage) apiMage;

    GUIAction gui = mage.getActiveGUI();
    if (gui != null) {
      gui.clicked(event);
      return;
    }

    // Check for temporary items and skill items
    InventoryAction action = event.getAction();
    InventoryType inventoryType = event.getInventory().getType();
    ItemStack clickedItem = event.getCurrentItem();

    boolean isDrop =
        event.getClick() == ClickType.DROP || event.getClick() == ClickType.CONTROL_DROP;
    boolean isSkill = clickedItem != null && Wand.isSkill(clickedItem);

    // Preventing putting skills in containers
    if (isSkill && inventoryType != InventoryType.CRAFTING) {
      if (!isDrop) {
        event.setCancelled(true);
      }
      return;
    }

    // Check for right-click-to-use
    if (isSkill && action == InventoryAction.PICKUP_HALF) {
      Spell spell = mage.getSpell(Wand.getSpell(clickedItem));
      if (spell != null) {
        spell.cast();
      }
      player.closeInventory();
      event.setCancelled(true);
      return;
    }

    if (clickedItem != null && NMSUtils.isTemporary(clickedItem)) {
      String message = NMSUtils.getTemporaryMessage(clickedItem);
      if (message != null && message.length() > 1) {
        mage.sendMessage(message);
      }
      ItemStack replacement = NMSUtils.getReplacement(clickedItem);
      event.setCurrentItem(replacement);
      event.setCancelled(true);
      return;
    }

    // Check for wearing spells
    ItemStack heldItem = event.getCursor();
    if (heldItem != null && event.getSlotType() == InventoryType.SlotType.ARMOR) {
      if (Wand.isSpell(heldItem)) {
        event.setCancelled(true);
        return;
      }
      if (Wand.isWand(clickedItem) || Wand.isWand(heldItem)) {
        controller.onArmorUpdated(mage);
      }
    }
    boolean isHotbar =
        event.getAction() == InventoryAction.HOTBAR_SWAP
            || event.getAction() == InventoryAction.HOTBAR_MOVE_AND_READD;
    if (isHotbar && event.getSlotType() == InventoryType.SlotType.ARMOR) {
      int slot = event.getHotbarButton();
      ItemStack item = mage.getPlayer().getInventory().getItem(slot);
      if (item != null && Wand.isSpell(item)) {
        event.setCancelled(true);
        return;
      }
      controller.onArmorUpdated(mage);
    }

    Wand activeWand = mage.getActiveWand();

    boolean isChest =
        inventoryType == InventoryType.CHEST
            || inventoryType == InventoryType.HOPPER
            || inventoryType == InventoryType.DISPENSER
            || inventoryType == InventoryType.DROPPER;
    boolean clickedWand = Wand.isWand(clickedItem);
    boolean isContainerSlot = event.getSlot() == event.getRawSlot();

    if (activeWand != null && activeWand.isInventoryOpen()) {
      // Kind of a hack to avoid hotbar items with 0 amounts disappearing :(
      Inventory inventory = player.getInventory();
      for (int i = 0; i < 8; i++) {
        ItemStack item = inventory.getItem(i);
        if (item != null && item.getAmount() == 0) {
          item.setAmount(1);
        }
      }

      // Don't allow the offhand slot to be messed with while the spell inventory is open
      if (event.getRawSlot() == 45) {
        event.setCancelled(true);
        return;
      }

      if (Wand.isSpell(clickedItem) && clickedItem.getAmount() != 1) {
        clickedItem.setAmount(1);
      }
      if (clickedWand) {
        event.setCancelled(true);
        if (dropChangesPages) {
          activeWand.cycleInventory();
        } else {
          activeWand.cycleHotbar(1);
        }
        return;
      }

      // So many ways to try and move the wand around, that we have to watch for!
      if (isHotbar && Wand.isWand(player.getInventory().getItem(event.getHotbarButton()))) {
        event.setCancelled(true);
        return;
      }

      // Can't wear spells
      if (event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY && clickedItem != null) {
        if (controller.isWearable(clickedItem)) {
          event.setCancelled(true);
          return;
        }
      }

      // Safety check for something that ought not to be possible
      // but perhaps happens with lag?
      if (Wand.isWand(event.getCursor())) {
        activeWand.closeInventory();
        event.setCursor(null);
        event.setCancelled(true);
        return;
      }
    } else if (activeWand != null) {
      // Check for changes that could have been made to the active wand
      Integer activeSlot = player.getInventory().getHeldItemSlot();
      if (activeSlot != null
          && (event.getSlot() == activeSlot
              || (event.getAction() == InventoryAction.HOTBAR_SWAP
                  && event.getHotbarButton() == activeSlot))) {
        mage.checkWand();
        activeWand = mage.getActiveWand();
      }
    } else if (clickedWand
        && Wand.Undroppable
        && !player.hasPermission("Magic.wand.override_drop")
        && isChest
        && !isContainerSlot) {
      Wand wand = new Wand(controller, clickedItem);
      if (wand.isUndroppable()) {
        event.setCancelled(true);
        return;
      }
    }

    // Check for armor changing
    if (event.getAction() == InventoryAction.MOVE_TO_OTHER_INVENTORY && clickedItem != null) {
      if (controller.isWearable(clickedItem)) {
        controller.onArmorUpdated(mage);
      }
    }

    // Check for dropping items out of a wand's inventory
    // or dropping undroppable wands
    if (isDrop) {
      if (clickedWand) {
        Wand wand = new Wand(controller, clickedItem);
        if (wand.isUndroppable()) {
          event.setCancelled(true);
          if (activeWand != null) {
            if (activeWand.getHotbarCount() > 1) {
              activeWand.cycleHotbar(1);
            } else {
              activeWand.closeInventory();
            }
          }
          return;
        }
      }
      if (activeWand != null && activeWand.isInventoryOpen()) {

        ItemStack droppedItem = clickedItem;

        if (!Wand.isSpell(droppedItem)) {
          mage.giveItem(droppedItem);
          event.setCurrentItem(null);
          event.setCancelled(true);
          return;
        }

        // This is a hack to deal with spells on cooldown disappearing,
        // Since the event handler doesn't match the zero-count itemstacks
        Integer slot = event.getSlot();
        int heldSlot = player.getInventory().getHeldItemSlot();
        Inventory hotbar = activeWand.getHotbar();
        if (hotbar != null
            && slot >= 0
            && slot <= hotbar.getSize()
            && slot != heldSlot
            && activeWand.getMode() == WandMode.INVENTORY) {
          if (slot > heldSlot) slot--;
          if (slot < hotbar.getSize()) {
            droppedItem = hotbar.getItem(slot);
          } else {
            slot = null;
          }
        } else {
          slot = null;
        }

        if (!controller.isSpellDroppingEnabled()) {
          player.closeInventory();
          String spellName = Wand.getSpell(droppedItem);
          if (spellName != null && !activeWand.isManualQuickCastDisabled()) {
            Spell spell = mage.getSpell(spellName);
            if (spell != null) {
              activeWand.cast(spell);
              // Just in case a spell has levelled up... jeez!
              if (hotbar != null && slot != null) {
                droppedItem = hotbar.getItem(slot);
              }
            }
          }
          event.setCancelled(true);

          // This is needed to avoid spells on cooldown disappearing from the hotbar
          if (hotbar != null && slot != null && mage.getActiveGUI() == null) {
            player.getInventory().setItem(event.getSlot(), droppedItem);
            player.updateInventory();
          }

          return;
        }
        ItemStack newDrop = controller.removeItemFromWand(activeWand, droppedItem);

        if (newDrop != null) {
          Location location = player.getLocation();
          Item item = location.getWorld().dropItem(location, newDrop);
          item.setVelocity(location.getDirection().normalize());
        } else {
          event.setCancelled(true);
        }
      }
      return;
    }

    // Check for wand cycling with active inventory
    if (activeWand != null) {
      WandMode wandMode = activeWand.getMode();
      if ((wandMode == WandMode.INVENTORY && inventoryType == InventoryType.CRAFTING)
          || (wandMode == WandMode.CHEST && inventoryType == InventoryType.CHEST)) {
        if (activeWand.isInventoryOpen()) {
          if (event.getAction() == InventoryAction.NOTHING) {
            int direction = event.getClick() == ClickType.LEFT ? 1 : -1;
            activeWand.cycleInventory(direction);
            event.setCancelled(true);
            return;
          }

          if (event.getSlotType() == InventoryType.SlotType.ARMOR) {
            event.setCancelled(true);
            return;
          }

          // Chest mode falls back to selection from here.
          if (event.getAction() == InventoryAction.PICKUP_HALF || wandMode == WandMode.CHEST) {
            controller.onPlayerActivateIcon(mage, activeWand, clickedItem);
            player.closeInventory();
            event.setCancelled(true);
          }
        }
      }
    }
  }
  public void interactRocketBoots(InventoryClickEvent event, ItemStack boots) {

    Player player = (Player) event.getWhoClicked();
    ClickType click = event.getClick();
    Boolean hasRocketMeta = boots.hasItemMeta();

    if (gamemodeCheck.check(player, GameMode.CREATIVE, GameMode.SPECTATOR)) {
      event.setCancelled(true);
      player.closeInventory();
      disableRocketBoots(player, false, false, false, false, false);
      commonString.messageSend(
          RocketInit.getPlugin(), player, true, RocketLanguage.RB_GAMEMODE_ERROR);
      return;
    }

    if (hasRocketMeta) {

      ItemMeta rocketMeta = boots.getItemMeta();

      if (rocketMeta.hasDisplayName())
        if (rocketMeta.getDisplayName().matches(ChatColor.RED + "Rocket Boots"))
          if (rocketMeta.hasLore()) {

            String rocketLore = rocketMeta.getLore().get(0);
            String variantLore = null;

            if (rocketMeta.getLore().size() >= 2)
              variantLore = ChatColor.stripColor(rocketMeta.getLore().get(1));

            Set<String> specialVariants = new HashSet<>(Collections.singletonList("Robin Hood"));

            if (variantLore != null)
              if (specialVariants.contains(variantLore)) {

                Variant variantType = Variant.getEnum(variantLore);

                if (variantType != null)
                  if (variantType == Variant.MONEY)
                    if (RocketInit.getVaultEconomy() == null) {
                      commonString.messageSend(
                          RocketInit.getPlugin(), player, true, RocketLanguage.RB_EQUIP_ERROR);
                      return;
                    }
              }

            if (rocketLore.matches(ChatColor.YELLOW + "Rocket Level I{0,3}V?X?"))
              if (!RocketInit.rocketUsage.contains(player.getUniqueId()))
                if (click == ClickType.MIDDLE) event.setCancelled(true);
                else attachRocketBoots(player, boots);
          }

    } else if (RocketInit.rocketSprint.containsKey(player.getUniqueId())) {

      commonString.messageSend(
          RocketInit.getPlugin(),
          player,
          true,
          new String[] {RocketLanguage.RB_COOLDOWN_TOUCH, RocketLanguage.RB_COOLDOWN_LAND});

      event.setCancelled(true);
      player.closeInventory();

    } else if (RocketInit.rocketPower.containsKey(player.getUniqueId()))
      disableRocketBoots(player, false, false, false, false, false);
  }
Beispiel #10
0
  @EventHandler(priority = EventPriority.HIGHEST)
  public void inventoryMenu(final InventoryClickEvent event) {

    // -------Armor wear handling--------
    if (event.getSlotType() == SlotType.ARMOR && event.getClick() != ClickType.DOUBLE_CLICK) {
      CEventHandler.handleArmor(
          (Player) event.getWhoClicked(), event.getCurrentItem(), true, event);
      CEventHandler.handleArmor((Player) event.getWhoClicked(), event.getCursor(), false, event);
      if (event.getCursor() == null)
        CEventHandler.handleArmor(
            (Player) event.getWhoClicked(), event.getCurrentItem(), false, event);

    } else if (event.getClick() == ClickType.SHIFT_LEFT) {
      ItemStack current = event.getCurrentItem();
      String typeS = current.getType().toString();
      PlayerInventory inv = event.getWhoClicked().getInventory();
      if ((typeS.endsWith("HELMET") && inv.getHelmet() == null)
          || (typeS.endsWith("CHESTPLATE") && inv.getChestplate() == null)
          || (typeS.endsWith("LEGGINGS") && inv.getLeggings() == null)
          || (typeS.endsWith("BOOTS") && inv.getBoots() == null))
        CEventHandler.handleArmor(
            (Player) event.getWhoClicked(), event.getCurrentItem(), false, event);
    }
    // ---------------------------------

    if (event.getCurrentItem() == null
        || event.getCurrentItem().getType() == Material.AIR
        || event.getRawSlot() < -1) {
      return;
    }
    if (event.getView().getTopInventory().getTitle().startsWith("CE")) {
      Inventory topInv = event.getView().getTopInventory();
      final Player p = (Player) event.getWhoClicked();
      ItemStack clickedItem = event.getCurrentItem();
      event.setResult(Result.ALLOW);

      event.setCancelled(true);

      // This is the back-button, located in the very last spot of each inventory
      if ((event.getRawSlot() == topInv.getSize() - 1)
          && event.getCurrentItem().getType() != Material.AIR) {
        p.closeInventory();
        p.openInventory(Tools.getPreviousInventory(topInv.getTitle()));
        return;
      }

      if (event.getRawSlot() < topInv.getSize()) {

        // Opens the clicked Enchantments inventory and loads the permissions if needed
        if (topInv.getTitle().equals(Tools.prefix + "Enchantments")) {
          p.closeInventory();
          p.openInventory(Tools.getEnchantmentMenu(p, clickedItem.getItemMeta().getDisplayName()));
          return;
        }

        // Opens the item inventory and loads the permissions if needed
        if (topInv.getTitle().equals(Tools.prefix + "Main Menu") && event.getRawSlot() == 4) {
          p.closeInventory();
          p.openInventory(Tools.getItemMenu(p));
          return;
        }

        // These are the specific menus, clicking one of them will lead to the enchanting menu,
        // which needs to be 'notified' of the enchantment to give and it's cost
        if (topInv.getTitle().equals(Tools.prefix + "Global")
            || topInv.getTitle().equals(Tools.prefix + "Bow")
            || topInv.getTitle().equals(Tools.prefix + "Armor")
            || topInv.getTitle().equals(Tools.prefix + "Helmet")
            || topInv.getTitle().equals(Tools.prefix + "Boots")
            || topInv.getTitle().equals(Tools.prefix + "Tool"))
          if (p.isOp()
              || Tools.checkPermission(
                  Tools.getEnchantmentByDisplayname(clickedItem.getItemMeta().getDisplayName()),
                  p)) {
            Inventory enchantingMenu = Main.CEEnchantingMenu;
            enchantingMenu.setItem(0, clickedItem);
            p.closeInventory();
            p.openInventory(enchantingMenu);
            return;
          } else {
            p.sendMessage(
                ChatColor.RED + "[CE] You do not have permission to buy this Enchantment.");
            return;
          }

        // This opens the Item Creation Menu
        if (topInv.getTitle().equals(Tools.prefix + "Items"))
          if (p.isOp()
              || Tools.checkPermission(
                  Tools.getItemByDisplayname(clickedItem.getItemMeta().getDisplayName()), p)) {

            Inventory approveMenu = Main.CEApproveMenu;
            approveMenu.setItem(0, clickedItem);
            p.closeInventory();
            p.openInventory(approveMenu);
            return;
          } else {
            p.sendMessage(ChatColor.RED + "[CE] You do not have permission to buy this Item.");
            return;
          }
      }

      if (topInv.getTitle().equals(Tools.prefix + "Enchanting")
          || topInv.getTitle().equals(Tools.prefix + "Item Creation")) {
        double cost = 0;
        ItemStack item = clickedItem;
        ItemMeta im = item.getItemMeta();
        String type = "";
        String successString = "";
        Boolean itemSet = false; // TODO: Solve this by adding item-types
        // Swimsuit swimsuit = (Swimsuit) Main.items.get(9);

        if (topInv.getTitle().equals(Tools.prefix + "Enchanting")) {
          if (event.getRawSlot() > topInv.getSize() && event.isLeftClick()) {
            CEnchantment ce =
                Tools.getEnchantmentByDisplayname(
                    topInv.getContents()[0].getItemMeta().getDisplayName());
            cost = ce.getCost();
            type = "enchantment " + ce.getDisplayName();
            if (!ce.getApplication().equals(Application.GLOBAL)
                && !Tools.isApplicationCorrect(ce.getApplication(), clickedItem.getType())) {
              String appS = ce.getApplication().toString().toLowerCase();
              p.sendMessage(
                  ChatColor.RED
                      + "[CE] This enchantment can only be applied to "
                      + ChatColor.GRAY
                      + (appS.endsWith("s") ? appS : appS + "s")
                      + ChatColor.RED
                      + ".");
              return;
            }
            List<String> lore = new ArrayList<String>();
            if (clickedItem.getItemMeta().hasLore()) {
              lore = clickedItem.getItemMeta().getLore();
              int number = 0;
              for (String s : lore) {
                CEnchantment c = Tools.getEnchantmentByDisplayname(s);
                if (c != null && c.originalName == ce.originalName) {
                  p.sendMessage(ChatColor.RED + "[CE] This item already has this enchantment!");
                  return;
                }
                if (c != null) number++;
              }
              if (Main.maxEnchants > 0 && number >= Main.maxEnchants) {
                p.sendMessage(
                    ChatColor.RED
                        + "[CE] This item has already reached the maximum of "
                        + ChatColor.GRAY
                        + Main.maxEnchants
                        + ChatColor.RED
                        + " enchantments.");
                return;
              }
            }

            lore.add(ce.getDisplayName());
            im.setLore(lore);
            item.setItemMeta(im);
          } else return;

        } else if (topInv.getTitle().equals(Tools.prefix + "Item Creation")) {
          item = topInv.getContents()[0];
          cost = Tools.getItemByDisplayname(item.getItemMeta().getDisplayName()).getCost();
          //					if(item.hasItemMeta() && item.getItemMeta().hasDisplayName() &&
          // item.getItemMeta().getDisplayName().equals(swimsuit.getDisplayName())) { //TODO: Always
          // keep the position of the Swimsuit updated
          //
          //
          //						itemSet = true;
          //
          //						int count = 0;
          //
          //						for(ItemStack i : event.getView().getBottomInventory())
          //							if(i == null || i.getType().equals(Material.AIR))
          //								count++;
          //
          //						if(count < 4) {
          //							p.sendMessage(ChatColor.RED + "[CE] Your Inventory is full!");
          //							return;
          //						}
          //
          //
          //					} else {

          if (event.getView().getBottomInventory().firstEmpty() < 0) {
            p.sendMessage(ChatColor.RED + "[CE] Your Inventory is full!");
            return;
          }

          //					}

          ItemMeta itemIm = item.getItemMeta();
          List<String> lore = itemIm.getLore();
          lore.remove(lore.size() - 1);
          itemIm.setLore(lore);
          item.setItemMeta(itemIm);
          type = "item " + itemIm.getDisplayName();
        }

        // TODO: Fix the cost for items
        //      rewrite the whole section to check which enchantment or item is selected

        successString =
            (ChatColor.GREEN + "[CE] You have created the " + type + ChatColor.GREEN + ".");

        if (!p.isOp() && cost != 0) {
          if (Main.econ.getBalance(p.getName()) >= cost) {
            EconomyResponse ecr = Main.econ.withdrawPlayer(p.getName(), cost);
            if (ecr.transactionSuccess())
              successString =
                  (ChatColor.GREEN
                      + "[CE] Purchased "
                      + type
                      + ChatColor.GREEN
                      + " for "
                      + cost
                      + " "
                      + ((cost == 1)
                          ? Main.econ.currencyNameSingular()
                          : Main.econ.currencyNamePlural()));
            else {
              Bukkit.getConsoleSender()
                  .sendMessage(ChatColor.RED + "[CE] An economy error has occured:");
              Bukkit.getConsoleSender().sendMessage(ChatColor.RED + ecr.errorMessage);
              p.closeInventory();
              return;
            }
          } else {
            p.sendMessage(ChatColor.RED + "[CE] You do not have enough money to buy this!");
            p.closeInventory();
            return;
          }
        }
        if (item != null) {
          if (!itemSet)
            if (type.startsWith("item") && event.getRawSlot() < topInv.getSize())
              p.setItemInHand(item);
            else p.getInventory().setItem(event.getSlot(), item);
          //					else {
          //						ItemStack cp = item.clone();
          //						ItemStack le = item.clone();
          //						ItemStack bo = item.clone();
          //
          //						String[] parts = swimsuit.parts;
          //
          //						cp.setType(Material.IRON_CHESTPLATE);
          //						le.setType(Material.IRON_LEGGINGS);
          //						bo.setType(Material.IRON_BOOTS);
          //
          //						im.setDisplayName(parts[1]);
          //						cp.setItemMeta(im);
          //						im.setDisplayName(parts[2]);
          //						le.setItemMeta(im);
          //						im.setDisplayName(parts[3]);
          //						bo.setItemMeta(im);
          //
          //						p.getInventory().addItem(item);
          //						p.getInventory().addItem(cp);
          //						p.getInventory().addItem(le);
          //						p.getInventory().addItem(bo);
          //
          //					}
        }
        p.closeInventory();
        p.sendMessage(successString);
        return;
      }

      if (event.getRawSlot() < topInv.getSize()) {

        p.closeInventory();
        try {
          p.openInventory(Tools.getNextInventory(clickedItem.getItemMeta().getDisplayName()));
        } catch (Exception e) {
          p.sendMessage(ChatColor.RED + "[CE] This feature is disabled.");
        }
      }
    }
  }