Solved Inventory Clear Plugin Failing to Get target

Discussion in 'Plugin Development' started by PQE, Jan 30, 2017.

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

    PQE

    Hey Bukkit,

    For some reason this clear inventory command I am creating does not work, and I'm not sure why. Despite me returning true after pretty much every statement, if I specify a player (args[0]) it will actually run the code at the very bottom instead of what it is told to do. Anybody understand why?

    Code:
    else if (cmd.getName().equalsIgnoreCase("ci")) {
                if (args.length == 0) {
                    if (player.hasPermission("ci.others")) {
                        Player targetPlayer = player.getServer ().getPlayer(args[0]);
                        if (targetPlayer == null) {
                            sender.sendMessage(ChatColor.RED + "" + targetPlayer +" Was not found!");
                            return true;
                        }
                        PlayerInventory ti = targetPlayer.getInventory();
                        ti.clear();
                        sender.sendMessage(ChatColor.GREEN + "" + targetPlayer + "'s inventory has been cleared!");
                        return true;
                    }
                    return true;
                }
                pi.clear();
                sender.sendMessage(ChatColor.GREEN + "Inventory cleared!");
            }  
    
    Code:
    name: Inventories
    main: com.alex.inventoriesPlugin.Inventories
    version: 2.0
    description: You can clear inventories, or give yourself a kit.
    depend: [Vault]
    
    commands:
      kit:
        usage: /<command>
        description: gives the player a stone sword
      
      ci:
        usage: /ci
        description: clears your inventory
      
    permissions:
      ci.others:
        description: Allows you to clear the inventory of another player
        default: op
    
    Thanks!
     
    Last edited: Jan 31, 2017
  2. Offline

    JanTuck

    You are checking if there are no args.
     
  3. Offline

    PQE

    *facepalm*.. I was going by the args[0] rule. Thanks
     
  4. Offline

    DoggyCode™

    Don't. If the lenght is 0, the array is empty, hence you can't get the index 0.

    It works like this:

    Index...: - 0 1 2 3 4 5 6 7 8
    Length: 0 1 2 3 4 5 6 7 8 9

    And mark thread as solved is solved.
     
Thread Status:
Not open for further replies.

Share This Page