Solved Trying to make it so a player can't join the game unless their inventory is empty

Discussion in 'Plugin Development' started by PerezHD, Dec 20, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    PerezHD

    Hello, I am trying my best to attempt to stop players from joining a mini gamemode with their inventories having items inside them.

    My method on when the attempts to join the gamemode.
    Code:
            else if (args[0].equalsIgnoreCase("join")) {
                if (p.hasPermission("ffa.join")){
                    if (!p.getGameMode().equals(GameMode.SURVIVAL)){
                        p.sendMessage(ChatColor.RED + "You must be in survival to join FFA!");
                    }
                    else if (p.getGameMode().equals(GameMode.SURVIVAL)){
                        if (!p.isEmpty()){
                            p.sendMessage(ChatColor.RED + "Please empty your inventory before joining FFA!");
                        }
                        else if (p.isEmpty() && p.getGameMode().equals(GameMode.SURVIVAL)){
                            this.main.joinGame(p);
                            Bukkit.broadcastMessage(ChatColor.YELLOW + p.getName() + ChatColor.AQUA + " has just joined the FFA game!" + ChatColor.GRAY + " (" + ChatColor.YELLOW + "/ffa join" + ChatColor.GRAY + ")");
                        }
                }
              }
            }

    My method for the p.isEmpty()
    Code:
      public static boolean isEmpty(Player player) {
          int empty = 0;
          int armors = 0;
          for (ItemStack item : player.getInventory().getContents()) {
                  if (item == null || item.getType() == Material.AIR) empty++;
          }
          for (ItemStack armor : player.getInventory().getArmorContents()) {
                  if (armor == null || armor.getType() == Material.AIR) armors++;
          }
          return empty == player.getInventory().getContents().length && armors == player.getInventory().getArmorContents().length;
      }
    I've tested it in-game, it still lets the player join the mini game with items in their inventory.
    Please help me :)
     
  2. Offline

    Skionz

    @PerezHD Your using Entity#isEmpty() with checks if a vehicle is empty. You never actually invoked your method.
     
  3. Offline

    AdamQpzm

    @PerezHD You call p.isEmpty(), not isEmpty(p)...
    Ninja'd :(
     
    Skionz likes this.
  4. Offline

    PerezHD

    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page