"Region" General API for creating 'cuboids'

Discussion in 'Plugin Development' started by pyraetos, Aug 31, 2011.

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

    pyraetos

    Will add more methods soon, but this is what I've got so far:
    Region (open)

    Code:java
    1. package org.nerdycast.RegionCreator;
    2. import org.bukkit.Location;
    3. import org.bukkit.Material;
    4. import org.bukkit.World;
    5.  
    6. public class Region {
    7. /* API for creating Regions
    8.   * like WorldEdit cuboids.
    9.   * author = pyraetos
    10.   */
    11.  
    12. private Location loc1;
    13. private Location loc2;
    14. private World world;
    15.  
    16. public Region(Location loc1, Location loc2, World world){
    17. this.loc1 = loc1;
    18. this.loc2 = loc2;
    19. this.world = world;
    20. }
    21.  
    22. public void set(Material material){
    23.  
    24. double a[] = {Math.floor(loc1.getX()), Math.floor(loc1.getY()), Math.floor(loc1.getZ())};
    25. double b[] = {Math.floor(loc2.getX()), Math.floor(loc2.getY()), Math.floor(loc2.getZ())};
    26.  
    27. for(int xcounter = (int)Math.min(a[0], b[0]);xcounter<=Math.max(a[0], b[0]);xcounter++){
    28. for(int ycounter = (int)Math.min(a[1], b[1]); ycounter<=Math.max(a[1], b[1]);ycounter++){
    29. for(int zcounter = (int)Math.min(a[2], b[2]);zcounter <= Math.max(a[2], b[2]);zcounter++){
    30. Location loc = new Location(world, xcounter, ycounter, zcounter);
    31. loc.getBlock().setType(material);
    32. }
    33. }
    34. }
    35. }
    36. }
    37.  

    Feel free to use this in any plugin! Just leave author pyraetos.
    Be sure to comment on how you like it.
    P.S. I believe this is in the right category since it has to do wiht plugin development.
     
  2. Offline

    bergerkiller

    @pyraetos and others: Please stop storing cuboids as two locations. I know you will eventually use two locations to generate cuboids, but this is only at RUNTIME. Plus, why store yaw, pitch, World and 3x4 extra bytes 2x extra? You only need blockx, y and z, and a single World. So here it is, can't be simpler...

    Code:
    package com.bergerkiller.bukkit.prisonerrpg;
    
    import org.bukkit.Location;
    
    public class Cuboid {
        public Cuboid(Location point1, Location point2) {
            this.xMin = Math.min(point1.getBlockX(), point2.getBlockX());
            this.xMax = Math.max(point1.getBlockX(), point2.getBlockX());
            this.yMin = Math.min(point1.getBlockY(), point2.getBlockY());
            this.yMax = Math.max(point1.getBlockY(), point2.getBlockY());
            this.zMin = Math.min(point1.getBlockZ(), point2.getBlockZ());
            this.zMax = Math.max(point1.getBlockZ(), point2.getBlockZ());
            this.world = point1.getWorld();
        }
    
        public int xMin, xMax, yMin, yMax, zMin, zMax;
        public World world;
    
        public boolean isIn(Location loc) {
            if (loc.getWorld() != this.world) return false;
            if (loc.getBlockX() < xMin) return false;
            if (loc.getBlockX() > xMax) return false;
            if (loc.getBlockY() < yMin) return false;
            if (loc.getBlockY() > yMax) return false;
            if (loc.getBlockZ() < zMin) return false;
            if (loc.getBlockZ() > zMax) return false;
            return true;
        }
        public int getXWidth() {
            return xMax - xMin;
        }
        public int getZWidth() {
            return zMax - zMin;
        }
        public int getHeight() {
            return yMax - yMin;
        }
        public int getArea() {
            return getHeight() * getXWidth() * getZWidth();
        }
    }
    Also, this should go into Resources, not plugin development. Don't feel attacked, I only want to prevent developers from using these types of storage. You will eventually end up saving and loading, and you'll have to do the same isIn operation every time. Mine can simply check if the x/y/z is in between two bounds, seems simpler...
     
  3. Offline

    cholo71796

  4. Or Regios hehe :p
    i think it'd be fair to assume that people would just extract the x, y, z and world from the locations instead of trying to save the location as a raw type.
     
  5. Offline

    bergerkiller

    @Adamki11s but they still use the same min/max algoritm every single time they want to know if a player or block is in the cuboid. It might be insignificant, but why let that be? Also, byte RAM usage difference:
    2x Location = 2 * (4 + 8 + 8 + 8 + 4 + 4) = 72 bytes
    my Cuboid = 6 * 4 + 4 = 28 bytes
    I do see a significant difference there. Also, you can easily get all 8 edges of the cuboid using this, with two locations you would have to do the same min/max.

    In short: mine stores the result of the min/max operation, requiring less calculations in the future, where you would have to do the same min/max if you used two Locations.
     
  6. That's true. But the question is this slight performance increase even necessary? In my Regios plugin I have a cache of chunk coordinates in and surrounding a region and then check through those coordinates to exclude regions from the set. Even if I had 100 regions I reckon the increase in efficiency would be negligible. I'm not trying to start an argument here by the way, just explaining my viewpoint.
     
  7. Offline

    bergerkiller

    I understand, but I, on the other hand, see every possible way to improve performance a way to improve performance. And it helps, if I didn't do that my trains would be buggy like hell... :D

    Not sure but maybe it does play a part when we deal with 10000 regions? For example, a multi-world plugin with on every world a huge city full of player homes... :p
     
  8. Yeah, Before I release the recode of regions I'm going to see how long it takes the move event to execute ;) I'm an efficiency freak too :p
     
Thread Status:
Not open for further replies.

Share This Page