Solved Problems with config in multiple classes

Discussion in 'Plugin Development' started by TJM4, Oct 11, 2015.

Thread Status:
Not open for further replies.
  1. Hello!

    I am fairly new to the Bukkit API and Java and I have just started work on a new, big, plugin and decided to give multiple classes ago. I followed a tutorial (Click Here) and had my classes set up in no time. Soon however I needed access to to the config and I soon find that it is not as easy as in 1 class. I follow this tutorial and I seem to understand it but I am getting an error:
    Error (open)

    Code:
    [21:07:03 ERROR]: Could not pass event PlayerJoinEvent to ClashOfCraftCore v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:297) ~[craftbukkit.jar:git-Bukkit-5cb9b70]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-5cb9b70]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-5cb9b70]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-5cb9b70]
            at net.minecraft.server.v1_8_R2.PlayerList.onPlayerJoin(PlayerList.java:282) [craftbukkit.jar:git-Bukkit-5cb9b70]
            at net.minecraft.server.v1_8_R2.PlayerList.a(PlayerList.java:142) [craftbukkit.jar:git-Bukkit-5cb9b70]
            at net.minecraft.server.v1_8_R2.LoginListener.b(LoginListener.java:115) [craftbukkit.jar:git-Bukkit-5cb9b70]
            at net.minecraft.server.v1_8_R2.LoginListener.c(LoginListener.java:53) [craftbukkit.jar:git-Bukkit-5cb9b70]
            at net.minecraft.server.v1_8_R2.NetworkManager.a(NetworkManager.java:222) [craftbukkit.jar:git-Bukkit-5cb9b70]
            at net.minecraft.server.v1_8_R2.ServerConnection.c(SourceFile:168) [craftbukkit.jar:git-Bukkit-5cb9b70]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:742) [craftbukkit.jar:git-Bukkit-5cb9b70]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:336) [craftbukkit.jar:git-Bukkit-5cb9b70]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:626) [craftbukkit.jar:git-Bukkit-5cb9b70]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:534) [craftbukkit.jar:git-Bukkit-5cb9b70]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_60]
    Caused by: java.lang.NullPointerException
            at me.TJM4.CoCore.CreateVillage.CreateVillageM(CreateVillage.java:32) ~[?:?]
            at me.TJM4.CoCore.FirstJoin.RecourcePackAccepted(FirstJoin.java:69) ~[?:?]
            at me.TJM4.CoCore.FirstJoin.onPlayerJoin(FirstJoin.java:49) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_60]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_60]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_60]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_60]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:295) ~[craftbukkit.jar:git-Bukkit-5cb9b70]
            ... 14 more
    


    Line 32 in CreateVillage is:
    Code:
    p.sendMessage(plugin.getConfig().getString("Test"));
    Line 69 in FirstJoin is:
    Code:
    cv.CreateVillageM(p);
    And line 49 in FirstJoin is:
    Code:
    RecourcePackAccepted(p);
    Just so you can look through all of my code, here it all is:

    Main Class (open)

    Code:
    package me.TJM4.CoCore;
    
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Main extends JavaPlugin{ //Extending JavaPlugin so that Bukkit knows its the main class...
        private static Plugin plugin;
        public void onEnable() {
            plugin = this;
           
           
            //Events:
           
            Bukkit.getServer().getPluginManager().registerEvents(new FirstJoin(), this);
           
            //Commands:
           
            //Config
            final FileConfiguration config = this.getConfig();
            config.options().copyDefaults(true);
            saveConfig();
           
        }
        public void onDisable() {
            plugin = null;//To stop memory leeks
        }
        //Much eaisier then registering events in 10 diffirent methods
        public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
            for (Listener listener : listeners) {
                Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
            }
        }
        //To access the plugin variable from other classes
        public static Plugin getPlugin() {
            return plugin;
        }
    }


    FirstJoin class (open)

    Code:
    package me.TJM4.CoCore;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    import com.connorlinfoot.actionbarapi.ActionBarAPI;
    import com.connorlinfoot.titleapi.TitleAPI;
    
    public class FirstJoin implements Listener{
        Main plugin;
       
        ArrayList<Player> NewJoins = new ArrayList<Player>();
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
           
            Player p = e.getPlayer();
           
           
            if (p.hasPlayedBefore()) {
                NewJoins.add(p);
                //Creating location
                World w = Bukkit.getWorld("BaseWorld");
                double x = 3.500;
                double y = 84;
                double z = 3.500;
               
                Location l = new Location(w, x, y, z);
           
                //Teleporting to location
               
                p.teleport(l);
                p.setGameMode(GameMode.SPECTATOR);
                p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 100000, 30));
                ActionBarAPI.sendActionBar(p, ChatColor.YELLOW + "Waiting for recource pack accpetion");
                //TODO Add force recource pack
                RecourcePackAccepted(p);
            }
           
    
        }
        @EventHandler
        public void MoveEvent(PlayerMoveEvent e) {
            Player p = e.getPlayer();
            p.sendMessage("Move");
            p.sendMessage(NewJoins.toString());
            if (NewJoins.contains(p)) {
                p.teleport(e.getFrom());
            }
        }
       
        @SuppressWarnings("deprecation")
        public void RecourcePackAccepted(Player p) {
            TitleAPI.sendFullTitle(p, 20, 300, 20, (ChatColor.YELLOW + "Welcome!"), (ChatColor.GOLD + "Creating village..."));
            ActionBarAPI.sendActionBar(p, ChatColor.YELLOW + "Recource pack downloaded " + ChatColor.LIGHT_PURPLE + "☺");
            CreateVillage cv = new CreateVillage(plugin);
            cv.CreateVillageM(p);
        }
    
    }
    


    CreateVillage class (open)

    Code:
    package me.TJM4.CoCore;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    
    import com.sk89q.worldedit.EditSession;
    import com.sk89q.worldedit.MaxChangedBlocksException;
    import com.sk89q.worldedit.Vector;
    import com.sk89q.worldedit.bukkit.BukkitWorld;
    import com.sk89q.worldedit.bukkit.WorldEditPlugin;
    import com.sk89q.worldedit.data.DataException;
    import com.sk89q.worldedit.schematic.MCEditSchematicFormat;
    
    @SuppressWarnings("deprecation")
    public class CreateVillage {
       
        Main plugin;
        
        public CreateVillage(Main instance) {
        plugin = instance;
        }
           
           
           
            public void CreateVillageM(Player p) {
               
                p.sendMessage("Hi");
                p.sendMessage(plugin.getConfig().getString("Test"));
            }
             
    }
    


    Also, when I join, this is what I see:
    View attachment 25460

    I hope someone can help :D

    Thanks,

    - TJM4
     
  2. Offline

    timtower Administrator Administrator Moderator

    @TJM4 Listener class: you never fill plugin.
     
    Zombie_Striker likes this.
  3. Offline

    Zombie_Striker

    This is a bad mix.

    Caused by: java.lang.NullPointerException
    at me.TJM4.CoCore.CreateVillage.CreateVillageM(CreateVillage.java:32) ~[?:?]

    p.sendMessage(plugin.getConfig().getString("Test"));

    As timtower said, you need to set plugin to something.
     
  4. Sorry, its just my way of learning

    Ok, this is going to sound very nooby and I am sorry for that but by setting plugin to something do you mean the following:

    Code:
     Main plugin = new Main();
    If that is the case I am getting an error when I add that as well. Please explain what you mean :'(
     
  5. @TJM4 Use a constructor for the main class
     
  6. Offline

    timtower Administrator Administrator Moderator

    @TJM4 That is not what we meant.
    Pass an instance of the plugin to the other classes using the constructor.
     
  7. Thanks alot to everyone, I fixed this in the end!
     
Thread Status:
Not open for further replies.

Share This Page