Item Rename Code Help??

Discussion in 'Plugin Development' started by West_Dover, May 7, 2013.

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

    West_Dover

    Hi im trying to make a plugin that renames the item that you are holding in your hand to a different name. Like using a Anvil. I only want it to edit the items or blocks name that is in your hand. This is all i have so far.

    Code:
    package main.pac;
     
    import java.util.logging.Logger;
     
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerItemHeldEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class A1 extends JavaPlugin{
       
        public static A1 plugin;
        public final Logger logger = Logger.getLogger("Minecraft");
       
        @Override
        public void onDisable() {
            this.logger.info("[ReNamer]" + " Shuting down Java Plugin!");
        }
       
        @Override
        public void onEnable() {
            this.logger.info("[ReNamer]" + " Version " + "0.0.1" + " Is Loading");
            this.logger.info("[ReNamer]" + " This is a Custom Plugin ment for Nova Use Only any other use will be Prosocuted! ");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("hand")) {
               
                   
                }
     
     
               
       
           
           
            return false;
           
       
        }
       
       
     
           
           
       
    }
     
  2. Offline

    felixfritz

    I guess the next step would be getting the item in his hand and changing its name. To get the ItemStack in the player's hand, simply say:
    PHP:
    ItemStack items player.getItemInHand();
    To edit the text of it, you have to get the ItemMeta, in which you can change the DisplayName, which is basically the name of the item:
    PHP:
    ItemMeta itemsMeta items.getItemMeta();
    itemsMeta.setDisplayName(args[0]); //Set it to the first string of the argument, so '/hand malloc' will rename the item in hand to malloc
    This would be it. To make it a little safer, I would add additional if-else-statements, eg. if the length of the arguments array is longer than 0 and if the player actually has an item in hand and not "nothing" (Material.AIR).

    An additional feature could be colors, eg. via &1 you get a dark blue color, &4 is a red color. To do that, you'd have to translate the color code in the ChatColor class.
    PHP:
    itemsMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&'args[0]);
    The first argument in translateAlternateColorCodes is the character / letter being replaced by the colors, in this case the &-letter.

    More information and methods can be seen in the documentation.
     
  3. Please could you write the final code?
    Thanks

    Please could you write the final code?
    Thanks

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

Share This Page