Plugin is null

Discussion in 'Plugin Development' started by HackintoshMan, May 4, 2013.

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

    HackintoshMan

    Code:
    private static MCFTF2 plugin;
     
     
        @SuppressWarnings("static-access")
        public CaptureTheFlagHandler(MCFTF2 plugin) {
            this.plugin = plugin;
            }
    This keeps returning null when I try to use plugin. How can I fix this?
     
  2. Offline

    chasechocolate

    Are you creating an instance of that class?
     
  3. Offline

    HackintoshMan

    chasechocolate

    ?? I know I have been asked this before, and I do not know the answer:'(
     
  4. Offline

    ZeusAllMighty11 Retired Staff

    If you don't know what you are doing with the constructor, you should review constructors in java
     
  5. Offline

    HackintoshMan

    **Pulls hair out**

    I know…Those are the only freaking things that I don NOT get about java!!!!!!!
     
  6. Offline

    devilquak

  7. Offline

    HackintoshMan

    devilquak
    Not main class…

    Code:
    package com.hackintoshman.mcftf2;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
     
    import com.hackintoshman.mcftf2.MCFTF2;
     
    public class CaptureTheFlagHandler implements Listener {
        public int captureTime = 5;
        public static Location pointOne;
        public static Location pointTwo;
        public static Location pointThree;
        private static World world1;
        private static World world2;
        private static World world3;
        public static boolean hasBeenCaptured1 = false;
        public static boolean hasBeenCaptured2 = false;
        public static boolean hasBeenCaptured3 = false;
     
        private MCFTF2 plugin;
     
     
        @SuppressWarnings("static-access")
        public CaptureTheFlagHandler(MCFTF2 plugin) {
            this.plugin = plugin;
            }
     
        public static void getPoints() {
            try {
                world1 = Bukkit.getWorld(plugin.getConfig().getString(
                        "Capture Points.Point1.world"));
                world2 = Bukkit.getWorld(plugin.getConfig().getString(
                        "Capture Points.Point1.world"));
                world3 = Bukkit.getWorld(plugin.getConfig().getString(
                        "Capture Points.Point1.world"));
            } catch (Exception e) {
                System.out.println(plugin + "");
                System.out.println(plugin.PREFIX + "===============");
                System.out.println(plugin.PREFIX + "Invalid World Config!");
                System.out.println(plugin.PREFIX + "===============");
            }
            try {
                pointOne.setWorld(world1);
                pointOne.setX(plugin.getConfig().getDouble(
                        "Capture Points.Point1.LocX"));
                pointOne.setY(plugin.getConfig().getDouble(
                        "Capture Points.Point1.LocY"));
                pointOne.setZ(plugin.getConfig().getDouble(
                        "Capture Points.Point1.LocZ"));
            } catch (Exception e) {
                System.out.println(plugin.PREFIX + "===============");
                System.out.println(plugin.PREFIX + "Invalid Point 1 Config!");
                System.out.println(plugin.PREFIX + "===============");
            }
            try {
                pointTwo.setWorld(world2);
                pointTwo.setX(plugin.getConfig().getDouble(
                        "Capture Points.Point2.LocX"));
                pointTwo.setY(plugin.getConfig().getDouble(
                        "Capture Points.Point2.LocY"));
                pointTwo.setZ(plugin.getConfig().getDouble(
                        "Capture Points.Point2.LocZ"));
            } catch (Exception e) {
                System.out.println(plugin.PREFIX + "===============");
                System.out.println(plugin.PREFIX + "Invalid Point 2 Config!");
                System.out.println(plugin.PREFIX + "===============");
            }
            try {
                pointThree.setWorld(world3);
                pointThree.setX(plugin.getConfig().getDouble(
                        "Capture Points.Point3.LocX"));
                pointThree.setY(plugin.getConfig().getDouble(
                        "Capture Points.Point3.LocY"));
                pointThree.setZ(plugin.getConfig().getDouble(
                        "Capture Points.Point3.LocZ"));
            } catch (Exception e) {
                System.out.println(plugin.PREFIX + "===============");
                System.out.println(plugin.PREFIX + "Invalid Point 3 Config!");
                System.out.println(plugin.PREFIX + "===============");
            }
        }
     
        public void gameHandler() {
            if (plugin.gameTime == 0 && hasBeenCaptured1 == true
                    && hasBeenCaptured2 == true && hasBeenCaptured3 == true) {
                plugin.gameWin("red", "ChatColor.RED");
            }
        }
     
        public void monitorPointOneLocation(Player player) {
            if (plugin.isCTF == true) {
                if (player.getLocation() == pointOne) {
                    System.out.println(player);
                    if (plugin.blue.hasPlayer(player) == true) {
                        System.out.println("player == point!");
                        if (hasBeenCaptured1 == false && hasBeenCaptured2 == false
                                && hasBeenCaptured3 == false) {
                            pointBeingCaptured(player, 1);
                        }
                    } else {
                        if (player.getLocation() != pointTwo) {
                            incorrectTeam(player);
                        }
                        return;
                    }
                }
            }
        }
     
        public void monitorPointTwoLocation(Player player) {
            if (plugin.isCTF == true) {
                if (player.getLocation() == pointTwo) {
                    System.out.println(player);
                    if (plugin.blue.hasPlayer(player) == true) {
                        System.out.println("player == point!");
                        if (hasBeenCaptured1 == true && hasBeenCaptured2 == false
                                && hasBeenCaptured3 == false) {
                            pointBeingCaptured(player, 2);
                        } else if (hasBeenCaptured1 == false
                                && hasBeenCaptured2 == false
                                && hasBeenCaptured3 == false) {
                            incorrectCapturePoint(player, 1);
                        }
                    } else {
                        if (player.getLocation() != pointTwo) {
                            incorrectTeam(player);
                        }
                        return;
                    }
                }
            }
        }
     
        public void monitorPointThreeLocation(Player player) {
            if (plugin.isCTF == true) {
                if (player.getLocation() == pointThree) {
                    System.out.println(player);
                    if (plugin.blue.hasPlayer(player) == true) {
                        System.out.println("player == point!");
                        if (hasBeenCaptured1 == true && hasBeenCaptured2 == true
                                && hasBeenCaptured3 == false) {
                            pointBeingCaptured(player, 3);
                        } else if (hasBeenCaptured1 == false
                                && hasBeenCaptured2 == false
                                && hasBeenCaptured3 == false) {
                            incorrectCapturePoint(player, 1);
                        } else if (hasBeenCaptured1 == true
                                && hasBeenCaptured2 == false
                                && hasBeenCaptured3 == false) {
                            incorrectCapturePoint(player, 2);
                        }
                    } else {
                        if (player.getLocation() != pointTwo) {
                            incorrectTeam(player);
                        }
                        return;
                    }
                }
            }
        }
     
        @SuppressWarnings("deprecation")
        public void pointBeingCaptured(Player player, int p) {
            plugin.getServer().broadcastMessage(
                    plugin.PREFIX + player.getName() + " is capturing point " + p
                            + "!");
     
            plugin.getServer().getScheduler()
                    .scheduleAsyncRepeatingTask(plugin, new Runnable() {
     
                        @Override
                        public void run() {
     
                            if (captureTime != 0) {
                                plugin.getServer().broadcastMessage(
                                        plugin.PREFIX + captureTime
                                                + " seconds left!");
                                captureTime--;
                            }
     
                        }
     
                    }, 0L, 20L);
        }
     
        public void incorrectCapturePoint(Player player, int p) {
            System.out.println("wrong point!");
            player.sendMessage(plugin.PREFIX + "You must capture point " + p
                    + "first!");
        }
     
        public void incorrectTeam(Player player) {
            player.sendMessage(plugin.PREFIX
                    + "You are trying to defend this point!");
        }
    }
    
    Main class

    Code:
    package com.hackintoshman.mcftf2;
     
    import java.io.File;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    import org.bukkit.scoreboard.Team;
     
    import com.hackintoshman.mcftf2.CaptureTheFlagHandler;
    import com.hackintoshman.mcftf2.commands.CommandSetPoints;
    import com.hackintoshman.mcftf2.commands.CommandStats;
    import com.hackintoshman.mcftf2.commands.CommandTeam;
    import com.hackintoshman.mcftf2.listeners.PlayerDeathListener;
    import com.hackintoshman.mcftf2.listeners.PlayerHitListener;
    import com.hackintoshman.mcftf2.listeners.PlayerJoinListener;
    import com.hackintoshman.mcftf2.listeners.PlayerMoveListener;
    import com.hackintoshman.mcftf2.listeners.PlayerQuitListener;
    import com.hackintoshman.mcftf2.listeners.PlayerRespawnListener;
     
    public class MCFTF2 extends JavaPlugin{
     
        public String PREFIX = ChatColor.GREEN + "" + ChatColor.BOLD + "[MCFTF2] "
                + ChatColor.AQUA;
        public int redScore = 0;
        public int blueScore = 0;
        public int gameTime;
        public boolean isCTF = true;
        public ScoreboardManager manager = null;
        public Scoreboard board = null;
        public Team red = null;
        public Team blue = null;
        public Location redSpawn;
        public Location blueSpawn;
        private static MCFTF2 instance;
     
        public static MCFTF2 getInstance() {
            return instance;
        }
     
        @Override
        public void onEnable() {
     
            System.out.println("[MCFTF2] Loading the scoreboard...");
         
         
            manager = Bukkit.getScoreboardManager();
            board = manager.getNewScoreboard();
            red = board.registerNewTeam("Red");
            blue = board.registerNewTeam("Blue");
            red.setAllowFriendlyFire(false);
            blue.setAllowFriendlyFire(false);
     
            System.out.println("[MCFTF2] Loading config...");
            loadConfig();
            saveConfig();
            CaptureTheFlagHandler.getPoints();
     
            getCommand("stats").setExecutor(new CommandStats(this));
            getCommand("setpoint").setExecutor(new CommandSetPoints(this));
            getCommand("team").setExecutor(new CommandTeam(this));
     
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(new PlayerDeathListener(this), this);
            pm.registerEvents(new PlayerHitListener(this), this);
            pm.registerEvents(new PlayerJoinListener(this), this);
            pm.registerEvents(new PlayerMoveListener(this), this);
            pm.registerEvents(new PlayerQuitListener(this), this);
            pm.registerEvents(new PlayerRespawnListener(this), this);
     
        }
     
        @Override
        public void onDisable() {
     
            System.out.println("[MCFTF2] MCFTF2 succesfully disabled!");
            System.out.println("[MCFTF2] Thanks for using!");
            saveConfig();
        }
     
        public void loadConfig() {
            boolean configFileExistant = false;
     
            if (new File("plugins/MCFTF2/").exists()) {
                configFileExistant = true;
            }
     
            getConfig().options().copyDefaults(true);
     
            if (!configFileExistant) {
                getConfig().addDefault("General.friendly_fire",
                        Boolean.valueOf(false));
     
            }
     
            saveConfig();
        }
     
        public void updatePrefix(Player killer, Player killed) {
            if (red.hasPlayer(killer.getPlayer()) == true) {
                killer.setDisplayName(ChatColor.GOLD
                        + "("
                        + ChatColor.GREEN
                        + ""
                        + getConfig().getInt(
                                "Stats.Players." + killer.getName() + ".Kills")
                        + ChatColor.GOLD
                        + ":"
                        + ChatColor.GREEN
                        + ""
                        + +getConfig().getInt(
                                "Stats.Players." + killer.getName() + ".Deaths")
                        + ChatColor.GOLD + ") " + ChatColor.RED + killer.getName()
                        + ChatColor.WHITE);
                killed.setDisplayName(ChatColor.GOLD
                        + "("
                        + ChatColor.GREEN
                        + ""
                        + getConfig().getInt(
                                "Stats.Players." + killed.getName() + ".Kills")
                        + ChatColor.GOLD
                        + ":"
                        + ChatColor.GREEN
                        + ""
                        + +getConfig().getInt(
                                "Stats.Players." + killed.getName() + ".Deaths")
                        + ChatColor.GOLD + ") " + ChatColor.BLUE + killed.getName()
                        + ChatColor.WHITE);
            }
     
            if (blue.hasPlayer(killer.getPlayer()) == true) {
                killer.setDisplayName(ChatColor.GOLD
                        + "("
                        + ChatColor.GREEN
                        + ""
                        + getConfig().getInt(
                                "Stats.Players." + killer.getName() + ".Kills")
                        + ChatColor.GOLD
                        + ":"
                        + ChatColor.GREEN
                        + ""
                        + +getConfig().getInt(
                                "Stats.Players." + killer.getName() + ".Deaths")
                        + ChatColor.GOLD + ") " + ChatColor.BLUE + killer.getName()
                        + ChatColor.WHITE);
                killed.setDisplayName(ChatColor.GOLD
                        + "("
                        + ChatColor.GREEN
                        + ""
                        + getConfig().getInt(
                                "Stats.Players." + killed.getName() + ".Kills")
                        + ChatColor.GOLD
                        + ":"
                        + ChatColor.GREEN
                        + ""
                        + +getConfig().getInt(
                                "Stats.Players." + killed.getName() + ".Deaths")
                        + ChatColor.GOLD + ") " + ChatColor.RED + killed.getName()
                        + ChatColor.WHITE);
            }
        }
     
        public void gameWin(String team, String color) {
            getServer().broadcastMessage(this.PREFIX + color + team + " has won the game!");
        }
    }
    And if I remove static…

    [​IMG]

    BTW, I love the fast responses from all of you bukkit members!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  8. Offline

    devilquak

    HackintoshMan

    Good lord...

    *recalls repressed war memories of his early days with static classes*

    This looks just like my first large-scale project attempt, which failed rather miserably as a result of my explicit use of static classes and variables...

    But yes, you should watch some tutorials on ways of instantiating your classes, and referring to them that way. Static things like this may "work" now in some cases, but later down the line it'll come back and bite you in the behind -- although it looks like it already has with your null exception.
     
    TheGreenGamerHD likes this.
  9. Offline

    HackintoshMan

    Yeah…this is my first big project.

    any tips for me?
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    Avoid the use of static fields, always.
     
  11. Offline

    HackintoshMan

    Sagacious_Zed
    Ok, how do I access variables in a non-static way?
     
  12. Offline

    chasechocolate

    HackintoshMan just create an instance of that class in your main class.
    Code:java
    1. //In onEnable():
    2. new CaptureTheFlagHandler(this);
     
  13. Offline

    HackintoshMan

    chasechocolate
    Thanks, That worked, but I keep getting this error:

    Code:
    [MCFTF2] Enabling MCFTF2 v1.0
    18:39:05 [INFO] [MCFTF2] Loading the scoreboard...
    18:39:05 [INFO] [MCFTF2] Loading config...
    18:39:05 [INFO] MCFTF2 v1.0
    18:39:05 [INFO] ?a?l[MCFTF2] ?b===============
    18:39:05 [INFO] ?a?l[MCFTF2] ?bInvalid World Config!
    18:39:05 [INFO] ?a?l[MCFTF2] ?b===============
    18:39:05 [INFO] ?a?l[MCFTF2] ?b===============
    18:39:05 [INFO] ?a?l[MCFTF2] ?bInvalid Point 1 Config!
    18:39:05 [INFO] ?a?l[MCFTF2] ?b===============
    18:39:05 [INFO] ?a?l[MCFTF2] ?b===============
    18:39:05 [INFO] ?a?l[MCFTF2] ?bInvalid Point 2 Config!
    18:39:05 [INFO] ?a?l[MCFTF2] ?b===============
    18:39:05 [INFO] ?a?l[MCFTF2] ?b===============
    18:39:05 [INFO] ?a?l[MCFTF2] ?bInvalid Point 3 Config!
    18:39:05 [INFO] ?a?l[MCFTF2] ?b===============
    
    The try block catches the failure. I know that the plugin is NOT null anymore (3rd line)

    Code:
    public static void getPoints() {
            try {
                world1 = Bukkit.getWorld(plugin.getConfig().getString(
                        "Capture Points.Point1.world"));
                world2 = Bukkit.getWorld(plugin.getConfig().getString(
                        "Capture Points.Point1.world"));
                world3 = Bukkit.getWorld(plugin.getConfig().getString(
                        "Capture Points.Point1.world"));
            } catch (Exception e) {
                System.out.println(plugin + "");
                System.out.println(plugin.PREFIX + "===============");
                System.out.println(plugin.PREFIX + "Invalid World Config!");
                System.out.println(plugin.PREFIX + "===============");
            }………….
     
  14. Offline

    Digi

    HackintoshMan
    Leave the code to print stack traces, they're lots more useful than that message.
    Once you have that stacktrace, try to figure it out and if you can't then post it :p
     
  15. Offline

    HackintoshMan

    Oh, I hadn't though of that!
     
  16. Offline

    Sagacious_Zed Bukkit Docs

    By accessing them in a non static context.
     
  17. Offline

    HackintoshMan

    How do I access them without accessing them in a static way?

    It catches at the lines that set values for the location. for instance:

    Code:
    pointTwo.setWorld(world2);
                pointTwo.setX(plugin.getConfig().getInt(
                        "Capture Points.Point2.LocX"));
                pointTwo.setY(plugin.getConfig().getInt(
                        "Capture Points.Point2.LocY"));
                pointTwo.setZ(plugin.getConfig().getInt(
                        "Capture Points.Point2.LocZ"));
    **Test post for my updated signature**

    Bump

    Is the bump rule 12 hours or 24? If it isn't 12 hours Im sorry.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  18. Offline

    molenzwiebel

    If you want to access variables the non-static way, use this.variable instead of variable
     
  19. Offline

    HackintoshMan

    [​IMG]

    says that I can not use this in static context. What do i need to change in order for me to change the context?

    Also, Does my signature show up in all of my messages? It doesn't fore me. I can only see it in my original post.
     
  20. Offline

    molenzwiebel

    You can not use this in a static function, either remove the static from the function or think of another way to get the variable
     
  21. Offline

    HackintoshMan

    How do I access a variable in a non static way?
     
  22. Offline

    Sagacious_Zed Bukkit Docs

    By not accessing them in a static context. e.g. static methods or static blocks. Additionally you may wish to qualify the access with a this, to denote that you really intend to use the variable of the current instance.
     
  23. Offline

    HackintoshMan

    Excuse my noobness, but by making a methd/variable staid, you are making it accessible throughout the project, am i correct? Is there any reason why I shouldn't make them static?
     
Thread Status:
Not open for further replies.

Share This Page