Example #1
0
 @EventHandler(priority = EventPriority.HIGHEST)
 public void SignChangeEvent(SignChangeEvent e) {
   if (e.getLine(0).equals("[CustomEnchant]"))
     if (!e.getPlayer().isOp()) e.setCancelled(true);
     else {
       String ench = e.getLine(1);
       CEnchantment ce = Tools.getEnchantmentByDisplayname(ench);
       if (ce == null) ce = Tools.getEnchantmentByOriginalname(ench);
       if (ce == null)
         for (CEnchantment ceT : Main.enchantments)
           if (Tools.checkForEnchantment(ench, ceT)) ce = ceT;
       if (ce == null) {
         e.getPlayer()
             .sendMessage(ChatColor.RED + "[CE] Could not find Custom Enchantment " + ench + ".");
         e.setCancelled(true);
         return;
       }
       try {
         Integer.parseInt(e.getLine(3).replaceAll("\\D+", ""));
       } catch (NumberFormatException ex) {
         e.getPlayer().sendMessage(ChatColor.RED + "[CE] The cost you entered is invalid.");
         e.setCancelled(true);
         return;
       }
       e.getPlayer()
           .sendMessage(
               ChatColor.GREEN
                   + "[CE] Successfully created a sign shop for the enchantment "
                   + ench
                   + ".");
     }
 }
Example #2
0
  @EventHandler(priority = EventPriority.HIGHEST)
  public void PlayerInteractEvent(PlayerInteractEvent e) {

    Player p = e.getPlayer();

    CEventHandler.handleEvent(p, e, interact);

    if (e.getAction().toString().startsWith("LEFT")) CEventHandler.handleEvent(p, e, interactL);
    else if (e.getAction().toString().startsWith("RIGHT")) {
      CEventHandler.handleEvent(p, e, interactR);

      // Check if the player has put armor on by rightclicking
      if (p.getItemInHand().getType() != Material.AIR) {
        ItemStack i = p.getItemInHand();
        String mat = i.getType().toString();
        PlayerInventory inv = p.getInventory();
        if ((mat.endsWith("BOOTS") && inv.getBoots() == null)
            || (mat.endsWith("LEGGINGS") && inv.getLeggings() == null)
            || (mat.endsWith("CHESTPLATE") && inv.getChestplate() == null)
            || (mat.endsWith("HELMET") && inv.getHelmet() == null))
          CEventHandler.handleArmor(p, e.getItem(), false, e);
      }
    }

    // Sign shop
    if (e.getClickedBlock() != null && e.getClickedBlock().getType().toString().contains("SIGN"))
      if (((Sign) e.getClickedBlock().getState()).getLine(0).equals("[CustomEnchant]")) {
        if (!Main.hasEconomy) {
          p.performCommand("ce menu");
        } else if (p.getItemInHand().getType() != Material.AIR) {
          Sign sign = ((Sign) e.getClickedBlock().getState());
          CEnchantment ce = Tools.getEnchantmentByDisplayname(sign.getLine(1));
          if (ce == null) Tools.getEnchantmentByOriginalname(sign.getLine(1));
          if (ce == null)
            for (CEnchantment ceT : Main.enchantments)
              if (Tools.checkForEnchantment(sign.getLine(1), ceT)) ce = ceT;
          if (ce == null) return;

          ItemStack inHand = p.getItemInHand();
          if (!Tools.isApplicable(inHand, ce)) {
            p.sendMessage(ChatColor.RED + "[CE] This enchantment can not be applied to this item.");
            return;
          }

          int cost = 0;
          try {
            cost = Integer.parseInt(sign.getLine(3).replaceAll("\\D+", ""));
          } catch (NumberFormatException ex) {
            return;
          }

          if (inHand.hasItemMeta() && inHand.getItemMeta().hasLore()) {
            List<String> lore = inHand.getItemMeta().getLore();
            for (int i = 0; i < lore.size(); i++)
              if (Tools.checkForEnchantment(lore.get(i), ce)) {
                int newLevel = Tools.getLevel(lore.get(i)) + 1;
                if (newLevel <= ce.getEnchantmentMaxLevel()) {
                  if (Main.econ.getBalance(p.getName()) >= cost) {
                    EconomyResponse ecr = Main.econ.withdrawPlayer(p.getName(), cost);
                    if (ecr.transactionSuccess())
                      p.sendMessage(
                          ChatColor.GREEN
                              + "[CE] Upgraded enchantment "
                              + ce.getDisplayName()
                              + ChatColor.RESET
                              + ""
                              + ChatColor.GREEN
                              + " for "
                              + cost
                              + " "
                              + ((cost == 1)
                                  ? Main.econ.currencyNameSingular()
                                  : Main.econ.currencyNamePlural()));
                    else {
                      p.sendMessage(ChatColor.RED + "[CE] An economy error has occured:");
                      p.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;
                  }
                  lore.set(i, ce.getDisplayName() + " " + Tools.intToLevel(newLevel));
                  ItemMeta im = inHand.getItemMeta();
                  im.setLore(lore);
                  inHand.setItemMeta(im);
                  return;
                } else {
                  p.sendMessage(
                      ChatColor.RED
                          + "[CE] You already have the maximum level of this enchantment!");
                  return;
                }
              }
          }

          List<String> lore = new ArrayList<String>();
          ItemMeta im = inHand.getItemMeta();

          if (inHand.getItemMeta().hasLore()) lore = im.getLore();

          if (Main.econ.getBalance(p.getName()) >= cost) {
            EconomyResponse ecr = Main.econ.withdrawPlayer(p.getName(), cost);
            if (ecr.transactionSuccess())
              p.sendMessage(
                  ChatColor.GREEN
                      + "[CE] Bought enchantment "
                      + ce.getDisplayName()
                      + ChatColor.RESET
                      + ""
                      + 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;
          }

          lore.add(ce.getDisplayName() + " I");
          im.setLore(lore);
          inHand.setItemMeta(im);
          return;

        } else {
          p.sendMessage(ChatColor.RED + "[CE] You do not have an item in your hand.");
          return;
        }
      }
  }
Example #3
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.");
        }
      }
    }
  }