Advertisement
Guest User

DEV' PLUGIN SPIGOT #19 - Cuboid

a guest
Aug 22nd, 2017
1,653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 27.56 KB | None | 0 0
  1. package fr.itsalexousd.uhchost.utils;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. import org.bukkit.Bukkit;
  10. import org.bukkit.Chunk;
  11. import org.bukkit.Location;
  12. import org.bukkit.World;
  13. import org.bukkit.block.Block;
  14. import org.bukkit.configuration.serialization.ConfigurationSerializable;
  15.  
  16. public class Cuboid implements Iterable<Block>, Cloneable, ConfigurationSerializable {
  17.    
  18.     /**
  19.      * This class is a region/cuboid from one location to another. It can be used for blocks protection and things like WorldEdit.
  20.      * @author desht (Original code), KingFaris10 (Editor of code)
  21.      */
  22.             protected final String worldName;
  23.             protected final int x1, y1, z1;
  24.             protected final int x2, y2, z2;
  25.      
  26.             /**
  27.              * Construct a Cuboid given two Location objects which represent any two corners of the Cuboid.
  28.              * Note: The 2 locations must be on the same world.
  29.              *
  30.              * @param l1 - One of the corners
  31.              * @param l2 - The other corner
  32.              */
  33.             public Cuboid(Location l1, Location l2) {
  34.                     if (!l1.getWorld().equals(l2.getWorld())) throw new IllegalArgumentException("Locations must be on the same world");
  35.                     this.worldName = l1.getWorld().getName();
  36.                     this.x1 = Math.min(l1.getBlockX(), l2.getBlockX());
  37.                     this.y1 = Math.min(l1.getBlockY(), l2.getBlockY());
  38.                     this.z1 = Math.min(l1.getBlockZ(), l2.getBlockZ());
  39.                     this.x2 = Math.max(l1.getBlockX(), l2.getBlockX());
  40.                     this.y2 = Math.max(l1.getBlockY(), l2.getBlockY());
  41.                     this.z2 = Math.max(l1.getBlockZ(), l2.getBlockZ());
  42.             }
  43.      
  44.             /**
  45.              * Construct a one-block Cuboid at the given Location of the Cuboid.
  46.              *
  47.              * @param l1 location of the Cuboid
  48.              */
  49.             public Cuboid(Location l1) {
  50.                     this(l1, l1);
  51.             }
  52.      
  53.             /**
  54.              * Copy constructor.
  55.              *
  56.              * @param other - The Cuboid to copy
  57.              */
  58.             public Cuboid(Cuboid other) {
  59.                     this(other.getWorld().getName(), other.x1, other.y1, other.z1, other.x2, other.y2, other.z2);
  60.             }
  61.      
  62.             /**
  63.              * Construct a Cuboid in the given World and xyz co-ordinates
  64.              *
  65.              * @param world - The Cuboid's world
  66.              * @param x1 - X co-ordinate of corner 1
  67.              * @param y1 - Y co-ordinate of corner 1
  68.              * @param z1 - Z co-ordinate of corner 1
  69.              * @param x2 - X co-ordinate of corner 2
  70.              * @param y2 - Y co-ordinate of corner 2
  71.              * @param z2 - Z co-ordinate of corner 2
  72.              */
  73.             public Cuboid(World world, int x1, int y1, int z1, int x2, int y2, int z2) {
  74.                     this.worldName = world.getName();
  75.                     this.x1 = Math.min(x1, x2);
  76.                     this.x2 = Math.max(x1, x2);
  77.                     this.y1 = Math.min(y1, y2);
  78.                     this.y2 = Math.max(y1, y2);
  79.                     this.z1 = Math.min(z1, z2);
  80.                     this.z2 = Math.max(z1, z2);
  81.             }
  82.      
  83.             /**
  84.              * Construct a Cuboid in the given world name and xyz co-ordinates.
  85.              *
  86.              * @param worldName - The Cuboid's world name
  87.              * @param x1 - X co-ordinate of corner 1
  88.              * @param y1 - Y co-ordinate of corner 1
  89.              * @param z1 - Z co-ordinate of corner 1
  90.              * @param x2 - X co-ordinate of corner 2
  91.              * @param y2 - Y co-ordinate of corner 2
  92.              * @param z2 - Z co-ordinate of corner 2
  93.              */
  94.             private Cuboid(String worldName, int x1, int y1, int z1, int x2, int y2, int z2) {
  95.                     this.worldName = worldName;
  96.                     this.x1 = Math.min(x1, x2);
  97.                     this.x2 = Math.max(x1, x2);
  98.                     this.y1 = Math.min(y1, y2);
  99.                     this.y2 = Math.max(y1, y2);
  100.                     this.z1 = Math.min(z1, z2);
  101.                     this.z2 = Math.max(z1, z2);
  102.             }
  103.      
  104.             /**
  105.              * Construct a Cuboid using a map with the following keys: worldName, x1, x2, y1, y2, z1, z2
  106.              * @param map - The map of keys.
  107.              */
  108.             public Cuboid(Map<String, Object> map) {
  109.                     this.worldName = (String) map.get("worldName");
  110.                     this.x1 = (Integer) map.get("x1");
  111.                     this.x2 = (Integer) map.get("x2");
  112.                     this.y1 = (Integer) map.get("y1");
  113.                     this.y2 = (Integer) map.get("y2");
  114.                     this.z1 = (Integer) map.get("z1");
  115.                     this.z2 = (Integer) map.get("z2");
  116.             }
  117.      
  118.             @Override
  119.             public Map<String, Object> serialize() {
  120.                     Map<String, Object> map = new HashMap<String, Object>();
  121.                     map.put("worldName", this.worldName);
  122.                     map.put("x1", this.x1);
  123.                     map.put("y1", this.y1);
  124.                     map.put("z1", this.z1);
  125.                     map.put("x2", this.x2);
  126.                     map.put("y2", this.y2);
  127.                     map.put("z2", this.z2);
  128.                     return map;
  129.             }
  130.      
  131.             /**
  132.              * Get the Location of the lower northeast corner of the Cuboid (minimum XYZ co-ordinates).
  133.              *
  134.              * @return Location of the lower northeast corner
  135.              */
  136.             public Location getLowerNE() {
  137.                     return new Location(this.getWorld(), this.x1, this.y1, this.z1);
  138.             }
  139.      
  140.             /**
  141.              * Get the Location of the upper southwest corner of the Cuboid (maximum XYZ co-ordinates).
  142.              *
  143.              * @return Location of the upper southwest corner
  144.              */
  145.             public Location getUpperSW() {
  146.                     return new Location(this.getWorld(), this.x2, this.y2, this.z2);
  147.             }
  148.      
  149.             /**
  150.              * Get the blocks in the Cuboid.
  151.              *
  152.              * @return The blocks in the Cuboid
  153.              */
  154.             public List<Block> getBlocks() {
  155.                     Iterator<Block> blockI = this.iterator();
  156.                     List<Block> copy = new ArrayList<Block>();
  157.                     while (blockI.hasNext())
  158.                             copy.add(blockI.next());
  159.                     return copy;
  160.             }
  161.      
  162.             /**
  163.              * Get the the centre of the Cuboid.
  164.              *
  165.              * @return Location at the centre of the Cuboid
  166.              */
  167.             public Location getCenter() {
  168.                     int x1 = this.getUpperX() + 1;
  169.                     int y1 = this.getUpperY() + 1;
  170.                     int z1 = this.getUpperZ() + 1;
  171.                     return new Location(this.getWorld(), this.getLowerX() + (x1 - this.getLowerX()) / 2.0, this.getLowerY() + (y1 - this.getLowerY()) / 2.0, this.getLowerZ() + (z1 - this.getLowerZ()) / 2.0);
  172.             }
  173.      
  174.             /**
  175.              * Get the Cuboid's world.
  176.              *
  177.              * @return The World object representing this Cuboid's world
  178.              * @throws IllegalStateException if the world is not loaded
  179.              */
  180.             public World getWorld() {
  181.                     World world = Bukkit.getWorld(this.worldName);
  182.                     if (world == null) throw new IllegalStateException("World '" + this.worldName + "' is not loaded");
  183.                     return world;
  184.             }
  185.      
  186.             /**
  187.              * Get the size of this Cuboid along the X axis
  188.              *
  189.              * @return      Size of Cuboid along the X axis
  190.              */
  191.             public int getSizeX() {
  192.                     return (this.x2 - this.x1) + 1;
  193.             }
  194.      
  195.             /**
  196.              * Get the size of this Cuboid along the Y axis
  197.              *
  198.              * @return      Size of Cuboid along the Y axis
  199.              */
  200.             public int getSizeY() {
  201.                     return (this.y2 - this.y1) + 1;
  202.             }
  203.      
  204.             /**
  205.              * Get the size of this Cuboid along the Z axis
  206.              *
  207.              * @return      Size of Cuboid along the Z axis
  208.              */
  209.             public int getSizeZ() {
  210.                     return (this.z2 - this.z1) + 1;
  211.             }
  212.      
  213.             /**
  214.              * Get the minimum X co-ordinate of this Cuboid
  215.              *
  216.              * @return      the minimum X co-ordinate
  217.              */
  218.             public int getLowerX() {
  219.                     return this.x1;
  220.             }
  221.      
  222.             /**
  223.              * Get the minimum Y co-ordinate of this Cuboid
  224.              *
  225.              * @return      the minimum Y co-ordinate
  226.              */
  227.             public int getLowerY() {
  228.                     return this.y1;
  229.             }
  230.      
  231.             /**
  232.              * Get the minimum Z co-ordinate of this Cuboid
  233.              *
  234.              * @return      the minimum Z co-ordinate
  235.              */
  236.             public int getLowerZ() {
  237.                     return this.z1;
  238.             }
  239.      
  240.             /**
  241.              * Get the maximum X co-ordinate of this Cuboid
  242.              *
  243.              * @return      the maximum X co-ordinate
  244.              */
  245.             public int getUpperX() {
  246.                     return this.x2;
  247.             }
  248.      
  249.             /**
  250.              * Get the maximum Y co-ordinate of this Cuboid
  251.              *
  252.              * @return      the maximum Y co-ordinate
  253.              */
  254.             public int getUpperY() {
  255.                     return this.y2;
  256.             }
  257.      
  258.             /**
  259.              * Get the maximum Z co-ordinate of this Cuboid
  260.              *
  261.              * @return      the maximum Z co-ordinate
  262.              */
  263.             public int getUpperZ() {
  264.                     return this.z2;
  265.             }
  266.      
  267.             /**
  268.              * Get the Blocks at the eight corners of the Cuboid.
  269.              *
  270.              * @return array of Block objects representing the Cuboid corners
  271.              */
  272.             public Block[] corners() {
  273.                     Block[] res = new Block[8];
  274.                     World w = this.getWorld();
  275.                     res[0] = w.getBlockAt(this.x1, this.y1, this.z1);
  276.                     res[1] = w.getBlockAt(this.x1, this.y1, this.z2);
  277.                     res[2] = w.getBlockAt(this.x1, this.y2, this.z1);
  278.                     res[3] = w.getBlockAt(this.x1, this.y2, this.z2);
  279.                     res[4] = w.getBlockAt(this.x2, this.y1, this.z1);
  280.                     res[5] = w.getBlockAt(this.x2, this.y1, this.z2);
  281.                     res[6] = w.getBlockAt(this.x2, this.y2, this.z1);
  282.                     res[7] = w.getBlockAt(this.x2, this.y2, this.z2);
  283.                     return res;
  284.             }
  285.      
  286.             /**
  287.              * Expand the Cuboid in the given direction by the given amount.  Negative amounts will shrink the Cuboid in the given direction.  Shrinking a cuboid's face past the opposite face is not an error and will return a valid Cuboid.
  288.              *
  289.              * @param dir - The direction in which to expand
  290.              * @param amount - The number of blocks by which to expand
  291.              * @return A new Cuboid expanded by the given direction and amount
  292.              */
  293.             public Cuboid expand(CuboidDirection dir, int amount) {
  294.                     switch (dir) {
  295.                     case North:
  296.                             return new Cuboid(this.worldName, this.x1 - amount, this.y1, this.z1, this.x2, this.y2, this.z2);
  297.                     case South:
  298.                             return new Cuboid(this.worldName, this.x1, this.y1, this.z1, this.x2 + amount, this.y2, this.z2);
  299.                     case East:
  300.                             return new Cuboid(this.worldName, this.x1, this.y1, this.z1 - amount, this.x2, this.y2, this.z2);
  301.                     case West:
  302.                             return new Cuboid(this.worldName, this.x1, this.y1, this.z1, this.x2, this.y2, this.z2 + amount);
  303.                     case Down:
  304.                             return new Cuboid(this.worldName, this.x1, this.y1 - amount, this.z1, this.x2, this.y2, this.z2);
  305.                     case Up:
  306.                             return new Cuboid(this.worldName, this.x1, this.y1, this.z1, this.x2, this.y2 + amount, this.z2);
  307.                     default:
  308.                             throw new IllegalArgumentException("Invalid direction " + dir);
  309.                     }
  310.             }
  311.      
  312.             /**
  313.              * Shift the Cuboid in the given direction by the given amount.
  314.              *
  315.              * @param dir - The direction in which to shift
  316.              * @param amount - The number of blocks by which to shift
  317.              * @return A new Cuboid shifted by the given direction and amount
  318.              */
  319.             public Cuboid shift(CuboidDirection dir, int amount) {
  320.                     return expand(dir, amount).expand(dir.opposite(), -amount);
  321.             }
  322.      
  323.             /**
  324.              * Outset (grow) the Cuboid in the given direction by the given amount.
  325.              *
  326.              * @param dir - The direction in which to outset (must be Horizontal, Vertical, or Both)
  327.              * @param amount - The number of blocks by which to outset
  328.              * @return A new Cuboid outset by the given direction and amount
  329.              */
  330.             public Cuboid outset(CuboidDirection dir, int amount) {
  331.                     Cuboid c;
  332.                     switch (dir) {
  333.                     case Horizontal:
  334.                             c = expand(CuboidDirection.North, amount).expand(CuboidDirection.South, amount).expand(CuboidDirection.East, amount).expand(CuboidDirection.West, amount);
  335.                             break;
  336.                     case Vertical:
  337.                             c = expand(CuboidDirection.Down, amount).expand(CuboidDirection.Up, amount);
  338.                             break;
  339.                     case Both:
  340.                             c = outset(CuboidDirection.Horizontal, amount).outset(CuboidDirection.Vertical, amount);
  341.                             break;
  342.                     default:
  343.                             throw new IllegalArgumentException("Invalid direction " + dir);
  344.                     }
  345.                     return c;
  346.             }
  347.      
  348.             /**
  349.              * Inset (shrink) the Cuboid in the given direction by the given amount.  Equivalent
  350.              * to calling outset() with a negative amount.
  351.              *
  352.              * @param dir - The direction in which to inset (must be Horizontal, Vertical, or Both)
  353.              * @param amount - The number of blocks by which to inset
  354.              * @return A new Cuboid inset by the given direction and amount
  355.              */
  356.             public Cuboid inset(CuboidDirection dir, int amount) {
  357.                     return this.outset(dir, -amount);
  358.             }
  359.      
  360.             /**
  361.              * Return true if the point at (x,y,z) is contained within this Cuboid.
  362.              *
  363.              * @param x     - The X co-ordinate
  364.              * @param y     - The Y co-ordinate
  365.              * @param z     - The Z co-ordinate
  366.              * @return true if the given point is within this Cuboid, false otherwise
  367.              */
  368.             public boolean contains(int x, int y, int z) {
  369.                     return x >= this.x1 && x <= this.x2 && y >= this.y1 && y <= this.y2 && z >= this.z1 && z <= this.z2;
  370.             }
  371.      
  372.             /**
  373.              * Check if the given Block is contained within this Cuboid.
  374.              *
  375.              * @param b     - The Block to check for
  376.              * @return true if the Block is within this Cuboid, false otherwise
  377.              */
  378.             public boolean contains(Block b) {
  379.                     return this.contains(b.getLocation());
  380.             }
  381.      
  382.             /**
  383.              * Check if the given Location is contained within this Cuboid.
  384.              *
  385.              * @param l     - The Location to check for
  386.              * @return true if the Location is within this Cuboid, false otherwise
  387.              */
  388.             public boolean contains(Location l) {
  389.                     if (!this.worldName.equals(l.getWorld().getName())) return false;
  390.                     return this.contains(l.getBlockX(), l.getBlockY(), l.getBlockZ());
  391.             }
  392.      
  393.             /**
  394.              * Get the volume of this Cuboid.
  395.              *
  396.              * @return The Cuboid volume, in blocks
  397.              */
  398.             public int getVolume() {
  399.                     return this.getSizeX() * this.getSizeY() * this.getSizeZ();
  400.             }
  401.      
  402.             /**
  403.              * Get the average light level of all empty (air) blocks in the Cuboid.  Returns 0 if there are no empty blocks.
  404.              *
  405.              * @return The average light level of this Cuboid
  406.              */
  407.             public byte getAverageLightLevel() {
  408.                     long total = 0;
  409.                     int n = 0;
  410.                     for (Block b : this) {
  411.                             if (b.isEmpty()) {
  412.                                     total += b.getLightLevel();
  413.                                     ++n;
  414.                             }
  415.                     }
  416.                     return n > 0 ? (byte) (total / n) : 0;
  417.             }
  418.      
  419.             /**
  420.              * Contract the Cuboid, returning a Cuboid with any air around the edges removed, just large enough to include all non-air blocks.
  421.              *
  422.              * @return A new Cuboid with no external air blocks
  423.              */
  424.             public Cuboid contract() {
  425.                     return this.contract(CuboidDirection.Down).contract(CuboidDirection.South).contract(CuboidDirection.East).contract(CuboidDirection.Up).contract(CuboidDirection.North).contract(CuboidDirection.West);
  426.             }
  427.      
  428.             /**
  429.              * Contract the Cuboid in the given direction, returning a new Cuboid which has no exterior empty space.
  430.              * E.g. A direction of Down will push the top face downwards as much as possible.
  431.              *
  432.              * @param dir - The direction in which to contract
  433.              * @return A new Cuboid contracted in the given direction
  434.              */
  435.             public Cuboid contract(CuboidDirection dir) {
  436.                     Cuboid face = getFace(dir.opposite());
  437.                     switch (dir) {
  438.                     case Down:
  439.                             while (face.containsOnly(0) && face.getLowerY() > this.getLowerY()) {
  440.                                     face = face.shift(CuboidDirection.Down, 1);
  441.                             }
  442.                             return new Cuboid(this.worldName, this.x1, this.y1, this.z1, this.x2, face.getUpperY(), this.z2);
  443.                     case Up:
  444.                             while (face.containsOnly(0) && face.getUpperY() < this.getUpperY()) {
  445.                                     face = face.shift(CuboidDirection.Up, 1);
  446.                             }
  447.                             return new Cuboid(this.worldName, this.x1, face.getLowerY(), this.z1, this.x2, this.y2, this.z2);
  448.                     case North:
  449.                             while (face.containsOnly(0) && face.getLowerX() > this.getLowerX()) {
  450.                                     face = face.shift(CuboidDirection.North, 1);
  451.                             }
  452.                             return new Cuboid(this.worldName, this.x1, this.y1, this.z1, face.getUpperX(), this.y2, this.z2);
  453.                     case South:
  454.                             while (face.containsOnly(0) && face.getUpperX() < this.getUpperX()) {
  455.                                     face = face.shift(CuboidDirection.South, 1);
  456.                             }
  457.                             return new Cuboid(this.worldName, face.getLowerX(), this.y1, this.z1, this.x2, this.y2, this.z2);
  458.                     case East:
  459.                             while (face.containsOnly(0) && face.getLowerZ() > this.getLowerZ()) {
  460.                                     face = face.shift(CuboidDirection.East, 1);
  461.                             }
  462.                             return new Cuboid(this.worldName, this.x1, this.y1, this.z1, this.x2, this.y2, face.getUpperZ());
  463.                     case West:
  464.                             while (face.containsOnly(0) && face.getUpperZ() < this.getUpperZ()) {
  465.                                     face = face.shift(CuboidDirection.West, 1);
  466.                             }
  467.                             return new Cuboid(this.worldName, this.x1, this.y1, face.getLowerZ(), this.x2, this.y2, this.z2);
  468.                     default:
  469.                             throw new IllegalArgumentException("Invalid direction " + dir);
  470.                     }
  471.             }
  472.      
  473.             /**
  474.              * Get the Cuboid representing the face of this Cuboid.  The resulting Cuboid will be one block thick in the axis perpendicular to the requested face.
  475.              *
  476.              * @param dir - which face of the Cuboid to get
  477.              * @return The Cuboid representing this Cuboid's requested face
  478.              */
  479.             public Cuboid getFace(CuboidDirection dir) {
  480.                     switch (dir) {
  481.                     case Down:
  482.                             return new Cuboid(this.worldName, this.x1, this.y1, this.z1, this.x2, this.y1, this.z2);
  483.                     case Up:
  484.                             return new Cuboid(this.worldName, this.x1, this.y2, this.z1, this.x2, this.y2, this.z2);
  485.                     case North:
  486.                             return new Cuboid(this.worldName, this.x1, this.y1, this.z1, this.x1, this.y2, this.z2);
  487.                     case South:
  488.                             return new Cuboid(this.worldName, this.x2, this.y1, this.z1, this.x2, this.y2, this.z2);
  489.                     case East:
  490.                             return new Cuboid(this.worldName, this.x1, this.y1, this.z1, this.x2, this.y2, this.z1);
  491.                     case West:
  492.                             return new Cuboid(this.worldName, this.x1, this.y1, this.z2, this.x2, this.y2, this.z2);
  493.                     default:
  494.                             throw new IllegalArgumentException("Invalid direction " + dir);
  495.                     }
  496.             }
  497.      
  498.             /**
  499.              * Check if the Cuboid contains only blocks of the given type
  500.              *
  501.              * @param blockId - The block ID to check for
  502.              * @return true if this Cuboid contains only blocks of the given type
  503.              */
  504.             @SuppressWarnings("deprecation")
  505.             public boolean containsOnly(int blockId) {
  506.                     for (Block b : this) {
  507.                             if (b.getTypeId() != blockId) return false;
  508.                     }
  509.                     return true;
  510.             }
  511.      
  512.             /**
  513.              * Get the Cuboid big enough to hold both this Cuboid and the given one.
  514.              *
  515.              * @param other - The other cuboid.
  516.              * @return A new Cuboid large enough to hold this Cuboid and the given Cuboid
  517.              */
  518.             public Cuboid getBoundingCuboid(Cuboid other) {
  519.                     if (other == null) return this;
  520.      
  521.                     int xMin = Math.min(this.getLowerX(), other.getLowerX());
  522.                     int yMin = Math.min(this.getLowerY(), other.getLowerY());
  523.                     int zMin = Math.min(this.getLowerZ(), other.getLowerZ());
  524.                     int xMax = Math.max(this.getUpperX(), other.getUpperX());
  525.                     int yMax = Math.max(this.getUpperY(), other.getUpperY());
  526.                     int zMax = Math.max(this.getUpperZ(), other.getUpperZ());
  527.      
  528.                     return new Cuboid(this.worldName, xMin, yMin, zMin, xMax, yMax, zMax);
  529.             }
  530.      
  531.             /**
  532.              * Get a block relative to the lower NE point of the Cuboid.
  533.              *
  534.              * @param x     - The X co-ordinate
  535.              * @param y     - The Y co-ordinate
  536.              * @param z     - The Z co-ordinate
  537.              * @return The block at the given position
  538.              */
  539.             public Block getRelativeBlock(int x, int y, int z) {
  540.                     return this.getWorld().getBlockAt(this.x1 + x, this.y1 + y, this.z1 + z);
  541.             }
  542.      
  543.             /**
  544.              * Get a block relative to the lower NE point of the Cuboid in the given World.  This
  545.              * version of getRelativeBlock() should be used if being called many times, to avoid
  546.              * excessive calls to getWorld().
  547.              *
  548.              * @param w     - The world
  549.              * @param x     - The X co-ordinate    
  550.              * @param y     - The Y co-ordinate    
  551.              * @param z     - The Z co-ordinate    
  552.              * @return The block at the given position
  553.              */
  554.             public Block getRelativeBlock(World w, int x, int y, int z) {
  555.                     return w.getBlockAt(this.x1 + x, y1 + y, this.z1 + z);
  556.             }
  557.      
  558.             /**
  559.              * Get a list of the chunks which are fully or partially contained in this cuboid.
  560.              *
  561.              * @return A list of Chunk objects
  562.              */
  563.             public List<Chunk> getChunks() {
  564.                     List<Chunk> res = new ArrayList<Chunk>();
  565.      
  566.                     World w = this.getWorld();
  567.                     int x1 = this.getLowerX() & ~0xf;
  568.                     int x2 = this.getUpperX() & ~0xf;
  569.                     int z1 = this.getLowerZ() & ~0xf;
  570.                     int z2 = this.getUpperZ() & ~0xf;
  571.                     for (int x = x1; x <= x2; x += 16) {
  572.                             for (int z = z1; z <= z2; z += 16) {
  573.                                     res.add(w.getChunkAt(x >> 4, z >> 4));
  574.                             }
  575.                     }
  576.                     return res;
  577.             }
  578.      
  579.             public Iterator<Block> iterator() {
  580.                     return new CuboidIterator(this.getWorld(), this.x1, this.y1, this.z1, this.x2, this.y2, this.z2);
  581.             }
  582.      
  583.             @Override
  584.             public Cuboid clone() {
  585.                     return new Cuboid(this);
  586.             }
  587.      
  588.             @Override
  589.             public String toString() {
  590.                     return new String("Cuboid: " + this.worldName + "," + this.x1 + "," + this.y1 + "," + this.z1 + "=>" + this.x2 + "," + this.y2 + "," + this.z2);
  591.             }
  592.      
  593.             public class CuboidIterator implements Iterator<Block> {
  594.                     private World w;
  595.                     private int baseX, baseY, baseZ;
  596.                     private int x, y, z;
  597.                     private int sizeX, sizeY, sizeZ;
  598.      
  599.                     public CuboidIterator(World w, int x1, int y1, int z1, int x2, int y2, int z2) {
  600.                             this.w = w;
  601.                             this.baseX = x1;
  602.                             this.baseY = y1;
  603.                             this.baseZ = z1;
  604.                             this.sizeX = Math.abs(x2 - x1) + 1;
  605.                             this.sizeY = Math.abs(y2 - y1) + 1;
  606.                             this.sizeZ = Math.abs(z2 - z1) + 1;
  607.                             this.x = this.y = this.z = 0;
  608.                     }
  609.      
  610.                     public boolean hasNext() {
  611.                             return this.x < this.sizeX && this.y < this.sizeY && this.z < this.sizeZ;
  612.                     }
  613.      
  614.                     public Block next() {
  615.                             Block b = this.w.getBlockAt(this.baseX + this.x, this.baseY + this.y, this.baseZ + this.z);
  616.                             if (++x >= this.sizeX) {
  617.                                     this.x = 0;
  618.                                     if (++this.y >= this.sizeY) {
  619.                                             this.y = 0;
  620.                                             ++this.z;
  621.                                     }
  622.                             }
  623.                             return b;
  624.                     }
  625.      
  626.                     public void remove() {
  627.                     }
  628.             }
  629.      
  630.             public enum CuboidDirection {
  631.                     North, East, South, West, Up, Down, Horizontal, Vertical, Both, Unknown;
  632.      
  633.                     public CuboidDirection opposite() {
  634.                             switch (this) {
  635.                             case North:
  636.                                     return South;
  637.                             case East:
  638.                                     return West;
  639.                             case South:
  640.                                     return North;
  641.                             case West:
  642.                                     return East;
  643.                             case Horizontal:
  644.                                     return Vertical;
  645.                             case Vertical:
  646.                                     return Horizontal;
  647.                             case Up:
  648.                                     return Down;
  649.                             case Down:
  650.                                     return Up;
  651.                             case Both:
  652.                                     return Both;
  653.                             default:
  654.                                     return Unknown;
  655.                             }
  656.                     }
  657.      
  658.             }
  659.            
  660. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement