Need help with custom item

Discussion in 'Plugin Development' started by FiberSprite, Aug 15, 2014.

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

    FiberSprite

    Soo im making a custom item. I want a user to right click it and be a one use item. But i want to add things it gives you. For example you right click it and you get diziness, blindness, and nausea. How would i add it? i know how to make it so you can craft the item but how do i add a name and the effects and make it a one use right click item?
     
  2. Offline

    teej107

  3. Offline

    JustinsCool15

    FiberSprite
    Post the code you have so far or what you think it would be and we'll help you out.
     
  4. Offline

    FiberSprite

    Code:java
    1. ItemStack stun = new ItemStack(Material.SAPLING, 5);
    2. ItemMeta bim = stun.getItemMeta();
    3. bim.setDisplayName("Stun");


    what i have so far
    I know how to make it craftable i just wanna know how to add the effects and make it a one use item
    JustinsCool15
     
  5. Offline

    JustinsCool15

  6. Offline

    FiberSprite

    JustinsCool15 could you show me a example please?

    Acutally all i need to know is how to add a name to the item. it wont work when i craft it.

    Code:
                    ItemStack blunt = new ItemStack(Material.SAPLING);
                            ItemMeta bim = blunt.getItemMeta();
                bim.setDisplayName("Blunt");
               
                ShapedRecipe cblunt = new ShapedRecipe(blunt);
           
                cblunt.shape("ppp","ggs","ppp");
               
                cblunt.setIngredient('p', Material.PAPER);
                cblunt.setIngredient('g', Material.LEAVES);
                cblunt.setIngredient('s', Material.STRING);
                
    bump. Please help

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

    DinosParkour

    FiberSprite
    PlayerInteractEvent
    Check if action is Right Click Air/Block
    Check for amount of item in hand
    • if it's more than 1, set the amount to 0
    • if it's 1, remove the item from the player's inventory
    Then add potion effects to the event's player
    eZ
     
  8. Offline

    FiberSprite

    Yeah i got the right click thing down i need help naming it. It wont name it
    DinosParkour
     
  9. Offline

    DinosParkour

    FiberSprite
    Code:java
    1. ItemStack leItem = new ItemStack(Material.DIAMOND);
    2. ItemMeta leMeta = leItem.getItemMeta();
    3. leMeta.setDisplayName("Hai I'm a Diamond");
    4. leItem.setItemMeta(leMeta);
    5. p.getInventory().addItem(leItem);
     
  10. Offline

    FiberSprite

    DinosParkour But is it possible to make it so i can craft it. Thats what im trying to do. Make is so you can craft it
     
  11. Offline

    DinosParkour

    FiberSprite can you give me your class? I've never used any events for crafting and i'd like to see if possible
     
  12. Offline

    FiberSprite

    Heres my class DinosParkour








    Code:java
    1. package me.FiberSprite.TTBCustomItems;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.inventory.ItemStack;
    6. import org.bukkit.inventory.ShapedRecipe;
    7. import org.bukkit.inventory.meta.ItemMeta;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class TTBCustomItems extends JavaPlugin
    11. {
    12. public static Player player;
    13. public static ItemStack blunt = new ItemStack(Material.FEATHER);
    14.  
    15. @SuppressWarnings("deprecation")
    16. public void onEnable()
    17. {
    18. getServer().getPluginManager().registerEvents(new EventsHandler(this),this);
    19.  
    20.  
    21. //Works Fine
    22. ItemStack bottle = new ItemStack(Material.EXP_BOTTLE, 1);
    23.  
    24. ShapedRecipe expBottle = new ShapedRecipe(bottle);
    25.  
    26. expBottle.shape("*%*","%B%","*%*");
    27.  
    28. expBottle.setIngredient('*', Material.INK_SACK, 2);
    29. expBottle.setIngredient('%', Material.SUGAR);
    30. expBottle.setIngredient('B', Material.GLASS_BOTTLE);
    31.  
    32.  
    33. //Works fine
    34. ItemStack saddle = new ItemStack(Material.SADDLE, 1);
    35.  
    36. ShapedRecipe csaddle = new ShapedRecipe(saddle);
    37.  
    38. csaddle.shape("lll","lls","lli");
    39.  
    40. csaddle.setIngredient('l', Material.LEATHER);
    41. csaddle.setIngredient('s', Material.STRING);
    42. csaddle.setIngredient('i', Material.IRON_INGOT);
    43.  
    44.  
    45.  
    46. //Cant get name to work when crafted
    47. ShapedRecipe cblunt = new ShapedRecipe(blunt);
    48.  
    49. cblunt.shape("ppp","ggs","ppp");
    50.  
    51. cblunt.setIngredient('p', Material.PAPER);
    52. cblunt.setIngredient('g', Material.LEAVES);
    53. cblunt.setIngredient('s', Material.STRING);
    54.  
    55. ItemMeta bim = blunt.getItemMeta();
    56. bim.setDisplayName("Blunt");
    57.  
    58. getServer().addRecipe(expBottle);
    59. getServer().addRecipe(csaddle);
    60. getServer().addRecipe(cblunt);
    61.  
    62. }
    63.  
    64. public void onDisable()
    65. {
    66.  
    67. }
    68.  
    69. }
    70.  
     
  13. Offline

    DinosParkour

    FiberSprite
    Code:java
    1. blunt.setItemMeta(bim)


    EDIT: I don't see anything like
    Code:java
    1. ItemStack blunt = new ItemStack(...)

    Maybe that's why?
     
  14. Offline

    FiberSprite

  15. Offline

    DinosParkour

    FiberSprite implement Listener and add this to your onEnable
    Code:java
    1. Bukkit.getPluginManager().registerEvents(this,this);
     
  16. Offline

    FiberSprite

  17. Offline

    DinosParkour

    Code:java
    1. @SuppressWarnings("deprecation")
    2. public void onEnable()
    3. {
    4. Bukkit.getPluginManager().registerEvents(this, this);
    5.  
    6.  
    7. //Works Fine
    8. ItemStack bottle = new ItemStack(Material.EXP_BOTTLE, 1);
    9.  
    10. ShapedRecipe expBottle = new ShapedRecipe(bottle);
    11.  
    12. expBottle.shape("*%*","%B%","*%*");
    13.  
    14. expBottle.setIngredient('*', Material.INK_SACK, 2);
    15. expBottle.setIngredient('%', Material.SUGAR);
    16. expBottle.setIngredient('B', Material.GLASS_BOTTLE);
    17.  
    18.  
    19. //Works fine
    20. ItemStack saddle = new ItemStack(Material.SADDLE, 1);
    21.  
    22. ShapedRecipe csaddle = new ShapedRecipe(saddle);
    23.  
    24. csaddle.shape("lll","lls","lli");
    25.  
    26. csaddle.setIngredient('l', Material.LEATHER);
    27. csaddle.setIngredient('s', Material.STRING);
    28. csaddle.setIngredient('i', Material.IRON_INGOT);
    29.  
    30.  
    31.  
    32. //NOW WORKS :D
    33. ItemStack blunt = new ItemStack(Material.FEATHER, 1);
    34.  
    35. ItemMeta bim = blunt.getItemMeta();
    36. bim.setDisplayName("Blunt");
    37. blunt.setItemMeta(bim);
    38.  
    39. ShapedRecipe cblunt = new ShapedRecipe(blunt);
    40.  
    41. cblunt.shape("ppp","ggs","ppp");
    42.  
    43. cblunt.setIngredient('p', Material.PAPER);
    44. cblunt.setIngredient('g', Material.LEAVES);
    45. cblunt.setIngredient('s', Material.STRING);
    46.  
    47. getServer().addRecipe(expBottle);
    48. getServer().addRecipe(csaddle);
    49. getServer().addRecipe(cblunt);
    50.  
    51. }

    FiberSprite what you needed to do was to set the item meta before the crafting recipe.
    Just copy this modified code:
     
  18. Offline

    FiberSprite

    DinosParkour That did work c: but now it wont work with my listner to make it right clickable and add effects :c do you think you could show me how ?
     
  19. Offline

    DinosParkour

    FiberSprite When you say it doesn't work? Does it give an error or it just won't do a thing??

    EDIT:paste the relative code if you want me to locate the problem easier.
     
  20. Offline

    FiberSprite

    Whale thats why i had the blunt as a publit static so i could access it from a diffrent class .
    DinosParkour
    Code:java
    1. package me.FiberSprite.TTBCustomItems;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.EventPriority;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.block.Action;
    8. import org.bukkit.event.player.PlayerInteractEvent;
    9. import org.bukkit.inventory.ItemStack;
    10. import org.bukkit.potion.PotionEffect;
    11. import org.bukkit.potion.PotionEffectType;
    12.  
    13. public class EventsHandler implements Listener
    14. {
    15. final TTBCustomItems plugin;
    16.  
    17. public EventsHandler(TTBCustomItems plugin)
    18. {
    19. this.plugin = plugin;
    20. }
    21.  
    22. @EventHandler(priority = EventPriority.HIGHEST)
    23. public void onPlayerInteract(PlayerInteractEvent e)
    24. {
    25. Player player = e.getPlayer();
    26. ItemStack item = player.getItemInHand();
    27. if(item != null)
    28. {
    29. if(player.getItemInHand() == TTBCustomItems.blunt)
    30. {
    31. if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK))
    32. {
    33. player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 250, 15));
    34. player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 250, 15)); player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 250, 15));
    35. player.getInventory().removeItem(TTBCustomItems.blunt);
    36. }
    37. }
    38. }
    39. }
    40.  
    41. }
    42.  
     
  21. Offline

    DinosParkour

    FiberSprite Did you register that class' Events? Because I was unaware of its existence earlier so I removed it from my test code.
     
  22. Offline

    FiberSprite

    Yes i did register it but i dont know how to get the item since you added it in the onEnable() DinosParkour
     
  23. Offline

    DinosParkour

    FiberSprite Just move it out of the onEnable again, I'm sure it'll still work.
     
  24. Offline

    FiberSprite

    DinosParkour Hmm it doesnt throw a error but it doesnt give me the effects i want it to
     
  25. Offline

    DinosParkour

    FiberSprite Try checking if the item has the name "Blunt" instead of referring to the main class' itemstack
     
  26. Offline

    FiberSprite

    It still wont i cant figure out why

    DinosParkour
    Code:java
    1. package me.FiberSprite.TTBCustomItems;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.EventPriority;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.block.Action;
    8. import org.bukkit.event.player.PlayerInteractEvent;
    9. import org.bukkit.potion.PotionEffect;
    10. import org.bukkit.potion.PotionEffectType;
    11.  
    12. public class EventsHandler implements Listener
    13. {
    14. final TTBCustomItems plugin;
    15.  
    16. public EventsHandler(TTBCustomItems plugin)
    17. {
    18. this.plugin = plugin;
    19. }
    20.  
    21. @EventHandler(priority = EventPriority.HIGHEST)
    22. public void onPlayerInteract(PlayerInteractEvent e)
    23. {
    24. Player player = e.getPlayer();
    25.  
    26. if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK))
    27. {
    28. if(player.getItemInHand().equals("Blunt"))
    29. {
    30. player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 250, 15));
    31. player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 250, 15)); player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 250, 15));
    32. player.getInventory().removeItem(TTBCustomItems.blunt);
    33. }
    34. }
    35.  
    36. }
    37.  
    38. }
    39.  
     
Thread Status:
Not open for further replies.

Share This Page