Search code examples
javageometryminecraftbukkit

Build a pyramid in a minecraft bukkit plugin


I want to build a simple pyramid in Minecraft using a method in a bukkit-plugin. The final result shall look like this:enter image description here

I wrote this code:

public static void buildPyramid(Location l) {
    Location pos;
    for(int i = -2; i <= 2; i++) {
        for(int j = -2; j <= 2; j++) {
            pos = l.clone().add(i, 0, j);
            Bukkit.broadcastMessage(Math.abs(i) + Math.abs(j) + ""); // for test
            int diff = Math.abs(i) + Math.abs(j);
            switch(diff) {
                case 2:
                    l.getBlock().setType(Material.BEDROCK);
                    break;
                case 1:
                    l.getBlock().setType(Material.BEDROCK);
                    pos.add(0, 1, 0);
                    l.getBlock().setType(Material.BEDROCK);
                    pos.add(0, -1, 0);
                    break;
                case 0:
                    l.getBlock().setType(Material.BEDROCK);
                    pos.add(0, 1, 0);
                    l.getBlock().setType(Material.BEDROCK);
                    pos.add(0, 1, 0);
                    l.getBlock().setType(Material.BEDROCK);
                    pos.add(0, -2, 0);
                    break;
                default:
                    break;

            }
        }
    }
}

Unfortunatly, what happens is that one bedrock is placed on Location l and nothing else happens. It is quite dispairing... any help?


Solution

  • Sorry guys, the solution is that simple. I used the wrong variable:

    public static void buildPyramid(Location l) {
        Location pos;
        for(int i = -2; i <= 2; i++) {
            for(int j = -2; j <= 2; j++) {
                pos = l.clone().add(i, 0, j);
                Bukkit.broadcastMessage(Math.abs(i) + Math.abs(j) + ""); // for test
                int diff = Math.abs(i) + Math.abs(j);
                switch(diff) {
                    case 2:
                        pos.getBlock().setType(Material.BEDROCK);
                        break;
                    case 1:
                        pos.getBlock().setType(Material.BEDROCK);
                        pos.add(0, 1, 0);
                        pos.getBlock().setType(Material.BEDROCK);
                        pos.add(0, -1, 0);
                        break;
                    case 0:
                        pos.getBlock().setType(Material.BEDROCK);
                        pos.add(0, 1, 0);
                        pos.getBlock().setType(Material.BEDROCK);
                        pos.add(0, 1, 0);
                        pos.getBlock().setType(Material.BEDROCK);
                        pos.add(0, -2, 0);
                        break;
                    default:
                        break;
    
                }
            }
        }
    }