Adding extra text to an existing item with a command.

Discussion in 'Plugin Development' started by Techno, Nov 2, 2014.

Thread Status:
Not open for further replies.
  1. Hey yall!
    I'm making a plugin called soulbound items and when I run the command holding something like a piece of dirt it would change it from 'Dirt' to 'Dirt [Soulbound]'.. But all that happens is this:

    Run command. Item changes to 'null [Soulbound]'

    Code:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
     
    Player p = (Player) sender;
     
    if (p.hasPermission("soulbounditems.use.apply")) {
    if (cmd.getLabel().equalsIgnoreCase("soulbound")) {
     
    ItemStack item = new ItemStack(p.getItemInHand());
    ItemMeta itemm = item.getItemMeta();
    itemm.setDisplayName(itemm.getDisplayName() + ChatColor.DARK_BLUE + " [" + ChatColor.ITALIC + ChatColor.BLUE + "Soulbound" + ChatColor.DARK_BLUE + "] ");
     
    p.getItemInHand().setItemMeta(itemm);
     
    return true;
    }
    }
     
    return false;
    }
    
    Does anyone know what to do to fix this?
    Thanks, Techno!
     
  2. Offline

    fireblast709

    Techno the display name will be null if not set before. Material names do not count as display names, thus it will return null :p.
     

  3. Ah okay, but what will I do to fix the problem?
     
  4. Offline

    97WaterPolo

    Techno
    I believe the only way would be to create an enum with all the user friendly names.

    If you only do items like diamond sword, cobblestone, etc, you can use item.getType().toString().replace("_","");
     

  5. Like in the item meta? Where it gets the current display name and adds the tag?
     
  6. Offline

    fireblast709

    97WaterPolo .name(), not .toString() afaik (at least, .name() is what's usually used)
     
  7. But guys, where will I put that little line of code? In the itemmeta or what?
     
  8. Offline

    fireblast709

    Techno replace itemm.getDisplayName() with it. Or if you want to support custom item names, which one you use should be based on itemm.hasDisplayName().
     
  9. Offline

    97WaterPolo

    fireblast709
    Would .name return the actual user friendly name?
     
  10. Offline

    fireblast709

    97WaterPolo In fact, after checking the source, they just seem to return the same value :p.
     
  11. fireblast709 97WaterPolo

    Thanks guys, I got it working but when I /soulbound an item the item would change to something like DIRT [Soulbound] or WOODAXE [Soulbound]
     
  12. Offline

    fireblast709

    Techno you could either:
    • Use toLowerCase() and manually uppercase the first characters.
    • Manually create a Map<Material, String> (or use MaterialData for datavalue support) in which you write your own name mapping.
    • Use a third party plugin (ex. Essentials) to deal with the names.
     
  13. Offline

    97WaterPolo

    Techno
    I have to agree with fire blast on this, you would have to either create your own map or rely on a third party plugin.

    For stuff like a wooden axe, you can substring it and have it so the correct letters are uppercase, but the issue is many items in the game are different than the material values. For one, any item that as a data value(wool, dyes, clay) would all return the original value. There are also a few material names that don't exactly correlate with the item most users know.
     
Thread Status:
Not open for further replies.

Share This Page