Solved Copying and altering ItemStack in loop

Discussion in 'Plugin Development' started by Remi1115, Jul 14, 2013.

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

    Remi1115

    Hello,

    I have now spend at least a few hours at this piece of code, and even after a good night rest I am still not understanding why it would not work.

    I want to give a player three items, all of which are the same, but with a different name. I was thinking about having a 'base' item with the name 'Iron Pickaxe ({st00f})'. After that I will loop three times, copying the base item and changing the placeholder.

    The problem is that everytime I change the name of the copied item, it will also change the name of the base item, after which the base item does not contain the placeholder anymore, and the other items can therefor not rename it.

    I stripped the code down to only the necessary parts for this, and see if I could get that to work. But I still can't.

    This is the code:

    Show Spoiler

    Code:Java
    1.  
    2. // This method does st00f. Saturday 13 July 2013.
    3. public void st00f(){
    4. // Make the base ItemStack and set the name with a placeholder.
    5. ItemStack itemStackPickaxe1 = new ItemStack(Material.IRON_PICKAXE);
    6. ItemMeta itemStackPickaxe1Meta = itemStackPickaxe1.getItemMeta();
    7. itemStackPickaxe1Meta.setDisplayName(ChatColor.RESET + "Iron Pickaxe ({st00f})");
    8. itemStackPickaxe1.setItemMeta(itemStackPickaxe1Meta);
    9.  
    10. for(int i = 0; i <= 2; i++){ // Loop three times.
    11. // Create a new itemStack that we want to alter.
    12. ItemStack itemStack = itemStackPickaxe1;
    13.  
    14. this.getLogger().info("ItemName before: '" + itemStack.getItemMeta().getDisplayName() + "'; "); // Debug before
    15.  
    16. // Modify the ItemMeta.
    17. ItemMeta itemMeta = itemStack.getItemMeta();
    18. itemMeta.setDisplayName(itemMeta.getDisplayName().replace("{st00f}", i + ""));
    19. itemStack.setItemMeta(itemMeta);
    20.  
    21. this.getLogger().info("ItemName after: '" + itemStack.getItemMeta().getDisplayName() + "'; "); // Debug after
    22.  
    23. if(i < 2){ // Making the debug messages easier to read.
    24. this.getLogger().info(" ");
    25. }
    26. }
    27. }
    28.  




    This is the output from the debugging messages:

    Show Spoiler

    Code:
    2013-07-14 10:45:17 [INFO] [St00f] ItemName before: '§rIron Pickaxe ({st00f})'; 
    2013-07-14 10:45:17 [INFO] [St00f] ItemName after: '§rIron Pickaxe (0)'; 
    2013-07-14 10:45:17 [INFO] [St00f]  
    2013-07-14 10:45:17 [INFO] [St00f] ItemName before: '§rIron Pickaxe (0)'; 
    2013-07-14 10:45:17 [INFO] [St00f] ItemName after: '§rIron Pickaxe (0)'; 
    2013-07-14 10:45:17 [INFO] [St00f]  
    2013-07-14 10:45:17 [INFO] [St00f] ItemName before: '§rIron Pickaxe (0)'; 
    2013-07-14 10:45:17 [INFO] [St00f] ItemName after: '§rIron Pickaxe (0)'; 
    


    Does anyone see what I am doing wrong? I guess it's probably just a beginners question.

    Remi
     
  2. Offline

    jayfella

    when you want to clone something, use .clone() - you will then get a new copy, not a reference to the old one, which is what is happening to you.
     
    Remi1115 likes this.
  3. Offline

    Remi1115

    Thank you!
    After looking it up and reading the Wikipedia page I see it's a normal Java method. Stupid that I did not know about that.
     
Thread Status:
Not open for further replies.

Share This Page