Development Assistance Getting Locations from another Class

Discussion in 'Plugin Help/Development/Requests' started by Raydond123, Jan 25, 2015.

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

    Raydond123

    Hello, my question today is how I would get locations that I defined in another class.
    What I mean is, let's say I had the class called WorldHandler and another one called CommandHandler.
    I defined a couple of locations within the WorldHandler class. What I want to do is get those locations within the CommandHandler class.

    Thanks in advance.
     
  2. Offline

    mythbusterma

  3. Offline

    Raydond123

  4. Offline

    1Rogue

    Code:java
    1. public class MyClass {
    2.  
    3. private final Location myLoc = /* ... */;
    4.  
    5. public Location getLocation() {
    6. return this.myLoc;
    7. }
    8.  
    9. }
     
  5. Offline

    Raydond123

    @1Rogue

    Okay, well within the worldHandler class currently, I have the locations inside another method.
    Here's what it looks like:

    Code:
    public class WorldHandler {
    
        public static void loadWorlds() {
         
            World ChainedClouds = Bukkit.getWorld("ChainedClouds");
            Location CC1 = new Location(ChainedClouds, 1, 1, 1);
            CC1.setPitch(0F);
            CC1.setYaw(0F);
            Location CC2 = new Location(ChainedClouds, 2, 2, 2);
            CC2.setPitch(0F);
            Main.status.put("ChainedClouds", true);
         
        }
    }
    Well, I was looking around and I think I've found the solution.
    Code:
        static Location CC1;
        static Location CC2;
    
        public static void loadWorlds() {
           
            World ChainedClouds = Bukkit.getWorld("ChainedClouds");
            Location CC1 = new Location(ChainedClouds, 1, 1, 1);
            CC1.setPitch(0F);
            CC1.setYaw(0F);
            Location CC2 = new Location(ChainedClouds, 2, 2, 2);
            CC2.setPitch(0F);
            Main.status.put("ChainedClouds", true);
           
        }
       
        public static Location getLocation() {
            return CC1;
        }
    Is this "okay" or would this cause problems?
     
    Last edited by a moderator: Jan 27, 2015
  6. Offline

    mythbusterma

    @Raydond123

    Well, the abuse of static is quite nasty, and those variable names are awful.

    So all in all, yes it will cause problems.
     
  7. @Raydond123
    Why are you using statics so much?
    You really need to take a look at naming conventions here.
    I.E. variable names should be lowerCamelCase.
    Such as: World chainedClouds = ...
     
  8. Offline

    Raydond123

    I already know the naming conventions... I still don't know why I named it ChainedClouds...
    Anyways, I renamed it. The thing about the statics is that Eclipse won't let me use them unless they're static.
     
  9. Offline

    WinX64

    It's most likely not a bug in your IDE. How are you trying to access your non-static variables?
     
  10. Offline

    Raydond123

    @BorisTheTerrible
    @WinX64

    It says that "You cannot make a static reference to a non-static method getLocationCC2() in type WorldHandler".
    Right now I have 6 classes within my project but I only think the problem is within four of them.
    Main Class:
    Code:
    package me.Raydond123.duelz;
    
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener{
    
        public void onEnable() {
         
            WorldHandler.loadWorlds();
            this.getCommand("duel").setExecutor(new CommandHandler(this));
         
            System.out.println("Duelz has been enabled!");
    
            Bukkit.getServer().getPluginManager().registerEvents(new PlayerHandler(), this);
            Bukkit.getServer().getPluginManager().registerEvents(new DuelHandler(), this);
        }
     
        public void onDisable() {
         
        }
     
        public static void consoleError() {
            System.out.println("This command cannot be used from the console!");
        }
     
        public static HashMap<String, Boolean> status = new HashMap<>();
        public static HashMap<String, Integer> time = new HashMap<>();
        public static HashMap<String, Boolean> requests = new HashMap<>();
        public static HashMap<String, String> duelData = new HashMap<>();
        public static HashMap<String, String> duelData2 = new HashMap<>();
        public static HashMap<String, Boolean> dueling = new HashMap<>();
        public static HashMap<String, Boolean> cheat = new HashMap<>();
     
    }
    
    CommandHandler Class:
    Code:
    package me.Raydond123.duelz;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class CommandHandler implements CommandExecutor{
    
        Main plugin;
     
        public CommandHandler(Main plugin) {
            this.plugin = plugin;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if(sender instanceof Player) {
         
            if(args.length == 0) {
                sender.sendMessage(ChatColor.RED + "Correct Usage: /duel (<player> or accept)");
            }
            if(args.length == 1) {
                Player player = (Player) sender;
                DuelCommand.executeDuelCommand(player, args);
            }
         
            } else {
                Main.consoleError();
            }
            return false;
        }
     
    }
    
    DuelCommand Class:
    Code:
    package me.Raydond123.duelz;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    
    public class DuelCommand {
    
        Main plugin;
     
        public DuelCommand(Main plugin) {
            this.plugin = plugin;
        }
     
        public void executeDuelCommand(Player sender, String[] args) {
         
            if(args[0].equalsIgnoreCase("accept")) {
                if(Main.requests.get(sender) != null) {
                    String player = sender.getName();
                    String sPlayer1 = Main.duelData.get(player);
                    Player player1 = Bukkit.getPlayer(sPlayer1);
                    Player player2 = (Player) sender;
                 
                    player1.teleport(WorldHandler.getLocationCC1());
                    player2.teleport(WorldHandler.getLocationCC2());
                 
                    player1.sendMessage(ChatColor.RED + "You are now dueling against, " + player + ".");
                    player2.sendMessage(ChatColor.RED + "You are now dueling against, " + sPlayer1 + ".");
                 
                    Main.dueling.put(sPlayer1, true);
                    Main.dueling.put(player, true);
                 
                    Main.time.remove(sPlayer1);
                    Main.requests.remove(player);
                } else {
                    sender.sendMessage(ChatColor.RED + "You do not have any pending duel requests.");
                }
            }else {
             
                String player = sender.getName();
                String playerName = args[0];
                Player p = Bukkit.getPlayer(playerName);
                if (p != null) {
                    if(Main.time.get(player) == null) {
                        if(Main.status.get("ChainedClouds")) {
                        Main.status.put("ChainedClouds", false);
                    Main.time.put(player, 60);
                    Main.requests.put(playerName, true);
                    Main.duelData.put(playerName, player);
                    Main.duelData2.put(player, playerName);
                    p.sendMessage(ChatColor.RED + "You have 60 seconds to accept a duel request from " + player + ".");
                    p.sendMessage(ChatColor.RED + "Use: /duel accept");
                 
                    final int counter = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                        @Override
                        public void run()  {
                            int previousTime = Main.time.get(player);
                            int time = previousTime - 1;
                            Main.time.put(player, time);
                        }
                    }, 0L, 1 * 20L);
                 
                    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                        public void run() {
                            Bukkit.getScheduler().cancelTask(counter);
                            Main.time.remove(player);
                            Main.requests.remove(playerName);
                        }
                    }, 60 * 20L);
                        } else {
                            sender.sendMessage(ChatColor.RED + "Someone is already in the arena!");
                        }
                    } else {
                        int remainingTime = Main.time.get(player);
                        sender.sendMessage(ChatColor.RED + "You still have a pending request with another player that has " + remainingTime + " seconds left.");
                    }
                } else {
                sender.sendMessage(ChatColor.RED + "That player is not online!");
                }
             
            }
         
        }
     
    }
    
    WorldHandler Class:
    Code:
    package me.Raydond123.duelz;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    
    
    public class WorldHandler {
     
        Location CC1;
        Location CC2;
    
        public static void loadWorlds() {
         
            World ChainedClouds = Bukkit.getWorld("ChainedClouds");
            Location CC1 = new Location(ChainedClouds, 1, 1, 1);
            CC1.setPitch(0F);
            CC1.setYaw(0F);
            Location CC2 = new Location(ChainedClouds, 2, 2, 2);
            CC2.setPitch(0F);
            Main.status.put("ChainedClouds", true);
         
        }
     
        public Location getLocationCC1() {
            return CC1;
        }
        public Location getLocationCC2() {
            return CC2;
        }
    }
    
     
  11. Offline

    WinX64

    You cannot access a non-static variable/method statically. You need to call them via a instance of them class they are located. In this case:
    Code:
    WorldHandler handler = new WorldHandler();
    handler.loadWorlds(); //Assuming your removed the static modifier from loadWorlds as well
    Location loc = handler.getLocationCC1();
     
  12. Offline

    Burnett

    @Raydond123 Do you understand what the static modifier does?
     
  13. Offline

    Raydond123

    @Burnett
    Yes, it only allows the certain method to be run from one instance.

    @WinX64
    Thanks, that worked.

    @WinX64
    Okay well in my DuelCommand class I have:
    Code:
    Main plugin;
       
        public DuelCommand(Main plugin) {
            this.plugin = plugin;
        }
    When I try to do:
    Code:
    DuelCommand dCmd = new DuelCommand();
    It gives me an error so would I do the following?
    Code:
    DuelCommand dCmd = new DuelCommand(plugin);
    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jan 27, 2015
  14. Offline

    coasterman10

    I think it would be best for you to find a Java book and peruse that before attempting to use the Bukkit API. If you don't understand the language, you won't be able to use the vocabulary either.
     
  15. Offline

    Raydond123

    @coasterman10
    And why would you suggest such a thing? I knew Java before trying to use the Bukkit API.
    There's a difference between having a question about the Bukkit API and not knowing Java.
     
  16. Offline

    WinX64

    Yes, where plugin is an instance of Main
     
  17. Offline

    Raydond123

    @WinX64
    Thanks!

    On a slightly related topic, I'm getting an error somewhere whilst trying to run a repeated task.
    This is the code that is run upon running: /duel <player>
    Code:
    String player = sender.getName();
                String playerName = args[0];
                Player p = Bukkit.getPlayer(playerName);
                if (p != null) {
                    if(Main.time.get(player) == null) {
                        if(!playerName.equals(player)) {
                        if(Main.status.get("ChainedClouds")) {
                        Main.status.put("ChainedClouds", false);
                    Main.time.put(player, 60);
                    Main.requests.put(playerName, true);
                    Main.duelData.put(playerName, player);
                    Main.duelData2.put(player, playerName);
                    p.sendMessage(ChatColor.RED + "You have 60 seconds to accept a duel request from " + player + ".");
                    p.sendMessage(ChatColor.RED + "Use: /duel accept");
                   
                    final int counter = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                        @Override
                        public void run()  {
                            int previousTime = Main.time.get(player);
                            int time = previousTime - 1;
                            Main.time.put(player, time);
                        }
                    }, 0L, 20L);
                   
                    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                        public void run() {
                            Bukkit.getScheduler().cancelTask(counter);
                            Main.time.remove(player);
                            Main.requests.remove(playerName);
                        }
                    }, 60L * 20L);
                        } else {
                            sender.sendMessage(ChatColor.RED + "Someone is already in the arena!");
                        }
                    } else {
                        sender.sendMessage(ChatColor.RED + "The person who you want to duel cannot be yourself!");
                    }
                    } else {
                        int remainingTime = Main.time.get(player);
                        sender.sendMessage(ChatColor.RED + "You still have a pending request with another player that has " + remainingTime + " seconds left.");
                    }
                } else {
                sender.sendMessage(ChatColor.RED + "That player is not online!");
                }
    It runs everything on top, and by on top I mean setting the time to 60.
    But after that, it just throws the following error on to console:
    Code:
    [13:32:01 INFO]: Raydond123 issued server command: /duel Mineflow_
    [13:32:01 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'duel
    ' in plugin Duelz v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) ~[spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServe
    r.java:645) ~[spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:1115) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java
    :950) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :26) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :53) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spi
    got-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_25]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_25]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:6
    83) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:3
    16) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:6
    23) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java
    :526) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_25]
    Caused by: java.lang.NullPointerException
            at me.Raydond123.duelz.DuelCommand.executeDuelCommand(DuelCommand.java:5
    4) ~[?:?]
            at me.Raydond123.duelz.CommandHandler.onCommand(CommandHandler.java:26)
    ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    got-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            ... 14 more
     
  18. Offline

    mythbusterma

    That's laughable. You don't know Java, accessing a field of one class from another class is a strictly Java concept that has nothing to with Bukkit, other than the field you're accessing happens to be part of the API of Bukkit, although it's no different to any other field.

    No, that is not at all what static does, try again.

    Static means that the method or field is associated with the class instead of being associated with a single instance of said class, and as such, does not need and instance of said class to be invoked/accessed.

    Someone who "knows Java" would have no issue debugging a simple NullPointerException.

    All in all, your efforts here are misdirected and should be focused elsewhere, perhaps: The Java™ Tutorials
     
  19. Offline

    teej107

  20. Offline

    mrCookieSlime

    Moved to Alternatives Section.
     
  21. Offline

    Raydond123

    I'll be sure to take a look through these. I'll post back to this thread if I can't fix the problem myself.

    I was unable to solve the problem.
    Well, I looked through the stack trace and it said that the errors occurred on line 54 of my DuelCommand class and on line 26 of my CommandHandler class.

    Here are the classes:
    CommandHandler.java:
    Code:
    package me.Raydond123.duelz;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class CommandHandler implements CommandExecutor{
    
        Main plugin;
    
        public CommandHandler(Main plugin) {
            this.plugin = plugin;
        }
    
        DuelCommand dCmd = new DuelCommand(plugin);
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if(sender instanceof Player) {
        
            if(args.length == 0) {
                sender.sendMessage(ChatColor.RED + "Correct Usage: /duel (<player> or accept)");
            }
            if(args.length == 1) {
                Player player = (Player) sender;
                dCmd.executeDuelCommand(player, args);
            }
        
            } else {
                Main.consoleError();
            }
            return false;
        }
    
    }
    
    DuelCommand.java:
    Code:
    package me.Raydond123.duelz;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    
    public class DuelCommand {
    
        Main plugin;
    
        public DuelCommand(Main plugin) {
            this.plugin = plugin;
        }
    
        WorldHandler wHandler = new WorldHandler();
    
        public void executeDuelCommand(Player sender, String[] args) {
        
            if(args[0].equalsIgnoreCase("accept")) {
                if(Main.requests.get(sender) != null) {
                    String player = sender.getName();
                    String sPlayer1 = Main.duelData.get(player);
                    Player player1 = Bukkit.getPlayer(sPlayer1);
                    Player player2 = (Player) sender;
                    player1.teleport(wHandler.getLocationCC1());
                    player2.teleport(wHandler.getLocationCC2());
                
                    player1.sendMessage(ChatColor.RED + "You are now dueling against, " + player + ".");
                    player2.sendMessage(ChatColor.RED + "You are now dueling against, " + sPlayer1 + ".");
                
                    Main.dueling.put(sPlayer1, true);
                    Main.dueling.put(player, true);
                
                    Main.time.remove(sPlayer1);
                    Main.requests.remove(player);
                } else {
                    sender.sendMessage(ChatColor.RED + "You do not have any pending duel requests.");
                }
            }else {
            
                String player = sender.getName();
                String playerName = args[0];
                Player p = Bukkit.getPlayer(playerName);
                if (p != null) {
                    if(Main.time.get(player) == null) {
                        if(!playerName.equals(player)) {
                        if(Main.status.get("ChainedClouds")) {
                        Main.status.put("ChainedClouds", false);
                    Main.time.put(player, 60);
                    Main.requests.put(playerName, true);
                    Main.duelData.put(playerName, player);
                    Main.duelData2.put(player, playerName);
                    p.sendMessage(ChatColor.RED + "You have 60 seconds to accept a duel request from " + player + ".");
                    p.sendMessage(ChatColor.RED + "Use - /duel accept");
                
                    final int counter = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                        @Override
                        public void run()  {
                            int previousTime = Main.time.get(player);
                            int time = previousTime - 1;
                            Main.time.put(player, time);
                        }
                    }, 0L, 20L);
                
                    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                        public void run() {
                            Bukkit.getScheduler().cancelTask(counter);
                            Main.time.remove(player);
                            Main.requests.remove(playerName);
                        }
                    }, 60L * 20L);
                        } else {
                            sender.sendMessage(ChatColor.RED + "Someone is already in the arena!");
                        }
                    } else {
                        sender.sendMessage(ChatColor.RED + "The person who you want to duel cannot be yourself!");
                    }
                    } else {
                        int remainingTime = Main.time.get(player);
                        sender.sendMessage(ChatColor.RED + "You still have a pending request with another player that has " + remainingTime + " seconds left.");
                    }
                } else {
                sender.sendMessage(ChatColor.RED + "That player is not online!");
                }
            
            }
        
        }
    
    }
    
    EDIT: Forgot to show the stack trace itself... :p

    Code:
    [17:12:58 INFO]: Raydond123 issued server command: /duel Mineflow_
    [17:12:58 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'duel
    ' in plugin Duelz v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) ~[spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServe
    r.java:645) ~[spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:1115) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java
    :950) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :26) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :53) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spi
    got-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_25]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_25]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:6
    83) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:3
    16) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:6
    23) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java
    :526) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_25]
    Caused by: java.lang.NullPointerException
            at me.Raydond123.duelz.DuelCommand.executeDuelCommand(DuelCommand.java:5
    4) ~[?:?]
            at me.Raydond123.duelz.CommandHandler.onCommand(CommandHandler.java:26)
    ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    got-1.8.jar:git-Spigot-fa7cbf9-00eba53]
    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jan 27, 2015
  22. Offline

    Burnett

    @Raydond123 You know what would really help? Indentations...Also it would be nice if, when you post your stacktrace you don't edit the code. It really helps when trying to find how line 26 "}" is calling executeDuelCommand. Oh and while I'm at it I might as well tell you what else is wrong. Your class that extends JavaPlugin shouldn't be called "Main" (see here). Some of your variable names are bad. You don't effectively utilize if, if else and else statements and you are still accessing your HashMaps statically. Learn more Java.
     
    Last edited: Jan 27, 2015
  23. Offline

    Raydond123

    @Burnett
    I never edited my code. I straight up copied and pasted it here and the lines match up.
    BUT there was a mistake on my part. I realized that upon exporting it from Eclipse that I was exporting it to the wrong destination instead of the plugins folder. So all the errors occurred because of an old version of the plugin.

    I've updated it now but it still gives me an error which I've been unable to solve.
    I'll be sure to change my variable names after I get this problem solved. Also, I made it so that I'm no longer accessing my HashMaps statically.
    The new problem is that upon start up it says that the plugin was already initialized. Not sure what the problem is but I think it has something to do with the WorldHandler class because it runs a method within the class on startup.

    Main Class:
    Main Class (open)
    Code:
    package me.Raydond123.duelz;
    
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener{
    
        WorldHandler wHandler = new WorldHandler();
    
        public void onEnable() {
        
            wHandler.loadWorlds();
            this.getCommand("duel").setExecutor(new CommandHandler(this));
        
            System.out.println("Duelz has been enabled!");
        
            Bukkit.getServer().getPluginManager().registerEvents(new DuelHandler(), this);
            Bukkit.getServer().getPluginManager().registerEvents(new PlayerHandler(), this);
        }
    
        public void onDisable() {
        
        }
    
        public static void consoleError() {
            System.out.println("This command cannot be used from the console!");
        }
    
        public HashMap<String, Boolean> status = new HashMap<>();
        public HashMap<String, Integer> time = new HashMap<>();
        public HashMap<String, Boolean> requests = new HashMap<>();
        public HashMap<String, String> duelData = new HashMap<>();
        public HashMap<String, String> duelData2 = new HashMap<>();
        public HashMap<String, Boolean> dueling = new HashMap<>();
        public HashMap<String, Boolean> cheat = new HashMap<>();
    
    }
    


    WorldHandler Class:
    WorldHandler (open)

    Code:
    package me.Raydond123.duelz;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    
    public class WorldHandler {
    
        Main Main = new Main();
    
        Location CC1;
        Location CC2;
    
        public void loadWorlds() {
        
            World ChainedClouds = Bukkit.getWorld("ChainedClouds");
            Location CC1 = new Location(ChainedClouds, 1, 1, 1);
            CC1.setPitch(0F);
            CC1.setYaw(0F);
            Location CC2 = new Location(ChainedClouds, 2, 2, 2);
            CC2.setPitch(0F);
            Main.status.put("ChainedClouds", true);
        
        }
    
        public Location getLocationCC1() {
            return CC1;
        }
        public Location getLocationCC2() {
            return CC2;
        }
    }
    


    StackTrace:
    StackTrace (open)

    Code:
    [18:36:10 ERROR]: Could not load 'plugins\Duelz.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.IllegalArgumentException: Pl
    ugin already initialized!
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:135) ~[spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:329) ~[spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:251) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.loadPlugins(CraftServer.ja
    va:290) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at net.minecraft.server.v1_8_R1.DedicatedServer.init(DedicatedServer.jav
    a:152) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java
    :494) [spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_25]
    Caused by: java.lang.IllegalArgumentException: Plugin already initialized!
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader
    .java:122) ~[spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[spigot
    -1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at me.Raydond123.duelz.Main.<init>(Main.java:9) ~[?:?]
            at me.Raydond123.duelz.WorldHandler.<init>(WorldHandler.java:9) ~[?:?]
            at me.Raydond123.duelz.Main.<init>(Main.java:11) ~[?:?]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    ~[?:1.8.0_25]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    ~[?:1.8.0_25]
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
    rce) ~[?:1.8.0_25]
            at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_2
    5]
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_25]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.jav
    a:76) ~[spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:131) ~[spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            ... 6 more
    Caused by: java.lang.IllegalStateException: Initial initialization
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader
    .java:125) ~[spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[spigot
    -1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at me.Raydond123.duelz.Main.<init>(Main.java:9) ~[?:?]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    ~[?:1.8.0_25]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    ~[?:1.8.0_25]
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
    rce) ~[?:1.8.0_25]
            at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_2
    5]
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_25]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.jav
    a:76) ~[spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:131) ~[spigot-1.8.jar:git-Spigot-fa7cbf9-00eba53]
            ... 6 more
     
    Last edited: Jan 27, 2015
Thread Status:
Not open for further replies.

Share This Page