Help Needed Using Variables in Multiple Methods | ItemStacks

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

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

    Benlewis9000

    So I have recently been trying to create a small plugin where the player type /gem, it opens a new inventory displaying a Gem and a book with a custom name, then they can take the book or gem and put it in there inv. So far that was all easy and works fine. However, now I am trying to have it so that when you click on the book it performs some code, then deletes the item stack.

    My code for it all so far is this:

    Code:java
    1. package me.benLewis.bukkitPluginEight;
    2.  
    3. import java.util.logging.Logger;
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Effect;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.inventory.Inventory;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.ItemMeta;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class walrus extends JavaPlugin{
    19.  
    20. // #### This is how I am getting the itemstack and its meta data ####
    21.  
    22. ItemStack fireCharge = new ItemStack(Material.ENCHANTED_BOOK);
    23. ItemMeta swordMeta = fireCharge.getItemMeta();
    24.  
    25. Logger myPluginLogger = Bukkit.getLogger();
    26.  
    27. public void onEnable(){
    28.  
    29. myPluginLogger.info(ChatColor.DARK_RED + "Bukkit Plugin EIGHT has been statted");
    30.  
    31. }
    32.  
    33. public void onDisable(){
    34.  
    35. myPluginLogger.info(ChatColor.DARK_RED + "Bukkit Plugin EIGHT has been disabled!");
    36. myPluginLogger.severe(ChatColor.DARK_RED + "SHUTTING DOWN");
    37. }
    38.  
    39. public boolean onCommand(CommandSender theSender, Command cmd, String commandLabel, String[] args){
    40.  
    41. if(theSender instanceof Player){
    42. Player player = (Player) theSender;
    43.  
    44. if(commandLabel.equalsIgnoreCase("gem")){
    45. ItemStack Gem = new ItemStack(Material.EMERALD);
    46. Inventory gemInv = Bukkit.createInventory(null, 9, ChatColor.GOLD + "The Gem of Seregon");
    47. gemInv.addItem(Gem);
    48. player.openInventory(gemInv);
    49.  
    50. // #### This is how I assign it the new metadata and put it in the inv ####
    51. swordMeta.setDisplayName(ChatColor.AQUA + "Spell: " + ChatColor.DARK_RED + "Fire Charge");
    52. fireCharge.setItemMeta(swordMeta);
    53. gemInv.addItem(fireCharge);
    54.  
    55.  
    56. }
    57.  
    58. if(commandLabel.equalsIgnoreCase("rename")){
    59.  
    60. if(args.length == 1){
    61.  
    62. ItemStack hand = player.getItemInHand();
    63. ItemMeta handMeta = hand.getItemMeta();
    64. handMeta.setDisplayName(ChatColor.WHITE + args[0]);
    65. hand.setItemMeta(handMeta);
    66.  
    67.  
    68. }else player.sendMessage(ChatColor.DARK_AQUA + "[" + ChatColor.GOLD + "SeregonNation" + ChatColor.DARK_AQUA + "] " + ChatColor.GOLD + "Try using" + ChatColor.DARK_AQUA + " /rename <new name>");
    69.  
    70.  
    71. }
    72. }
    73.  
    74. return false;
    75.  
    76. }
    77. // ###############################
    78. // THIS IS THE BIT I AM WORKING ON
    79. // ###############################
    80.  
    81. @SuppressWarnings("deprecation")
    82. @EventHandler
    83. public void onClickEvent(PlayerInteractEvent event){
    84.  
    85. final Player player = event.getPlayer();
    86.  
    87. // #### And this is where I am trying to execute the code if they click with the item in their hand ####
    88.  
    89. if(player.getItemInHand().getItemMeta() == swordMeta){
    90. player.playEffect(player.getLocation(), Effect.MOBSPAWNER_FLAMES, 1);
    91. player.sendMessage(ChatColor.DARK_AQUA + "[" + ChatColor.GOLD + "SeregonNation" + ChatColor.DARK_AQUA + "] " + ChatColor.GOLD + "" + ChatColor.BOLD + "Your fiery fury has been unleashed!");
    92. this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    93. public void run() {
    94. player.playEffect(player.getLocation(), Effect.MOBSPAWNER_FLAMES, 1);
    95. }
    96. }, 2L);
    97. }
    98. }
    99. }


    The specific bit of code that is not working is:

    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onClickEvent(PlayerInteractEvent event){
    4.  
    5. //this gets a player through the event, not a command
    6. final Player player = event.getPlayer();
    7.  
    8. // #### And this is where I am trying to execute the code if they click with the item in their hand ####
    9.  
    10. if(player.getItemInHand().getItemMeta() == swordMeta){
    11. player.playEffect(player.getLocation(), Effect.MOBSPAWNER_FLAMES, 1);
    12. player.sendMessage(ChatColor.DARK_AQUA + "[" + ChatColor.GOLD + "SeregonNation" + ChatColor.DARK_AQUA + "] " + ChatColor.GOLD + "" + ChatColor.BOLD + "Your fiery fury has been unleashed!");
    13. this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    14. public void run() {
    15. player.playEffect(player.getLocation(), Effect.MOBSPAWNER_FLAMES, 1);
    16. }
    17. }, 2L);
    18. }
    19. }


    It is not a problem with the commands or plugin.yml, they are all fine and the console does not print any errors. Any feedback or help would be greatly appreciated.

    Thanks for reading,
    Ben

    *CLICK HERE FOR FULL PASTEBIN*
     
  2. Offline

    MrInspector

    You're not registering events! D:
     
  3. Offline

    Benlewis9000

    MrInspector
    Oh, I've never done that before, how is it done?

    Ok I tried sticking this line in my onEnable:
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
    It did nothing, and I'm still not really sure on what I am doing.

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

    jonny1011

    Change

    public class walrus extends JavaPlugin{

    To

    public class walrus extends JavaPlugin implements Listener {
     
    Benlewis9000 likes this.
  5. Offline

    MrInspector

    Benlewis9000 likes this.
  6. Offline

    Benlewis9000

    jonny1011 MrInspector
    Eclipse put that line in for me already, still no luck on it working. I am starting to think it may just be my code, not the listener. Any other ideas?
     
  7. Offline

    MrInspector

    Can you show me your full code now?
     
  8. Offline

    Benlewis9000

  9. Offline

    MrInspector

    Okay question: Is the plugin loading in at all? (like does the other features work?)
     
  10. Offline

    Benlewis9000

    Oh, yeah :) I am getting the logger messages and the commands all work :) no errors printing either, it just purely is not responding to my PlayerInterectEvent section. Would you like to test it with the .jar?
     
  11. Offline

    MrInspector

    It's okay, but thanks for asking!

    Okay try this, put this right under this code so it looks like this

    Code:java
    1. player.playEffect(player.getLocation(), Effect.MOBSPAWNER_FLAMES, 1);
    2. System.out.println("DID THIS WORK!?!?!?");
     
  12. Offline

    Benlewis9000

    MrInspector
    Do you mean the player.sendMessage? That println wouldnt do anything on a Bukkit plugin. Or if it does, then my code isnt working.
     
  13. Offline

    MrInspector

    I mean add the print line under the player playEffect

    I use it for debugging, you can also broadcast a message w/ bukkit.broadcastmessage if you like

    after the plugin loads in, try doing it and see if it prints the message
     
  14. Benlewis9000 it should print out a line in the console
    ninja'd by MrInspector
    but, for your problem, you can make some fields in your main class, and then get them in the other class, and other methods.
     
    MrInspector likes this.
  15. Offline

    Benlewis9000

    MrInspector
    It is in there, along with a sendMessage, and I am still getting nothing.
    Datdenkikniet
    I think thats a bit beyond me for now, I am only a very small time Dev just beginning with Java. I might give it a go if I can though.
     
  16. Offline

    Benlewis9000

    MrInspector
    Okay, so I performed a quick test by taking out the line if(player.getItemInHand().getItemMeta() == swordMeta) and keeping:
    Code:java
    1. public void onClickEvent(PlayerInteractEvent event){
    2.  
    3. //this gets a player through the event, not a command
    4. final Player player = event.getPlayer();
    5.  
    6. // #### And this is where I am trying to execute the code if they click with the item in their hand ####
    7.  
    8. //if(player.getItemInHand().getItemMeta() == swordMeta){
    9. player.playEffect(player.getLocation(), Effect.MOBSPAWNER_FLAMES, 1);
    10. System.out.println("DID THIS WORK!?!?!?");
    11. player.sendMessage(ChatColor.DARK_AQUA + "[" + ChatColor.GOLD + "SeregonNation" + ChatColor.DARK_AQUA + "] " + ChatColor.GOLD + "" + ChatColor.BOLD + "Your fiery fury has been unleashed!");
    12. this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    13. public void run() {
    14. player.playEffect(player.getLocation(), Effect.MOBSPAWNER_FLAMES, 1);
    15. }
    16. }, 2L);
    17. }

    and when I randomly left click in game it worked!
    Therefore, it must be the player.getItemInHand line that is causing the error! Anyone notice anything wrong with it? Datdenkikniet
     
  17. Offline

    MrInspector

    I had a suspicion. :p
     
  18. Offline

    Benlewis9000

    MrInspector
    So how do I fix it to only run when I have the specific item in my hand?
     
  19. Offline

    Vandrake

    Check if the item is in hand before running it, or learn how to code before hand x.x
     
  20. Offline

    MrInspector

    Try something like this. ;)
    Code:java
    1. if ((e.getCurrentItem().getType() == Material.ITEM)
    2. && (e.getCurrentItem().getItemMeta().getDisplayName()
    3. .equals("Name")))
     
  21. Offline

    Vandrake

    If the Item has no itemmeta that will return a NPE in the console
     
  22. Offline

    MrInspector

    Eh, I'm sure if he can think of something. :p

    Benlewis9000
    This is the code from my mc1v1, if it helps you go ahead and use it, just adjust to your needs.

    Code:java
    1. @EventHandler
    2. public void onInventoryClick(InventoryClickEvent e) {
    3. if ((e.getCurrentItem().getType() == Material.BLAZE_ROD)
    4. && (e.getCurrentItem().getItemMeta().getDisplayName()
    5. .equals(ChatColor.RED + "1v1 Stick")))
    6. e.setCancelled(true);
    7. else if ((e.getCurrentItem().getType() == Material.BLAZE_ROD)
    8. && (e.getCurrentItem().getItemMeta().getDisplayName()
    9. .equals(ChatColor.RED + "Buffed 1v1 Stick")))
    10. e.setCancelled(true);
    11. else if ((e.getCurrentItem().getType() == Material.BLAZE_ROD)
    12. && (e.getCurrentItem().getItemMeta().getDisplayName()
    13. .equals(ChatColor.RED + "Buffed 1v1 Stick w/ Speed II")))
    14. e.setCancelled(true);
    15. else
    16. ;
    17. }
     
    Benlewis9000 likes this.
  23. Offline

    Benlewis9000

    Vandrake
    ''Check if the item is in hand before running it, or learn how to code before hand x.x''
    What do you think I am trying to do? How am I supposed to learn without writing code and seeking help when in doubt? This is my way of learning! I do my research, follow series of tutorials, then test my knowledge by trying to create custom plugins based on what I know. When all I can do fails to work, I don't really have a choice but to come here and seek help. By doing so I am learning. So thank you very much for your comment, but I find it highly illogical and invalid. Please take your negativity elsewhere :)
    MrInspector
    Thanks for that :) I will try it out now!
    Also, Vandrake, focusing on your line 'CHeck if the item is in hand', what do you think this line of code is for:
    if(player.getItemInHand().getItemMeta() == swordMeta){
     
    MrInspector likes this.
  24. Offline

    diage

    As a note on this.. == compares primatives or enums reliably, otherwise it will not provide you the boolean answer you are expecting. (Has more to do with memory locations.)

    All java objects implement a .equals method which something at some point may or may not override. I might recommend using the .equals method for any non primitive or enum value assuming whoever created the object you're toying with implemented .equals. Also, ItemStacks have methods such as issimiliar which can check material and etc.

    A bit of advice is to look through the bukkit API for helper methods that can help you figure out what exactly you need (such as .hasMeta or .isSimiliar or whatever the exact names of the methods are.)
     
    Benlewis9000 likes this.
  25. Offline

    Benlewis9000

    MrInspector
    Thank you so much mate :D deleted a few brackets and, obviously, added my material and name, and it all worked great! Hopefully now I can take this further and add warps and 'spells' I have been working on to it :) Thank you once again!
     
    MrInspector likes this.
  26. Offline

    MrInspector

    No problem!

    Good luck! :D
     
Thread Status:
Not open for further replies.

Share This Page