Solved Check for a clear inventory [Need Help]

Discussion in 'Plugin Development' started by Josh014, Dec 17, 2013.

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

    Josh014

    Hey,
    So I want to check if a players inventory is clear. I have this code but somehow it doesn't work...?

    Code:
    Code:java
    1. if(args[0].contains("Join") || args[0].contains("join")){
    2.  
    3. if(plugin.getConfig().get("Game.Name") != null){
    4. if(host.contains(player.getName())){
    5. player.sendMessage(Hoster);
    6. }
    7. else if(!host.contains(player.getName())){
    8. if(pi.getArmorContents() == null && pi.getContents() == null){
    9. join.add(player.getName());
    10. player.teleport(new Location(world, x, y, z, yaw, pitch));
    11. }
    12. else if(pi.getArmorContents() != null && pi.getContents() != null){
    13. player.sendMessage(clearInv);
    14. }
    15. }
    16. }
    17. else if(plugin.getConfig().get("Game.Name") == null){
    18. player.sendMessage(NoGame);
    19. }
    20. }


    And is it possible to check the crafting slots of a players inventory? So players can't hide stuff in it?
     
  2. Offline

    DrMedia

    I think you have to loop through each slot in the inventory and check if it's null or equals Material.AIR.
     
  3. Offline

    Josh014

    DrMedia
    Isn't there a more easy way to do it?
     
  4. Offline

    DrMedia

    I do not believe so..
    Code:
    boolean empty = false;
    for(ItemStack slot : player.getInventory().getContents()){
    if(slot != null || slot != new ItemStack(Material.AIR)) empty = true;
    }
     
  5. Offline

    Josh014

    DrMedia Hmm, where do I need to place this? What I mean is where can I place the rest of the code when it is checked if it is empty?
     
  6. Offline

    DrMedia

    Josh014
    Code:
    if(args[0].contains("Join") || args[0].contains("join")){
     
                    if(plugin.getConfig().get("Game.Name") != null){
                        if(host.contains(player.getName())){
                            player.sendMessage(Hoster);
                        }
                        else if(!host.contains(player.getName())){
                            boolean empty = true;
    for(ItemStack slot : pi.getInventory().getContents()){
    if(slot != null || slot != new ItemStack(Material.AIR)) empty = false;
                            if(empty){
                            join.add(player.getName());
                            player.teleport(new Location(world, x, y, z, yaw, pitch));
                            }
                            else if(pi.getArmorContents() != null && pi.getContents() != null){
                                player.sendMessage(clearInv);
                            }
                        }
                    }
                    else if(plugin.getConfig().get("Game.Name") == null){
                        player.sendMessage(NoGame);
                    }
                }
     
  7. Offline

    Josh014

    DrMedia
    Alright so I have this but it still won't work...

    Code:
    Code:java
    1. if(args[0].contains("Join") || args[0].contains("join")){
    2.  
    3. if(plugin.getConfig().get("Game.Name") != null){
    4. if(host.contains(player.getName())){
    5. player.sendMessage(Hoster);
    6. }
    7. else if(!host.contains(player.getName())){
    8. boolean empty = true;
    9. for(ItemStack slot : pi.getContents()){
    10. if(slot != null || slot != new ItemStack(Material.AIR)) empty = false;
    11. }
    12. if(empty){
    13. join.add(player.getName());
    14. player.teleport(new Location(world, x, y, z, yaw, pitch));
    15. }
    16. else if(!empty){
    17. player.sendMessage(clearInv);
    18. }
    19. }
    20. }
    21. else if(plugin.getConfig().get("Game.Name") == null){
    22. player.sendMessage(NoGame);
    23. }
    24. }


    It sends me the message when the inventory isn't empty
     
  8. Offline

    DrMedia

    Yes, that's what it is meant to do.

    Josh014

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  9. Offline

    Josh014

    DrMedia Oh I forgot to say that it sends a message too when my inventory is clear.
     
  10. Offline

    DrMedia

    Oh, ok... Replace the empty =false; with player.sendMessage(slot.getItemMeta().getDisplayName()); Tell me what is returned when you do this and your inventory is empty. Josh014

    Whoops, change it to this and try again.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  11. Offline

    Josh014

    DrMedia Uh lol? What do you mean? With what do I need to change it? XD
     
  12. Offline

    DrMedia

    Josh014
    Sorry, didn't explain it clear enough.
    Replace
    if(slot !=null|| slot !=new ItemStack(Material.AIR)) empty =false;
    With
    if(slot !=null|| slot.getType()!= Material.AIR) empty =false;

    Colours messed up there.. Just copy the line, it still has the code :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  13. Offline

    Josh014

    DrMedia Haha lemme check

    DrMedia
    I get an error in the console it says on line 103 and that's the line I changed.

    Code:
    Code:java
    1. if(args[0].contains("Join") || args[0].contains("join")){
    2.  
    3. if(plugin.getConfig().get("Game") != null){
    4. if(host.contains(player.getName())){
    5. player.sendMessage(Hoster);
    6. }
    7. if(join.contains(player.getName())){
    8. player.sendMessage(AlreadyIG);
    9. }
    10. else if(!host.contains(player.getName()) && !join.contains(player.getName())){
    11. boolean empty = true;
    12. for(ItemStack slot : pi.getContents()){
    13. if(slot != null || slot.getType()!= Material.AIR) empty =false;
    14. }
    15. if(empty){
    16. /*
    17.   * Code when join \/
    18.   */
    19. join.add(player.getName());
    20. player.teleport(new Location(world, x, y, z, yaw, pitch));
    21. /*
    22.   * Code when join /\
    23.   */
    24. }
    25. else if(!empty){
    26. player.sendMessage(clearInv);
    27. }
    28. }
    29. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  14. Offline

    DrMedia

    Care to post the error?
     
  15. Offline

    Josh014

    DrMedia
    org.bukkit.command.CommandException: Unhandled exception executing command 'e' i
    n plugin MCPEvent v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:19
    2)
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServe
    r.java:543)
    at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:923)
    at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :803)
    at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :28)
    at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat
    .java:47)
    at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    )
    at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134)
    at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    45)
    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    43)
    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    35)
    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :447)
    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17)
    Caused by: java.lang.NullPointerException
    at me.josh014.Event.Commands.onCommand(Commands.java:103)
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    ... 13 more
     
  16. Offline

    DrMedia

    Remove the Material.AIR bit.. Just use slot != null
     
  17. Offline

    Josh014

  18. Offline

    DrMedia

    :)
     
    Josh014 likes this.
  19. Offline

    Josh014

    DrMedia
    There is still one problem... It doesn't check for the armour contents... so I did this:
    Code:java
    1. if(slot != null && pi.getArmorContents() != null) empty = false;

    But that doesn't work...
     
  20. Offline

    DrMedia

     
  21. Offline

    Josh014

    DrMedia
    I did that and it still won't work. It sends me a message that I have to clear my inventory when my armourcontents is equal to null.
     
  22. Offline

    DrMedia

    Is your inventory also empty?
     
  23. Offline

    moose517

    just a suggestion, wouldn't it be easier to do args[0].equalsIgnoreCase("join") rather than the way you currently have it hehe?
     
  24. Offline

    Josh014

  25. Offline

    DrMedia


    He may have multiple commands such as /arena join1, /arena join2 etc? :p
     
  26. Offline

    moose517

    rather than see if args[0] contains Join or Join doing .equalsIgnoreCase("join") will return true if its any variation of join regardless of how caps are done, so it could be Join, join, jOin, JOIn etc.

    while true it would be a matter of if elsing like hes doing now just a little more compact. OP can do whatever of course it was just a suggestion :p
     
    DrMedia likes this.
  27. Offline

    Josh014

    moose517 Thank you. I was searching for it currently for my force stop command xD It activated the stop command too haha. Thank you man :)

    But eh how do I need to solve this problem?

    DrMedia Wups forgot to mention your name lol

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  28. Offline

    DrMedia

    Code:
    if(args[0].contains("Join") || args[0].contains("join")){
     
    if(plugin.getConfig().get("Game") != null){
    if(host.contains(player.getName())){
    player.sendMessage(Hoster);
    }
    if(join.contains(player.getName())){
    player.sendMessage(AlreadyIG);
    }
    else if(!host.contains(player.getName()) && !join.contains(player.getName())){
    boolean empty = true;
    for(ItemStack slot : pi.getContents()){
    if(slot != null) empty =false;
    }
    for(ItemStack slot : pi.getArmorContents()){
    if(slot != null) empty =false; pi.sendMessage(slot.getMetaData().getDisplayName());
    }
    if(empty){
    /*
    * Code when join \/
    */
    join.add(player.getName());
    player.teleport(new Location(world, x, y, z, yaw, pitch));
    /*
    * Code when join /\
    */
    }
    else if(!empty){
    player.sendMessage(clearInv);
    }
    }
    }
    
     
  29. Offline

    Josh014

    DrMedia
    Hmm I don't get it? You want to send a Playerinventory (pi) a message. Lol?
     
  30. Offline

    DrMedia

    Yes, to see what the result is.

    Oh wait, that's an inventory.. Whoops.. Make it player xD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
Thread Status:
Not open for further replies.

Share This Page