Renaming an Item on Crafting

Discussion in 'Plugin Development' started by mattrick, Aug 2, 2013.

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

    mattrick

    Right now I am developing a plugin where when you craft something, it will give it lore with information like who crafted it. Are there any ways to do that? (Bonus Question: Is there a way to see which player crafted it.) Is there even a crafting event?
     
  2. Offline

    xTrollxDudex

    mattrick16
    On prepare item craft event you would do event.getInventory().getResult() which returns an ItemStack. You can now edit the result.
    Then you can use setResult to the edited itemstack
     
  3. Offline

    tommycake50

    How is the secret to life qgdjhosuvnfgjs?
    anyway you listen for a craftitemevent.
    i dont know the ins and outs of it but thats the event.
     
  4. Offline

    mattrick


    Thanks Trollx! Is there a way to get which player crafted it?


    Hmm my sig on my computer doesn't show the the spoiler :confused:
    See?:
    [​IMG]

    Edit: I fixed it....
    Edit 2: My sig not the problem.....

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  5. Offline

    tommycake50

    I hit inspect element and expanded the tags till i found it :p.
     
    mattrick16 and xTrollxDudex like this.
  6. Offline

    xTrollxDudex

    mattrick16
    I believe it is event.getView().getPlayer()
     
  7. Offline

    mattrick

    Lol trolling the troll I like it......I just put an image of the spoiler now.....

    It says cannot convert human entity to player. Should I just change the variable type to human entity then?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  8. Offline

    xTrollxDudex

  9. Offline

    CubieX

    You can create a custom ItemStack and give it a description and lore (by using .getMetaData() and then changing description and lore and setting the new ItemMeta back to the ItemStack)
    and then use setResult() in the "CraftItemEvent" for example to set the resulting ItemStack to your custom ItemStack.

    Always have a look to the Bukkit API documentation, if you do not now what methods are available and how they can be used.

    Which player crafted it, can be retrieved by the event too.
    There is a method .getWhoClicked().
    You can cast a HumanEntity to Player. But as HumanEntity also has a "getName()" method, I think you do not need that here.
     
  10. Offline

    mattrick

    xTrollxDudex
    It says cannot convert from human entity to player.
     
  11. Offline

    xTrollxDudex

    mattrick16
    So what do you have right now? I think you can cast player on event.getView().getPlayer()
     
  12. Offline

    mattrick

    Code:java
    1. @EventHandler
    2. public void onCraft(PrepareItemCraftEvent e){
    3. ItemStack item = e.getInventory().getResult();
    4. HumanEntity player = e.getView().getPlayer();
    5. ItemMeta meta = item.getItemMeta();
    6. ArrayList<String> lore = new ArrayList<String>();
    7. lore.add("Crafted by:"+ player);
    8. meta.setLore(lore);
    9. item.setItemMeta(meta);


    Thats my code but when I craft, nothing happens to the resulting item.
     
  13. Offline

    clienthax

    Code:java
    1. @EventHandler
    2. public void onCraft(PrepareItemCraftEvent e){
    3. ItemStack item = e.getInventory().getResult();
    4. HumanEntity player = e.getView().getPlayer();
    5. ItemMeta meta = item.getItemMeta();
    6. ArrayList<String> lore = new ArrayList<String>();
    7. lore.add("Crafted by:"+ player);
    8. meta.setLore(lore);
    9. item.setItemMeta(meta);
    10. e.getInventory().setResult(item);
    11. }
    12.  
     
  14. Offline

    mattrick

    Still doesn't work.....
     
  15. Offline

    clienthax

    woops, should be player.getName();
    Code:java
    1. @EventHandler
    2. public void onCraft(PrepareItemCraftEvent e){
    3. ItemStack item = e.getInventory().getResult();
    4. HumanEntity player = e.getView().getPlayer();
    5. ItemMeta meta = item.getItemMeta();
    6. ArrayList<String> lore = new ArrayList<String>();
    7. lore.add("Crafted by:"+ player.getName());
    8. meta.setLore(lore);
    9. item.setItemMeta(meta);
    10. e.getInventory().setResult(item);
    11. }
     
  16. Offline

    mattrick

    Nope......It doesn't add lore still.....do I need to make a listener class for this?
     
  17. Offline

    xTrollxDudex

    mattrick16
    event.getInventory().setResult(item);
    You may need to create a new itemstack based on the result.

    Also, try casting to player to get the player. And use player.getName()
     
    mattrick16 likes this.
  18. Offline

    mattrick


    Yeah thats what clienthax did but it didn't work.
     
  19. Offline

    clienthax

    it works perfectly fine on my server, you did register your events right..
     
    mattrick16 likes this.
  20. Offline

    mattrick


    Ahhh....I feel stupid I forgot to register the events it works now. Thanks everybody!

    Edit: Minor bug but I figured I'd ask anyway. It says Crafted By: player even before I crafted it. Is there a way to fix this?
     
  21. Offline

    xTrollxDudex

    clienthax
    I can't find the getName() method for HumanEntity
     
  22. Offline

    clienthax

  23. Offline

    xTrollxDudex

    clienthax
    No way I missed that *facepalm*
     
Thread Status:
Not open for further replies.

Share This Page