Solved Accessing other classes

Discussion in 'Plugin Development' started by Paljaspers, Jul 28, 2015.

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

    Paljaspers

    Hello

    Recently I started getting into making plugins and I'm still learning so the code I'm about so show might not be 100% efficient or correct according to java code conventions.

    I'm making a plugin which has a lot of classes because I handle a lot of stuff in different classes (Teams, Score, Monster spawns, ...). But I'm not sure how to access other classes "correctly". What I have been doing up until now is making everything static and just accessing other classes by using their class name. But I've been told this is not a good way to code your plugin.

    I realize that this question has probably been answered many times, I've been looking on google trying to phrase my problems as clearly as possible but couldn't really find a way to solve it, or didn't have enough knowledge of the code that was posted to use it on my own code. I always try to solve problems on my own but this one I just can't figure out and is something I run into every time I make a plugin.

    The plugin itself is too big to post so I'll post an example.

    My main class: (Which has actually nothing to do with this)
    Code:
    public class Example extends JavaPlugin {
    
        @Override
        public void onLoad() {
            PluginManager pm = Bukkit.getPluginManager();
            pm.registerEvents(new PlayerJoin(), this);
        }
    
        @Override
        public void onDisable() {
    
        }
    
    }
    A class that handles something (in this case the amount of kills a player has)
    Code:
    public class PlayerData {
    
        private static Map<Player, Integer> playerKills = new HashMap<>();
    
        public static void createPlayer(Player player) {
            playerKills.put(player, 0);
        }
    //thise one would be used in a command
        public static int getKills(Player player) {
            return playerKills.get(player);
        }
    //this one would be used in "EntityDeathEven"
        public static void addKill(Player player) {
            int playerKillAmount;
    
            playerKillAmount = playerKills.get(player);
            playerKillAmount++;
            playerKills.put(player, playerKillAmount);
        }
    A class where I have to access my handler
    Code:
    public class PlayerJoin implements Listener {
    
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
    
            if(!player.hasPlayedBefore()) {
                PlayerData.createPlayer(player);
            }
        }
    }
    Thanks already for looking into my problem and (trying) to help
     
  2. Offline

    rocky0745

    I'm pretty sure you could just make a new instance of the class:
    Code:
    PlayerData data = new PlayerData();
    data.createPlayer(player);
    
     
    Shortninja66 likes this.
  3. Offline

    teej107

    I highly recommend taking a break from making Bukkit plugins and learn OOP and Java. This problem of yours has nothing to do with Bukkit but your lack of knowledge on Java. To solve your problem, use this tutorial: https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html.
    Static does have its uses and I can assure you that the accessibility from other classes is NOT one of them.
     
    Last edited: Jul 29, 2015
    Shortninja66 likes this.
  4. Offline

    Paljaspers

    What I have done is create instances off my manager classes in my Main class. The problem is that they contain data so when I have to access them from other classes I have to acess my Main class again. Here is a concrete example from my plugin

    My main
    Code:
    public class Main extends JavaPlugin{
    
        Main plugin;
        private String pluginPath;
        private Config config = new Config(plugin);
    
        @Override
        public void onEnable() {
            PluginManager pm = Bukkit.getPluginManager();
            plugin = this;
            pluginPath = getDataFolder().getAbsolutePath();
            config.loadConfigFile();
        }
    public String getPluginPath() {
    return pluginPath;}
    }
    My config
    Code:
    public class Config {
        Main main;
        public Config(Main plugin) {
            this.main = plugin;
        }
        private String pluginPath = main.getPluginPath();
    }
    The error (Line 24 in config is where I try to get the pluginpath)
    Code:
    [07:14:55] [Server thread/ERROR]: Could not load 'plugins\DTV.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:135) ~[spigot.jar:git-Spigot-f94fe8f-2642f9b]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[spigot.jar:git-Spigot-f94fe8f-2642f9b]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [spigot.jar:git-Spigot-f94fe8f-2642f9b]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:291) [spigot.jar:git-Spigot-f94fe8f-2642f9b]
        at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:198) [spigot.jar:git-Spigot-f94fe8f-2642f9b]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:524) [spigot.jar:git-Spigot-f94fe8f-2642f9b]
        at java.lang.Thread.run(Thread.java:745) [?:1.7.0_79]
    Caused by: java.lang.NullPointerException
        at me.paljasper.dtv.Config.<init>(Config.java:24) ~[?:?]
        at me.paljasper.dtv.Main.<init>(Main.java:16) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.7.0_79]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[?:1.7.0_79]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.7.0_79]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[?:1.7.0_79]
        at java.lang.Class.newInstance(Class.java:379) ~[?:1.7.0_79]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot.jar:git-Spigot-f94fe8f-2642f9b]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[spigot.jar:git-Spigot-f94fe8f-2642f9b]
        ... 6 more
     
    Last edited: Jul 28, 2015
  5. Offline

    guitargun

    A common error I had as well. Try getting that instance from the main after that class has loaded. I cant give you an example now since I am on the phone
     
  6. @Paljaspers
    Code:
    Main main;
    public Config(Main plugin) {
      this.main = plugin;
    }
    private String pluginPath = main.getPluginPath(); //This is called before the constructor, that's why it throws a NullPointerException
     
  7. Offline

    Paljaspers

    @megamichiel

    This works fine, thanks a lot!

    Code:
        public Config(Main plugin) {
            this.main = plugin;
            this.pluginPath = main.getPluginPath();
        }
     
Thread Status:
Not open for further replies.

Share This Page