World generator doesn't generate?

Discussion in 'Plugin Development' started by Baummann, Jan 5, 2012.

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

    Baummann

    I've written a world generator which generates a volcano.
    Code:
    package me.baummann.plugins.volcanoes;
    
    import java.util.Random;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    
    public class VolcanoGen {
        private int blocks = 0;
        private boolean generated = false;
        public boolean generate(World world, Random random, int i, int j, int k) {
            if (world.getBlockAt(i, j - 1, k).getType() == Material.AIR || world.getBlockAt(i, j + 1, k).getType() != Material.AIR) {
                return false;
            }
    
            int radius = 30;
            int height = 0;
            for (int l = 0; l < 20; l++) {
                radius--;
                height++;
                generateDisc(radius, height, new Location(world, i, j, k), Material.STONE, random);
            }
    
            radius = 29;
            height = 0;
            for (int l = 0; l < 20; l++) {
                radius--;
                height++;
                generateDisc(radius, height, new Location(world, i, j, k), Material.LAVA, random);
            }
            generated = true;
            return true;
        }
    
        public boolean hasGenerated() {
            return generated;
        }
    
        public int blocksUsed() {
            return blocks;
        }
    
        public void generateDisc(int radius, int height, Location loc, Material mat, Random rand) {
            for (int x = -radius; x <= radius; x++) {
                for (int z = -radius; z <= radius; z++) {
                    for (int y = 0; y <= height; y++) {
                        if (x * x + z * z < radius) {
                            if (mat == Material.LAVA) {
                                setBlock(new Location(loc.getWorld(), loc.getX() + x + rand.nextInt(3), y, loc.getZ() + z + rand.nextInt(3)), mat);
                                continue;
                            }
                            int kindOfMovement = rand.nextInt(4);
                            if (kindOfMovement == 0) {
                                setBlock(new Location(loc.getWorld(), loc.getX() + x + rand.nextInt(3), y, loc.getZ() + z + rand.nextInt(3)), mat);
                            } else if (kindOfMovement == 1) {
                                setBlock(new Location(loc.getWorld(), loc.getX() + x - rand.nextInt(3), y, loc.getZ() + z - rand.nextInt(3)), mat);
                            } else if (kindOfMovement == 2) {
                                setBlock(new Location(loc.getWorld(), loc.getX() + x + rand.nextInt(3), y, loc.getZ() + z - rand.nextInt(3)), mat);
                            } else if (kindOfMovement == 3) {
                                setBlock(new Location(loc.getWorld(), loc.getX() + x - rand.nextInt(3), y, loc.getZ() + z + rand.nextInt(3)), mat);
                            } else {
                                setBlock(new Location(loc.getWorld(), loc.getX() + x - rand.nextInt(3), y, loc.getZ() + z - rand.nextInt(3)), mat);
                            }
                        }
                    }
                }
            }
        }
    
        private void setBlock(Location loc, Material material) {
            Block block = loc.getBlock();
            block.setType(material);
            block.getState().update(true);
            blocks++;
        }
    }
    
    Code:
    package me.baummann.plugins.volcanoes;
    
    import java.util.Random;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class VolcanoCommand implements CommandExecutor {
        private Volcanoes plugin;
        public VolcanoCommand(Volcanoes instance) {
            plugin = instance;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String cmdLine, String[] split) {
            if (!(sender instanceof Player)) {
                msg(sender, "&cCan't use that in console!");
                return true;
            }
    
            final Player player = (Player) sender;
            if (!player.hasPermission("volcanoes.volcano")) {
                msg(player, "&cNo permission!");
                return true;
            }
    
            Bukkit.getServer().broadcastMessage("Lag incoming!");
    
            VolcanoGen gen = new VolcanoGen();
            gen.generate(player.getWorld(), new Random(), player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ());
            msg(player, "&2Generation finished!");
            return true;
        }
    
        public void msg(CommandSender sender, String msg) {
            String parsedMessage = msg.replace('&', '\u00A7');
            sender.sendMessage(parsedMessage);
        }
    }
    
    When I use /volcano, it shows "Generation finished!" but nothing else happens. No lag, no volcano just text.
     
  2. Your method generate(...) will always return false if you use the player's coordinates.
    As far as I know, the eye height of the player will be used. That means the block below the eye height is always air, so your method returns false with that check.

    if (world.getBlockAt(i, j - 1, k).getType() == Material.AIR || world.getBlockAt(i, j + 1, k).getType() != Material.AIR) { return false; }
     
  3. Offline

    Baummann

    Client side, the player position is the eye height. Server side the player position is the player's legs.
    That's not the problem. I tried to use
    Code:
    if (!gen.generate(...)) {
    player.sendMessage("Can't generate here!");
    } else {
    player.sendMessage("Generation finished!");
    }
    before which worked like a charm.
     
Thread Status:
Not open for further replies.

Share This Page