How to remove armor if player wears armor?

Discussion in 'Plugin Development' started by dabananaboat, Oct 27, 2011.

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

    dabananaboat

    [EDIT]: Solved, thanks a lot guys!

    Hey,

    I'm working on a plugin which sets your armor, but can also remove it.
    However, I am not sure how to remove armor if the player wears it.

    Armor material doesn't matter, it just removes it.
     
  2. Offline

    Kaikz

    Get their inventory, then .setHelmet, .setChestplate, .setLeggings and .setBoots to nothing (Air).
     
  3. Offline

    dabananaboat

    I'm probably doing it wrong.

    I'm getting an error and an instant disconnect.


    This calls it:
    Code:
    else if(armorType.equalsIgnoreCase("remove")) {
        RemoveArmor(player);
    }
    
    public void RemoveArmor(Player player) {
        player.getInventory().setHelmet(new ItemStack(Material.AIR, 1));
        player.getInventory().setChestplate(new ItemStack(Material.AIR, 1));
        player.getInventory().setLeggings(new ItemStack(Material.AIR, 1));
        player.getInventory().setBoots(new ItemStack(Material.AIR, 1));
    }
    What am I doing wrong?

    Would be nice if people actually respond.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  4. Try setting it to null instead of air.
     
  5. Offline

    dabananaboat

    I tried that, but I get the same error.
     
  6. Code:java
    1. public void RemoveArmor(Player player) {
    2. player.getInventory().setHelmet(null);
    3. player.getInventory().setChestplate(null);
    4. player.getInventory().setLeggings(null);
    5. player.getInventory().setBoots(null);
    6. }


    Did you really try that?
    If yes, post your whole code.
     
  7. My guess is that you did
    Code:java
    1.  
    2. public void RemoveArmor(Player player) {
    3. player.getInventory().setHelmet(new ItemStack(null, 1));
    4. player.getInventory().setChestplate(new ItemStack(null, 1));
    5. player.getInventory().setLeggings(new ItemStack(null, 1));
    6. player.getInventory().setBoots(new ItemStack(null, 1));
    7. }

    But just do as @Pandemoneus said.
     
  8. Offline

    dabananaboat

    @Pandemoneus
    I really tried that, but it just gave the same error.

    My current (and still bugging) code for that:
    Code:
    //RemoveArmor Method.
    public void RemoveArmor(Player player) {
        player.getInventory().setHelmet(new ItemStack(0, 1));
        player.getInventory().setChestplate(new ItemStack(0, 1));
        player.getInventory().setLeggings(new ItemStack(0, 1));
        player.getInventory().setBoots(new ItemStack(0, 1));
    }
    My set armor method works as I wanted to.
    This is my entire '/dbb armor <armor>' command.

    Code:
    else if(type.equalsIgnoreCase("armor")) {
        String armorType = args[1].toString();
        if(armorType.equalsIgnoreCase("leather")) {
            SetArmor("leather", player);
        }
    
        else if(armorType.equalsIgnoreCase("iron")) {
            SetArmor("iron", player);
        }
    
        else if(armorType.equalsIgnoreCase("gold")) {
            SetArmor("gold", player);
        }
    
        else if(armorType.equalsIgnoreCase("diamond")) {
            SetArmor("diamond", player);
        }
    
        else if(armorType.equalsIgnoreCase("chainmail")) {
            SetArmor("chainmail", player);
        }
    
        else if(armorType.equalsIgnoreCase("remove")) {
            RemoveArmor(player);
        }
    
        else {
            player.sendMessage("Unknown armor type.");
            SendCommands(player);
        }
    }
    @r3Fuze
    I got it set to
    Code:
    //RemoveArmor Method.
    public void RemoveArmor(Player player) {
        player.getInventory().setHelmet(new ItemStack(null, 1));
        player.getInventory().setChestplate(new ItemStack(null, 1));
        player.getInventory().setLeggings(new ItemStack(null, 1));
        player.getInventory().setBoots(new ItemStack(null, 1));
    }
    the bug is partially solved, I don't get kicked anymore.
    However, I still get a NullPointerException

    The armor doesn't get removed.

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

    nisovin

    Are you sure you put the code exactly as he had it? It should work fine.

    Edit: That's NOT what he said to put.
     
  10. Offline

    dabananaboat

    *facepalms* I'm an idiot.

    Forgot to remove new ItemStack.
    It works now. Thanks a lot guys!
     
    Limeth and r3Fuze like this.
Thread Status:
Not open for further replies.

Share This Page