use of com.sk89q.worldedit.BlockVector in project TriggerReactor by wysohn.
the class WorldguardSupport method getRegion.
/**
* @param world
* @param regionName
* @return array of min/max locations. [0] for smallest, [1] for largest. Always length of 2.
* returns null if provided region name doesn't exists.
*/
public Location[] getRegion(World world, String regionName) {
Location[] locs = new Location[2];
ProtectedRegion region = wg.getRegionManager(world).getRegion(regionName);
if (region == null)
return null;
BlockVector min = region.getMinimumPoint();
BlockVector max = region.getMaximumPoint();
locs[0] = new Location(world, min.getX(), min.getY(), min.getZ());
locs[1] = new Location(world, max.getX(), max.getY(), max.getZ());
return locs;
}
use of com.sk89q.worldedit.BlockVector in project LandLord by SpatiumPrinceps.
the class WorldGuardHandler method claim.
public void claim(UUID owner, String landname, World world, Location down, Location upper) {
BlockVector vec1 = OwnedLand.locationToVec(down);
BlockVector vec2 = OwnedLand.locationToVec(upper);
ProtectedCuboidRegion pr = new ProtectedCuboidRegion(landname, vec1, vec2);
DefaultDomain ownerDomain = new DefaultDomain();
ownerDomain.addPlayer(owner);
pr.setOwners(ownerDomain);
// flag management
pr = setDefaultFlags(pr, down.getChunk());
RegionManager manager = wg.getRegionContainer().get(world);
if (manager != null) {
manager.addRegion(pr);
}
}
use of com.sk89q.worldedit.BlockVector in project AreaShop by NLthijs48.
the class Utils method getWorldEditRegionsInSelection.
/**
* Get all WorldGuard regions intersecting with a WorldEdit selection.
* @param selection The selection to check
* @return A list with all the WorldGuard regions intersecting with the selection
*/
public static List<ProtectedRegion> getWorldEditRegionsInSelection(Selection selection) {
// Get all regions inside or intersecting with the WorldEdit selection of the player
World world = selection.getWorld();
RegionManager regionManager = AreaShop.getInstance().getWorldGuard().getRegionManager(world);
ArrayList<ProtectedRegion> result = new ArrayList<>();
Location selectionMin = selection.getMinimumPoint();
Location selectionMax = selection.getMaximumPoint();
for (ProtectedRegion region : regionManager.getRegions().values()) {
BlockVector regionMin = region.getMinimumPoint();
BlockVector regionMax = region.getMaximumPoint();
if ((// x part, resolves to true if the selection and region overlap anywhere on the x-axis
(regionMin.getBlockX() <= selectionMax.getBlockX() && regionMin.getBlockX() >= selectionMin.getBlockX()) || (regionMax.getBlockX() <= selectionMax.getBlockX() && regionMax.getBlockX() >= selectionMin.getBlockX()) || (selectionMin.getBlockX() >= regionMin.getBlockX() && selectionMin.getBlockX() <= regionMax.getBlockX()) || (selectionMax.getBlockX() >= regionMin.getBlockX() && selectionMax.getBlockX() <= regionMax.getBlockX())) && (// Y part, resolves to true if the selection and region overlap anywhere on the y-axis
(regionMin.getBlockY() <= selectionMax.getBlockY() && regionMin.getBlockY() >= selectionMin.getBlockY()) || (regionMax.getBlockY() <= selectionMax.getBlockY() && regionMax.getBlockY() >= selectionMin.getBlockY()) || (selectionMin.getBlockY() >= regionMin.getBlockY() && selectionMin.getBlockY() <= regionMax.getBlockY()) || (selectionMax.getBlockY() >= regionMin.getBlockY() && selectionMax.getBlockY() <= regionMax.getBlockY())) && (// Z part, resolves to true if the selection and region overlap anywhere on the z-axis
(regionMin.getBlockZ() <= selectionMax.getBlockZ() && regionMin.getBlockZ() >= selectionMin.getBlockZ()) || (regionMax.getBlockZ() <= selectionMax.getBlockZ() && regionMax.getBlockZ() >= selectionMin.getBlockZ()) || (selectionMin.getBlockZ() >= regionMin.getBlockZ() && selectionMin.getBlockZ() <= regionMax.getBlockZ()) || (selectionMax.getBlockZ() >= regionMin.getBlockZ() && selectionMax.getBlockZ() <= regionMax.getBlockZ()))) {
result.add(region);
}
}
return result;
}
use of com.sk89q.worldedit.BlockVector in project AreaShop by NLthijs48.
the class GeneralRegion method calculateVolume.
/**
* Calculate the volume of the region (could be expensive for polygon regions).
* @return Number of blocks in the region
*/
private long calculateVolume() {
// Use own calculation for polygon regions, as WorldGuard does not implement it and returns 0
ProtectedRegion region = getRegion();
if (region instanceof ProtectedPolygonalRegion) {
BlockVector min = region.getMinimumPoint();
BlockVector max = region.getMaximumPoint();
// Exact, but slow algorithm
if (getWidth() * getDepth() < 100) {
long surface = 0;
for (int x = min.getBlockX(); x <= max.getBlockX(); x++) {
for (int z = min.getBlockZ(); z <= max.getBlockZ(); z++) {
if (region.contains(x, min.getBlockY(), z)) {
surface++;
}
}
}
return surface * getHeight();
} else // Estimate, but quick algorithm
{
List<BlockVector2D> points = region.getPoints();
int numPoints = points.size();
if (numPoints < 3) {
return 0;
}
double area = 0;
int x1, x2, z1, z2;
for (int i = 0; i <= numPoints - 2; i++) {
x1 = points.get(i).getBlockX();
z1 = points.get(i).getBlockZ();
x2 = points.get(i + 1).getBlockX();
z2 = points.get(i + 1).getBlockZ();
area = area + ((z1 + z2) * (x1 - x2));
}
x1 = points.get(numPoints - 1).getBlockX();
z1 = points.get(numPoints - 1).getBlockZ();
x2 = points.get(0).getBlockX();
z2 = points.get(0).getBlockZ();
area = area + ((z1 + z2) * (x1 - x2));
area = Math.ceil(Math.abs(area) / 2);
return (long) (area * getHeight());
}
} else {
return region.volume();
}
}
use of com.sk89q.worldedit.BlockVector in project AreaShop by NLthijs48.
the class StackCommand method execute.
@Override
public void execute(CommandSender sender, String[] args) {
// Check permission
if (!sender.hasPermission("areashop.stack")) {
plugin.message(sender, "stack-noPermission");
return;
}
// Only from ingame
if (!(sender instanceof Player)) {
plugin.message(sender, "cmd-onlyByPlayer");
return;
}
final Player player = (Player) sender;
// Specify enough arguments
if (args.length < 5) {
plugin.message(sender, "stack-help");
return;
}
// Check amount
int tempAmount = -1;
try {
tempAmount = Integer.parseInt(args[1]);
} catch (NumberFormatException e) {
// Incorrect number
}
if (tempAmount <= 0) {
plugin.message(player, "stack-wrongAmount", args[1]);
return;
}
// Check gap
int gap;
try {
gap = Integer.parseInt(args[2]);
} catch (NumberFormatException e) {
plugin.message(player, "stack-wrongGap", args[2]);
return;
}
// Check region type
if (!"rent".equalsIgnoreCase(args[4]) && !"buy".equalsIgnoreCase(args[4])) {
plugin.message(sender, "stack-help");
return;
}
// Get WorldEdit selection
final Selection selection = plugin.getWorldEdit().getSelection(player);
if (selection == null) {
plugin.message(player, "stack-noSelection");
return;
}
// Get or create group
RegionGroup group = null;
if (args.length > 5) {
group = plugin.getFileManager().getGroup(args[5]);
if (group == null) {
group = new RegionGroup(plugin, args[5]);
plugin.getFileManager().addGroup(group);
}
}
// Get facing of the player (must be clearly one of the four directions to make sure it is no mistake)
BlockFace facing = Utils.yawToFacing(player.getLocation().getYaw());
if (player.getLocation().getPitch() > 45) {
facing = BlockFace.DOWN;
} else if (player.getLocation().getPitch() < -45) {
facing = BlockFace.UP;
}
if (!(facing == BlockFace.NORTH || facing == BlockFace.EAST || facing == BlockFace.SOUTH || facing == BlockFace.WEST || facing == BlockFace.UP || facing == BlockFace.DOWN)) {
plugin.message(player, "stack-unclearDirection", facing.toString().toLowerCase().replace('_', '-'));
return;
}
Vector shift = new BlockVector(0, 0, 0);
if (facing == BlockFace.SOUTH) {
shift = shift.setZ(-selection.getLength() - gap);
} else if (facing == BlockFace.WEST) {
shift = shift.setX(selection.getWidth() + gap);
} else if (facing == BlockFace.NORTH) {
shift = shift.setZ(selection.getLength() + gap);
} else if (facing == BlockFace.EAST) {
shift = shift.setX(-selection.getWidth() - gap);
} else if (facing == BlockFace.DOWN) {
shift = shift.setY(-selection.getHeight() - gap);
} else if (facing == BlockFace.UP) {
shift = shift.setY(selection.getHeight() + gap);
}
AreaShop.debug(" calculated shift vector: " + shift + ", with facing=" + facing);
// Create regions and add them to AreaShop
final String nameTemplate = args[3];
final int regionsPerTick = plugin.getConfig().getInt("adding.regionsPerTick");
final boolean rentRegions = "rent".equalsIgnoreCase(args[4]);
final int amount = tempAmount;
final RegionGroup finalGroup = group;
final Vector finalShift = shift;
String type;
if (rentRegions) {
type = "rent";
} else {
type = "buy";
}
Message groupsMessage = Message.empty();
if (group != null) {
groupsMessage = Message.fromKey("stack-addToGroup").replacements(group.getName());
}
plugin.message(player, "stack-accepted", amount, type, gap, nameTemplate, groupsMessage);
plugin.message(player, "stack-addStart", amount, regionsPerTick * 20);
new BukkitRunnable() {
private int current = -1;
private RegionManager manager = AreaShop.getInstance().getWorldGuard().getRegionManager(selection.getWorld());
private int counter = 1;
private int tooLow = 0;
private int tooHigh = 0;
@Override
public void run() {
for (int i = 0; i < regionsPerTick; i++) {
current++;
if (current < amount) {
// Create the region name
String counterName = counter + "";
int minimumLength = plugin.getConfig().getInt("stackRegionNumberLength");
while (counterName.length() < minimumLength) {
counterName = "0" + counterName;
}
String regionName;
if (nameTemplate.contains("#")) {
regionName = nameTemplate.replace("#", counterName);
} else {
regionName = nameTemplate + counterName;
}
while (manager.getRegion(regionName) != null || AreaShop.getInstance().getFileManager().getRegion(regionName) != null) {
counter++;
counterName = counter + "";
minimumLength = plugin.getConfig().getInt("stackRegionNumberLength");
while (counterName.length() < minimumLength) {
counterName = "0" + counterName;
}
if (nameTemplate.contains("#")) {
regionName = nameTemplate.replace("#", counterName);
} else {
regionName = nameTemplate + counterName;
}
}
// Add the region to WorldGuard (at startposition shifted by the number of this region times the blocks it should shift)
BlockVector minimum = new BlockVector(selection.getNativeMinimumPoint().add(finalShift.multiply(current)));
BlockVector maximum = new BlockVector(selection.getNativeMaximumPoint().add(finalShift.multiply(current)));
// Check for out of bounds
if (minimum.getBlockY() < 0) {
tooLow++;
continue;
} else if (maximum.getBlockY() > 256) {
tooHigh++;
continue;
}
ProtectedCuboidRegion region = new ProtectedCuboidRegion(regionName, minimum, maximum);
manager.addRegion(region);
// Add the region to AreaShop
if (rentRegions) {
RentRegion rent = new RentRegion(regionName, selection.getWorld());
if (finalGroup != null) {
finalGroup.addMember(rent);
}
rent.runEventCommands(GeneralRegion.RegionEvent.CREATED, true);
plugin.getFileManager().addRent(rent);
rent.handleSchematicEvent(GeneralRegion.RegionEvent.CREATED);
rent.runEventCommands(GeneralRegion.RegionEvent.CREATED, false);
rent.update();
} else {
BuyRegion buy = new BuyRegion(regionName, selection.getWorld());
if (finalGroup != null) {
finalGroup.addMember(buy);
}
buy.runEventCommands(GeneralRegion.RegionEvent.CREATED, true);
plugin.getFileManager().addBuy(buy);
buy.handleSchematicEvent(GeneralRegion.RegionEvent.CREATED);
buy.runEventCommands(GeneralRegion.RegionEvent.CREATED, false);
buy.update();
}
}
}
if (current >= amount) {
if (player.isOnline()) {
int added = amount - tooLow - tooHigh;
Message wrong = Message.empty();
if (tooHigh > 0) {
wrong.append(Message.fromKey("stack-tooHigh").replacements(tooHigh));
}
if (tooLow > 0) {
wrong.append(Message.fromKey("stack-tooLow").replacements(tooLow));
}
plugin.message(player, "stack-addComplete", added, wrong);
}
this.cancel();
}
}
}.runTaskTimer(plugin, 1, 1);
}
Aggregations