Renaming items

Discussion in 'Plugin Development' started by Postkutsche, Oct 28, 2012.

  1. Offline

    Postkutsche

    My BukkitDev Profile
    My Plugins (0)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    I just created a little API to rename any item you want in Minecraft 1.4.
    It changes itemnames like using an anvil.

    You can easily copy+paste it from here: http://pastie.org/5126792

    Easy to use:
    Code (Text):
    1. ItemStack anvil = new ItemStack(Material.ANVIL);
    2. NamedItemStack namedItemStack = new NamedItemStack(anvil);
    3. // Give it a name
    4. namedItemStack.setName("Minecraft 1.4 is there!");
    5. // Reset the name to default
    6. namedItemStack.setName(null);
    7. // Get the item's name (returns null if it hasn't been renamed)
    8. namedItemStack.getName();
     
  2. Offline

    stirante

    My BukkitDev Profile
    My Plugins (1)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    I created it almost month ago :p
     
  3. Offline

    Postkutsche

    My BukkitDev Profile
    My Plugins (0)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Lol, I haven't seen it :confused:
     
  4. Offline

    Xx_LeetGamer_xX

    My BukkitDev Profile
    My Plugins (2)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    I get an exception:

    Code (Text):
    1. Caused by: java.lang.ClassCastException: org.bukkit.inventory.ItemStack cannot b
    2. e cast to org.bukkit.craftbukkit.inventory.CraftItemStack
    On this line: (Line 14)
    Code (Text):
    1. CraftItemStack cis = ((CraftItemStack)this.itemStack);
    How can I fix this?
     
  5. Offline

    epicfacecreeper

    My BukkitDev Profile
    My Plugins (2)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Add CraftBukkit.jar to your libraries.
     
  6. Offline

    Xx_LeetGamer_xX

    My BukkitDev Profile
    My Plugins (2)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    It is already...
     
  7. Offline

    Postkutsche

    My BukkitDev Profile
    My Plugins (0)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Have you checked your imports? I believe you're casting a net.minecraft-ItemStack to a CraftItemStack.
     
  8. Offline

    Comphenix

    My BukkitDev Profile
    My Plugins (8)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    That's because the class above expects a CraftItemStack - that is, an item stack that's created by Minecraft and wrapped by Craftbukkit, and not a bog standard "Bukkit" item stack.

    To be fair, that's exactly what OP did in the code example (which is clearly not tested). :p

    To fix this, convert the ItemStack you created to a CraftItemStack first:
    Code:java
    1. private void performAction(Player player) {
    2. ItemStack brick = toCraftBukkit(new ItemStack(Material.BRICK));
    3. NamedItemStack namedItemStack = new NamedItemStack(brick);
    4. namedItemStack.setName("Minecraft 1.4 is there!");
    5.  
    6. player.setItemInHand(brick);
    7. }
    8.  
    9. private static ItemStack toCraftBukkit(ItemStack stack) {
    10. if (!(stack instanceof CraftItemStack))
    11. return new CraftItemStack(stack);
    12. else
    13. return stack;
    14. }
     
  9. Offline

    Postkutsche

    My BukkitDev Profile
    My Plugins (0)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    The code was tested and works fine for me.

    EDIIT: Ahhh.. now I know what you mean, I'll fix it when I come home.
     
    Comphenix likes this.
  10. Offline

    xXSniperzzXx_SD

    My BukkitDev Profile
    My Plugins (9)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    This doesn't work right... And by that i mean that there are no errors or anything, it just doesn't rename it... and in case it matters i'm trying to rename a redstone
     
  11. Offline

    fireblast709

    My BukkitDev Profile
    My Plugins (3)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    post the code
     
  12. Offline

    finalblade1234

    My BukkitDev Profile
    My Plugins (4)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    NamedItemStack cannot be resovled into a type?
    Any help?
     
  13. Offline

    chasechocolate

    My BukkitDev Profile
    My Plugins (4)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Just use ItemMeta.
     
    coobro123 likes this.
  14. Offline

    coobro123

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    So which one of these codes work? Lol
     
  15. Offline

    stirante

    My BukkitDev Profile
    My Plugins (1)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Use Bukkit's api ItemMeta.
     

Share This Page