Solved Cuboid Area

Discussion in 'Plugin Development' started by xDeeKay, May 24, 2015.

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

    xDeeKay

    I'm trying to define a region and replace all the blocks within it, but it always seems to be offset 1 way when I mess with the values. I suck at math, so any help would be appreciated.

    Here's what I have:
    Code:
        public void resetFloor() {
           
            World world = Bukkit.getWorld(plugin.getConfig().getString("spleef.world"));
           
            int x1 = plugin.getConfig().getInt("spleef.floor.corner1.x");
            int x2 = plugin.getConfig().getInt("spleef.floor.corner2.x");
           
            int z1 = plugin.getConfig().getInt("spleef.floor.corner1.z");
            int z2 = plugin.getConfig().getInt("spleef.floor.corner2.z");
           
            int y = plugin.getConfig().getInt("spleef.floor.corner1.y");
           
            int xmin = Math.min(x1, x2);
            int xmax = Math.max(x1, x2);
            int zmin = Math.min(z1, z2);
            int zmax = Math.max(z1, z2);
    
            for (int x = xmin; x < xmax; x++) {
                for (int z = zmin; z < zmax; z++) {
                    world.getBlockAt(x, y, z).setTypeId(plugin.getConfig().getInt("spleef.floor.block"));
                }
            }
        }
     
  2. Offline

    Signatured

    Rather off topic solution, but I might as well tell you :p

    Instead of making a cuboid to set the floor, when a player is in an arena, when ever they break a snow block add it to an array list. One the arena that player is in ends, get the location of all the blocks in that arraylist and set it to snow block.

    I'm not good at making cuboids myself, so this is just another way to accomplish this :p
     
  3. Offline

    xDeeKay

    @Signatured Thanks for the reply. Yeah, I came across some other threads suggesting that too, if all else fails I might do that instead. My code is doing almost what it's supposed to, I just think it's a little offset somewhere, and I can't see where it's doing that.
     
  4. Offline

    SrNicks_

    @xDeeKay
    Make sure you have something wrong, i made the code here.

    Code:
            World world = Bukkit.getWorld(plugin.getConfig().getString("spleef.world"));
            int x1 = . plug-in GetConfig ( ) . getInt ( "spleef.floor.corner1.x" ) ;
            int x2 = . plug-in GetConfig ( ) . getInt ( "spleef.floor.corner2.x" ) ;    
            int z1 = . plug-in GetConfig ( ) . getInt ( "spleef.floor.corner1.z" ) ;
            int z2 = . plug-in GetConfig ( ) . getInt ( "spleef.floor.corner2.z" ) ;    
            int y = plugin. GetConfig ( ) . getInt ( "spleef.floor.corner1.y" ) ;
              Location locmx = new Location(world, x1,y,z1);
                           Location loc = new Location(world, x2,y,z2);                  
                           Vector min = loc.toVector();
                           Vector max = locmx.toVector();
                           for(int x = min.getBlockX();x <= max.getBlockX(); x=x+1){
                               for(int y = min.getBlockY();y <= max.getBlockY(); y=y+1){
                                   for(int z = min.getBlockZ();z <= max.getBlockZ(); z=z+1){
                                       Location tmpblock = new Location(p.getWorld(), x, y, z);
                                       tmpblock.getBlock().setTypeId(plugin.getConfig().getInt("spleef.floor.block"));
     
  5. Offline

    Signatured

    By offset, do you mean it's 1 short one way and 1 long the other?
     
  6. Offline

    xDeeKay

    @SrNicks_ Trying in a moment, thanks.
    @Signatured Yeah, exactly that. In a 5x10 area I was testing on, it would set 3x12 basically. 1 block less on 2 sides, and 1 blocks more on the other 2 sides.

    Edit: @SrNicks_ This didn't work, it's not placing any blocks now.
     
    Last edited: May 24, 2015
  7. Offline

    Zombie_Striker

    @xDeeKay Are you sure those are the exact X,Y, and Z for the start/end? Try increasing /decreasing the value.

    [edit] REMOVE THE '=' FROM 'x/y/z <= max'. That is what is messing you up.
     
    Last edited: May 24, 2015
  8. Offline

    xDeeKay

    @Zombie_Striker I didn't use <= in my code, unless you're talking about the code SrNicks_ posted?
     
  9. Offline

    Zombie_Striker

    @xDeeKay Then just increas the number or decrease it (x -1< max || x+1 < max)
     
  10. Offline

    xDeeKay

    @Zombie_Striker I've just tried this, and although it worked with something like a 5x5 area, it didn't seem to work on a 15x15 area for some reason, which has me totally baffled.

    On line 18 and 19 I changed:
    Code:
            for (int x = xmin; x < xmax +1; x++) {
                for (int z = zmin -1; z < zmax; z++) {
     
  11. Offline

    Zombie_Striker

    Can you debug?
    Code:
    System.out.println("xmax "+xmax);
    System.out.println("zmax "+zmax);
    System.out.println("x "+x);
    what is xmax and zmax? If you were to print out all the 'x' possible, would they ever go over/stop before it reaches xmax?
     
  12. Offline

    567legodude

    @xDeeKay Try this and see if it works.
    Code:
    public static void fillCuboid(int x1, int y1, int z1, int x2, int y2, int z2, World world, Material block) {
        int a, b = 0;
        a = x1;
        b = x2;
        x1 = Math.min(a, b);
        x2 = Math.max(a, b);
        a = y1;
        b = y2;
        y1 = Math.min(a, b);
        y2 = Math.max(a, b);
        a = z1;
        b = z2;
        z1 = Math.min(a, b);
        z2 = Math.max(a, b);
        for (int x = x1; x <= x2; x++) {
            for (int y = y1; y <= y2; y++) {
                for (int z = z1; z <= z2; z++) {
                    world.getBlockAt(x, y, z).setType(block);
                }
            }
        }
    }
     
  13. Offline

    xDeeKay

    @567legodude Thanks, I just tried this, but the same thing is happening. It's still being offset 1 way.

    I should mention I have tried storing my x and z locations with +0.5 to get the center of the block, and I have also stored the locations without +0.5.

    This picture is before running the code. It's a 30x30 platform.
    The bottom right green wool is position 1 at x: 2.5, y: 45, and z: -15.5
    The top left green wool is position 2 at x: -26.5, y: 45, and z: -44.5
    http://i.imgur.com/gj8uxgA.jpg

    This picture is after running the code.
    http://i.imgur.com/VyYstvq.jpg

    As seen, the side closest to me is being set by 1 more block.
    Any ideas?
     
  14. Offline

    Zombie_Striker

    @xDeeKay
    Subtract that dimension (x or z) by 1. The problem you have been having this whole time is that you are not putting in the correct beginning/end concordance. That is why it was missing a space and why it is now going over.
     
  15. Offline

    xDeeKay

    @Zombie_Striker Can you elaborate, am I subtracting 1 when storing the locations, or while resetting the floor?
     
  16. Offline

    Zombie_Striker

    @xDeeKay
    From what it looks like, you need to subtract one period. It does not matter where. You need to subtract one when you store it. Whats happening is you are adding +1 to that side, which is why it's causing the error. Make sure you have the exact concordance of t

    Can you take a screengrab of the XYZ at one corner of the area you want filled, and the XYZ of the other corner. So I can see exactly what you should put for the X1Y1Z1 and the X2Y2Z2.
     
  17. Offline

    567legodude

    @xDeeKay I'm a bit late to the conversation, but it's failing because you are not giving it the right coordinates to begin with, the method works perfectly fine. You just need to make sure to put the exact coordinates of the blocks to be filled. Without adding or subtracting.

    Edit: One thing you can try is having the server send you a message with the coordinates whenever you click a block.
     
  18. Offline

    xDeeKay

    @Zombie_Striker @567legodude Aha, I think I got it now. I'm now storing the locations as they are, but adding +1 to x and z when running the reset method, I believe this works the same as you suggested. Thanks guys
     
Thread Status:
Not open for further replies.

Share This Page