Solved So why wont this work?

Discussion in 'Plugin Development' started by spiroulis, Dec 28, 2014.

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

    spiroulis

    So im trying to get the cuboid between 2 locations but i get an error. It says something about line 27 of the command class and something about line 9 of the cuboid class. Heres what i got so far:
    Command Class
    Code:
    public class SetCrystalCommand implements CommandExecutor{
    private main main;
       
        public SetCrystalCommand(main plugin) {
            main = plugin;
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
           
            Player player = (Player)sender;
            if(cmd.getName().equalsIgnoreCase("MCDDSetCrystal")){
                if(player.isOp()){
                    if(main.getConfig().getBoolean("Pos1.enabled") != true || main.getConfig().getBoolean("Pos2.enabled") != true){
                        player.sendMessage(ChatColor.RED + "You need to set a region first!");
                        return true;
                    }
                   
                    if(!(new Cuboid().contains(player.getLocation()))){ //line 27
                        player.sendMessage(ChatColor.RED + "The crystals must be inside the region you set!");
                        return true;
                    }
                }
            }
            return false;
        }
    }
    And Cuboid Class
    Code:
    public class Cuboid {
        private main main;
       
        Location l1 = new Location((World) main.getConfig().get("Pos1.world"), main.getConfig().getDouble("Pos1.x"), main.getConfig().getDouble("Pos1.y"), main.getConfig().getDouble("Pos1.z"));//line 9
        Location l2 = new Location((World) main.getConfig().get("Pos2.world"), main.getConfig().getDouble("Pos2.x"), main.getConfig().getDouble("Pos2.y"), main.getConfig().getDouble("Pos2.z"));
       
        int x1 = Math.min(l1.getBlockX(), l2.getBlockX());
        int y1 = Math.min(l1.getBlockY(), l2.getBlockY());
        int z1 = Math.min(l1.getBlockZ(), l2.getBlockZ());
        int x2 = Math.max(l1.getBlockX(), l2.getBlockX());
        int y2 = Math.max(l1.getBlockY(), l2.getBlockY());
        int z2 = Math.max(l1.getBlockZ(), l2.getBlockZ());
       
       
        public boolean contains(Location loc) {
              return loc.getBlockX() >= l1.getBlockX() && loc.getBlockX() <= l2.getBlockX()
                  && loc.getBlockY() >= l1.getBlockY() && loc.getBlockY() <= l2.getBlockY()
                  && loc.getBlockZ() >= l1.getBlockZ() && loc.getBlockZ() <= l2.getBlockZ();
            }
    }
    and heres the error in case i missed something
    Code:
    [00:38:03] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'mcddsetcrystal' in plugin McDungeonDefenders v0.5
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:742) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:956) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
        at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
        at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
        at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
        at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
    Caused by: java.lang.NullPointerException
        at me.spiroulis.MCDD.Cuboid.<init>(Cuboid.java:9) ~[?:?]
        at me.spiroulis.MCDD.SetCrystalCommand.onCommand(SetCrystalCommand.java:27) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
        ... 13 more
    [00:39:00]
     
  2. Offline

    _Filip

    What is line 9 of the cuboid class including the imports and package statement?
     
  3. Offline

    HeadGam3z

    main is null in your Cuboid class.
     
  4. Offline

    spiroulis

    @_Filip mate i already left a note at the code xd
    but anyways here it is :) :
    Code:
        Location l1 = new Location((World) main.getConfig().get("Pos1.world"), main.getConfig().getDouble("Pos1.x"), main.getConfig().getDouble("Pos1.y"), main.getConfig().getDouble("Pos1.z"));
    
     
  5. Offline

    HeadGam3z

    Main. Is. Null.
     
    spiroulis likes this.
  6. Offline

    spiroulis

    @HeadGam3z oh sry didnt see your last post my internet is acting
    kinda stupidly. So how do i change it?
     
  7. Offline

    HeadGam3z

    Initialize it? :p You did it in your other class.
     
  8. Offline

    spiroulis

    @HeadGam3z oh i tried doing that before i post this but still same error poped up. And just to make sure i did it correctly this is what i did:
    Code:
    public class Cuboid {
        private main main;
       
        public Cuboid(main plugin) {
            main = plugin;
        }
       
        Location l1 = new Location((World) main.getConfig().get("Pos1.world"), main.getConfig().getDouble("Pos1.x"), main.getConfig().getDouble("Pos1.y"), main.getConfig().getDouble("Pos1.z"));
        Location l2 = new Location((World) main.getConfig().get("Pos2.world"), main.getConfig().getDouble("Pos2.x"), main.getConfig().getDouble("Pos2.y"), main.getConfig().getDouble("Pos2.z"));
       
        int x1 = Math.min(l1.getBlockX(), l2.getBlockX());
        int y1 = Math.min(l1.getBlockY(), l2.getBlockY());
        int z1 = Math.min(l1.getBlockZ(), l2.getBlockZ());
        int x2 = Math.max(l1.getBlockX(), l2.getBlockX());
        int y2 = Math.max(l1.getBlockY(), l2.getBlockY());
        int z2 = Math.max(l1.getBlockZ(), l2.getBlockZ());
       
       
        public boolean contains(Location loc) {
              return loc.getBlockX() >= l1.getBlockX() && loc.getBlockX() <= l2.getBlockX()
                  && loc.getBlockY() >= l1.getBlockY() && loc.getBlockY() <= l2.getBlockY()
                  && loc.getBlockZ() >= l1.getBlockZ() && loc.getBlockZ() <= l2.getBlockZ();
            }
    }
    Bump?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  9. Offline

    Rocoty

    @spiroulis And...how do you instantiate it? (Post the main class)
     
  10. Offline

    spiroulis

    @Rocoty I...I dont...xd. But i also dont know how
    Code:
    public class main extends JavaPlugin{
       
        public void onEnable(){
            System.out.println("McDungeonDefenders has been enabled ^-^");
           
            getCommand("MCDDSetPos1").setExecutor(new SetRegionCommand(this));
            getCommand("MCDDSetPos2").setExecutor(new SetPos2Command(this));
            getCommand("MCDDSetDifficulty").setExecutor(new SetDifficultyCommand(this));
            getCommand("MCDDSetCrystal").setExecutor(new SetCrystalCommand(this));
           
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
       
        public void onDisable(){
            System.out.println("McDungeonDefenders has been disabled :(");
        }
    
    }
     
  11. Offline

    Rocoty

    @spiroulis Sorry my bad. I replied without properly reading the stacktrace. You instantiate Cuboid in the other class you posted. However, this is irrelevant. Your problem is that you initialise the locations outside the constructor, dereferencing main in the process. However, at this point main does not have a value. Move the initialisation of the locations into the constructor, below the assignment of main.
     
    spiroulis likes this.
  12. Offline

    spiroulis

    @HeadGam3z @Rocoty Thank you both for helping :). What i did to solve this is instead of having a Cuboid class i did this:
    Code:
    public class SetCrystalCommand implements CommandExecutor{
    private main main;
    double playerx;
    double playery;
    double playerz;
       
        public SetCrystalCommand(main plugin) {
            main = plugin;
        }
       
        public boolean isInside(Location loc, Location l1, Location l2) {
            int x1 = Math.min(l1.getBlockX(), l2.getBlockX());
            int y1 = Math.min(l1.getBlockY(), l2.getBlockY());
            int z1 = Math.min(l1.getBlockZ(), l2.getBlockZ());
            int x2 = Math.max(l1.getBlockX(), l2.getBlockX());
            int y2 = Math.max(l1.getBlockY(), l2.getBlockY());
            int z2 = Math.max(l1.getBlockZ(), l2.getBlockZ());
            return playerx >= x1 && playerx <= x2 && playery >= y1 && playery <= y2 && playerz >= z1 && playerz <= z2;
    }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
           
            Player player = (Player)sender;
            if(cmd.getName().equalsIgnoreCase("MCDDSetCrystal")){
                if(player.isOp()){
                    if(main.getConfig().getBoolean("Pos1.enabled") != true || main.getConfig().getBoolean("Pos2.enabled") != true){
                        player.sendMessage(ChatColor.RED + "You need to set a region first!");
                        return true;
                    }
               
                        playerx = player.getLocation().getX();
                        playery = player.getLocation().getY();
                        playerz = player.getLocation().getZ();
                            if(!(isInside(player.getLocation(), new Location(player.getWorld(), main.getConfig().getDouble("Pos1.x"), main.getConfig().getDouble("Pos1.y"), main.getConfig().getDouble("Pos1.z")),new Location (player.getWorld(), main.getConfig().getDouble("Pos2.x"), main.getConfig().getDouble("Pos2.y"), main.getConfig().getDouble("Pos2.z"))))){
                                player.sendMessage(ChatColor.RED + "The crystals must be inside the region you set!");
                                return true;
     
  13. Offline

    Rocoty

    @spiroulis You know, you could have just used Vector#isInAABB(Vector, Vector)
     
  14. Offline

    spiroulis

    @Rocoty oh... well i will use it in future plugins, thanks for the info though :)
     
Thread Status:
Not open for further replies.

Share This Page