private void removeBalance(GamePlayer p, int x) {
   if (SkyWarsReloaded.getCfg().usingExternalEcomony()) {
     SkyWarsReloaded.econ.withdrawPlayer(p.getP(), x);
   } else {
     p.setBalance(p.getBalance() - x);
   }
 }
 public boolean canPurchase(GamePlayer gamePlayer, ParticleItem effect) {
   if (SkyWarsReloaded.getCfg().usingExternalEcomony()) {
     return effect.getCost() > 0
         && (SkyWarsReloaded.econ.getBalance(gamePlayer.getP()) >= effect.getCost());
   } else {
     return effect.getCost() > 0 && (gamePlayer.getBalance() >= effect.getCost());
   }
 }
 @Override
 public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
   boolean hasPerm = false;
   if (!(sender instanceof Player)) {
     sender.sendMessage(new Messaging.MessageFormatter().format("error.must-be-player"));
   } else if (sender instanceof Player) {
     Player player = (Player) sender;
     if (SkyWarsReloaded.perms.has(player, "swr.play")) {
       hasPerm = true;
     }
   } else {
     sender.sendMessage(new Messaging.MessageFormatter().format("error.cmd-no-perm"));
   }
   if (hasPerm) {
     if (args.length == 1) {
       if (sender instanceof Player) {
         Player player = (Player) sender;
         GamePlayer gPlayer = SkyWarsReloaded.getPC().getPlayer(player);
         Game game = gPlayer.getGame();
         if (game != null) {
           game.deletePlayer(gPlayer, true);
           if (game.getState() == GameState.PREGAME || game.getState() == GameState.PLAYING) {
             game.checkForWinner();
           }
         } else {
           if (SkyWarsReloaded.get().getConfig().getBoolean("gameVariables.allowSpectating")) {
             if (SkyWarsReloaded.getSpectate().isSpectator(player)) {
               SkyWarsReloaded.getSpectate().setSpectating(player, false, true);
               gPlayer.setSpectating(false);
               gPlayer.getSpecGame().removeSpectator(gPlayer);
               String world = SkyWarsReloaded.get().getConfig().getString("spawn.world");
               int x = SkyWarsReloaded.get().getConfig().getInt("spawn.x");
               int y = SkyWarsReloaded.get().getConfig().getInt("spawn.y");
               int z = SkyWarsReloaded.get().getConfig().getInt("spawn.z");
               Location loc =
                   new Location(SkyWarsReloaded.get().getServer().getWorld(world), x, y, z);
               player.teleport(loc);
             }
           }
         }
       }
     } else {
       sender.sendMessage(ChatColor.RED + "USAGE: /swr quit");
     }
   }
   return true;
 }
  public BuyProjEffectMenu(final GamePlayer gamePlayer) {
    List<ParticleItem> availableItems = SkyWarsReloaded.getProjC().getParticleItems();

    int rowCount = menuSlotsPerRow;
    while (rowCount < 45 && rowCount < menuSize) {
      rowCount += menuSlotsPerRow;
    }

    SkyWarsReloaded.getIC()
        .create(
            gamePlayer.getP(),
            menuName,
            rowCount,
            new IconMenu.OptionClickEventHandler() {
              @Override
              public void onOptionClick(IconMenu.OptionClickEvent event) {
                if (gamePlayer.inGame()) {
                  gamePlayer.getP().closeInventory();
                  return;
                }

                event.setWillClose(false);
                event.setWillDestroy(false);

                String name =
                    ChatColor.stripColor(
                        ChatColor.translateAlternateColorCodes('&', event.getName()));
                if (name.equalsIgnoreCase(
                    ChatColor.stripColor(
                        ChatColor.translateAlternateColorCodes(
                            '&',
                            new Messaging.MessageFormatter()
                                .format("menu.return-to-lobbymenu"))))) {
                  gamePlayer.getP().closeInventory();
                  if (!gamePlayer.inGame()) {
                    SkyWarsReloaded.get()
                        .getServer()
                        .getScheduler()
                        .scheduleSyncDelayedTask(
                            SkyWarsReloaded.get(),
                            new Runnable() {
                              @Override
                              public void run() {
                                new LobbyMainMenu(gamePlayer);
                              }
                            },
                            2);
                  }
                  return;
                }

                ParticleItem effect = SkyWarsReloaded.getProjC().getByName(name);

                if (effect == null) {
                  return;
                }

                if (!canPurchase(gamePlayer, effect)) {
                  event
                      .getPlayer()
                      .sendMessage(
                          new Messaging.MessageFormatter().format("error.not-enough-balance-shop"));
                  return;
                } else {
                  if (!hasEffectPermission(gamePlayer, effect)) {
                    removeBalance(gamePlayer, effect.getCost());
                    gamePlayer.addPerm(premissionPrefix + effect.getEffect().toLowerCase());
                    SkyWarsReloaded.getScore().getScoreboard(event.getPlayer());
                    if (!gamePlayer.inGame()) {
                      SkyWarsReloaded.get()
                          .getServer()
                          .getScheduler()
                          .scheduleSyncDelayedTask(
                              SkyWarsReloaded.get(),
                              new Runnable() {
                                @Override
                                public void run() {
                                  updateBuyEffectMenu(gamePlayer);
                                }
                              },
                              2);
                    }
                  }
                }
              }
            });

    ArrayList<Integer> placement =
        new ArrayList<Integer>(
            Arrays.asList(
                0, 2, 4, 6, 8, 9, 11, 13, 15, 17, 18, 20, 22, 24, 26, 27, 29, 31, 33, 35));

    for (int iii = 0; iii < availableItems.size(); iii++) {
      if (iii >= menuSize) {
        break;
      }

      ParticleItem effect = availableItems.get(iii);
      List<String> loreList = Lists.newLinkedList();
      if (effect.getCost() != -1) {
        if (!gamePlayer.inGame()) {
          if (!hasEffectPermission(gamePlayer, effect)) {
            if (SkyWarsReloaded.getCfg().usingExternalEcomony()) {
              loreList.add(
                  "\247r\2476Price\247f: \247"
                      + (SkyWarsReloaded.econ.getBalance(gamePlayer.getP()) >= effect.getCost()
                          ? 'a'
                          : 'c')
                      + effect.getCost());
            } else {
              loreList.add(
                  "\247r\2476Price\247f: \247"
                      + (gamePlayer.getBalance() >= effect.getCost() ? 'a' : 'c')
                      + effect.getCost());
            }
            loreList.add(" ");
          } else {
            loreList.add(new Messaging.MessageFormatter().format("menu.buyprojeffect-purchased"));
          }

          if (gamePlayer.getP() != null) {
            SkyWarsReloaded.getIC()
                .setOption(
                    gamePlayer.getP(),
                    placement.get(iii),
                    SkyWarsReloaded.getCfg().getTrailEffectsMenuItem(),
                    effect.getName(),
                    loreList.toArray(new String[loreList.size()]));
          }
        }
      }
    }

    List<String> loreList5 = Lists.newLinkedList();
    SkyWarsReloaded.getIC()
        .setOption(
            gamePlayer.getP(),
            44,
            new ItemStack(Material.TORCH, 1),
            new Messaging.MessageFormatter().format("menu.return-to-lobbymenu"),
            loreList5.toArray(new String[loreList5.size()]));

    if (!gamePlayer.inGame()) {
      SkyWarsReloaded.getIC().show(gamePlayer.getP());
    }
  }
 public boolean hasEffectPermission(GamePlayer player, ParticleItem effect) {
   return (player.getP().isOp()
       || player.getP().hasPermission(premissionPrefix + effect.getEffect().toLowerCase())
       || player.hasPerm(premissionPrefix + effect.getEffect().toLowerCase()));
 }
  public void updateBuyEffectMenu(final GamePlayer gamePlayer) {
    if (!SkyWarsReloaded.getIC().has(gamePlayer.getP())
        || gamePlayer.inGame()
        || !SkyWarsReloaded.getIC()
            .getMenu(gamePlayer.getP())
            .getName()
            .equalsIgnoreCase(menuName)) {
      gamePlayer.getP().closeInventory();
      return;
    } else {
      List<ParticleItem> availableItems = SkyWarsReloaded.getProjC().getParticleItems();
      ArrayList<Integer> placement =
          new ArrayList<Integer>(
              Arrays.asList(
                  0, 2, 4, 6, 8, 9, 11, 13, 15, 17, 18, 20, 22, 24, 26, 27, 29, 31, 33, 35));

      for (int iii = 0; iii < availableItems.size(); iii++) {
        if (iii >= menuSize) {
          break;
        }

        ParticleItem effect = availableItems.get(iii);
        List<String> loreList = Lists.newLinkedList();
        if (effect.getCost() != -1) {
          if (!gamePlayer.inGame()) {
            if (!hasEffectPermission(gamePlayer, effect)) {
              if (SkyWarsReloaded.getCfg().usingExternalEcomony()) {
                loreList.add(
                    "\247r\2476Price\247f: \247"
                        + (SkyWarsReloaded.econ.getBalance(gamePlayer.getP()) >= effect.getCost()
                            ? 'a'
                            : 'c')
                        + effect.getCost());
              } else {
                loreList.add(
                    "\247r\2476Price\247f: \247"
                        + (gamePlayer.getBalance() >= effect.getCost() ? 'a' : 'c')
                        + effect.getCost());
              }
              loreList.add(" ");
            } else {
              loreList.add(new Messaging.MessageFormatter().format("menu.buyprojeffect-purchased"));
            }

            if (gamePlayer.getP() != null) {
              SkyWarsReloaded.getIC()
                  .setOption(
                      gamePlayer.getP(),
                      placement.get(iii),
                      SkyWarsReloaded.getCfg().getTrailEffectsMenuItem(),
                      effect.getName(),
                      loreList.toArray(new String[loreList.size()]));
            }
          }
        }
      }

      List<String> loreList5 = Lists.newLinkedList();
      SkyWarsReloaded.getIC()
          .setOption(
              gamePlayer.getP(),
              44,
              new ItemStack(Material.TORCH, 1),
              new Messaging.MessageFormatter().format("menu.return-to-lobbymenu"),
              loreList5.toArray(new String[loreList5.size()]));

      if (!gamePlayer.inGame()) {
        SkyWarsReloaded.getIC().update(gamePlayer.getP());
      }
    }
  }
  public void openKitMenu(final GamePlayer gamePlayer) {
    List<GameKit> availableKits = Lists.newArrayList(kitMap.values());

    int rowCount = menuSlotsPerRow;
    while (rowCount < availableKits.size() && rowCount < menuSize) {
      rowCount += menuSlotsPerRow;
    }

    SkyWarsReloaded.getIC()
        .create(
            gamePlayer.getP(),
            menuName,
            rowCount,
            new IconMenu.OptionClickEventHandler() {
              @Override
              public void onOptionClick(IconMenu.OptionClickEvent event) {
                if (gamePlayer.isPlaying()) {
                  event
                      .getPlayer()
                      .sendMessage(
                          new Messaging.MessageFormatter().format("error.can-not-pick-kit"));
                  return;
                }

                if (gamePlayer.getGame().getState() != GameState.INLOBBY) {
                  event
                      .getPlayer()
                      .sendMessage(
                          new Messaging.MessageFormatter().format("error.can-not-pick-kit"));
                  return;
                }

                GameKit kit =
                    SkyWarsReloaded.getKC().getByName(ChatColor.stripColor(event.getName()));
                if (kit == null) {
                  return;
                }

                if (!hasPermission(event.getPlayer(), kit)
                    && !hasFreePermission(event.getPlayer(), kit)) {
                  event
                      .getPlayer()
                      .sendMessage(
                          new Messaging.MessageFormatter().format("error.no-permission-kit"));
                  return;
                }

                if (!hasFreePermission(event.getPlayer(), kit)) {
                  if (!canPurchase(gamePlayer, kit)) {
                    event
                        .getPlayer()
                        .sendMessage(
                            new Messaging.MessageFormatter().format("error.not-enough-balance"));
                    return;
                  } else {
                    SkyWarsReloaded.econ.withdrawPlayer(gamePlayer.getP(), kit.getCost());
                  }
                }

                event.setWillClose(true);
                event.setWillDestroy(true);

                gamePlayer.setSelectedKit(kit);
                gamePlayer.setKitSelected(true);

                event
                    .getPlayer()
                    .sendMessage(
                        new Messaging.MessageFormatter()
                            .setVariable("kit", kit.getName())
                            .format("game.enjoy-kit"));
              }
            });

    for (int iii = 0; iii < availableKits.size(); iii++) {
      if (iii >= menuSize) {
        break;
      }

      GameKit kit = availableKits.get(iii);
      List<String> loreList = Lists.newLinkedList();
      boolean canPurchase = false;

      if (isPurchaseAble(kit)) {
        loreList.add(
            "\247r\2476Price\247f: \247"
                + (SkyWarsReloaded.econ.getBalance(gamePlayer.getP()) >= kit.getCost() ? 'a' : 'c')
                + kit.getCost());
        loreList.add(" ");

        if (canPurchase(gamePlayer, kit)) {
          canPurchase = true;
        }

      } else if (!hasPermission(gamePlayer.getP(), kit)) {
        loreList.add("No permission");
        loreList.add(" ");

      } else {
        canPurchase = true;
      }

      loreList.addAll(kit.getLores());

      SkyWarsReloaded.getIC()
          .setOption(
              gamePlayer.getP(),
              kit.getPosition(),
              kit.getIcon(),
              "\247r\247" + (canPurchase ? 'a' : 'c') + kit.getName(),
              loreList.toArray(new String[loreList.size()]));
    }

    SkyWarsReloaded.getIC().show(gamePlayer.getP());
  }
 public void givePotionEffects(GamePlayer gamePlayer, GameKit kit) {
   for (PotionEffect pEffect : kit.getPotionEffects()) {
     gamePlayer.getP().addPotionEffect(pEffect);
   }
 }
 public boolean canPurchase(GamePlayer gamePlayer, GameKit kit) {
   return kit.getCost() > 0
       && (SkyWarsReloaded.econ.getBalance(gamePlayer.getP()) >= kit.getCost());
 }