Improve WorldEdit encapsulation

This commit is contained in:
Brokkonaut
2018-07-31 17:58:31 +02:00
parent c73b29e43b
commit b80777a9a0
3 changed files with 49 additions and 27 deletions

View File

@@ -389,7 +389,7 @@ public class CommandsHandler implements CommandExecutor {
sender.sendMessage(ChatColor.RED + "You are not allowed to rollback more than " + rollbackMaxTime + " minutes");
return false;
}
if (rollbackMaxArea > 0 && (params.sel == null && params.loc == null || params.radius > rollbackMaxArea || params.sel != null && (params.sel.getSelection().getLength() > rollbackMaxArea || params.sel.getSelection().getWidth() > rollbackMaxArea))) {
if (rollbackMaxArea > 0 && (params.sel == null && params.loc == null || params.radius > rollbackMaxArea || params.sel != null && (params.sel.getSizeX() > rollbackMaxArea || params.sel.getSizeZ() > rollbackMaxArea))) {
sender.sendMessage(ChatColor.RED + "You are not allowed to rollback an area larger than " + rollbackMaxArea + " blocks");
return false;
}

View File

@@ -10,8 +10,6 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import java.util.*;
import static de.diddiz.LogBlock.Session.getSession;
@@ -349,9 +347,9 @@ public final class QueryParams implements Cloneable {
} else if (sel != null) {
compileLocationQuery(
where,
sel.getSelection().getMinimumPoint().getBlockX(), sel.getSelection().getMaximumPoint().getBlockX(),
sel.getSelection().getMinimumPoint().getBlockY(), sel.getSelection().getMaximumPoint().getBlockY(),
sel.getSelection().getMinimumPoint().getBlockZ(), sel.getSelection().getMaximumPoint().getBlockZ()
sel.getMinimumPoint().getBlockX(), sel.getMaximumPoint().getBlockX(),
sel.getMinimumPoint().getBlockY(), sel.getMaximumPoint().getBlockY(),
sel.getMinimumPoint().getBlockZ(), sel.getMaximumPoint().getBlockZ()
);
}
@@ -458,9 +456,9 @@ public final class QueryParams implements Cloneable {
} else if (sel != null) {
compileLocationQuery(
where,
sel.getSelection().getMinimumPoint().getBlockX(), sel.getSelection().getMaximumPoint().getBlockX(),
sel.getSelection().getMinimumPoint().getBlockY(), sel.getSelection().getMaximumPoint().getBlockY(),
sel.getSelection().getMinimumPoint().getBlockZ(), sel.getSelection().getMaximumPoint().getBlockZ()
sel.getMinimumPoint().getBlockX(), sel.getMaximumPoint().getBlockX(),
sel.getMinimumPoint().getBlockY(), sel.getMaximumPoint().getBlockY(),
sel.getMinimumPoint().getBlockZ(), sel.getMaximumPoint().getBlockZ()
);
}
@@ -774,7 +772,7 @@ public final class QueryParams implements Cloneable {
public void setSelection(RegionContainer container) {
this.sel = container;
world = BukkitAdapter.adapt(sel.getSelection().getWorld());
world = sel.getWorld();
}
public void setPlayer(String playerName) {

View File

@@ -2,7 +2,6 @@ package de.diddiz.worldedit;
import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.regions.CuboidRegion;
@@ -12,18 +11,29 @@ import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.BlockVector;
import org.bukkit.util.Vector;
public class RegionContainer implements Cloneable {
private Region selection;
private World world;
private BlockVector min = new BlockVector();
private BlockVector max = new BlockVector();
public RegionContainer(Region sel) {
this.selection = sel;
public RegionContainer(World world, Vector first, Vector second) {
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()));
}
public static RegionContainer fromPlayerSelection(Player player, Plugin plugin) {
LocalSession session = ((WorldEditPlugin) plugin).getSession(player);
com.sk89q.worldedit.world.World weWorld = BukkitAdapter.adapt(player.getWorld());
public static RegionContainer fromPlayerSelection(Player player, Plugin worldEditPlugin) {
LocalSession session = ((WorldEditPlugin) worldEditPlugin).getSession(player);
World world = player.getWorld();
com.sk89q.worldedit.world.World weWorld = BukkitAdapter.adapt(world);
if (!weWorld.equals(session.getSelectionWorld())) {
throw new IllegalArgumentException("No selection defined");
}
@@ -39,29 +49,43 @@ public class RegionContainer implements Cloneable {
if (!(selection instanceof CuboidRegion)) {
throw new IllegalArgumentException("You have to define a cuboid selection");
}
return new RegionContainer(selection);
com.sk89q.worldedit.Vector weMin = selection.getMinimumPoint();
com.sk89q.worldedit.Vector weMax = selection.getMaximumPoint();
Vector min = new Vector(weMin.getBlockX(), weMin.getBlockY(), weMin.getBlockZ());
Vector max = new Vector(weMax.getBlockX(), weMax.getBlockY(), weMax.getBlockZ());
return new RegionContainer(world, min, max);
}
public static RegionContainer fromCorners(World world, Location first, Location second) {
com.sk89q.worldedit.world.World weWorld = BukkitAdapter.adapt(world);
Vector firstVector = BukkitAdapter.asVector(first);
Vector secondVector = BukkitAdapter.asVector(second);
return new RegionContainer(new CuboidRegion(weWorld, firstVector, secondVector));
return new RegionContainer(world, first.toVector(), second.toVector());
}
public Region getSelection() {
return selection;
public World getWorld() {
return world;
}
public void setSelection(Region selection) {
this.selection = selection;
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;
}
@Override
public RegionContainer clone() {
try {
RegionContainer clone = (RegionContainer) super.clone();
clone.min = min.clone();
clone.max = max.clone();
return clone;
} catch (final CloneNotSupportedException ex) {
throw new Error("RegionContainer should be cloneable", ex);