[Gelöst] Blöcke einer Welt

Wenn ihr bei eurem Plugin nicht weiter kommt, könnt ihr hier euer Problem beschreiben und um Hilfe bitten.

[Gelöst] Blöcke einer Welt

Beitragvon Onemeter » Fr 22. Apr 2016, 13:57

Sehr geehrte PostCrafter Community,

ich wollte heute mal nachfragen wie / ob es möglich ist aus einer Welt die Blöcke komplett beim Laden auszulesen um bestimmte Blöcke zu filtern und deren Location zu bekommen.
Zuletzt geändert von Onemeter am So 24. Apr 2016, 18:57, insgesamt 1-mal geändert.
Bei Fragen stehe ich gerne weiter zur verfügung und verbleibe mit freundlichen Grüßen,
Onemeter
Benutzeravatar
Onemeter
 
Beiträge: 131
Registriert: So 23. Jun 2013, 16:55
Wohnort: Bayern

Re: Blöcke einer Welt

Beitragvon Letsplaybar » Fr 22. Apr 2016, 14:56

klar in dem du eine Region erstellst mit der Cuboid klasse in der die gesammte Welt drinnen ist. beachte aber das er nur geladene Chunks durchsehen kann d.h. am besten im Move event vom Spieler einen bestimmten Radius durchsuchen. !!!BEACHTE DAS WIRD SEHR VIEL RESSOURCEN KOSTEN!!!

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

I hacked 127.0.0.1!!! :)
Benutzeravatar
Letsplaybar
 
Beiträge: 195
Registriert: Mo 1. Feb 2016, 17:21
Wohnort: Berlin

Re: Blöcke einer Welt

Beitragvon Onemeter » Sa 23. Apr 2016, 10:20

Letsplaybar hat geschrieben:klar in dem du eine Region erstellst mit der Cuboid klasse in der die gesammte Welt drinnen ist. beachte aber das er nur geladene Chunks durchsehen kann d.h. am besten im Move event vom Spieler einen bestimmten Radius durchsuchen. !!!BEACHTE DAS WIRD SEHR VIEL RESSOURCEN KOSTEN!!!

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

Danke für die Antwort, aber ich habe mit einem Freund schon eine einfachere Version geschrieben die durchaus weniger Leistung zieht aber auch den gewünschten Effekt hat. Vielen Dank trotzdem! :)
Bei Fragen stehe ich gerne weiter zur verfügung und verbleibe mit freundlichen Grüßen,
Onemeter
Benutzeravatar
Onemeter
 
Beiträge: 131
Registriert: So 23. Jun 2013, 16:55
Wohnort: Bayern


Zurück zu Hilfe und Support

Wer ist online?

Mitglieder in diesem Forum: Bing [Bot], Google [Bot], MSN [Bot] und 16 Gäste