[Util] LocationUtils (updated with more codes!)

Discussion in 'Resources' started by ArthurMaker, Nov 25, 2013.

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

    ArthurMaker

    I'm making this little library with the help of some tutorials that I found through the internet! You will can do circles, lines, cuboids and much more with it. Here is the code:

    Get it on GitHub!
    Click here!

    Here is a simple example that what you can do with it:

    CREDITS:
    - CaptainBern (cube code)
    - skore87 (getNearbyEntities code)
     
    Ironraptor3 and JPG2000 like this.
  2. Offline

    JPG2000

    ArthurMaker That example in the config was really neat! I'll mess around with this later.
     
    ArthurMaker likes this.
  3. Offline

    PVP9dc

    how can i do something with all locations or can you make a simple example for playing an effect in a circle?
     
  4. Offline

    ArthurMaker

    I used it for the example:
    (it is very primitive, you can improve it...)

    Code:java
    1. public int escudo = 0;
    2.  
    3. public int getEscudo(){
    4. return escudo;
    5. }
    6.  
    7. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    8.  
    9. final Player finalPlayer = (Player) sender;
    10.  
    11. if(cmd.getName().equalsIgnoreCase("teste")){
    12.  
    13. escudo = 0;
    14.  
    15. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    16. public void run(){
    17.  
    18. if(getEscudo() == 0){
    19. for(Location loc : LocationUtils.getCircle(finalPlayer.getLocation(), 12)){
    20. finalPlayer.playEffect(loc, Effect.EXTINGUISH, 5);
    21. finalPlayer.playEffect(loc, Effect.MOBSPAWNER_FLAMES, 5);
    22. }
    23. }
    24.  
    25. if(getEscudo() == 1){
    26. for(Location loc : LocationUtils.getCircle(finalPlayer.getLocation(), 12)){
    27. loc.setY((loc.getY()) + 2);
    28. finalPlayer.playEffect(loc, Effect.EXTINGUISH, 5);
    29. finalPlayer.playEffect(loc, Effect.MOBSPAWNER_FLAMES, 5);
    30. }
    31. }
    32.  
    33. if(getEscudo() == 2){
    34. for(Location loc : LocationUtils.getCircle(finalPlayer.getLocation(), 11)){
    35. loc.setY((loc.getY()) + 3);
    36. finalPlayer.playEffect(loc, Effect.EXTINGUISH, 5);
    37. finalPlayer.playEffect(loc, Effect.MOBSPAWNER_FLAMES, 5);
    38. }
    39. }
    40.  
    41. if(getEscudo() == 3){
    42. for(Location loc : LocationUtils.getCircle(finalPlayer.getLocation(), 10)){
    43. loc.setY((loc.getY()) + 4);
    44. finalPlayer.playEffect(loc, Effect.EXTINGUISH, 5);
    45. finalPlayer.playEffect(loc, Effect.MOBSPAWNER_FLAMES, 5);
    46. }
    47. }
    48.  
    49. if(getEscudo() == 4){
    50. for(Location loc : LocationUtils.getCircle(finalPlayer.getLocation(), 9)){
    51. loc.setY((loc.getY()) + 4);
    52. finalPlayer.playEffect(loc, Effect.EXTINGUISH, 5);
    53. finalPlayer.playEffect(loc, Effect.MOBSPAWNER_FLAMES, 5);
    54. }
    55. }
    56.  
    57. if(getEscudo() == 5){
    58. for(Location loc : LocationUtils.getCircle(finalPlayer.getLocation(), 8)){
    59. loc.setY((loc.getY()) + 5);
    60. finalPlayer.playEffect(loc, Effect.EXTINGUISH, 5);
    61. finalPlayer.playEffect(loc, Effect.MOBSPAWNER_FLAMES, 5);
    62. }
    63. }
    64.  
    65. if(getEscudo() == 6){
    66. for(Location loc : LocationUtils.getCircle(finalPlayer.getLocation(), 7)){
    67. loc.setY((loc.getY()) + 5);
    68. finalPlayer.playEffect(loc, Effect.EXTINGUISH, 5);
    69. finalPlayer.playEffect(loc, Effect.MOBSPAWNER_FLAMES, 5);
    70. }
    71. }
    72.  
    73. if(getEscudo() == 7){
    74. for(Location loc : LocationUtils.getCircle(finalPlayer.getLocation(), 6)){
    75. loc.setY((loc.getY()) + 5);
    76. finalPlayer.playEffect(loc, Effect.EXTINGUISH, 5);
    77. finalPlayer.playEffect(loc, Effect.MOBSPAWNER_FLAMES, 5);
    78. }
    79. }
    80.  
    81. if(getEscudo() == 8){
    82. Location enderloc = finalPlayer.getLocation();
    83. enderloc.setY((enderloc.getY()) + 6);
    84. finalPlayer.playEffect(enderloc, Effect.ENDER_SIGNAL, 5);
    85. for(Location loc : LocationUtils.getCircle(finalPlayer.getLocation(), 5)){
    86. loc.setY((loc.getY()) + 6);
    87. finalPlayer.playSound(loc, Sound.ENDERMAN_DEATH, 1.0F, 7.0F);
    88. finalPlayer.playSound(loc, Sound.EXPLODE, 0.3F, 3.5F);
    89. Effects.CLOUD.play(loc, 0, 0, 0, 0, 3);
    90. finalPlayer.playSound(loc, Sound.ORB_PICKUP, 0.5F, 10.0F);
    91. Effects.HAPPY_VILLAGER.play(loc, 0, 0, 0, 1, 5);
    92. }
    93. }
    94.  
    95. if(getEscudo() == 9){
    96. for(Location loc : LocationUtils.getCircle(finalPlayer.getLocation(), 7)){
    97. loc.setY((loc.getY()) + 6);
    98. finalPlayer.playSound(loc, Sound.BAT_TAKEOFF, 0.3F, 5.0F);
    99. Effects.CLOUD.play(loc, 0, 0, 0, 0, 3);
    100. finalPlayer.playSound(loc, Sound.ORB_PICKUP, 0.5F, 10.0F);
    101. Effects.HAPPY_VILLAGER.play(loc, 0, 0, 0, 1, 5);
    102. }
    103. }
    104.  
    105. if(getEscudo() == 10){
    106. for(Location loc : LocationUtils.getCircle(finalPlayer.getLocation(), 8)){
    107. loc.setY((loc.getY()) + 5);
    108. finalPlayer.playSound(loc, Sound.BAT_IDLE, 0.3F, 2.5F);
    109. Effects.CLOUD.play(loc, 0, 0, 0, 0, 3);
    110. finalPlayer.playSound(loc, Sound.ORB_PICKUP, 0.5F, 10.0F);
    111. Effects.HAPPY_VILLAGER.play(loc, 0, 0, 0, 1, 5);
    112. Effects.ENCHANTMENT_TABLE.play(loc, 0, 0, 0, 1, 7);
    113. }
    114. }
    115.  
    116. if(getEscudo() == 11){
    117. for(Location loc : LocationUtils.getCircle(finalPlayer.getLocation(), 9)){
    118. loc.setY((loc.getY()) + 4);
    119. finalPlayer.playSound(loc, Sound.ORB_PICKUP, 0.5F, 10.0F);
    120. Effects.HAPPY_VILLAGER.play(loc, 0, 0, 0, 1, 5);
    121. finalPlayer.playSound(loc, Sound.FIZZ, 0.3F, 10.0F);
    122. Effects.FIREWORKS_SPARK.play(loc, 0, 0, 0, 0, 10);
    123. }
    124. }
    125.  
    126. if(getEscudo() == 12){
    127. for(Location loc : LocationUtils.getCircle(finalPlayer.getLocation(), 10)){
    128. loc.setY((loc.getY()) + 4);
    129. finalPlayer.playSound(loc, Sound.ORB_PICKUP, 0.5F, 10.0F);
    130. Effects.HAPPY_VILLAGER.play(loc, 0, 0, 0, 1, 5);
    131. finalPlayer.playSound(loc, Sound.CAT_PURR, 0.3F, 3.5F);
    132. Effects.FIREWORKS_SPARK.play(loc, 0, 0, 0, 0, 8);
    133. }
    134. }
    135.  
    136. if(getEscudo() == 13){
    137. for(Location loc : LocationUtils.getCircle(finalPlayer.getLocation(), 11)){
    138. loc.setY((loc.getY()) + 3);
    139. finalPlayer.playSound(loc, Sound.ORB_PICKUP, 0.5F, 10.0F);
    140. Effects.HAPPY_VILLAGER.play(loc, 0, 0, 0, 0, 3);
    141. finalPlayer.playSound(loc, Sound.CHICKEN_EGG_POP, 1.0F, 0.5F);
    142. Effects.FIREWORKS_SPARK.play(loc, 0, 0, 0, 1, 5);
    143. }
    144. }
    145.  
    146. if(getEscudo() == 14){
    147. for(Location loc : LocationUtils.getCircle(finalPlayer.getLocation(), 12)){
    148. loc.setY((loc.getY()) + 2);
    149. finalPlayer.playSound(loc, Sound.ORB_PICKUP, 0.5F, 10.0F);
    150. Effects.HAPPY_VILLAGER.play(loc, 0, 0, 0, 0, 1);
    151. Effects.FIREWORKS_SPARK.play(loc, 0, 0, 0, 0, 2);
    152. Effects.MOB_SPELL.play(loc, 0, 0, 0, 3, 2);
    153. }
    154. escudo = ((escudo) + 1);
    155. }
    156.  
    157. if(getEscudo() == 15){
    158. for(Location loc : LocationUtils.getCircle(finalPlayer.getLocation(), 12)){
    159. finalPlayer.playSound(loc, Sound.EXPLODE, 0.5F, 3.5F);
    160. Effects.CLOUD.play(loc, 0, 0, 0, 0, 5);
    161. Effects.MOB_SPELL.play(loc, 0, 2, 0, 3, 2);
    162. }
    163. Effects.HUGE_EXPLOSION.play(finalPlayer.getLocation(), 0, 1, 0, 0, 1);
    164. for(Entity mobs : finalPlayer.getNearbyEntities(13.0, 6.0, 13.0)){
    165. if(!(mobs == finalPlayer)){
    166. LivingEntity mob = (LivingEntity)mobs;
    167. mob.getWorld().playEffect(mob.getLocation(), Effect.EXTINGUISH, 5);
    168. mob.getWorld().playEffect(mob.getLocation(), Effect.MOBSPAWNER_FLAMES, 5);
    169. mob.setHealth(0.0);
    170. }
    171. }
    172. Bukkit.getScheduler().cancelAllTasks();
    173. escudo = -1;
    174. }
    175. escudo = ((escudo) + 1);
    176. }
    177. }, 0, 6);
    178. }
    179. }
     
  5. ArthurMaker Use this for cubes:
    Code:java
    1. public static List<Location> calculateCube(Location start, Location end){
    2.  
    3. if(start.getWorld().getName() != end.getWorld().getName()){
    4. throw new UnsupportedOperationException("Start and end location need to be in the same world!");
    5. }
    6.  
    7. List<Location> locations = new ArrayList<Location>();
    8.  
    9. int minX = (int) Math.min(start.getX(), end.getX());
    10. int maxX = (int) Math.max(start.getX(), end.getX());
    11.  
    12. int minY = (int) Math.min(start.getY(), end.getY());
    13. int maxY = (int) Math.max(start.getY(), end.getY());
    14.  
    15. int minZ = (int) Math.min(start.getZ(), end.getZ());
    16. int maxZ = (int) Math.max(start.getZ(), end.getZ());
    17.  
    18. for(int x = minX; x <= maxX; x++){
    19. for(int y = minY; y <= maxY; y++){
    20. for(int z = minZ; z <= maxZ; z++){
    21. locations.add(new Location(start.getWorld(), x, y, z));
    22. }
    23. }
    24. }
    25. return locations;
    26. }
     
  6. Offline

    Pik0

    ArthurMaker Ahha adiciona-me skype podias me ajudar um bocadinho a começar numas coisas =D podiamos fazer uns plugins juntos:

    skype: pikopastor
     
  7. Offline

    ArthurMaker


    Hey thanks! :D
    I'll put it into the library. o/

    Ok! Vou te adicionar, cara. ;)
     
  8. Offline

    xTrollxDudex

    ArthurMaker
    That's just killing me, please use spaces so I can actually figure out what the code does/so no one needs to reformat this.
     
  9. Offline

    ArthurMaker

    I'm doing it right now and writing the new codes. ;)

    UPDATED!
    New codes! :3

    - getPlain();
    - getBlocks();
    - getLine();
    - getNearbyEntities().

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
    xTrollxDudex likes this.
  10. Offline

    xTrollxDudex

    ArthurMaker
    You could've made getNearbyEntities simpler :p
    PHP:
    public Set<EntitygetNearybyEntities(Location ldouble r) {
        
    Set<LivingEntityset = new HashSet<LivingEntity>();
        
    LivingEntity e = (LivingEntityl.getWorld().spawn(lBat.class);
        
    e.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBLITY1010);

        for(
    Entity ent e.getNearbyEntities(rrr)) {
            if(
    ent instance of LivingEntity) {
                
    set.add((LivingEntityent);
            }
        }
        
    e.remove()

        return 
    set;
    }
     
  11. Offline

    SoThatsIt

    shouldnt you e.remove() as well?
     
  12. Offline

    RainoBoy97

    I would give it slowness 100 too, just incase :p
    And e.remove() ;)
     
  13. Offline

    xTrollxDudex

    Oops. Yeah :p

    Also, slowness is not really needed, most of the time, Java can handle the amount of entities before the Bat is able to move or even show particles :p
     
  14. Offline

    RainoBoy97

    Fair enough!
     
  15. Offline

    bobacadodl

    You should also add spheres, cylinders, etc. Just look through worldedit's code for the algorithms.
     
  16. Offline

    ArthurMaker


    Yeah, I want to add more codes...
    Well, I'll add a code to get the location of a projectile's block, soon. ;)
     
  17. Offline

    bobacadodl


    Code:
    public List<Location> drawLine(Location start, Location end, double width, boolean filled){
            Vector pos1 = start.toVector();
            Vector pos2 = end.toVector();
            if(!start.getWorld().equals(end.getWorld())) return new ArrayList<Location>();
            World world = start.getWorld();
            Set<Location> vset = new HashSet<Location>();
     
            boolean notdrawn = true;
     
            int x1 = pos1.getBlockX(), y1 = pos1.getBlockY(), z1 = pos1.getBlockZ();
            int x2 = pos2.getBlockX(), y2 = pos2.getBlockY(), z2 = pos2.getBlockZ();
            int tipx = x1, tipy = y1, tipz = z1;
            int dx = Math.abs(x2 - x1), dy = Math.abs(y2 - y1), dz = Math.abs(z2 - z1);
     
            if (dx + dy + dz == 0) {
                vset.add(new Location(world,tipx, tipy, tipz));
                notdrawn = false;
            }
     
            if (Math.max(Math.max(dx, dy), dz) == dx && notdrawn) {
                for (int domstep = 0; domstep <= dx; domstep++) {
                    tipx = x1 + domstep * (x2 - x1 > 0 ? 1 : -1);
                    tipy = (int) Math.round(y1 + domstep * ((double) dy) / ((double) dx) * (y2 - y1 > 0 ? 1 : -1));
                    tipz = (int) Math.round(z1 + domstep * ((double) dz) / ((double) dx) * (z2 - z1 > 0 ? 1 : -1));
     
                    vset.add(new Location(world,tipx, tipy, tipz));
                }
                notdrawn = false;
            }
     
            if (Math.max(Math.max(dx, dy), dz) == dy && notdrawn) {
                for (int domstep = 0; domstep <= dy; domstep++) {
                    tipy = y1 + domstep * (y2 - y1 > 0 ? 1 : -1);
                    tipx = (int) Math.round(x1 + domstep * ((double) dx) / ((double) dy) * (x2 - x1 > 0 ? 1 : -1));
                    tipz = (int) Math.round(z1 + domstep * ((double) dz) / ((double) dy) * (z2 - z1 > 0 ? 1 : -1));
     
                    vset.add(new Location(world,tipx, tipy, tipz));
                }
                notdrawn = false;
            }
     
            if (Math.max(Math.max(dx, dy), dz) == dz && notdrawn) {
                for (int domstep = 0; domstep <= dz; domstep++) {
                    tipz = z1 + domstep * (z2 - z1 > 0 ? 1 : -1);
                    tipy = (int) Math.round(y1 + domstep * ((double) dy) / ((double) dz) * (y2-y1>0 ? 1 : -1));
                    tipx = (int) Math.round(x1 + domstep * ((double) dx) / ((double) dz) * (x2-x1>0 ? 1 : -1));
     
                    vset.add(new Location(world,tipx, tipy, tipz));
                }
                notdrawn = false;
            }
     
            vset = getBallooned(vset, width);
            if (!filled) {
                vset = getHollowed(vset);
            }
            return new ArrayList<Location>(vset);
        }
     
        public static List<Location> getCylinder(Location center, double radiusX, double radiusZ, int height, boolean filled) {
            Vector pos = center.toVector();
            World world = center.getWorld();
            List<Location> blocks = new ArrayList<Location>();
            radiusX += 0.5;
            radiusZ += 0.5;
     
            if (height == 0) {
                return blocks;
            } else if (height < 0) {
                height = -height;
                pos = pos.subtract(new Vector(0, height, 0));
            }
     
            if (pos.getBlockY() < 0) {
                pos = pos.setY(0);
            } else if (pos.getBlockY() + height - 1 > world.getMaxHeight()) {
                height = world.getMaxHeight() - pos.getBlockY() + 1;
            }
     
            final double invRadiusX = 1 / radiusX;
            final double invRadiusZ = 1 / radiusZ;
     
            final int ceilRadiusX = (int) Math.ceil(radiusX);
            final int ceilRadiusZ = (int) Math.ceil(radiusZ);
     
            double nextXn = 0;
            forX: for (int x = 0; x <= ceilRadiusX; ++x) {
                final double xn = nextXn;
                nextXn = (x + 1) * invRadiusX;
                double nextZn = 0;
                forZ: for (int z = 0; z <= ceilRadiusZ; ++z) {
                    final double zn = nextZn;
                    nextZn = (z + 1) * invRadiusZ;
     
                    double distanceSq = lengthSq(xn, zn);
                    if (distanceSq > 1) {
                        if (z == 0) {
                            break forX;
                        }
                        break forZ;
                    }
     
                    if (!filled) {
                        if (lengthSq(nextXn, zn) <= 1 && lengthSq(xn, nextZn) <= 1) {
                            continue;
                        }
                    }
     
                    for (int y = 0; y < height; ++y) {
     
                        blocks.add(pos.add(new Vector(x,y,z)).toLocation(world));
                        blocks.add(pos.add(new Vector(-x, y, z)).toLocation(world));
                        blocks.add(pos.add(new Vector(x,y,-z)).toLocation(world));
                        blocks.add(pos.add(new Vector(-x,y,-z)).toLocation(world));
                    }
                }
            }
     
            return blocks;
        }
     
        public static List<Location> getSphere(Location center, double radiusX, double radiusY, double radiusZ, boolean filled){
            Vector pos = center.toVector();
            World world = center.getWorld();
            List<Location> blocks = new ArrayList<Location>();
     
            radiusX += 0.5;
            radiusY += 0.5;
            radiusZ += 0.5;
     
            final double invRadiusX = 1 / radiusX;
            final double invRadiusY = 1 / radiusY;
            final double invRadiusZ = 1 / radiusZ;
     
            final int ceilRadiusX = (int) Math.ceil(radiusX);
            final int ceilRadiusY = (int) Math.ceil(radiusY);
            final int ceilRadiusZ = (int) Math.ceil(radiusZ);
     
            double nextXn = 0;
            forX: for (int x = 0; x <= ceilRadiusX; ++x) {
                final double xn = nextXn;
                nextXn = (x + 1) * invRadiusX;
                double nextYn = 0;
                forY: for (int y = 0; y <= ceilRadiusY; ++y) {
                    final double yn = nextYn;
                    nextYn = (y + 1) * invRadiusY;
                    double nextZn = 0;
                    forZ: for (int z = 0; z <= ceilRadiusZ; ++z) {
                        final double zn = nextZn;
                        nextZn = (z + 1) * invRadiusZ;
     
                        double distanceSq = lengthSq(xn, yn, zn);
                        if (distanceSq > 1) {
                            if (z == 0) {
                                if (y == 0) {
                                    break forX;
                                }
                                break forY;
                            }
                            break forZ;
                        }
                        if (!filled) {
                            if (lengthSq(nextXn, yn, zn) <= 1 && lengthSq(xn, nextYn, zn) <= 1 && lengthSq(xn, yn, nextZn) <= 1) {
                                continue;
                            }
                        }
                        blocks.add(pos.add(new Vector(x,y,z)).toLocation(world));
                        blocks.add(pos.add(new Vector(-x,y,z)).toLocation(world));
                        blocks.add(pos.add(new Vector(x,-y,z)).toLocation(world));
                        blocks.add(pos.add(new Vector(x,y,-z)).toLocation(world));
                        blocks.add(pos.add(new Vector(-x,-y,z)).toLocation(world));
                        blocks.add(pos.add(new Vector(x,-y,-z)).toLocation(world));
                        blocks.add(pos.add(new Vector(-x,y,-z)).toLocation(world));
                        blocks.add(pos.add(new Vector(-x,-y,-z)).toLocation(world));
                    }
                }
            }
     
            return blocks;
        }
     
        private static Set<Location> getBallooned(Set<Location> vset, double radius) {
            Set<Location> returnset = new HashSet<Location>();
            int ceilrad = (int) Math.ceil(radius);
     
            for (Location v : vset) {
                int tipx = v.getBlockX(), tipy = v.getBlockY(), tipz = v.getBlockZ();
     
                for (int loopx = tipx - ceilrad; loopx <= tipx + ceilrad; loopx++) {
                    for (int loopy = tipy - ceilrad; loopy <= tipy + ceilrad; loopy++) {
                        for (int loopz = tipz - ceilrad; loopz <= tipz + ceilrad; loopz++) {
                            if (hypot(loopx - tipx, loopy - tipy, loopz - tipz) <= radius) {
                                returnset.add(new Location(v.getWorld(),loopx, loopy, loopz));
                            }
                        }
                    }
                }
            }
            return returnset;
        }
     
        private static Set<Location> getHollowed(Set<Location> vset) {
            Set<Location> returnset = new HashSet<Location>();
            for (Location v : vset) {
                double x = v.getX(), y = v.getY(), z = v.getZ();
                if (!(vset.contains(new Location(v.getWorld(),x + 1, y, z)) &&
                        vset.contains(new Location(v.getWorld(),x - 1, y, z)) &&
                        vset.contains(new Location(v.getWorld(),x, y + 1, z)) &&
                        vset.contains(new Location(v.getWorld(),x, y - 1, z)) &&
                        vset.contains(new Location(v.getWorld(),x, y, z + 1)) &&
                        vset.contains(new Location(v.getWorld(),x, y, z - 1)))) {
                    returnset.add(v);
                }
            }
            return returnset;
        }
     
        private static final double lengthSq(double x, double y, double z) {
            return (x * x) + (y * y) + (z * z);
        }
     
        private static final double lengthSq(double x, double z) {
            return (x * x) + (z * z);
        }
     
        private static double hypot(double... pars) {
            double sum = 0;
            for (double d : pars) {
                sum += Math.pow(d, 2);
            }
            return Math.sqrt(sum);
        }
    There, converted some of worldedit's fancy stuff :p
    The line method lets you make hollow lines and have a radius
     
    comniemeer likes this.
  18. Offline

    SoThatsIt

    cylinders are just circles stacked on top of each other ;)

    example code for cylinder (only vertical but can easily be changed to horizontal, you would also have to change circle code to make vertical circles though)

    Code:
    public List<Location> getCylinder(Location centre, int radius, int height){
            List<Location> circle = getCircle(Location centerLoc, int radius);
            List<Location> cylinder = new ArrayList<Location>();
            for(Location loc : circle){//loop through all locations in circle
                for(int i=0; i<height; i++){//loop through all heights
                    cylinder.add(new Location(loc.getWorld(), loc.getX(), loc.getY()+i, loc.getZ()));
                }
            }
            return cylinder;
        }
    this is just a simpler, less hardware intensive, but less versatile way to make cylinders.
     
  19. Offline

    bobacadodl

    Yup, that's more efficient, but worldedit's method allows you do generate cylinders with different x and z radii, and hollow ones too :)
     
  20. Offline

    SoThatsIt

    as i said less versatile but cheaper on your hardware ;)
     
    bobacadodl likes this.
  21. Offline

    comniemeer

    bobacadodl
    The getCylinder-method does not work for me, it generates with the height of 1 only a quarter of a cylinder. Also, it generates some single blocks sticking out of the quarter with each one block AIR between them (approximately: [x][ ][x][ ][x][ ][x][ ]...here comes the quarter...).

    Could you add the option to fill the circle completely with your getCircle-method?
     
  22. Offline

    ArthurMaker

    comniemeer

    Code:java
    1. public static List<Location> getSphere(Location center, double radiusX, double radiusY, double radiusZ, boolean filled){
    2. Vector pos = center.toVector();
    3. World world = center.getWorld();
    4. List<Location> blocks = new ArrayList<Location>();
    5.  
    6. radiusX += 0.5;
    7. radiusY += 0.5;
    8. radiusZ += 0.5;
    9.  
    10. final double invRadiusX = 1 / radiusX;
    11. final double invRadiusY = 1 / radiusY;
    12. final double invRadiusZ = 1 / radiusZ;
    13.  
    14. final int ceilRadiusX = (int) Math.ceil(radiusX);
    15. final int ceilRadiusY = (int) Math.ceil(radiusY);
    16. final int ceilRadiusZ = (int) Math.ceil(radiusZ);
    17.  
    18. double nextXn = 0;
    19. forX: for (int x = 0; x <= ceilRadiusX; ++x) {
    20. final double xn = nextXn;
    21. nextXn = (x + 1) * invRadiusX;
    22. double nextYn = 0;
    23. forY: for (int y = 0; y <= ceilRadiusY; ++y) {
    24. final double yn = nextYn;
    25. nextYn = (y + 1) * invRadiusY;
    26. double nextZn = 0;
    27. forZ: for (int z = 0; z <= ceilRadiusZ; ++z) {
    28. final double zn = nextZn;
    29. nextZn = (z + 1) * invRadiusZ;
    30.  
    31. double distanceSq = lengthSq(xn, yn, zn);
    32. if (distanceSq > 1) {
    33. if (z == 0) {
    34. if (y == 0) {
    35. break forX;
    36. }
    37. break forY;
    38. }
    39. break forZ;
    40. }
    41. if (!filled) {
    42. if (lengthSq(nextXn, yn, zn) <= 1 && lengthSq(xn, nextYn, zn) <= 1 && lengthSq(xn, yn, nextZn) <= 1) {
    43. continue;
    44. }
    45. }
    46. blocks.add(pos.add(new Vector(x,y,z)).toLocation(world));
    47. blocks.add(pos.add(new Vector(-x,y,z)).toLocation(world));
    48. blocks.add(pos.add(new Vector(x,-y,z)).toLocation(world));
    49. blocks.add(pos.add(new Vector(x,y,-z)).toLocation(world));
    50. blocks.add(pos.add(new Vector(-x,-y,z)).toLocation(world));
    51. blocks.add(pos.add(new Vector(x,-y,-z)).toLocation(world));
    52. blocks.add(pos.add(new Vector(-x,y,-z)).toLocation(world));
    53. blocks.add(pos.add(new Vector(-x,-y,-z)).toLocation(world));
    54. }
    55. }
    56. }
    57.  
    58. return blocks;
    59. }


    Try to use X = Z, ok?

    Code:java
    1. public List<Location> getCylinder(Location centre, int radius, int height){
    2. List<Location> circle = getCircle(Location centerLoc, int radius);
    3. List<Location> cylinder = new ArrayList<Location>();
    4. for(Location loc : circle){//loop through all locations in circle
    5. for(int i=0; i<height; i++){//loop through all heights
    6. cylinder.add(new Location(loc.getWorld(), loc.getX(), loc.getY()+i, loc.getZ()));
    7. }
    8. }
    9. return cylinder;
    10. }
     
  23. Offline

    comniemeer

Thread Status:
Not open for further replies.

Share This Page