Failing to check armour?

Discussion in 'Plugin Development' started by CeramicTitan, Oct 21, 2012.

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

    CeramicTitan

    Code:
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
     
    public class revertCommand implements CommandExecutor {
     
    ItemStack diamonds = new ItemStack(Material.DIAMOND, 24);
    ItemStack helmet_diamonds = new ItemStack(Material.DIAMOND_HELMET, 1);
    ItemStack chestplate_diamonds = new ItemStack(Material.DIAMOND_CHESTPLATE, 1);
    ItemStack leggings_diamonds = new ItemStack(Material.DIAMOND_LEGGINGS, 1);
    ItemStack boots_diamonds = new ItemStack(Material.DIAMOND_BOOTS, 1);
     
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if(cmd.getName().equalsIgnoreCase("revert")){
    Player p = (Player)sender;
    ItemStack helmet = p.getInventory().getHelmet();
    ItemStack chest = p.getInventory().getChestplate();
    ItemStack pants = p.getInventory().getLeggings();
    ItemStack boots = p.getInventory().getBoots();
    if(p instanceof Player){
    if(args.length == 1){
    if(args[0].equalsIgnoreCase("diamond")){
    if(boots == boots_diamonds && boots != null){
    if(pants == leggings_diamonds && pants != null){
    if(chest == chestplate_diamonds){
    if(helmet == helmet_diamonds){
    System.out.println("passed!");
    }else{
    sender.sendMessage("you forgetted your tard helmet.");
    }
    }else{
    sender.sendMessage("y u no has a chestplate?!");
    }
    }else{
    sender.sendMessage("Put on some pants boy!");
    }
    }else{
    sender.sendMessage("Your boots werent diamond or are null");
    }
    }else{
    p.sendMessage(ChatColor.RED + "Incorrect usage: /revert <armour type>");
    }
    }
     
    }
    }
    return true;
    }

    Always returns this: sender.sendMessage("Your boots werent diamond or are null");
     
  2. Offline

    Georgeto

    boots == boots_diamonds will always be false, because the both variables contain pointers to their instances. With == you compare the pointers of the both variables, and they are not equal.
    You have to compare the TypeId of the Items.

    Code:
    package me.test;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
     
    public class revertCommand implements CommandExecutor {
     
        ItemStack diamonds = new ItemStack(Material.DIAMOND, 24);
        ItemStack helmet_diamonds = new ItemStack(Material.DIAMOND_HELMET, 1);
        ItemStack chestplate_diamonds = new ItemStack(Material.DIAMOND_CHESTPLATE, 1);
        ItemStack leggings_diamonds = new ItemStack(Material.DIAMOND_LEGGINGS, 1);
        ItemStack boots_diamonds = new ItemStack(Material.DIAMOND_BOOTS, 1);
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("revert")) {
            Player p = (Player) sender;
            ItemStack helmet = p.getInventory().getHelmet();
            ItemStack chest = p.getInventory().getChestplate();
            ItemStack pants = p.getInventory().getLeggings();
            ItemStack boots = p.getInventory().getBoots();
            if (p instanceof Player) {
            if (args.length == 1) {
                if (args[0].equalsIgnoreCase("diamond")) {
                if (boots != null && boots.getTypeId() == boots_diamonds.getTypeId()) {
                    if (pants != null && pants.getTypeId() == leggings_diamonds.getTypeId()) {
                    if (chest != null && chest.getTypeId() == chestplate_diamonds.getTypeId()) {
                        if (helmet != null && helmet.getTypeId() == helmet_diamonds.getTypeId()) {
                        System.out.println("passed!");
                        } else {
                        sender.sendMessage("you forgetted your tard helmet.");
                        }
                    } else {
                        sender.sendMessage("y u no has a chestplate?!");
                    }
                    } else {
                    sender.sendMessage("Put on some pants boy!");
                    }
                } else {
                    sender.sendMessage("Your boots werent diamond or are null");
                }
                } else {
                p.sendMessage(ChatColor.RED + "Incorrect usage: /revert <armour type>");
                }
            }
     
            }
        }
        return true;
        }
    }
     
    CeramicTitan likes this.
  3. Offline

    CeramicTitan

    Thanks mate this was awesome :D

    Code:
    package me.ceramictitan.NanoSuits.Commands;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
     
    public class revertCommand implements CommandExecutor {
     
        ItemStack diamonds = new ItemStack(Material.DIAMOND, 24);
        ItemStack helmet_diamonds = new ItemStack(Material.DIAMOND_HELMET, 1);
        ItemStack chestplate_diamonds = new ItemStack(Material.DIAMOND_CHESTPLATE, 1);
        ItemStack leggings_diamonds = new ItemStack(Material.DIAMOND_LEGGINGS, 1);
        ItemStack boots_diamonds = new ItemStack(Material.DIAMOND_BOOTS, 1);
        ItemStack air = new ItemStack(Material.AIR, 1);
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("revert")) {
            Player p = (Player) sender;
            ItemStack helmet = p.getInventory().getHelmet();
            ItemStack chest = p.getInventory().getChestplate();
            ItemStack pants = p.getInventory().getLeggings();
            ItemStack boots = p.getInventory().getBoots();
            if (p instanceof Player) {
            if (args.length == 1) {
                if (args[0].equalsIgnoreCase("diamond")) {
                if (boots.getTypeId() == boots_diamonds.getTypeId()) {
                    System.out.println("passed!");
                }else if (boots.getTypeId() == air.getTypeId()) {
                    System.out.println("passed!");
                }
                }
            }
            }
        }
        return true;
                }
     
    }
    This is my updated code. :/ when the player wears nothing I get an NPE.

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

    Georgeto

    If the Player wears nothing, helmet, chest, pants and boots are null.
    To prevent a NPE you have to check first if boots != null.
    Code:
    ...
    if (boots != null && boots.getTypeId() == boots_diamonds.getTypeId()) {
    ...
     
    CeramicTitan likes this.
Thread Status:
Not open for further replies.

Share This Page