Right Click On Item Has No Effect

Discussion in 'Plugin Development' started by Matthiaantje, Sep 1, 2016.

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

    Matthiaantje

    Hello, I'm back again ...
    My problem is: I created a custom enchanted book an changed the display name.
    Then, in my code, I check that a playerinteractevent is a right click(air and block), then that the item in hand is an enchanted book, an then that the that the item has a display name, if the item right clicked with completed these steps, it'll open an inventory, here's the code:
    Code:
    package cord.vanity;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class VanityPlugin extends JavaPlugin{
    
       public void onEnable()
       {
         System.out.println(ChatColor.AQUA + "Vanity Enabled!");
       }
       
       public static VanityPlugin getInstance()
       {
         return (VanityPlugin)JavaPlugin.getPlugin(VanityPlugin.class);
       }
       
       public static Inventory VanityMenu = Bukkit.createInventory(null, 54, ChatColor.GOLD + "Vanity Menu");
       {
         ItemStack Hats = DataBase.Skulls;
         ItemMeta hatMeta = Hats.getItemMeta();
         hatMeta.setDisplayName(ChatColor.BLUE + "Hats");
         Hats.setItemMeta(hatMeta);
         VanityMenu.setItem(10, Hats);
         ItemStack WardRobe = DataBase.Clothes;
         ItemMeta clothMeta = WardRobe.getItemMeta();
         clothMeta.setDisplayName(ChatColor.RED + "WardRobe");
         WardRobe.setItemMeta(clothMeta);
         VanityMenu.setItem(12, WardRobe);
         ItemStack Gadgets = DataBase.Gadgets;
         ItemMeta gadgetMeta = Gadgets.getItemMeta();
         gadgetMeta.setDisplayName(ChatColor.GREEN + "Gadgets!");
         Gadgets.setItemMeta(gadgetMeta);
         VanityMenu.setItem(14, Gadgets);
         ItemStack Trails = DataBase.Trails;
         ItemMeta trailMeta = Trails.getItemMeta();
         trailMeta.setDisplayName(ChatColor.GOLD + "Trails");
         Trails.setItemMeta(trailMeta);
         VanityMenu.setItem(16, Trails);
       }
       
       public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
       {
         if(cmd.getName().equalsIgnoreCase("vanity"))
         {
           Player p = (Player) sender;
           ItemStack Menu = DataBase.VanityMeny;
           ItemMeta menuMeta = Menu.getItemMeta();
           menuMeta.setDisplayName(ChatColor.AQUA + "Vanity Menu");
           Menu.setItemMeta(menuMeta);
           p.getInventory().addItem(Menu);
         }
         return false;
       }
       
       public void onVanity(PlayerInteractEvent e)
       {
         Player p = (Player) e.getPlayer();
         if(e.getAction() == Action.RIGHT_CLICK_AIR ||e.getAction() == Action.RIGHT_CLICK_BLOCK)
         {
           if(p.getInventory().getItemInHand().getType() == Material.ENCHANTED_BOOK)
           {
             if(p.getInventory().getItemInHand().getItemMeta().getDisplayName() == "Vanity Menu")
             {
               p.openInventory(VanityMenu);
             }
           }
         }
       }
    }
    
    Plugin.yml:
    Code:
    main: cord.vanity.VanityPlugin
    name: Vanity
    version: 1.0
    author: Matthiaantje
    commands:
      vanity:
      description: Gives The Vanity Item
    
    DataBase:
    Code:
    package cord.vanity;
    
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
    
    public class DataBase {
    
       VanityPlugin plugin = VanityPlugin.getInstance();
       public static boolean DataActive = true;
       
       /*
        * Main Menu Items
        */
       
       public static ItemStack Skulls = new ItemStack(Material.SKULL, 1);
       public static ItemStack Clothes = new ItemStack(Material.LEATHER_CHESTPLATE, 1);
       public static ItemStack Gadgets = new ItemStack(Material.REDSTONE, 1);
       public static ItemStack Trails = new ItemStack(Material.RED_ROSE, 1);
       public static ItemStack Miniatures = new ItemStack(Material.SKULL, 1);
       public static ItemStack Pets = new ItemStack(Material.BONE, 1);
       public static ItemStack Disguise = new ItemStack(Material.SKULL, 1);
       public static ItemStack Eomoji = new ItemStack(Material.SKULL, 1);
       
       /*
        * Item That will be given at command "vanity"
        */
       
       public static ItemStack VanityMeny = new ItemStack(Material.ENCHANTED_BOOK);
       
    }
    
    Please, can someone help ASAP, I really need this!!!
     
  2. Offline

    Zombie_Striker

    What is your issue? This is what is currently happening. What do you want to happen?
    [EDIT] You forgot the @EventHandler tag. All events MUST have this tag in order to work.
     
  3. Offline

    Matthiaantje

    Ow, How stupid!!!
    But, it still doesn't work, it's just:
    when a player rightclicks with the book, in air or on a block, it'll open up an inventory
     
  4. Offline

    Zombie_Striker

    Most likely this is your issues. Strings cannot be compared using ==. Use .equals instead.
     
    Rocologo01 likes this.
  5. Offline

    kameronn

    @Matthiaantje
    Or maybe this is his issue in his imports
    "import net.md_5.bungee.api.ChatColor"

    Also I would recommend using .equalsIgnoreCase instead
     
  6. Offline

    Matthiaantje

    Still doesn't work -_-
    And do I have to change the Chatcolor import to the chatcolor from bukkit?
     
  7. Offline

    Zombie_Striker

    @Matthiaantje
    Not really. It is still a good idea to use the org.Bukkit chatcolor, but it is not what is causing your problem.

    Have you debugged? What checks are you missing/ stop working?
     
  8. Offline

    Matthiaantje

    Error: Could not find or load main class org.bukkit.craftbukkit.libs.org.ibex.nestedvm.ClassFileCompiler

    That's the only thing it said
     
  9. Offline

    Zombie_Striker

    @Matthiaantje
    Debugging and Eclipses debug button are two different things. Eclipses debug feature runs the code. Actual debugging meaning doing something to show that your code is working. This can be printing out a message like "This if statement works!" or "The displayname is "+displayname. Try printing out values inside the event to figure out what is wrong.
     
  10. Offline

    Matthiaantje

    Hmmm, really can't see what is going on weird here..., everything works fine, but it just won't let meopen up that damn inventory
     
  11. Offline

    Zombie_Striker

    Do the following:
    1. Send a message to yourself inside the event (right after you create the player variable) to see if the event runs. If you do not see the message, make sure you register the class.
    2. If that does work, send a message under the action if statement. If this does not work, ricking the item is not working.
    3. If that does work, move down to the second if statement. If this does not work, then the type is not a book/ the item can be null.
    4. If that does work, move to the last if statement. If this does not work, the if statement is not working correctly.
    5. Finally, if you see a message at the very bottom if statement, that means the inventory is broken.
     
  12. Does this even compile?!?!?!?
     
    Higgsboson728 and Matthiaantje like this.
  13. Offline

    Matthiaantje

    Yup, it compiles, made to test a small command that opens op the inventory, with the inventory itself is nothing wrong, it's really just the way to acces the inventory...
     
  14. @bwfcwalshy
    For whatever reason, you can actually put things in curly brackets wherever you want, and the compiler just accepts it.
     
  15. Offline

    Matthiaantje

    How would you do it then? I think this is the most effective way of doing it...
    And, yes, the compiler accepts it... if I run a smaal command that opens the inventory, it worked:)
     
  16. @AlvinB I know but in the brackets he does things that can only be done in a method, any IDE should pick up that it isn't a valid method and should not compile it.
     
  17. Offline

    Matthiaantje

    Okay, I think I found an Bukkit scammer on yt xD, but in his vid, he showed the world to do it like this :p
     
  18. @Matthiaantje Learning of YouTube is not a good idea. Learn the basics of Java then look at tutorials (Offical preferably) you won't have any issues then.
     
  19. Offline

    Matthiaantje

    Okay, but one thing, this is what the inventory looks like NOW:
    Code:
    public void Menu()
       {
         VanityMenu = Bukkit.createInventory(null, 54, ChatColor.GOLD + "Vanity Menu");
         ItemStack hats = DataBase.Skulls;
         ItemStack WardRobe = DataBase.Clothes;
         ItemStack Gadgets = DataBase.Gadgets;
         ItemStack Trails = DataBase.Trails;
         SkullMeta hatMeta = (SkullMeta) hats.getItemMeta();
         hatMeta.setDisplayName(ChatColor.GREEN +"Hats");
         hatMeta.setOwner("Seska_Rotan");
         hats.setItemMeta(hatMeta);
         ItemMeta clothMeta = WardRobe.getItemMeta();
         clothMeta.setDisplayName(ChatColor.GREEN + "WardRobe");
         WardRobe.setItemMeta(clothMeta);
         ItemMeta gadgetMeta = Gadgets.getItemMeta();
         gadgetMeta.setDisplayName(ChatColor.GREEN + "Gadgets");
         Gadgets.setItemMeta(gadgetMeta);
         ItemMeta trailMeta = Trails.getItemMeta();
         trailMeta.setDisplayName(ChatColor.GREEN + "Trails");
         VanityMenu.setItem(10, hats);
         VanityMenu.setItem(12, WardRobe);
         VanityMenu.setItem(14, Gadgets );
         VanityMenu.setItem(16, Trails);
       }
    
    But then, How do I get it that if a player clicks the enchanted book it opens the inventory:
    (code for the book AND optional command:)
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
       {
         if(cmd.getName().equalsIgnoreCase("vanity"))
         {
           Player p = (Player) sender;
           ItemStack Menu = DataBase.VanityMeny;
           ItemMeta menuMeta = Menu.getItemMeta();
           menuMeta.setDisplayName(ChatColor.AQUA + "Vanity Menu");
           Menu.setItemMeta(menuMeta);
           p.getInventory().addItem(Menu);
           p.sendMessage(ChatColor.RED + "If opening the book isn't working, please type /vaninv to open op an additional Inventory!");
         }
         if(cmd.getName().equalsIgnoreCase("vaninv"))
         {
           Player p = (Player) sender;
           p.openInventory(VanityMenu);
         }
         return false;
       }
    
     
  20. @bwfcwalshy
    Just pasted said code into IntelliJ, no errors at all.
     
  21. Offline

    Matthiaantje

    It doesn't give me errors. It Just doesn't work...
    And how to check (using inventoryclickevent) for a enchanted book named "Vanity Menu"

    Verstuurd vanaf mijn HUAWEI SCL-L21 met Tapatalk
     
  22. @Matthiaantje Do a quick check which outputs wether the item is the same as the item in hand or not. I'd bet it's a problem with the check.
     
    Last edited by a moderator: Sep 1, 2016
  23. Offline

    Matthiaantje

    Okay, so, that problem is solved...
    I can now succesfully open the inventory, but now, there's nothing IN it... altough I have set Items IN it...
    upload_2016-9-2_8-4-12.png

    Here's the code
    Code:
    package cord.vanity;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.inventory.meta.SkullMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import net.md_5.bungee.api.ChatColor;
    import net.milkbowl.vault.economy.Economy;
    
    public class VanityPlugin extends JavaPlugin implements Listener{
       
       public static Economy eco = null;
       public static double VanityCoin = 0;
       public static Inventory VanityMenu;
    
       public void onEnable()
       {
         System.out.println(ChatColor.AQUA + "Vanity Enabled!");
         this.getServer().getPluginManager().registerEvents(this, this);
         VanityMenu = Bukkit.createInventory(null, 54, ChatColor.GOLD + "Vanity Menu");
         ItemStack hats = DataBase.Skulls;
         ItemStack WardRobe = DataBase.Clothes;
         ItemStack Gadgets = DataBase.Gadgets;
         ItemStack Trails = DataBase.Trails;
         SkullMeta hatMeta = (SkullMeta) hats.getItemMeta();
         hatMeta.setDisplayName(ChatColor.GREEN +"Hats");
         hatMeta.setOwner("Seska_Rotan");
         hats.setItemMeta(hatMeta);
         ItemMeta clothMeta = WardRobe.getItemMeta();
         clothMeta.setDisplayName(ChatColor.GREEN + "WardRobe");
         WardRobe.setItemMeta(clothMeta);
         ItemMeta gadgetMeta = Gadgets.getItemMeta();
         gadgetMeta.setDisplayName(ChatColor.GREEN + "Gadgets");
         Gadgets.setItemMeta(gadgetMeta);
         ItemMeta trailMeta = Trails.getItemMeta();
         trailMeta.setDisplayName(ChatColor.GREEN + "Trails");
         VanityMenu.setItem(10, hats);
         VanityMenu.setItem(12, WardRobe);
         VanityMenu.setItem(14, Gadgets );
         VanityMenu.setItem(16, Trails);
       }
       public static VanityPlugin getInstance()
       {
         return (VanityPlugin)JavaPlugin.getPlugin(VanityPlugin.class);
       }
       public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
       {
         if(cmd.getName().equalsIgnoreCase("vanity"))
         {
           Player p = (Player) sender;
           ItemStack vanityMenu = new ItemStack(Material.ENDER_CHEST);
           p.getInventory().addItem(vanityMenu);
         }
         if(cmd.getName().equalsIgnoreCase("invMenu"))
         {
           Player p = (Player) sender;
           p.sendMessage(ChatColor.GREEN + "Given Inventory!!!");
           p.openInventory(VanityMenu);
         }
         return false;
       }
       
       public void Menu(Player p)
       {
       }
       
       @EventHandler
       public void onVanity(PlayerInteractEvent e)
       {
         Player p = (Player) e.getPlayer();
         if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK)
         {
           if(p.getItemInHand().getType().equals(Material.ENDER_CHEST))
           {
             p.openInventory(VanityMenu);
           }
         }
       }
    }
    
     
  24. 1. You not need a message in onEnable() saying the plugin was registered, bukkit automatically does this for you.
    2. Try moving the part where the menu "VanityMenu" was created from onEnable to the actual command. Also, if you want to get good at bukkit, come on the forums.
    3. Check if the player placed the block, cancel event.
    4. CONVENTIONS
    Disclaimer: Read this section whole if you want this word to never come up again.
    • Check if the sender is not an instance of a sender, and do some code. That way, when the console tries to open a menu, it will get a message saying that the console cannot perform the command.
    • Variable names should always be capitalized in this sequence: firstSecondThirdOnward
    • Your get instance should look like this:
    Code:
    public static instance instance; //Assuming your class name is instance
    @Override
    onEnable() {
    instance = this;
    }
    @Override()
    onDisable() {
    instance = null;
    }
    /**
    The proper way to static-ly access the instance
    @return the plugin if enable, null if disabled
    */
    public static  getInstace() {
    return instance;
    }
    
    
     
    Last edited: Sep 2, 2016
Thread Status:
Not open for further replies.

Share This Page