Getting block underneath a block trough a Location

Discussion in 'Plugin Development' started by Kevinzuman22, Jul 7, 2016.

Thread Status:
Not open for further replies.
  1. My current code:
    Code:
    else if(args[0].equals("check")) {
                        String worldname = args[1];
                        double x = settings.getSpawns().getInt("spawns." + worldname + ".x");
                        double y = settings.getSpawns().getInt("spawns." + worldname + ".y");
                        double z = settings.getSpawns().getInt("spawns." + worldname + ".z");
                        float yaw = settings.getSpawns().getInt("spawns." + worldname + ".yaw");
                        float pitch = settings.getSpawns().getInt("spawns." + worldname + ".pitch");
                      
                        Location loc = new Location(null, x, y - 1, z, yaw, pitch);
                      
                        p.sendMessage(ChatColor.BLUE + "--> " + ChatColor.WHITE + "WorldSpawns - Spawn of " + ChatColor.GOLD + worldname + ChatColor.BLUE + " <--");
                        if(settings.getSpawns().get("spawns." + worldname) == null) {
                            p.sendMessage(ChatColor.WHITE + "The spawn of this world has not been set yet.");
                        } else {
                            p.sendMessage(ChatColor.GRAY + "X: " + ChatColor.WHITE + x + ".");
                            p.sendMessage(ChatColor.GRAY + "Y: " + ChatColor.WHITE + y + ".");
                            p.sendMessage(ChatColor.GRAY + "Z: " + ChatColor.WHITE + z + ".");
                            p.sendMessage(ChatColor.GRAY + "Yaw: " + ChatColor.WHITE + yaw + ".");
                            p.sendMessage(ChatColor.GRAY + "Pitch: " + ChatColor.WHITE + pitch + ".");
                            if(loc.getBlock().getType().equals(Material.AIR)) {
                                p.sendMessage(ChatColor.GRAY + "In air: " + ChatColor.WHITE + "true.");
                            } else {
                                p.sendMessage(ChatColor.GRAY + "In air: " + ChatColor.WHITE + "false.");
                            }
                        }
                        return true;
                    }

    Problem:

    I made a plugin that sets a spawn for every seperate world. I made a command that can check the location of a world's spawn. I wanted it to show the X-axis, the Y-axix, the Z-axis and the yaw and the pitch and as well if it is set in the air or not. All those things I was able to do, except for showing if it is set in air or not. I tried to do it by getting the location of the spawn, and subtract 1 of the Y-axis to get the block under the set spawn. I didn't seem to work. I kept getting a NullPointerException, and I didn't quite understand why I was getting one. I'm pretty sure it should work in theory, but appearantly not in the actual game.

    Also, I am using a SettingsManager class to save and load all the spawns that have been set, or to set new ons etc.. If it can help, here is the whole class of it: http://pastebin.com/DMUjgd3u. And if this could help, here is the complete CommandExecutor class of the command: http://pastebin.com/cW7PqsPs. It probably has some bad practice ways of doing stuff, but I am still kinda learning, I've only been doing this for a year, trying to learn most of the stuff by myself.

    What you can do to recreate the problem:
    Trying to get a block near another block using a Location.

    Possible problematic line(s):

    "Location loc = new Location(null, x, y - 1, z, yaw, pitch);" (Line 9).
    "if(loc.getBlock().getType().equals(Material.AIR)) {" (Line 21).

    Error message (If any):

    http://pastebin.com/7UjLMkXx.

    What you have tried:

    I have also tried using BlockFace to get the block, but then I still get errors. If you want, I can try it again, and then get the error and post it on PasteBin if it could be of any use.
     
  2. Offline

    Zombie_Striker

    First, you are getting the doubles and floats before you even know if they exist. What if "spawns.worldname" is null? Then before you even get to that line, you will be getting NPEs (which I assume is happening).

    Use == to compare enums. The method ".equal" is is used for comparing if the values are the same. Use == for numbers and enums.

    Main problem: You set the world to null. Since null means "nothing" or "not an object", how can nothing have locations or blocks? You need to provide a world. Do this by using
    "Bukkit.getWorld(world name)"
    To get the world's instance. If the world does not exist, this will return null so make sure the world is not null before doing anything with it.

    Rule of thumb when it comes to Java (or any other OOP language), you should almost never provide null values for methods.
     
  3. This helped, thanks! I didn't noticed the null I put at the world in Location, and the == fixed it.

    Now, I got it that if the block underneath the block where the spawn is set, is air, it will say it is in the air. But now if I slightly float above the block, and then set the spawn there, it will say that it is not in the air, because the block under it is still air. How would I let this still say it is in the air?
     
  4. Offline

    Zombie_Striker

    @Kevinzuman22
    This would require you to subtract a number such as 0.25 or 0.5 instead of 1 for the y. If, for some reason, subtracting a double gives you an error, cast the x,y,and z variables to doubles.
     
Thread Status:
Not open for further replies.

Share This Page