Advertisement
Guest User

Bukkit | Faris - Cuboid class

a guest
Oct 24th, 2013
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.18 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import java.util.Map;
  6.  
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.Location;
  9. import org.bukkit.World;
  10. import org.bukkit.block.Block;
  11. import org.bukkit.configuration.serialization.ConfigurationSerializable;
  12. import org.bukkit.entity.Player;
  13.  
  14. public class Cuboid implements Iterable<Block>, Cloneable, ConfigurationSerializable {
  15.     protected String worldName;
  16.     protected final int xPos1, yPos1, zPos1;
  17.     protected final int xPos2, yPos2, zPos2;
  18.  
  19.     public Cuboid(Location loc) {
  20.         this(loc, loc);
  21.     }
  22.  
  23.     public Cuboid(Cuboid cuboid) {
  24.         this(cuboid.getWorld(false), cuboid.xPos1, cuboid.yPos1, cuboid.zPos1, cuboid.xPos2, cuboid.yPos2, cuboid.zPos2);
  25.     }
  26.  
  27.     public Cuboid(Location loc1, Location loc2) {
  28.         if (loc1 != null && loc2 != null) {
  29.             if (loc1.getWorld() != null && loc2.getWorld() != null) {
  30.                 if (!loc1.getWorld().equals(loc2.getWorld())) throw new IllegalStateException("The 2 locations of the cuboid must be in the same world!");
  31.             }
  32.             this.worldName = loc1.getWorld() != null ? loc1.getWorld().getName() : "";
  33.             this.xPos1 = Math.min(loc1.getBlockX(), loc2.getBlockX());
  34.             this.yPos1 = Math.min(loc1.getBlockY(), loc2.getBlockY());
  35.             this.zPos1 = Math.min(loc1.getBlockZ(), loc2.getBlockZ());
  36.             this.xPos2 = Math.max(loc1.getBlockX(), loc2.getBlockX());
  37.             this.yPos2 = Math.max(loc1.getBlockY(), loc2.getBlockY());
  38.             this.zPos2 = Math.max(loc1.getBlockZ(), loc2.getBlockZ());
  39.         } else {
  40.             this.worldName = "";
  41.             this.xPos1 = 0;
  42.             this.yPos1 = 0;
  43.             this.zPos1 = 0;
  44.             this.xPos2 = 0;
  45.             this.yPos2 = 0;
  46.             this.zPos2 = 0;
  47.         }
  48.     }
  49.  
  50.     public Cuboid(World world, int x1, int y1, int z1, int x2, int y2, int z2) {
  51.         this.worldName = world.getName();
  52.         this.xPos1 = Math.min(x1, x2);
  53.         this.xPos2 = Math.max(x1, x2);
  54.         this.yPos1 = Math.min(y1, y2);
  55.         this.yPos2 = Math.max(y1, y2);
  56.         this.zPos1 = Math.min(z1, z2);
  57.         this.zPos2 = Math.max(z1, z2);
  58.     }
  59.  
  60.     public Cuboid(Map<String, Object> map) {
  61.         this.worldName = (String) map.get("worldName");
  62.         this.xPos1 = (Integer) map.get("x1");
  63.         this.xPos2 = (Integer) map.get("x2");
  64.         this.yPos1 = (Integer) map.get("y1");
  65.         this.yPos2 = (Integer) map.get("y2");
  66.         this.zPos1 = (Integer) map.get("z1");
  67.         this.zPos2 = (Integer) map.get("z2");
  68.     }
  69.  
  70.     public List<Block> getBlocks() {
  71.         List<Block> blockList = new ArrayList<Block>();
  72.         World world = this.getWorld(true);
  73.         if (world != null) {
  74.             for (int x = this.xPos1; x <= this.xPos2; x++) {
  75.                 for (int y = this.yPos1; y <= this.yPos2; y++) {
  76.                     for (int z = this.zPos1; z <= this.zPos2; z++) {
  77.                         blockList.add(world.getBlockAt(x, y, z));
  78.                     }
  79.                 }
  80.             }
  81.             return blockList;
  82.         } else {
  83.             return new ArrayList<Block>();
  84.         }
  85.     }
  86.  
  87.     public List<Player> getPlayers() {
  88.         List<Player> playerList = new ArrayList<Player>();
  89.         World world = this.getWorld(true);
  90.         if (world != null) {
  91.             List<Player> worldPlayers = world.getPlayers();
  92.             for (int x = this.xPos1; x <= this.xPos2; x++) {
  93.                 for (int y = this.yPos1; y <= this.yPos2; y++) {
  94.                     for (int z = this.zPos1; z <= this.zPos2; z++) {
  95.                         for (Player player : worldPlayers) {
  96.                             Location pLoc = player.getLocation();
  97.                             if ((int) pLoc.getX() == x && (int) pLoc.getY() == y && (int) pLoc.getZ() == z) playerList.add(player);
  98.                         }
  99.                     }
  100.                 }
  101.             }
  102.             return playerList;
  103.         } else {
  104.             return new ArrayList<Player>();
  105.         }
  106.     }
  107.  
  108.     public int getLowerX() {
  109.         return this.xPos1;
  110.     }
  111.  
  112.     public int getLowerY() {
  113.         return this.yPos1;
  114.     }
  115.  
  116.     public int getLowerZ() {
  117.         return this.zPos1;
  118.     }
  119.  
  120.     public int getUpperX() {
  121.         return this.xPos2;
  122.     }
  123.  
  124.     public int getUpperY() {
  125.         return this.yPos2;
  126.     }
  127.  
  128.     public int getUpperZ() {
  129.         return this.zPos2;
  130.     }
  131.  
  132.     public int getVolume() {
  133.         return (this.xPos2 - this.xPos1 + 1) * (this.yPos2 - this.yPos1 + 1) * (this.zPos2 - this.zPos1 + 1);
  134.     }
  135.  
  136.     public Location getLowerLocation() {
  137.         return new Location(this.getWorld(false), this.xPos1, this.yPos1, this.zPos1);
  138.     }
  139.  
  140.     public Location getUpperLocation() {
  141.         return new Location(this.getWorld(false), this.xPos2, this.yPos2, this.zPos2);
  142.     }
  143.  
  144.     public World getWorld(boolean bypassErrors) {
  145.         World world = Bukkit.getWorld(this.worldName);
  146.         if (world == null) {
  147.             if (!bypassErrors) throw new IllegalStateException("World '" + this.worldName + "' is not loaded");
  148.         }
  149.         return world;
  150.     }
  151.  
  152.     public void setWorld(World world) {
  153.         if (world != null) this.worldName = world.getName();
  154.     }
  155.  
  156.     @Override
  157.     public Map<String, Object> serialize() {
  158.         Map<String, Object> map = new HashMap<String, Object>();
  159.         map.put("worldName", this.worldName);
  160.         map.put("x1", this.xPos1);
  161.         map.put("y1", this.yPos1);
  162.         map.put("z1", this.zPos1);
  163.         map.put("x2", this.xPos2);
  164.         map.put("y2", this.yPos2);
  165.         map.put("z2", this.zPos2);
  166.         return map;
  167.     }
  168.  
  169.     @Override
  170.     public Iterator<Block> iterator() {
  171.         return this.getBlocks().iterator();
  172.     }
  173.  
  174.     @Override
  175.     public Cuboid clone() {
  176.         return new Cuboid(this);
  177.     }
  178.  
  179.     @Override
  180.     public String toString() {
  181.         return this.worldName + ":" + this.xPos1 + " " + this.yPos1 + " " + this.zPos1 + ":" + this.xPos2 + " " + this.yPos2 + " " + this.zPos2;
  182.     }
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement