Inventory Not Opening - Custom Plugin - HELP! <3

Discussion in 'Spigot Plugin Development' started by 88p, Jun 10, 2017.

  1. 88p

    88p

    I have made this java class, following a tutorial as previous inventorys/guis I have made have produced some bugs this is my main class. I have no idea why my plugin doesn't work. Please assist.

    Code (Text):
    package me.88p;

    import org.bukkit.Bukkit;
    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.plugin.java.JavaPlugin;

    public class MenuInv extends JavaPlugin implements Listener {
       
        private Menu menu;
       
        public void onEnable() {
            menu = new Menu(this);
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            }
       
        @EventHandler
       
    public void onPlayerInteract(PlayerInteractEvent e) {
            if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
            menu.show(e.getPlayer());
        }
    }
     

    This is my other class file.

    Code (Text):
    package me.88p;

    import java.util.Arrays;

    import org.bukkit.Bukkit;
    import org.bukkit.DyeColor;
    import org.bukkit.GameMode;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.material.Wool;
    import org.bukkit.plugin.Plugin;

    public class Menu implements Listener {
       
        private Inventory inv;
        private ItemStack c, s, a;
       
        public Menu(Plugin p) {
            inv = Bukkit.getServer().createInventory(null, 9, "Gamemode Chooser");
           
            c = createItem(DyeColor.GREEN,"Creative");
            s = createItem(DyeColor.YELLOW, "Suvival");
            a = createItem(DyeColor.RED, "Adventure");
           
            inv.setItem(1, c);
            inv.setItem(3, s);
            inv.setItem(5, a);
           
           
            Bukkit.getServer().getPluginManager().registerEvents(this, p);
        }
       
        private ItemStack createItem(DyeColor dc, String name) {
            ItemStack i = new Wool(dc).toItemStack(2);
            ItemMeta im = i.getItemMeta();
            im.setDisplayName(name);
            im.setLore(Arrays.asList("Set your gamemode"," to " +name.toLowerCase() + "mode!"));
            i.setItemMeta(im);
            return i;
        }
       
        public void show(Player p) {
            p.openInventory(inv);
        }
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (!e.getInventory().equals(inv)) return;
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Survival")) {
                e.getWhoClicked().setGameMode(GameMode.SURVIVAL);
            }
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Creative")) {
                e.getWhoClicked().setGameMode(GameMode.CREATIVE);
            }
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Adventure")) {
                e.getWhoClicked().setGameMode(GameMode.ADVENTURE);
            }
        }
    }
    Thanks in advance.
     
  2. Are there any errors in the console?
     
    • Agree Agree x 1
  3. 88p

    88p

    Nope, when I click nothing happens

    Its like there is an error in the listener? I just cant seem to find it :D
     
  4. You need to register the listener for Menu in your main class, in the onEnable method add

    Code (Text):
     Bukkit.getServer().getPluginManager().registerEvents(new Menu(), this);
    I'm pretty sure you cannot register events from any class that does not extend JavaPlugin

    And why not just do all of this in the PlayerInteractEvent? Why do it in another class?
     
    • Useful Useful x 1
  5. 88p

    88p

    When I change the code already there, it says this >> "The constructor Menu() is undefined"
     
  6. Try creating an instance of your plugin main class in Menu
    and then registering the listener using new Menu(this)
     
    • Useful Useful x 1
  7. 88p

    88p

    Alright, I have changed it to this, It doesnt show any errors in console or syntax errors.
    (Main Class)
    Code (Text):
    package me.88p;

    import org.bukkit.Bukkit;
    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.plugin.java.JavaPlugin;

    public class MenuInv extends JavaPlugin implements Listener {
     
        private Menu menu;
     
        public void onEnable() {
            menu = new Menu(this);
            Bukkit.getServer().getPluginManager().registerEvents(menu, this);
            }
     
        @EventHandler
     
    public void onPlayerInteract(PlayerInteractEvent e) {
            if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
            menu.show(e.getPlayer());
        }
    }
     
    (Secondary Class)
    Code (Text):
    package me.88p;

    import java.util.Arrays;

    import org.bukkit.Bukkit;
    import org.bukkit.DyeColor;
    import org.bukkit.GameMode;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.material.Wool;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Menu extends JavaPlugin implements Listener {
     
        private Inventory inv;
        private ItemStack c, s, a;
     
        public Menu(Plugin p) {
            inv = Bukkit.getServer().createInventory(null, 9, "Gamemode Chooser");
         
            c = createItem(DyeColor.GREEN,"Creative");
            s = createItem(DyeColor.YELLOW, "Suvival");
            a = createItem(DyeColor.RED, "Adventure");
         
            inv.setItem(1, c);
            inv.setItem(3, s);
            inv.setItem(5, a);
         
         
            Bukkit.getServer().getPluginManager().registerEvents(this, p);
        }
     
        private ItemStack createItem(DyeColor dc, String name) {
            ItemStack i = new Wool(dc).toItemStack(2);
            ItemMeta im = i.getItemMeta();
            im.setDisplayName(name);
            im.setLore(Arrays.asList("Set your gamemode"," to " + name.toLowerCase() + "mode!"));
            i.setItemMeta(im);
            return i;
        }
     
        public void show(Player p) {
            p.openInventory(inv);
        }
     
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (!e.getInventory().equals(inv)) return;
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Survival")) {
                e.getWhoClicked().setGameMode(GameMode.SURVIVAL);
            }
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Creative")) {
                e.getWhoClicked().setGameMode(GameMode.CREATIVE);
            }
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Adventure")) {
                e.getWhoClicked().setGameMode(GameMode.ADVENTURE);
            }
        }
    }
    Thanks again.





    EDIT: But it still doesn't work :D
     
  8. Wrong, as long you're having the class with the 'extends JavaPlugin' passes you can register an event from anywhere you want.
     
  9. Try debugging, is the event firing? debug you're code and say until where it comes.

    My guess, you aren't right-clicking on a block.
     
  10. 88p

    88p

    I assure you i am clicking on a block and in the air for testing, still nothing
     
  11. Your main class should and only should be a singleton. You're creating more than 1 instance of it.
    Make your "Menu" class not extend JavaPlugin.
    Your classes should also only have 1 purpose. In this case, move the event from your main class to the secondary class.

    EDIT: Your plugin should definitely throw errors because it will not accept a plugin with 2 classes extending JavaPlugin. I suggest you be more familiar with Java before moving on to Spigot
     
  12. 88p

    88p

    Okay, I have made it display a message to the user if the event is fired, and there is no message displayed I have made sure to click on a block and in the air, please help me <3
     
  13. 88p

    88p

    i changed my plugin to follow the "only 1 javaplugin extend" and now it doesnt even load :D
     
  14. depending on which class you're using, the class you extend JavaPlugin should not have a constructor. show me the errors in the console.
     
  15. 88p

    88p

    10.06 08:20:12 [Server] ERROR Could not load 'plugins/ServerStuff.jar' in folder 'plugins'
    10.06 08:20:12 [Server] INFO org.bukkit.plugin.InvalidPluginException: Cannot find main class `me.88p.menuinv'



    Thats the error
     
  16. Look at that second quoted path very carefully. Are you sure its correct? (Hint: it is case sensitive)
     
  17. 88p

    88p

    New error! means that we are fixing things :D

    10.06 08:28:07 [Server] INFO Enabling ServerStuff v1.0
    10.06 08:28:07 [Server] ERROR Error occurred while enabling ServerStuff v1.0 (Is it up to date?)
    10.06 08:28:07 [Server] INFO java.lang.IllegalArgumentException: Plugin already initialized!



    Even though I restarted the server twice.
     
  18. can you show me your updated code now?
     
  19. 88p

    88p

    Secondary
    Code (Text):
    package me.88p;

    import java.util.Arrays;

    import org.bukkit.Bukkit;
    import org.bukkit.DyeColor;
    import org.bukkit.GameMode;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.material.Wool;
    import org.bukkit.plugin.Plugin;

    public class Menu implements Listener {
     
        private Inventory inv;
        private ItemStack c, s, a;
     
        public Menu(Plugin p) {
            inv = Bukkit.getServer().createInventory(null, 9, "Gamemode Chooser");
         
            c = createItem(DyeColor.GREEN,"Creative");
            s = createItem(DyeColor.YELLOW, "Suvival");
            a = createItem(DyeColor.RED, "Adventure");
         
            inv.setItem(1, c);
            inv.setItem(3, s);
            inv.setItem(5, a);
         
         
            Bukkit.getServer().getPluginManager().registerEvents(this, p);
        }
     
        private ItemStack createItem(DyeColor dc, String name) {
            ItemStack i = new Wool(dc).toItemStack(2);
            ItemMeta im = i.getItemMeta();
            im.setDisplayName(name);
            im.setLore(Arrays.asList("Set your gamemode"," to " + name.toLowerCase() + "mode!"));
            i.setItemMeta(im);
            return i;
        }
     
        public void show(Player p) {
            p.openInventory(inv);
        }
     
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (!e.getInventory().equals(inv)) return;
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Survival")) {
                e.getWhoClicked().setGameMode(GameMode.SURVIVAL);
            }
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Creative")) {
                e.getWhoClicked().setGameMode(GameMode.CREATIVE);
            }
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Adventure")) {
                e.getWhoClicked().setGameMode(GameMode.ADVENTURE);
            }
        }
    }
    Main
    Code (Text):
    package me.88p;

    import org.bukkit.Bukkit;
    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.plugin.java.JavaPlugin;

    public class MenuInv extends JavaPlugin implements Listener {
     
        private Menu menu;
     
        public void onEnable() {
            menu = new Menu(this);
            Bukkit.getServer().getPluginManager().registerEvents(menu, this);
            }
     
        @EventHandler
     
    public void onPlayerInteract(PlayerInteractEvent e) {
            if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
            menu.show(e.getPlayer());
            e.getPlayer().sendMessage("Debug Test");
        }
    }
     
     
  20. you should only register events in your main class, not other classes. you are also not registering the player interact event in your main class.