[Abandoned] Making a hollow cube ( Or hollow anything )!

Discussion in 'Plugin Development' started by Slamakans, Nov 24, 2011.

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

    Slamakans

    So I'm trying to create a plugin that when someone that has enabled the plugin destroys a dirt block, it spawns a shelter.

    Code:
    Code:
    public void onBlockBreak(BlockBreakEvent event){
    
            Player player = event.getPlayer();
    
            Block block = event.getBlock();
            World world = block.getLocation().getWorld();
    
            if((block.getType() == Material.DIRT) && (plugin.isEnabled(player))){
    
                int x_start = block.getX() - 2;
                int y_start = block.getY();
                int z_start = block.getZ() - 2;
    
                int x_length = x_start + 4;
                int y_length = y_start + 4;
                int z_length = z_start + 4;
    
                for(int x_o = x_start; x_o <= x_length; x_o++){
    
                    for(int y_o = y_start; y_o <= y_length; y_o++){
    
                        for(int z_o = z_start; z_o <= z_length; z_o++){
    
                            Block blockToChange = world.getBlockAt(x_o,y_o,z_o);
                            if((x_o == x_start + 2 && (y_o == y_start || y_o == y_start + 1) && z_o == z_start) || (x_o != x_start && y_o != y_length && z_o != z_start) || (x_o != x_length && z_o != z_length)){
    
                                blockToChange.setTypeId(0);
    
                            }else{
    
                                blockToChange.setTypeId(3);
    
                            }
    
                        }
    
                    }
    
                }
    
            }
    
        }
    Not calling block.Update(); isn't the problem :)
     
  2. Offline

    ItsHarry

    You forgot to call Block.update();
     
  3. Offline

    ThePageXL

    What exactly isnt working?
     
  4. Offline

    tips48

    You've tested block.update() ?
     
  5. Offline

    desht

    Not sure what this Block update() method @ItsHarry and @tips48 are talking about - the Block class doesn't have an update() method. They may be thinking about BlockState, which is not relevant here - this code is just setting the type ID of some blocks.

    @Slamakans your code layout looks basically OK, so I'd concentrate on that big unwieldy logic statement you have to detect the walls. It isn't very readable. What actually goes wrong when you break the block? Does nothing happen? Or does the whole cuboid get set to air, or to dirt? Or some exception?

    I'd break that logic statement down (haven't tested this, mind):
    PHP:
    boolean type 0;
    if (
    x_o == x_start || x_o == x_length) {
      
    type 3;
    } else if (
    y_o == y_start || y_o == y_length) {
      
    type 3;
    } else if (
    z_o == z_start || z_o == z_length) {
      
    type 3;
    }
    blockToChange.setTypeId(type);
    You probably also want to make some checks to ensure the player isn't embedded in the dirt you're adding, I'll leave that up to you :)

    I'd also consider parameterising the various constants in that method (material types, cube position and size). That's the sort of thing that might be best taken from a config file.

    Or to go a step further, create a Shelter class which has the responsibility of drawing itself, detecting where its walls are etc. (it could even store a backup of the original terrain for if/when it's removed). Java is an OO language, use its features :)
     
  6. Offline

    Slamakans

    @desht

    Thank you for your post :)

    I have now tidied up the logic of the code. All block placing works and nothing gets placed wrongly.

    My door I try to create will just fall down and become an item to pick up. I got it to work before but accidentally lost that little piece of code.

    Also, when the code is enabled and starts building the shelter, the player who activated it gets "Internal server error" and this shows up in the console:
    Code:
    [Severe] Failed to handle packet: java.lang.NullPointerException
    java.lang.NullPointerException
            at net.minecraft.server.InventoryPlayer.b(InventoryPlayer.java:315)
            at net.minecraft.server.EntityHuman.b(EntityHuman.java:476)
            at net.minecraft.server.ItemInWorldManager.c(ItemInWorldManager.java:219
    )
            at net.minecraft.server.ItemInWorldManager.a(ItemInWorldManager.java:165
    )
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:500)
            at net.minecraft.server.Packet14BlockDig.a(SourceFile:43)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:92)
            at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:516)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:414)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:457)

    This is my code so far:


    Code:
    package me.slamakans.awesome;
    
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockListener;
    
    public class ShelterBlockListener extends BlockListener {
        public static Shelter plugin;
    
        public ShelterBlockListener(Shelter instance){
            plugin = instance;
        }
    
        public void onBlockBreak(BlockBreakEvent event){
    
            Player player = event.getPlayer();
    
            Block block = event.getBlock();
            World world = block.getLocation().getWorld();
    
            Location torchLoc = block.getLocation();
            torchLoc.setY(torchLoc.getY() + 1);
    
            int buildBlock = block.getTypeId();
    
            Boolean isValidBlock = buildBlock == 1 || buildBlock == 3 || buildBlock == 4 || buildBlock == 5 || buildBlock == 20 || buildBlock == 24 || buildBlock == 87 || buildBlock == 35;
    
            Material torch = Material.TORCH;
            Material redstonetorch = Material.REDSTONE_TORCH_ON;
            int typeId = 0;
            byte dataByte = 0;
    
            if(isValidBlock && world.getBlockAt(torchLoc).getType() == redstonetorch && plugin.isEnabled(player)){
    
                world.getBlockAt(torchLoc).setTypeId(0);
    
                int x_start = block.getX() - 2;
                int y_start = block.getY();
                int z_start = block.getZ() - 2;
    
                int x_length = x_start + 4;
                int y_length = y_start + 2;
                int z_length = z_start + 4;
    
                for(int x_o = x_start; x_o <= x_length; x_o++){
    
                    for(int y_o = y_start; y_o <= y_length; y_o++){
    
                        for(int z_o = z_start; z_o <= z_length; z_o++){
    
                            Boolean x_o123 = x_o == x_start + 1 || x_o == x_start + 2 || x_o == x_start + 3;
                            Boolean y_o01 = y_o == y_start || y_o == y_start + 1;
                            Boolean z_o123 = z_o == z_start + 1 || z_o == z_start + 2 || z_o == z_start + 3;
    
                            Boolean isBlockInside = x_o123 && y_o01 && z_o123;
                            Boolean isWorkbench = x_o == x_start + 1 && y_o == y_start && z_o == z_start + 3;
                            Boolean isFurnace = x_o == x_start + 1 && y_o == y_start + 1 && z_o == z_start + 3;
                            Boolean isChest = x_o == x_start + 3 && y_o == y_start && z_o == z_start + 3;
    
                            Boolean doorSpot = x_o == x_start + 2 && (y_o == y_start || y_o == y_start + 1) && z_o == z_start;
                            Boolean lowerDoor = x_o == x_start + 2 && y_o == y_start && z_o == z_start;
                            Boolean topDoor = x_o == x_start + 2 && y_o == y_start + 1 && z_o == z_start;
    
                            Block blockToChange = world.getBlockAt(x_o,y_o,z_o);
                            if(isBlockInside){
    
                                if(isWorkbench){
    
                                    typeId = 58;
    
                                }else{
    
                                    if(isFurnace){
    
                                        typeId = 61;
                                        dataByte = 2;
    
                                    }else{
    
                                        if(isChest){
    
                                            typeId = 54;
                                            dataByte = 2;
    
                                        }else{
    
                                                typeId = 0;
    
                                        }
    
                                    }
    
                                }
    
                            }else{
    
                                    if(doorSpot){
    
                                            if(lowerDoor){
    
                                                typeId = 64;
                                                dataByte = 1;
    
                                            }else if(topDoor){
    
                                                typeId = 64;
                                                dataByte = 1 | 8;
    
                                            }
     
                                    }else{
    
                                        typeId = buildBlock;
    
                                    }
    
                            }
    
                            blockToChange.setTypeId(typeId);
                            blockToChange.setData(dataByte);
    
                        }
    
                    }
    
                }
    
                Block t = block.getRelative(0,1,1);
                t.setType(torch);
    
            }else{
    
                if(isValidBlock && world.getBlockAt(torchLoc).getType() == torch && plugin.isEnabled(player)){
    
                    world.getBlockAt(torchLoc).setTypeId(0);
    
                    int x_start = block.getX() - 2;
                    int y_start = block.getY();
                    int z_start = block.getZ() - 2;
    
                    int x_length = x_start + 4;
                    int y_length = y_start + 2;
                    int z_length = z_start + 4;
    
                    for(int x_o = x_start; x_o <= x_length; x_o++){
    
                        for(int y_o = y_start; y_o <= y_length; y_o++){
    
                            for(int z_o = z_start; z_o <= z_length; z_o++){
    
                                Boolean x_o123 = x_o == x_start + 1 || x_o == x_start + 2 || x_o == x_start + 3;
                                Boolean y_o01 = y_o == y_start || y_o == y_start + 1;
                                Boolean z_o123 = z_o == z_start + 1 || z_o == z_start + 2 || z_o == z_start + 3;
    
                                Boolean isBlockInside = x_o123 && y_o01 && z_o123;
    
                                Block blockToChange = world.getBlockAt(x_o,y_o,z_o);
                                if(isBlockInside){
                                    typeId = 0;
    
                                }else{
    
                                    typeId = buildBlock;
    
                                }
    
                                blockToChange.setTypeId(typeId);
    
                            }
    
                        }
    
                    }
    
                    Block t = block.getRelative(0,1,1);
                    t.setType(torch);
                    t.setData((byte) 4);
    
                }
    
            }
    
        }
    
    }
     
  7. Offline

    iffa

    You will have to do some data setting with the door, I believe.
     
  8. Offline

    Slamakans

    @iffa
    Thanks for trying to help me but if you look here you can see I have already set those datas. :)


    Code:
    if(doorSpot){
    
                                            if(lowerDoor){
    
                                                typeId = 64;
                                                dataByte = 1;
    
                                            }else if(topDoor){
    
                                                typeId = 64;
                                                dataByte = 1 | 8;
    
                                            }
    I fixed the door problem but I am still disconnecting when destroying the block.

    I read something about it being too many block manipulations at one time or something.

    Code:
    Code:
    package me.slamakans.awesome;
    
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockListener;
    
    public class ShelterBlockListener extends BlockListener {
        public static Shelter plugin;
    
        public ShelterBlockListener(Shelter instance){
            plugin = instance;
        }
    
        public void onBlockBreak(BlockBreakEvent event){
    
            Player player = event.getPlayer();
    
            Block block = event.getBlock();
            World world = block.getLocation().getWorld();
    
            Location torchLoc = block.getLocation();
            torchLoc.setY(torchLoc.getY() + 1);
    
            int buildBlock = block.getTypeId();
    
            Boolean isValidBlock = buildBlock == 1 || buildBlock == 3 || buildBlock == 4 || buildBlock == 5 || buildBlock == 20 || buildBlock == 24 || buildBlock == 87 || buildBlock == 35;
    
            Material torch = Material.TORCH;
            Material redstonetorch = Material.REDSTONE_TORCH_ON;
            int typeId = 0;
    
            if(isValidBlock && world.getBlockAt(torchLoc).getType() == redstonetorch && plugin.isEnabled(player)){
    
                world.getBlockAt(torchLoc).setTypeId(0);
    
                int x_start = block.getX() - 2;
                int y_start = block.getY();
                int z_start = block.getZ() - 2;
    
                int x_length = x_start + 4;
                int y_length = y_start + 2;
                int z_length = z_start + 4;
    
                for(int x_o = x_start; x_o <= x_length; x_o++){
    
                    for(int y_o = y_start; y_o <= y_length; y_o++){
    
                        for(int z_o = z_start; z_o <= z_length; z_o++){
    
                            Boolean x_o123 = x_o == x_start + 1 || x_o == x_start + 2 || x_o == x_start + 3;
                            Boolean y_o01 = y_o == y_start || y_o == y_start + 1;
                            Boolean z_o123 = z_o == z_start + 1 || z_o == z_start + 2 || z_o == z_start + 3;
    
                            Boolean isBlockInside = x_o123 && y_o01 && z_o123;
    
                            Block blockToChange = world.getBlockAt(x_o,y_o,z_o);
                            if(isBlockInside){
                                typeId = 0;
    
                            }else{
    
                                typeId = buildBlock;
    
                            }
    
                            blockToChange.setTypeId(typeId);
    
                        }
    
                    }
    
                }
    
                Block wb = block.getRelative(-1,0,1);
                Block f = block.getRelative(-1,1,1);
                Block c = block.getRelative(1,0,1);
                Block t = block.getRelative(0,1,1);
    
                Block ld = block.getRelative(0,0,-2);
                Block td = block.getRelative(0,1,-2);
    
                wb.setTypeId(58);
    
                f.setTypeId(61);
                f.setData((byte) 2);
    
                c.setTypeId(54);
                c.setData((byte) 2);
    
                t.setType(torch);
                t.setData((byte) 4);
    
                ld.setTypeId(64);
                ld.setData((byte) 1);
    
                td.setTypeId(64);
                td.setData((byte) (1 | 8));
    
            }else{
    
                if(isValidBlock && world.getBlockAt(torchLoc).getType() == torch && plugin.isEnabled(player)){
    
                    world.getBlockAt(torchLoc).setTypeId(0);
    
                    int x_start = block.getX() - 2;
                    int y_start = block.getY();
                    int z_start = block.getZ() - 2;
    
                    int x_length = x_start + 4;
                    int y_length = y_start + 2;
                    int z_length = z_start + 4;
    
                    for(int x_o = x_start; x_o <= x_length; x_o++){
    
                        for(int y_o = y_start; y_o <= y_length; y_o++){
    
                            for(int z_o = z_start; z_o <= z_length; z_o++){
    
                                Boolean x_o123 = x_o == x_start + 1 || x_o == x_start + 2 || x_o == x_start + 3;
                                Boolean y_o01 = y_o == y_start || y_o == y_start + 1;
                                Boolean z_o123 = z_o == z_start + 1 || z_o == z_start + 2 || z_o == z_start + 3;
    
                                Boolean isBlockInside = x_o123 && y_o01 && z_o123;
    
                                Block blockToChange = world.getBlockAt(x_o,y_o,z_o);
                                if(isBlockInside){
                                    typeId = 0;
    
                                }else{
    
                                    typeId = buildBlock;
    
                                }
    
                                blockToChange.setTypeId(typeId);
    
                            }
    
                        }
    
                    }
    
                    Block t = block.getRelative(0,1,1);
                    t.setType(torch);
                    t.setData((byte) 4);
    
                }
    
            }
    
        }
    
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
Thread Status:
Not open for further replies.

Share This Page