Skip to main content

@EventHandler

  @EventHandler
  public void onPlayerInteract(PlayerInteractEvent event)
  {
    if ((event.getAction() != Action.RIGHT_CLICK_BLOCK) && (event.getAction() != Action.RIGHT_CLICK_AIR))
    {
      return;
    }
    Player player = event.getPlayer();
    for (String pouch : ConfigUtils.getPouches().getConfigurationSection("").getKeys(false))
    {
      if ((event.getHand() == EquipmentSlot.HAND) && (player.getInventory().getItemInMainHand().isSimilar(ItemUtils.getPouch(pouch, "item"))) || ((event.getHand() == EquipmentSlot.OFF_HAND) && (!player.getInventory().getItemInMainHand().isSimilar(ItemUtils.getPouch(pouch, "item"))) && (player.getInventory().getItemInOffHand().isSimilar(ItemUtils.getPouch(pouch, "item")))))
      {
          String [] reward = ConfigUtils.getPouches().getString(pouch + ".item.reward").split("-");
        if (!event.isCancelled())
        {
          event.setCancelled(true);
        }
          if (PouchManager.openingPlayers.containsKey(player))
        {
          for (String soundlist : ConfigUtils.getConfig().getStringList("sounds.cooldown"))
          {
            String[] sound = soundlist.split(";");
            player.playSound(player.getLocation(), Sound.valueOf(sound[0]), Float.parseFloat(sound[1]), Float.parseFloat(sound[2]));
          }
          player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(ColorUtils.addColors(ConfigUtils.getMessages().getString("cooldown"))));
          return;
        }
          if (ConfigUtils.getPouches().getString(pouch + ".item.currency").equalsIgnoreCase("EXP"))
          {
          if (player.getTotalExperience() + Integer.parseInt(reward[1]) > ConfigUtils.getConfig().getInt("maximum-experience"))
          {
              player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(ColorUtils.addColors(ConfigUtils.getMessages().getString("maximum-experience"))));
            for (String soundlist : ConfigUtils.getConfig().getStringList("sounds.maximum-amount"))
            {
              String[] sound = soundlist.split(";");
              player.playSound(player.getLocation(), Sound.valueOf(sound[0]), Float.parseFloat(sound[1]), Float.parseFloat(sound[2]));
            }
            return;
          }
          }
          else if (ConfigUtils.getPouches().getString(pouch + ".item.currency").equalsIgnoreCase("VAULT"))
          {
            if (EconomyManager.getEconomy() == null)
            {
              player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(ColorUtils.addColors(ConfigUtils.getMessages().getString("economy-not-found"))));
            for (String soundlist : ConfigUtils.getConfig().getStringList("sounds.economy-not-found"))
            {
              String[] sound = soundlist.split(";");
              player.playSound(player.getLocation(), Sound.valueOf(sound[0]), Float.parseFloat(sound[1]), Float.parseFloat(sound[2]));
            }
              return;
            }
            if (EconomyManager.getEconomy().getBalance(player) + Integer.parseInt(reward[1]) > ConfigUtils.getConfig().getInt("maximum-money"))
          {
              player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(ColorUtils.addColors(ConfigUtils.getMessages().getString("maximum-money"))));
            for (String soundlist : ConfigUtils.getConfig().getStringList("sounds.maximum-amount"))
            {
              String[] sound = soundlist.split(";");
              player.playSound(player.getLocation(), Sound.valueOf(sound[0]), Float.parseFloat(sound[1]), Float.parseFloat(sound[2]));
            }
            return;
          }
          }
          if ((event.getHand() == EquipmentSlot.HAND) && (player.getInventory().getItemInMainHand().isSimilar(ItemUtils.getPouch(pouch, "item"))))
          {
            player.getInventory().getItemInMainHand().setAmount(player.getInventory().getItemInMainHand().getAmount() - 1);
          }
          else if ((event.getHand() == EquipmentSlot.OFF_HAND) && (!player.getInventory().getItemInMainHand().isSimilar(ItemUtils.getPouch(pouch, "item"))) && (player.getInventory().getItemInOffHand().isSimilar(ItemUtils.getPouch(pouch, "item"))))
          {
            player.getInventory().getItemInOffHand().setAmount(player.getInventory().getItemInOffHand().getAmount() - 1);
          }
        ArrayList<Color> colors = new ArrayList<Color>();
        for (String fireworkColor : ConfigUtils.getPouches().getStringList(pouch + ".firework-colors"))
        {
          colors.add(ColorUtils.parseColor(fireworkColor));
        }
          PouchManager.openPouch(event.getPlayer(), Integer.parseInt(reward[0]), Integer.parseInt(reward[1]), ConfigUtils.getPouches().getString(pouch + ".item.currency"), ConfigUtils.getPouches().getStringList(pouch + ".messages"), ConfigUtils.getPouches().getStringList(pouch + ".broadcast"), colors);
          break;
      }
    }
  }