How to get blocks in a radius

Discussion in 'Plugin Development' started by JavaNoob, Oct 8, 2020.

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

    JavaNoob

    Hi! I am trying to get a radius around a player so that I can make the blocks fall. I don't know if this will work, because I haven't tested it yet. The problem I have is that I don't know how to pull blocks out of the array and change their properties so that I can test the radius function. How do I get the blocks from the array that I made and change it to like air or something?
    (Please don't make fun of me for the messiness, I'm trying my best)
    Code:
    publicList<Block>onRadius(Location loc, int x){
    
            List<Block> blocks = newArrayList<Block>();
    
            loc.add(x, 0, 0);
    
            var serv = Bukkit.getServer();
    
            int z = 1;
    
            int y = 0;
    
            while (y != x) {
    
                loc.add(x - 1, 0, z);
    
                y++;
    
                z++;
    
                Block block = (Block) Bukkit.getServer().getWorld(serv.getName()).getBlockAt(loc);
    
                blocks.add(block);
    
            }
    
            int n = x / -1;
    
            y = 0;
    
            while (y != n) {
    
                loc.add(x + 1, 0, z);
    
                z--;
    
                y++;
    
                Block block = (Block) Bukkit.getServer().getWorld(serv.getName()).getBlockAt(loc);
    
                blocks.add(block);
    
            }
    
            int a = 0;
    
            a=blocks.size();
    
            int b = 0;
    
            while (b != a) {
    
                b++;
    
            }
    
            return blocks;
    
        }
    
    (If you are confused, yes, I did change the radius code and also edit the post.)
     
    Last edited: Oct 8, 2020
  2. Offline

    spuddy

    If I understood your question correctly, this is how I would pull blocks out of a list and set them to air blocks.

    Code:
    for(Block b : blocks) {
        b.setType(Material.AIR);
    }

    On an alternative note, I have a similar function in some plugin somewhere that I can't for the life of me find. But the code to identify blocks in a radius around a location went something like this.
    Code:
        public static Set<Block> sphereAround(Location location, int radius) {
            Set<Block> sphere = new HashSet<Block>();
            Block center = location.getBlock();
            for(int x = -radius; x <= radius; x++) {
                for(int y = -radius; y <= radius; y++) {
                    for(int z = -radius; z <= radius; z++) {
                        Block b = center.getRelative(x, y, z);
                        if(center.getLocation().distance(b.getLocation()) <= radius) {
                            sphere.add(b);
                        }
                    }
                 
                }         
            }     
            return sphere;
        }
    Not sure if it'd be faster than how you're doing it though.
     
    Last edited: Oct 8, 2020
  3. Offline

    JavaNoob

    i
     
    Last edited: Oct 11, 2020
  4. Offline

    JavaNoob

    i don't know what is wrong with this. It says null exception, but I can't find what I am not filling. Can you maybe see something?
    Code:
    public List<Block>onRadius(Location loc, int x){
    
    List<Block> blocks = newArrayList<Block>();
    
    loc.add(x, 0, 0);
    
    var serv = Bukkit.getServer();
    
    int z = 1;
    
    int y = 0;
    
    while (y != x) {
    
    loc.add(x - 1, 0, z);
    
    y++;
    
    z++;
    
    Block block = (Block) Bukkit.getServer().getWorld(serv.getName()).getBlockAt(loc);
    
    blocks.add(block);
    
    }
    
    int n = x / -1;
    
    y = 0;
    
    while (y != n) {
    
    loc.add(x + 1, 0, z);
    
    z--;
    
    y++;
    
    Block block = (Block) Bukkit.getServer().getWorld(serv.getName()).getBlockAt(loc);
    
    blocks.add(block);
    
    }
    
    int a = 0;
    
    a=blocks.size();
    
    int b = 0;
    
    while (b != a) {
    
    b++;
    
    }
    
    return blocks;
    
    
     
  5. Offline

    CraftCreeper6

    @JavaNoob
    I'm assuming its a formatting issue when you copy pasted but your ArrayList doesn't have a space between new and ArrayList.

    Also, getWorld could be null.
     
  6. Offline

    JavaNoob

    I checked, and yes I just formatted it wrong. I fixed up the getServer(), and I don't believe that is the problem. I tried to run it again after and it still says NullPointerException. Here is the terminal log, if you can maybe see anything.

    org.bukkit.command.CommandException: Unhandled exception executing command 'fstart' in plugin Gravity v1.0

    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:48) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]

    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]

    at org.bukkit.craftbukkit.v1_13_R2.CraftServer.dispatchCommand(CraftServer.java:704) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]

    at net.minecraft.server.v1_13_R2.PlayerConnection.handleCommand(PlayerConnection.java:1621) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]

    at net.minecraft.server.v1_13_R2.PlayerConnection.a(PlayerConnection.java:1461) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]

    at net.minecraft.server.v1_13_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]

    at net.minecraft.server.v1_13_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]

    at net.minecraft.server.v1_13_R2.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:9) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]

    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]

    at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]

    at net.minecraft.server.v1_13_R2.SystemUtils.a(SourceFile:199) [spigot.jar:git-Spigot-1a3504a-84f3da3]

    at net.minecraft.server.v1_13_R2.MinecraftServer.b(MinecraftServer.java:896) [spigot.jar:git-Spigot-1a3504a-84f3da3]

    at net.minecraft.server.v1_13_R2.DedicatedServer.b(DedicatedServer.java:417) [spigot.jar:git-Spigot-1a3504a-84f3da3]

    at net.minecraft.server.v1_13_R2.MinecraftServer.a(MinecraftServer.java:831) [spigot.jar:git-Spigot-1a3504a-84f3da3]

    at net.minecraft.server.v1_13_R2.MinecraftServer.run(MinecraftServer.java:729) [spigot.jar:git-Spigot-1a3504a-84f3da3]

    at java.lang.Thread.run(Thread.java:835) [?:?]

    Caused by: java.lang.NullPointerException

    at me.nay.zombie.Main.onRadius(Main.java:34) ~[?:?]

    at me.nay.zombie.Main.onCommand(Main.java:58) ~[?:?]

    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-1a3504a-84f3da3]

    ... 15 more

    and the code.

    Code:
    public class Main extends JavaPlugin implements Listener {
    
       
    
        @Override
    
        public void onEnable(){
    
            Bukkit.getPluginManager().registerEvents(this, this);
    
        }
    
       
    
        public List<Block> onRadius(Location loc, int x){
    
            List<Block> blocks = new ArrayList<Block>();
    
            loc.add(x,0,0);
    
            Server serv = (Server) Bukkit.getServer();
    
            int z = 1;
    
            int y = 0;
    
            while (y != x) {
    
                loc.add(x - 1, 0, z);
    
                y++;
    
                z++;
    
                Block block = (Block) Bukkit.getServer().getWorld(serv.getName()).getBlockAt(loc);
    
                blocks.add(block);
    
            }
    
            int n = x / -1;
    
            y = 0;
    
            while (y != n) {
    
                loc.add(x + 1, 0, z);
    
                z++;
    
                y--;
    
                Block block = (Block) Bukkit.getServer().getWorld(serv.getName()).getBlockAt(loc);
    
                blocks.add(block);
    
            }
    
            for (Block bl : blocks) {
    
                bl.setType(Material.AIR);
    
                bl.getWorld().spawnFallingBlock(bl.getLocation(), bl.getBlockData());
    
            }
    
            return blocks;
    
        }
    
       
    
        @Override
    
        public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args){
    
           
    
            if(cmd.getName().equalsIgnoreCase("fstart")){
    
                Player player = (Player) Bukkit.getPlayer(args[0]);
    
                onRadius(player.getLocation(),5);
    
            }
    
           
    
            return false;
    
        }
    
    
     
  7. Offline

    CraftCreeper6

    What is line 34 in Main?

    Sent from my LYA-L09 using Tapatalk
     
  8. Offline

    JavaNoob

    Block block = (Block) Bukkit.getServer.getWorld(serv.getName()).getBlockAt(loc);

    Is the serv variable maybe not pulling the server correctly?
     
  9. Offline

    CraftCreeper6

    @JavaNoob
    Unlikely, are you certain the world name is correct?

    The only other thing I can think of is that loc is null.
     
  10. Offline

    JavaNoob

    Is the world name just what you named it manually? If so, I can just put that in myself to test.
     
  11. Offline

    CraftCreeper6

    @JavaNoob
    Yes, it's whatever the name is inside your file system.
     
  12. Offline

    JavaNoob

    Okay, I put it in and tried it and I'm still getting the same error. For the loc, I am putting in onRadius(player.getLocation, 5); that is what I am putting in for the method. Here is the changed code.

    Code:
    @Override
    
        public void onEnable(){
    
            Bukkit.getPluginManager().registerEvents(this, this);
    
        }
    
       
    
        public List<Block> onRadius(Location loc, int x){
    
            List<Block>blocks = new ArrayList<Block>();
    
            loc.add(x,0,0);
    
            int z = 1;
    
            int y = 0;
    
            while (y != x) {
    
                loc.add(x - 1, 0, z);
    
                y++;
    
                z++;
    
                Block block = (Block) Bukkit.getServer().getWorld("Plugin Tester Server").getBlockAt(loc);
    
                blocks.add(block);
    
            }
    
            int n = x / -1;
    
            y = 0;
    
            while (y != n) {
    
                loc.add(x + 1, 0, z);
    
                z++;
    
                y--;
    
                Block block = (Block) Bukkit.getServer().getWorld("Plugin Tester Server").getBlockAt(loc);
    
                blocks.add(block);
    
            }
    
            for (Block bl : blocks) {
    
                bl.setType(Material.AIR);
    
                bl.getWorld().spawnFallingBlock(bl.getLocation(),bl.getBlockData());
    
            }
    
            return blocks;
    
        }
    
       
    
        @Override
    
        public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args){
    
           
    
            if(cmd.getName().equalsIgnoreCase("fstart")){
    
                Player player = (Player) sender;
    
                onRadius(player.getLocation(), 5);
    
            }
    
           
    
            return false;
    
        }
    
    (There could be formatting issues again, it is not like that in the actual code)
     
Thread Status:
Not open for further replies.

Share This Page