Need Help Adding Custom Items to Inventories

Discussion in 'Plugin Development' started by Benlewis9000, Feb 28, 2014.

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

    Benlewis9000

    I have recently just been working on a simple, personal plugin, perhaps to be used on my server in the future, where you type a command, it opens an inventory, and you can take one of the custom items. Doing this was relatively easy, as I could just use the line "gemInv.addItem(Gem);" for example. Now I am trying to move on, and make more efficient plugins, where the user only has to type something like /gem and it gives them the custom Gem.
    In this scenario I am coding it so that they type /rpg, and it gives them a book with the name 'Warp RPG Snapshot', and when they click it it performs some codes and teleports them to my desired location. I have been try to add it to the players inventory with the line:
    Code:java
    1. player.getInventory().addItem(new ItemStack(RPGWarp, 30);
    2. // I do, for the record, have RPGWarp as a valid variable outside the method. That bit is fine.

    however, the bit where I have RPGWarp only takes either an int or a Material.WHATEVER, which I am not trying to use. How would I get round this and add the item with the cutom MetaData?
    The full classes code is only this, but for now it should be fine:
    Code:java
    1. package me.benLewis.customItems;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.inventory.ItemStack;
    8. import org.bukkit.inventory.meta.ItemMeta;
    9.  
    10. public class warprpg {
    11.  
    12. ItemStack RPGWarp = new ItemStack(Material.ENCHANTED_BOOK);
    13. ItemMeta RPGWarpMeta = RPGWarp.getItemMeta();
    14.  
    15. public boolean onCommand(CommandSender theSender, Command cmd, String commandLabel, String[] args){
    16.  
    17. if(theSender instanceof Player){
    18. Player player = (Player) theSender;
    19.  
    20. if(commandLabel.equalsIgnoreCase("rpg")){
    21.  
    22. player.getInventory().addItem(new ItemStack(RPGWarp, 30);
    23.  
    24. }
    25. }
    26. return false;
    27. }
    28. }
     
  2. Offline

    Forseth11

    Benlewis9000 Get it's meta data and set it's display name before adding the item.
     
  3. Offline

    iSexyChocobo

    Code:java
    1. ItemStack RPGWarp =new ItemStack(Material.ENCHANTED_BOOK,1, (short) 600);
    2. //You can replace 600 with any other number..
     
  4. Offline

    Benlewis9000

    Forseth11 iSexyChocobo
    Thanks to both of you guys for the quick response, but I literally just found and tested another way that seemed to work pretty well:
    Code:java
    1. player.getInventory().addItem(RPGWarp);

    I guess I just over thought the situation and ended up complicating matters further xD Found this after doing some research in another persons topic who had the same issues like me. However, I now have a new issue...
    I added my onClickEvent method for the custom item, followed by some code, but when I try to use it it just prints this:
    Code:
    [22:25:01] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to Custom Items v1.1
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:481) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:466) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:191) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:161) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:604) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.a(SourceFile:60) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.handle(SourceFile:9) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    Caused by: java.lang.NullPointerException
        at me.benLewis.customItems.items.onClickEvent(items.java:52) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_15]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_15]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_15]
        at java.lang.reflect.Method.invoke(Method.java:601) ~[?:1.7.0_15]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        ... 15 more
    The method code is:
    Code:java
    1. @EventHandler
    2. public void onClickEvent(PlayerInteractEvent event){
    3.  
    4. final Player player = event.getPlayer();
    5.  
    6. if ((player.getItemInHand().getType() == Material.ENCHANTED_BOOK)
    7. && (player.getItemInHand()).getItemMeta().getDisplayName()
    8. .equals(ChatColor.GREEN + "" + ChatColor.BOLD + "Warp" + ChatColor.WHITE + "RPG Snapshot")){
    9.  
    10. player.sendMessage("Verified");
    11.  
    12. }
    13. }
     
  5. To set a displayname, you need to give the ItemStack an ItemMeta. it would look like that:
    Code:Java
    1.  
    2. ItemStack book = new ItemStack(Material.ENCHANTED_BOOK);
    3. ItemMeta bookmeta = book.getItemMeta();
    4. bookmeta.setDisplayName("This is the item name!");
    5. book.setItemMeta(bookmeta);
    6. p.getInventory.addItem(book);
    7.  

    if you have any questions just Tagh me with @FisheyLP :)
     
  6. Offline

    Benlewis9000

    FisheyLP
    Thanks for the reply, but if you read the the OP you will realise I can and have already done that, and it doesn't really answer my question. Also, we've moved on to me now getting these console errors about the PlayerInteravtEvent. If you know anything on that it would be greatly appreciated :)
     
  7. why final Player ?? just use
    Code:Java
    1.  
    2. Player player = event.getPlayer();
    3.  

    and these ChatColor.RED things are much to big, just use minecraft colors codes e.g. like that:
    Code:Java
    1.  
    2. p.sendMessage("§aGreen, §4Red, §eYellow");
    3.  


    Benlewis9000
    could you post the line 52 of your code here? Because the console says the error is on line 52

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

    Benlewis9000

    As of now, line 51 is 100% empty. Also, never knew you could use the § symbol, thanks for that! Player is final because it is used in multiple methods throughout the code. Does your line change that?
    Anyway, I think the line the code is now pointing at is:
    Code:java
    1. .equals(ChatColor.GREEN + "" + ChatColor.BOLD + "Warp" + ChatColor.WHITE + "RPG Snapshot")){

    The full onClick method is:
    Code:java
    1. @EventHandler
    2. public void onClickEvent(PlayerInteractEvent event){
    3.  
    4. final Player player = event.getPlayer();
    5.  
    6. if ((player.getItemInHand().getType() == Material.ENCHANTED_BOOK)
    7. && (player.getItemInHand()).getItemMeta().getDisplayName()
    8. .equals(ChatColor.GREEN + "" + ChatColor.BOLD + "Warp" + ChatColor.WHITE + "RPG Snapshot")){ //this is 71
    9.  
    10. player.sendMessage("Verified");
    11.  
    12. }
    13. }


    FisheyLP
    Forgot to tag :b

    Forseth11 iSexyChocobo FisheyLP

    This is the full class code:
    http://pastebin.com/Yj8v2f5A

    This is the full error code:
    http://pastebin.com/0Uhc8c54

    Any help whatsoever would be very much appreciated. I can not see the flaw in my code anywhere. The stacktrace points to line 71 and my onClickEvent. To me though, they look fine, and have worked in the past.

    Finally cracked where the error is coming from! If the enchanted book is called the specific name, it prints the code, but otherwise it doesn't know what to do and prints the NPE error. Just going to try and add some sort of return statement or else statement now.

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

    iSexyChocobo

    Benlewis9000

    Code:java
    1. package chocobo.clickme;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.inventory.meta.ItemMeta;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17.  
    18. public class ClickMe extends JavaPlugin implements Listener {
    19.  
    20. private static ClickMe instance;
    21. public static final ClickMe getPlugin(){
    22. return instance;
    23. }
    24.  
    25. public void onDisable()
    26. {
    27. }
    28.  
    29. public void onEnable()
    30. {
    31. instance = this;
    32. Bukkit.getPluginManager().registerEvents(this, getPlugin());
    33. }
    34.  
    35.  
    36. @EventHandler
    37. public void onPlayerInteract(PlayerInteractEvent event){
    38. Player player = event.getPlayer();
    39. ItemStack item = player.getItemInHand();
    40. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    41. if ((item.getType() == Material.ENCHANTED_BOOK)
    42. && (item).getItemMeta().getDisplayName()
    43. .equals(ChatColor.GREEN + "" + ChatColor.BOLD + "Warp" + ChatColor.WHITE + "RPG Snapshot")){
    44. player.sendMessage("Verified"); }
    45. else player.sendMessage("You're probably doing it wrong."); }
    46. }
    47.  
    48.  
    49. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    50. Player player = (Player)sender;
    51. if (commandLabel.equalsIgnoreCase("getbook"))
    52. player.sendMessage("Here's your book..");
    53. ItemStack book = new ItemStack(Material.ENCHANTED_BOOK);
    54. ItemMeta bookmeta = book.getItemMeta();
    55. bookmeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Warp" + ChatColor.WHITE + "RPG Snapshot");
    56. book.setItemMeta(bookmeta);
    57. player.getInventory().setItem(1, book);
    58. {
    59. }
    60. return false;
    61. }
    62. }


    That's a sample I coded for you. Feel free to grab any piece of code from there, it fully works as far as I know.
     
    Benlewis9000 likes this.
  10. Offline

    Benlewis9000

    iSexyChocobo
    Thanks for the code, its all working great and stuff, but if a player clicks an Enchanted Book with now other values, it still prints out huge lines of error code. This wouldn't be a problem if it didn't spam up my logs, which I will need to refer to in the future. Then again, I guess there are no vanilla ways to get Enchanted Books with no actual value in game... It still annoys me though.
    I added this to try and deter it, it did not work;
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4. ItemStack item = player.getItemInHand();
    5. if (event.getAction() == Action.RIGHT_CLICK_AIR) {
    6. if ((item.getType() == Material.ENCHANTED_BOOK)
    7. && (item).getItemMeta().getDisplayName()
    8. .equals(ChatColor.GREEN + "" + ChatColor.BOLD + "Warp" + ChatColor.WHITE + " RPG Snapshot")){
    9. player.sendMessage("§4Error: §cThe RPG Snapshot is not yet available!"); }
    10.  
    11. //### THIS BIT ###
    12.  
    13. else if((item.getType() == Material.ENCHANTED_BOOK)
    14. && (item).getItemMeta().getDisplayName()
    15. .equals("Enchanted Book")){
    16.  
    17. player.sendMessage("This book has no abilities!");
    18.  
    19. }
    20. }
    21. }


    Do you think we could continue it over skype? Benjaminlewis310

    Anyone got any solutions to stopping the error if the enchanted book is not called the specific name?

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

    Arcoz

    Try chaging the else if part to else return;

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4. ItemStack item = player.getItemInHand();
    5. if (event.getAction() == Action.RIGHT_CLICK_AIR) {
    6. if ((item.getType() == Material.ENCHANTED_BOOK)
    7. && (item).getItemMeta().getDisplayName()
    8. .equals(ChatColor.GREEN + "" + ChatColor.BOLD + "Warp" + ChatColor.WHITE + " RPG Snapshot")){
    9. player.sendMessage("§4Error: §cThe RPG Snapshot is not yet available!");
    10. } else return;
    11. }
    12. }


    Benlewis9000

    If that doesn't work change your code to this:

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract22(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4. ItemStack item = player.getItemInHand();
    5. if (event.getAction() == Action.RIGHT_CLICK_AIR) {
    6. if (item.getType() == Material.ENCHANTED_BOOK){
    7. if((item).getItemMeta().getDisplayName().equals(ChatColor.GREEN + "" + ChatColor.BOLD + "Warp" + ChatColor.WHITE + " RPG Snapshot")){
    8. player.sendMessage("§4Error: §cThe RPG Snapshot is not yet available!");
    9. } else return;
    10. }
    11. return;
    12. }
    13. }
    14.  
     
  12. Offline

    Benlewis9000

    Arcoz
    I believe I tried that and it failed to work, I will try again tomorrow though, thanks for the reply.
     
  13. Offline

    iSexyChocobo

    Benlewis9000
    I'm not entirely sure on what was causing the issue, but I've bypassed it by setting the durability to 1 so that it won't interrupt with normal enchanted books, only if someone spawns one in with /item or something.
    Code:java
    1. package chocobo.clickme;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.inventory.meta.ItemMeta;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17.  
    18. public class ClickMe extends JavaPlugin implements Listener {
    19.  
    20. private static ClickMe instance;
    21. public static final ClickMe getPlugin(){
    22. return instance;
    23. }
    24.  
    25. public void onDisable()
    26. {
    27. }
    28.  
    29. public void onEnable()
    30. {
    31. instance = this;
    32. Bukkit.getPluginManager().registerEvents(this, getPlugin());
    33. }
    34.  
    35.  
    36. @EventHandler
    37. public void onPlayerInteract(PlayerInteractEvent event){
    38. Player player = event.getPlayer();
    39. ItemStack item = player.getItemInHand();
    40. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    41. if ((item.getType() == (Material.ENCHANTED_BOOK) && player.getItemInHand().getDurability() == 1)
    42. && (item).getItemMeta().getDisplayName()
    43. .equals(ChatColor.GREEN + "" + ChatColor.BOLD + "Warp" + ChatColor.WHITE + "RPG Snapshot")){
    44. player.sendMessage("Verified"); }
    45. } else return; {
    46. }
    47. return;
    48. }
    49.  
    50.  
    51.  
    52. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    53. Player player = (Player)sender;
    54. if (commandLabel.equalsIgnoreCase("getbook"))
    55. player.sendMessage("Here's your book..");
    56. ItemStack book = new ItemStack(Material.ENCHANTED_BOOK,1, (short) 1);
    57. ItemMeta bookmeta = book.getItemMeta();
    58. bookmeta.setDisplayName(ChatColor.GREEN + "" + ChatColor.BOLD + "Warp" + ChatColor.WHITE + "RPG Snapshot");
    59. book.setItemMeta(bookmeta);
    60. player.getInventory().setItem(1, book);
    61. {
    62. }
    63. return false;
    64. }
    65. }
     
    Benlewis9000 likes this.
  14. Offline

    Benlewis9000

    iSexyChocobo
    Nice idea, did it work? Also does it show up an ugly durability bar I game or is it kept looking the same?
     
  15. Offline

    iSexyChocobo

    Benlewis9000 It works perfectly fine, and nope, it won't show any durability bars since enchanted books aren't supposed to lose durability normally in the first place :)
     
  16. Offline

    Benlewis9000

    iSexyChocobo
    Awesome! Thanks for the help, I really appreciate it! I'll be sure to test this out tomorrow (I'm now making a warp/spell plugin based on books) and if you want the code at the end just ask and il share it with you, seeing as you have been a great help so far.
     
    iSexyChocobo likes this.
Thread Status:
Not open for further replies.

Share This Page