Solved this.plugin will will not work

Discussion in 'Spigot Plugin Development' started by ironbolt, Mar 28, 2020.

Thread Status:
Not open for further replies.
  1. Hello!
    When I put
    Code (Java):
    this.plugin = plugin
    into a class, it says cannot resolve symbol plugin. Does anyone know the problem? I. made sure the class was initialized in my main class. This is the code:
    Code (Java):
    package com.funzbolt.megasurvival.ui;

    import com.funzbolt.megasurvival.Main;
    import com.funzbolt.megasurvival.utils.Utils;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.Plugin;

    public class GamemodeUi {

        public static Inventory inv;
        public static String inventory_name;
        public static int inv_rows = 4 * 9;
        public GamemodeUi(Main plugin){
    this.plugin = plugin
        }
        public static void initalize() {
            inventory_name = Utils.chat("&6&lTest GUI");

            inv = Bukkit.createInventory(null, inv_rows);
        }

        public static Inventory GUI(Player p) {
            Inventory toReturn = Bukkit.createInventory(null, inv_rows, inventory_name);
            if (p.hasPermission("megasurvival.gamemode")) {
                Utils.createItem(inv, 4, 1, 1, "&cTest Item", "&7This is lore line 1", "&bSecond line", "&3Third line");
            } else {
                p.sendMessage(Utils.chat(plugin.getConfig().getString("no_permission")));
            }

            toReturn.setContents(inv.getContents());
            return toReturn;
        }

        public static void clicked(Player p, int slot, ItemStack clicked, Inventory inv) {
            if (clicked.getItemMeta().getDisplayName().equalsIgnoreCase(Utils.chat("&cTest Item"))) {
                p.setDisplayName(Utils.chat("&8[&6*&8] &6&lYou have successfully made a GUI!"));
                p.closeInventory();
            }
        }
    }
     
    This is the main class code:
    Code (Java):
    package com.funzbolt.megasurvival;

    import com.funzbolt.megasurvival.commands.GamemodeCommand;
    import com.funzbolt.megasurvival.listeners.GamemodeUiClickListener;
    import com.funzbolt.megasurvival.ui.GamemodeUi;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {
        public void onEnable(){
            saveDefaultConfig();
            new GamemodeUiClickListener(this);
            GamemodeUi.initalize();
            new GamemodeCommand(this);
        }

    }
     
    I'm new to coding so if this is an easy fix, please don't judge me.
     
    • Funny Funny x 1
  2. I mean you have no variable to hold that instance? this.plugin will reffer to a variable named "plugin" inside the class
     
    • Agree Agree x 2
    • Like Like x 1
    • Useful Useful x 1
  3. But I use the same type of variable in other classes and it works. This is one of those classes.
    Code (Java):
    package com.funzbolt.megasurvival.commands;

    import com.funzbolt.megasurvival.Main;
    import com.funzbolt.megasurvival.ui.GamemodeUi;
    import com.funzbolt.megasurvival.utils.Utils;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class GamemodeCommand implements CommandExecutor {

        private Main plugin;

        public GamemodeCommand(Main plugin){
            this.plugin = plugin;

            plugin.getCommand("gamemode").setExecutor(this);
        }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

            if (!(sender instanceof Player)){
                sender.sendMessage(Utils.chat(plugin.getConfig().getString("not_a_player_message")));
                return true;
            }

            Player p = (Player) sender;

            if (p.hasPermission("megasurvival.gamemode")){
                p.openInventory(GamemodeUi.GUI(p));
            }
            return false;
        }
    }
     
     
  4. I just put private Main plugin; into the clas, but when I do plugin.getConfig, it says this:
    Non-static field 'plugin' cannot be referenced from a static context
     
  5. Why are you abusing static in the class? just noticed everything is static
     
    • Agree Agree x 1
    • Useful Useful x 1
  6. I followed a tutorial on how to do create a GUI.
     
  7. Is it possible for me to use plugin.getConfig in this situation?
     
  8. Yes, but you can figure that out by yourself :p
    Not too hard, try and Google the word constructor and what 'this' means.
     
    • Useful Useful x 1
  9. so do i search up spigot constructor this?
     
  10. No just java constructor and java this. Your problem has nothing to do with spigot
     
    • Useful Useful x 1
  11. I searched up java this and looked at w3 schools. They have the exact some code as me (except i is plugin)
     
  12. Okay...
    'this' is a reference to the current object. You can imagine it as a pointer pointing to itself. It doesn't work in your case because it doesn't have the class variable 'plugin' it can point to (this.plugin).
    Btw where you already put 'this' is inside a constructor but you didn't seem to understand it.
    While a lot of people complain that you shouldn't learn java through Minecraft plugins, I don't have a problem with it. In fact I started java with plugins too. But what really is important, is that you don't copy code from other sources. Be interested, see new code and question it and look something up if you don't understand it. Go beyond Minecraft plugins, look at the bigger field of java itself.
     
    • Useful Useful x 1
  13. What are you trying to achieve exactly?

    As for your static abuse, remove all static entirely. To access your methods create an instance of your main class, and you can then pass that instance around to any additional classes that require it.

    Code (Java):
    GameModeGUI gui = new GameModeGUI(this);

    gui.open(player);
    etc..
    Your method names aren't clear. Like "GUI" could easily be renamed to open and that would make far more sense. Naming of your variables also doesn't follow proper java naming conventions, "inventory_name" should be "inventoryName".

    Additionally, look into getter and setters, because with these you can declare your fields as private, then retrieve them with getters.

    Code (Java):
    gui.getInventoryName();
    If any of this doesn't make sense, a quick google search will provide additional examples.
     
    • Useful Useful x 1
  14. Search on google java basics, it will help you massively in the long run
     
    • Useful Useful x 1
  15. Thanks for the help and advice, I will make sure to not entirely copy from other sources and try to create my own. How did this.plugin work in the other classes? I'm doing the exact same thing in this class that I did in the other class I put in.

    Thank you for your help I will do the changes you are telling me to do. Also, do I have to remove all things with static? What I'm trying to do is send a player a message from the config but I can't access the config without plugin.getConfig. plugin.getConfig does not work because this.plugin does not work.

    Thank you for the advice, I will make sure to do it.
     
  16. There are some acceptable uses of static, such as methods that belong to the class itself, but plugin.getConfig is not one. What you need to do is pass an instance of your main class into additional classes to then be able to call methods from it.

    Code (Java):
    //IN MAIN CLASS

    SomeClass someClass = new SomeClass(this)
    //^^ Passing main class ("this"), into the constructor of another class



    //IN SOME CLASS

    private MainClass plugin;

    public SomeClass (MainClass plugin) {

    this.plugin = plugin;

    }

    //^^ Taking the instance, and assigning it to our plugin variable to then be used throughout our methods in this class

     
    From here, we can now call plugin.getConfig in SomeClass, if that makes sense.
     
    • Useful Useful x 1
  17. Hi Wick. I did what you told me to do, but I still get the message "Non-static field 'plugin' cannot be referenced from a static context"
    Here is my code.
    Main class:
    Code (Java):
    package com.funzbolt.megasurvival;

    import com.funzbolt.megasurvival.commands.GamemodeCommand;
    import com.funzbolt.megasurvival.listeners.GamemodeUiClickListener;
    import com.funzbolt.megasurvival.ui.GamemodeUi;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {
        public void onEnable(){
            saveDefaultConfig();
            new GamemodeUiClickListener(this);
            GamemodeUi.initalize();
            new GamemodeUi(this);
            new GamemodeCommand(this);
        }

    }
     
    GamemodeUi:
    Code (Java):
    package com.funzbolt.megasurvival.ui;

    import com.funzbolt.megasurvival.Main;
    import com.funzbolt.megasurvival.utils.Utils;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;

    public class GamemodeUi {

        public static Inventory inv;
        public static String inventory_name;
        public static int inv_rows = 4 * 9;
        private Main plugin;
        public GamemodeUi(Main plugin){
            this.plugin = plugin;
        }
        public static void initalize() {
            inventory_name = Utils.chat("&6&lTest GUI");

            inv = Bukkit.createInventory(null, inv_rows);
        }

        public static Inventory GUI(Player p) {
            Inventory toReturn = Bukkit.createInventory(null, inv_rows, inventory_name);
            if (p.hasPermission("megasurvival.gamemode")) {
                Utils.createItem(inv, 4, 1, 1, "&cTest Item", "&7This is lore line 1", "&bSecond line", "&3Third line");
            } else {
                p.sendMessage(Utils.chat(plugin.getConfig().getString("no_permission")));
            }

            toReturn.setContents(inv.getContents());
            return toReturn;
        }

        public static void clicked(Player p, int slot, ItemStack clicked, Inventory inv) {
            if (clicked.getItemMeta().getDisplayName().equalsIgnoreCase(Utils.chat("&cTest Item"))) {
                p.setDisplayName(Utils.chat("&8[&6*&8] &6&lYou have successfully made a GUI!"));
                p.closeInventory();
            }
        }
    }
     
     
  18. Yes, you cannot access a non-static field in a static context, or in other words, you cannot use a non-static variable in a static method. Remove the static keyword from your methods like I said.
     
    • Useful Useful x 1
  19. Ok, I have done what you said to do and there are no more errors in that class. In another class though, there is an error that states the folowing.
    Non-static method 'GUI(org.bukkit.entity.Player)' cannot be referenced from a static context
    This is the code:

    Code (Java):
    package com.funzbolt.megasurvival.commands;

    import com.funzbolt.megasurvival.Main;
    import com.funzbolt.megasurvival.ui.GamemodeUi;
    import com.funzbolt.megasurvival.utils.Utils;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class GamemodeCommand implements CommandExecutor {

        private Main plugin;

        public GamemodeCommand(Main plugin){
            this.plugin = plugin;

            plugin.getCommand("gamemode").setExecutor(this);
        }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

            if (!(sender instanceof Player)){
                sender.sendMessage(Utils.chat(plugin.getConfig().getString("not_a_player_message")));
                return true;
            }

            Player p = (Player) sender;

            if (p.hasPermission("megasurvival.gamemode")){
                p.openInventory(GamemodeUi.GUI(p));
            }
            return false;
        }
    }
     
    Edit: I'm sorry for all of the questions.
     
  20. This is basic java, but I'll guide you none the less.

    When accessing anything static, it involves taking the class name directly.
    Code (Java):
    ClassName.MethodName
    ClassName.VariableName
    This is what you are trying to do, but you cant because I told you to remove static keywords, as we want to avoid this. It's abusing static, it's lazy coding, you are trying to take the easy route, but it goes against OOP which java is.

    Instead, we can create instances of classes, and use the instance, there are different ways to approach this, but if its a generic handling class with methods, having one instance in your main class and passing that around will work.

    So, in our main class we can create an instance of GamemodeUi, let's call it guiHandler since that's kinda what it is.

    Code (Java):
    private GamemodeUi guiHandler;

    //then, when plugin enables (in onEnable method)

    guiHandler = new GamemodeUi(this);

    //this is called initializing, we are creating an instance of GamemodeUi
     
    Now, with our newly created instance, let's pass it into classes that need it, exactly like we did with the instance of our main class.

    Code (Java):

    new GamemodeCommand(this, guiHandler);
    //now we pass the instance of GamemodeUi into the GamemodeCommand instance
     
    Code (Java):
    public class GamemodeCommand implements CommandExecutor {

        private Main plugin;
        private GamemodeUi guiHandler;

        public GamemodeCommand(Main plugin, GamemodeUi guiHandler){
            this.plugin = plugin;
            this.guiHandler = guiHandler;
        }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

            //now we can use this GamemodeUi instance to call its methods or access variables

            guiHandler.GUI(player)
         //but as I said before, GUI should be renamed something like "open" or "openGUI" so it becomes

            guiHandler.openGUI(player);
    //it just looks better and reading it back, makes genuine sense right?
         

         }
    }
     
    #20 Wick__, Mar 29, 2020
    Last edited: Mar 29, 2020
Thread Status:
Not open for further replies.