Renaming item in hand

Discussion in 'Plugin Development' started by Abyssal, Nov 15, 2015.

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

    Abyssal

    Hey,
    Trying to make it so when you do /rename <name> the item in your hands name is changed.
    I need it so when a player puts a "_" it replaces it with a space, but I think that is the part that is making the error.
    Here is my code:
    Code:
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
                if(label.equalsIgnoreCase("rename") || label.equalsIgnoreCase("name")){
                  
                    Player player = (Player) sender;
                    ItemStack item = new ItemStack(player.getInventory().getItemInHand().getType());
                    ItemMeta itemmeta = (ItemMeta) item.getItemMeta();
                    String argument = args[0].toString().replace("_"," ");
                  
                    if(!(sender.hasPermission("RenameItem.rename"))){
                        sender.sendMessage("§cYou need the permission §4RenameItem.rename §cto do this!");
                        return true;
                    }
                  
                    if(!(sender instanceof Player)){
                        sender.sendMessage("§cYou must be a player to execute this command!");
                        return true;
                    }
                  
                    if(args.length == 0 || args.length > 1){
                        sender.sendMessage("§cUsage: /rename <name>");
                        return true;
                    }
                  
                    if(item.equals(Material.AIR)){
                        sender.sendMessage("§cYou cannot rename air!");
                        return true;
                    }
                  
                    itemmeta.setDisplayName(colours(argument));
                    item.setItemMeta(itemmeta);
                    sender.sendMessage("§aItem name succesfully changed to: " + colours(argument));
                    return true;
                }
    The error: http://i.imgur.com/2qs8kf3.png

    The error occurs when I do /rename

    When I do /rename &aHello_this_is_a_test, it comes up with the message "Item name successfully changed to:..." which is all correct. but then it doesn't actually change the item name
     
  2. Online

    timtower Administrator Administrator Moderator

    @Abyssal String argument = args[0].toString().replace("_"," ")
    That is your error, you access args[0] before you check the size
     
  3. Offline

    Abstract97

    @Abyssal Also, i would advise checking that the sender is a player, before you even think about casting the sender to the player, because at the moment you cast the sender to the player, then check if the sender is an instanceof the player afterwards, this would cause a ClassCastException if the sender was the console.
     
  4. Offline

    Scimiguy

    And dont use label, use cmd.getName()
     
  5. Offline

    Abyssal

    @timtower
    @Abstract97
    @Scimiguy

    Hi there, thank you for your responses!
    I have fixed that issue, but now when I do /rename <name>, it says the message "Item renamed successfully", but doesn't actually rename the item.
    Code:
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
                if(command.getName().equalsIgnoreCase("rename") || label.equalsIgnoreCase("name")){
                    if(!(sender.hasPermission("RenameItem.rename"))){
                        sender.sendMessage("§cYou need the permission §4RenameItem.rename §cto do this!");
                        return true;
                    }
                   
                    if(!(sender instanceof Player)){
                        sender.sendMessage("§cYou must be a player to execute this command!");
                        return true;
                    }
                   
                    if(args.length == 0 || args.length > 1){
                        sender.sendMessage("§cUsage: /rename <name>");
                        return true;
                    }
                    Player player = (Player) sender;
                    ItemStack item = new ItemStack(player.getInventory().getItemInHand().getType());
                    if(item.equals(Material.AIR)){
                        sender.sendMessage("§cYou cannot rename air!");
                        return true;
                    }
                   
                    ItemMeta itemmeta = item.getItemMeta();
                    String argument = args[0].toString().replace("_"," ");
                    itemmeta.setDisplayName(colours(argument));
                    item.setItemMeta(itemmeta);
                    sender.sendMessage("§aItem name succesfully changed!");
                    return true;
                }
    
    Everything else works though
     
  6. Offline

    RoboticPlayer

    A) onCommand can be overridden
    B) Stated by @Scimiguy use cmd.getName instead of commandLabel
    C) As @Abstract97 said, check before casting
    D) Player#getInventory().getItemInHand().getType() returns a Material, yet you are using it as an ItemStack. Get rid of getType
    E) As said by @timtower you are using args[0] before checking if it exists
    F) args[0] returns a string
    G) Use the ChatColor enum instead of the section sign
    H) Just curious, how many time have you had args.length == 0 but it's not > 1? Useless check. You only need one of them
    I) Use == when comparing enums
    J) Can we see your "colours" method?
     
  7. Offline

    567legodude

    @Abyssal Because you are creating a new instance of the itemstack, so modifying it will not change the item in the player's hand.
     
  8. Offline

    Abyssal

    @567legodude
    How would I make it not create a new instance?

    EDIT: nevermind, fixed it, thanks!
     
  9. Offline

    567legodude

    keywords "can be"
    Did you see their second post, they did that.
    And that too.
    Either one can be used, preference of the coder.
    They do that to check if the command they typed is in the correct format. It should only continue if they typed 1 argument, which is what they check for.
     
  10. Offline

    RoboticPlayer

    For C and E, I was typing it when the OP replied. And for H, I misread it as checking if there were 0 arguments or less than 1 arguments (instead of more than one).

    Edit: As for the ChatColor enum, what if the color codes were to change (unlikely, but possible). The colors would be messed up if the coder used the section sign, but if they used the ChatColor enum it wouldn't matter.
     
  11. Offline

    Scimiguy

    @567legodude
    ChatColor should ALWAYS be used instead of the section symbol.
    It's not preference, it's just lazy unsafe coding

    @Abyssal
    You're not setting the item back in the player's hand after changing the meta
     
    Zombie_Striker likes this.
  12. Offline

    567legodude

    @henderry2019 That simply means they should update their code.
    It's the same as updating a plugin from 1.7 to 1.8
    @Scimiguy ChatColor evaluates to to the section sign, you are really just removing an in-between step.
     
  13. Offline

    Scimiguy

    @567legodude
    And making it unsafe, and by your exact definition, lazy.

    ChatColor should always be used. That's it.
     
  14. Offline

    567legodude

    @Scimiguy I never mentioned lazy at all. ChatColor is just the enum version of the section sign, they are both the same. How is the section sign unsafe when that's what comes out when using ChatColor?

    If you save ChatColor.BLUE to the config, you would get "§9", not anything else.
     
  15. Offline

    Scimiguy

    This is not the place for this discussion.

    @Abyssal
    As above,
    You're not setting the item back in the player's hand after changing the meta
     
  16. Offline

    567legodude

    @Scimiguy That's what I said. He is creating a new instance of the item so modifying it will not update the item in the player's hand.

    And even if you do use ChatColor, it still saves as the section sign, so either way you are forced to use that in the string.
     
Thread Status:
Not open for further replies.

Share This Page