Files
LogBlock/src/main/java/de/diddiz/worldedit/CuboidRegion.java
T

92 lines
3.2 KiB
Java
Raw Normal View History

package de.diddiz.worldedit;
2018-07-21 15:51:16 +02:00
import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.math.BlockVector3;
2018-07-21 15:51:16 +02:00
import com.sk89q.worldedit.regions.Region;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
2018-07-31 17:58:31 +02:00
import org.bukkit.util.BlockVector;
2018-07-31 18:00:28 +02:00
public class CuboidRegion implements Cloneable {
2018-07-31 17:58:31 +02:00
private World world;
private BlockVector min = new BlockVector();
private BlockVector max = new BlockVector();
public CuboidRegion(World world, BlockVector3 first, BlockVector3 second) {
2018-07-31 17:58:31 +02:00
this.world = world;
this.min.setX(Math.min(first.getBlockX(),second.getBlockX()));
this.min.setY(Math.min(first.getBlockY(),second.getBlockY()));
this.min.setZ(Math.min(first.getBlockZ(),second.getBlockZ()));
this.max.setX(Math.max(first.getBlockX(),second.getBlockX()));
this.max.setY(Math.max(first.getBlockY(),second.getBlockY()));
this.max.setZ(Math.max(first.getBlockZ(),second.getBlockZ()));
2015-03-22 20:15:04 +05:00
}
2018-07-31 18:00:28 +02:00
public static CuboidRegion fromPlayerSelection(Player player, Plugin worldEditPlugin) {
2018-07-31 17:58:31 +02:00
LocalSession session = ((WorldEditPlugin) worldEditPlugin).getSession(player);
World world = player.getWorld();
com.sk89q.worldedit.world.World weWorld = BukkitAdapter.adapt(world);
2018-07-21 15:51:16 +02:00
if (!weWorld.equals(session.getSelectionWorld())) {
throw new IllegalArgumentException("No selection defined");
}
Region selection;
try {
selection = session.getSelection(weWorld);
} catch (IncompleteRegionException e) {
throw new IllegalArgumentException("No selection defined");
}
2015-03-22 20:15:04 +05:00
if (selection == null) {
throw new IllegalArgumentException("No selection defined");
}
2018-07-31 18:00:28 +02:00
if (!(selection instanceof com.sk89q.worldedit.regions.CuboidRegion)) {
2015-03-22 20:15:04 +05:00
throw new IllegalArgumentException("You have to define a cuboid selection");
}
BlockVector3 min = selection.getMinimumPoint();
BlockVector3 max = selection.getMaximumPoint();
2018-07-31 18:00:28 +02:00
return new CuboidRegion(world, min, max);
2015-03-22 20:15:04 +05:00
}
2018-07-31 18:00:28 +02:00
public static CuboidRegion fromCorners(World world, Location first, Location second) {
return new CuboidRegion(world, BukkitAdapter.asBlockVector(first), BukkitAdapter.asBlockVector(second));
2015-03-22 20:15:04 +05:00
}
2018-07-31 17:58:31 +02:00
public World getWorld() {
return world;
2015-03-22 20:15:04 +05:00
}
2018-07-31 17:58:31 +02:00
public BlockVector getMinimumPoint() {
return min;
}
public BlockVector getMaximumPoint() {
return max;
}
public int getSizeX() {
return max.getBlockX() - min.getBlockX() + 1;
}
public int getSizeZ() {
return max.getBlockZ() - min.getBlockZ() + 1;
2015-03-22 20:15:04 +05:00
}
@Override
2018-07-31 18:00:28 +02:00
public CuboidRegion clone() {
try {
2018-07-31 18:00:28 +02:00
CuboidRegion clone = (CuboidRegion) super.clone();
2018-07-31 17:58:31 +02:00
clone.min = min.clone();
clone.max = max.clone();
return clone;
} catch (final CloneNotSupportedException ex) {
throw new Error("RegionContainer should be cloneable", ex);
}
}
}