Use a long to refer to the volume of a region to prevent overflow (#1350)

* Use a long to refer to the volume of a region, and rename the method to getVolume

* Fixed issues noted in review

* Forgot to floor

* Fixed review notes

* Can use a long here rather than BigDecimal

* Improve javadocs

* style

(cherry picked from commit 328030fd6281e58a4ea1d0cdd0a2e274da90afbe)
Dieser Commit ist enthalten in:
Matthew Miller 2020-06-08 05:50:17 -04:00 committet von MattBDev
Ursprung a23b182de5
Commit 33adba4a6f
27 geänderte Dateien mit 225 neuen und 124 gelöschten Zeilen

Datei anzeigen

@ -20,11 +20,6 @@
package com.sk89q.bukkit.util;
import com.sk89q.util.ReflectionUtil;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@ -33,6 +28,12 @@ import org.bukkit.command.PluginIdentifiableCommand;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.plugin.Plugin;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class CommandRegistration {
static {
@ -61,6 +62,7 @@ public class CommandRegistration {
}
return null;
}
public boolean register(List<CommandInfo> registered) {
CommandMap commandMap = getCommandMap();
if (registered == null || commandMap == null) {
@ -82,12 +84,13 @@ public class CommandRegistration {
if (fallbackCommands != null) {
return fallbackCommands;
}
CommandMap commandMap = ReflectionUtil.getField(plugin.getServer().getPluginManager(), "commandMap");
if (commandMap == null) {
Bukkit.getServer().getLogger().severe(plugin.getDescription().getName() +
": Could not retrieve server CommandMap, using fallback instead!");
fallbackCommands = commandMap = new SimpleCommandMap(Bukkit.getServer());
Bukkit.getServer().getPluginManager().registerEvents(new FallbackRegistrationListener(fallbackCommands), plugin);
Bukkit.getServer().getLogger().severe(plugin.getDescription().getName() +
": Could not retrieve server CommandMap, using fallback instead!");
fallbackCommands = commandMap = new SimpleCommandMap(Bukkit.getServer());
Bukkit.getServer().getPluginManager().registerEvents(new FallbackRegistrationListener(fallbackCommands), plugin);
} else {
serverCommandMap = commandMap;
}

Datei anzeigen

@ -50,7 +50,7 @@ public class AsyncPreloader implements Preloader, Runnable {
MutablePair<World, Set<BlockVector2>> existing = cancelAndGet(player);
try {
Region region = session.getSelection(world);
if (!(region instanceof CuboidRegion) || region.getArea() > 50466816) {
if (!(region instanceof CuboidRegion) || region.getVolume() > 50466816) {
// TOO LARGE or NOT CUBOID
return;
}

Datei anzeigen

@ -52,7 +52,7 @@ public class LimitExtent extends PassthroughExtent {
@Override
public List<? extends Entity> getEntities(Region region) {
limit.THROW_MAX_CHECKS(region.getArea());
limit.THROW_MAX_CHECKS(region.getVolume());
try {
return getExtent().getEntities(region);
} catch (FaweException e) {
@ -223,8 +223,8 @@ public class LimitExtent extends PassthroughExtent {
@Override
public void addCaves(Region region) throws WorldEditException {
limit.THROW_MAX_CHECKS(region.getArea());
limit.THROW_MAX_CHANGES(region.getArea());
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
try {
getExtent().addCaves(region);
} catch (FaweException e) {
@ -236,8 +236,8 @@ public class LimitExtent extends PassthroughExtent {
@Override
public void generate(Region region, GenBase gen) throws WorldEditException {
limit.THROW_MAX_CHECKS(region.getArea());
limit.THROW_MAX_CHANGES(region.getArea());
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
try {
getExtent().generate(region, gen);
} catch (FaweException e) {
@ -249,8 +249,8 @@ public class LimitExtent extends PassthroughExtent {
@Override
public void addSchems(Region region, Mask mask, List<ClipboardHolder> clipboards, int rarity, boolean rotate) throws WorldEditException {
limit.THROW_MAX_CHECKS(region.getArea());
limit.THROW_MAX_CHANGES(region.getArea());
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
try {
getExtent().addSchems(region, mask, clipboards, rarity, rotate);
} catch (FaweException e) {
@ -262,8 +262,8 @@ public class LimitExtent extends PassthroughExtent {
@Override
public void spawnResource(Region region, Resource gen, int rarity, int frequency) throws WorldEditException {
limit.THROW_MAX_CHECKS(region.getArea());
limit.THROW_MAX_CHANGES(region.getArea());
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
try {
getExtent().spawnResource(region, gen, rarity, frequency);
} catch (FaweException e) {
@ -275,8 +275,8 @@ public class LimitExtent extends PassthroughExtent {
@Override
public void addOre(Region region, Mask mask, Pattern material, int size, int frequency, int rarity, int minY, int maxY) throws WorldEditException {
limit.THROW_MAX_CHECKS(region.getArea());
limit.THROW_MAX_CHANGES(region.getArea());
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
try {
getExtent().addOre(region, mask, material, size, frequency, rarity, minY, maxY);
} catch (FaweException e) {
@ -288,8 +288,8 @@ public class LimitExtent extends PassthroughExtent {
@Override
public void addOres(Region region, Mask mask) throws WorldEditException {
limit.THROW_MAX_CHECKS(region.getArea());
limit.THROW_MAX_CHANGES(region.getArea());
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
try {
getExtent().addOres(region, mask);
} catch (FaweException e) {
@ -301,7 +301,7 @@ public class LimitExtent extends PassthroughExtent {
@Override
public List<Countable<BlockType>> getBlockDistribution(Region region) {
limit.THROW_MAX_CHECKS(region.getArea());
limit.THROW_MAX_CHECKS(region.getVolume());
try {
return getExtent().getBlockDistribution(region);
} catch (FaweException e) {
@ -314,7 +314,7 @@ public class LimitExtent extends PassthroughExtent {
@Override
public List<Countable<BlockState>> getBlockDistributionWithData(Region region) {
limit.THROW_MAX_CHECKS(region.getArea());
limit.THROW_MAX_CHECKS(region.getVolume());
try {
return getExtent().getBlockDistributionWithData(region);
} catch (FaweException e) {
@ -327,7 +327,7 @@ public class LimitExtent extends PassthroughExtent {
@Override
public int countBlocks(Region region, Set<BaseBlock> searchBlocks) {
limit.THROW_MAX_CHECKS(region.getArea());
limit.THROW_MAX_CHECKS(region.getVolume());
try {
return getExtent().countBlocks(region, searchBlocks);
} catch (FaweException e) {
@ -340,7 +340,7 @@ public class LimitExtent extends PassthroughExtent {
@Override
public int countBlocks(Region region, Mask searchMask) {
limit.THROW_MAX_CHECKS(region.getArea());
limit.THROW_MAX_CHECKS(region.getVolume());
try {
return getExtent().countBlocks(region, searchMask);
} catch (FaweException e) {
@ -353,7 +353,7 @@ public class LimitExtent extends PassthroughExtent {
@Override
public <B extends BlockStateHolder<B>> int setBlocks(Region region, B block) throws MaxChangedBlocksException {
limit.THROW_MAX_CHANGES(region.getArea());
limit.THROW_MAX_CHANGES(region.getVolume());
try {
return getExtent().setBlocks(region, block);
} catch (FaweException e) {
@ -366,7 +366,7 @@ public class LimitExtent extends PassthroughExtent {
@Override
public int setBlocks(Region region, Pattern pattern) throws MaxChangedBlocksException {
limit.THROW_MAX_CHANGES(region.getArea());
limit.THROW_MAX_CHANGES(region.getVolume());
try {
return getExtent().setBlocks(region, pattern);
} catch (FaweException e) {
@ -379,8 +379,8 @@ public class LimitExtent extends PassthroughExtent {
@Override
public <B extends BlockStateHolder<B>> int replaceBlocks(Region region, Set<BaseBlock> filter, B replacement) throws MaxChangedBlocksException {
limit.THROW_MAX_CHECKS(region.getArea());
limit.THROW_MAX_CHANGES(region.getArea());
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
try {
return getExtent().replaceBlocks(region, filter, replacement);
} catch (FaweException e) {
@ -393,8 +393,8 @@ public class LimitExtent extends PassthroughExtent {
@Override
public int replaceBlocks(Region region, Set<BaseBlock> filter, Pattern pattern) throws MaxChangedBlocksException {
limit.THROW_MAX_CHECKS(region.getArea());
limit.THROW_MAX_CHANGES(region.getArea());
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
try {
return getExtent().replaceBlocks(region, filter, pattern);
} catch (FaweException e) {
@ -407,8 +407,8 @@ public class LimitExtent extends PassthroughExtent {
@Override
public int replaceBlocks(Region region, Mask mask, Pattern pattern) throws MaxChangedBlocksException {
limit.THROW_MAX_CHECKS(region.getArea());
limit.THROW_MAX_CHANGES(region.getArea());
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
try {
return getExtent().replaceBlocks(region, mask, pattern);
} catch (FaweException e) {
@ -421,8 +421,8 @@ public class LimitExtent extends PassthroughExtent {
@Override
public int center(Region region, Pattern pattern) throws MaxChangedBlocksException {
limit.THROW_MAX_CHECKS(region.getArea());
limit.THROW_MAX_CHANGES(region.getArea());
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
try {
return getExtent().center(region, pattern);
} catch (FaweException e) {
@ -448,8 +448,8 @@ public class LimitExtent extends PassthroughExtent {
@Override
public <T extends Filter> T apply(Region region, T filter, boolean full) {
limit.THROW_MAX_CHECKS(region.getArea());
limit.THROW_MAX_CHANGES(region.getArea());
limit.THROW_MAX_CHECKS(region.getVolume());
limit.THROW_MAX_CHANGES(region.getVolume());
try {
return getExtent().apply(region, filter, full);
} catch (FaweException e) {

Datei anzeigen

@ -162,6 +162,9 @@ public class FaweLimit {
public void THROW_MAX_CHANGES(int amt) {
if ((MAX_CHANGES -= amt) <= 0) throw FaweCache.MAX_CHANGES;
}
public void THROW_MAX_CHANGES(long amt) {
if ((MAX_CHANGES -= amt) <= 0) throw FaweCache.MAX_CHANGES;
}
public void THROW_MAX_FAILS(int amt) {
if ((MAX_FAILS -= amt) <= 0) throw FaweCache.MAX_CHECKS;
@ -171,6 +174,10 @@ public class FaweLimit {
if ((MAX_CHECKS -= amt) <= 0) throw FaweCache.MAX_CHECKS;
}
public void THROW_MAX_CHECKS(long amt) {
if ((MAX_CHECKS -= amt) <= 0) throw FaweCache.MAX_CHECKS;
}
public void THROW_MAX_ITERATIONS(int amt) {
if ((MAX_ITERATIONS -= amt) <= 0) throw FaweCache.MAX_ITERATIONS;
}

Datei anzeigen

@ -87,14 +87,12 @@ public class FuzzyRegionSelector extends PassthroughExtent implements RegionSele
@Override
public void explainPrimarySelection(Actor actor, LocalSession session, BlockVector3 position) {
int size = this.region.getArea();
player.print(Caption.of("fawe.worldedit.selector.selector.fuzzy.pos1", position, "(" + region.getArea() + ")"));
player.print(Caption.of("fawe.worldedit.selector.selector.fuzzy.pos1", position, "(" + region.getVolume() + ")"));
}
@Override
public void explainSecondarySelection(Actor actor, LocalSession session, BlockVector3 position) {
int size = this.region.getArea();
player.print(Caption.of("fawe.worldedit.selector.selector.fuzzy.pos2", position, "(" + region.getArea() + ")"));
player.print(Caption.of("fawe.worldedit.selector.selector.fuzzy.pos2", position, "(" + region.getVolume() + ")"));
}
@Override

Datei anzeigen

@ -176,7 +176,7 @@ public class ClipboardCommands {
lazyClipboard.setOrigin(session.getPlacementPosition(actor));
session.setClipboard(new ClipboardHolder(lazyClipboard));
actor.print(Caption.of("fawe.worldedit.copy.command.copy" , region.getArea()));
actor.print(Caption.of("fawe.worldedit.copy.command.copy" , region.getVolume()));
}
// @Command(

Datei anzeigen

@ -104,14 +104,14 @@ public class ExpandCommands {
private static void expandVert(LocalSession session, Actor actor, World world) throws IncompleteRegionException {
Region region = session.getSelection(world);
try {
int oldSize = region.getArea();
long oldSize = region.getVolume();
region.expand(
BlockVector3.at(0, (world.getMaxY() + 1), 0),
BlockVector3.at(0, -(world.getMaxY() + 1), 0));
session.getRegionSelector(world).learnChanges();
int newSize = region.getArea();
long newSize = region.getVolume();
session.getRegionSelector(world).explainRegionAdjust(actor, session);
int changeSize = newSize - oldSize;
long changeSize = newSize - oldSize;
actor.printInfo(
TranslatableComponent.of("worldedit.expand.expanded.vert", TextComponent.of(changeSize))
);
@ -134,7 +134,7 @@ public class ExpandCommands {
@MultiDirection
List<BlockVector3> direction) throws WorldEditException {
Region region = session.getSelection(world);
int oldSize = region.getArea();
long oldSize = region.getVolume();
if (reverseAmount == 0) {
for (BlockVector3 dir : direction) {
@ -147,11 +147,11 @@ public class ExpandCommands {
}
session.getRegionSelector(world).learnChanges();
int newSize = region.getArea();
long newSize = region.getVolume();
session.getRegionSelector(world).explainRegionAdjust(actor, session);
int changeSize = newSize - oldSize;
long changeSize = newSize - oldSize;
actor.printInfo(TranslatableComponent.of("worldedit.expand.expanded", TextComponent.of(changeSize)));
}

Datei anzeigen

@ -339,7 +339,7 @@ public class SelectionCommands {
List<BlockVector3> direction) throws WorldEditException {
try {
Region region = session.getSelection(world);
int oldSize = region.getArea();
long oldSize = region.getVolume();
if (reverseAmount == 0) {
for (BlockVector3 dir : direction) {
region.contract(dir.multiply(amount));
@ -350,7 +350,7 @@ public class SelectionCommands {
}
}
session.getRegionSelector(world).learnChanges();
int newSize = region.getArea();
long newSize = region.getVolume();
session.getRegionSelector(world).explainRegionAdjust(actor, session);
@ -501,7 +501,7 @@ public class SelectionCommands {
actor.printInfo(TranslatableComponent.of("worldedit.size.size", TextComponent.of(size.toString())));
actor.printInfo(TranslatableComponent.of("worldedit.size.distance", TextComponent.of(region.getMaximumPoint().distance(region.getMinimumPoint()))));
actor.printInfo(TranslatableComponent.of("worldedit.size.blocks", TextComponent.of(region.getArea())));
actor.printInfo(TranslatableComponent.of("worldedit.size.blocks", TextComponent.of(region.getVolume())));
}
@Command(

Datei anzeigen

@ -19,7 +19,6 @@
package com.sk89q.worldedit.extension.platform;
import com.boydti.fawe.config.Caption;
import com.boydti.fawe.object.task.AsyncNotifyQueue;
import com.sk89q.worldedit.EditSession;
@ -701,10 +700,10 @@ public abstract class AbstractPlayerActor implements Actor, Player, Cloneable {
* @return
*/
public Region getLargestRegion() {
int area = 0;
long area = 0;
Region max = null;
for (Region region : this.getCurrentRegions()) {
final int tmp = region.getArea();
final long tmp = region.getVolume();
if (tmp > area) {
area = tmp;
max = region;

Datei anzeigen

@ -27,8 +27,9 @@ public class SelectionPoint2DEvent implements CUIEvent {
protected final int id;
protected final int blockX;
protected final int blockZ;
protected final int area;
protected final long area;
@Deprecated
public SelectionPoint2DEvent(int id, BlockVector2 pos, int area) {
this.id = id;
this.blockX = pos.getX();
@ -36,6 +37,7 @@ public class SelectionPoint2DEvent implements CUIEvent {
this.area = area;
}
@Deprecated
public SelectionPoint2DEvent(int id, BlockVector3 pos, int area) {
this.id = id;
this.blockX = pos.getX();
@ -43,6 +45,20 @@ public class SelectionPoint2DEvent implements CUIEvent {
this.area = area;
}
public SelectionPoint2DEvent(int id, BlockVector2 pos, long area) {
this.id = id;
this.blockX = pos.getX();
this.blockZ = pos.getZ();
this.area = area;
}
public SelectionPoint2DEvent(int id, BlockVector3 pos, long area) {
this.id = id;
this.blockX = pos.getX();
this.blockZ = pos.getZ();
this.area = area;
}
@Override
public String getTypeId() {
return "p2";

Datei anzeigen

@ -25,14 +25,21 @@ public class SelectionPointEvent implements CUIEvent {
protected final int id;
protected final BlockVector3 pos;
protected final int area;
protected final long area;
@Deprecated
public SelectionPointEvent(int id, BlockVector3 pos, int area) {
this.id = id;
this.pos = pos;
this.area = area;
}
public SelectionPointEvent(int id, BlockVector3 pos, long area) {
this.id = id;
this.pos = pos;
this.area = area;
}
@Override
public String getTypeId() {
return "p";

Datei anzeigen

@ -20,6 +20,7 @@
package com.sk89q.worldedit.regions;
import com.boydti.fawe.object.collection.BlockVectorSet;
import com.google.common.primitives.Longs;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
@ -44,7 +45,7 @@ public abstract class AbstractRegion extends AbstractSet<BlockVector3> implement
@Override
public int size() {
return getArea();
return com.google.common.primitives.Ints.saturatedCast(getVolume());
}
@Override
@ -106,19 +107,14 @@ public abstract class AbstractRegion extends AbstractSet<BlockVector3> implement
return points;
}
/**
* Get the number of blocks in the region.
*
* @return number of blocks
*/
@Override
public int getArea() {
public long getVolume() {
BlockVector3 min = getMinimumPoint();
BlockVector3 max = getMaximumPoint();
return (max.getX() - min.getX() + 1) *
(max.getY() - min.getY() + 1) *
(max.getZ() - min.getZ() + 1);
return (max.getX() - min.getX() + 1L) *
(max.getY() - min.getY() + 1L) *
(max.getZ() - min.getZ() + 1L);
}
/**
@ -218,7 +214,7 @@ public abstract class AbstractRegion extends AbstractSet<BlockVector3> implement
int result = worldHash ^ (worldHash >>> 32);
result = 31 * result + this.getMinimumPoint().hashCode();
result = 31 * result + this.getMaximumPoint().hashCode();
result = 31 * result + this.getArea();
result = (int) (31 * result + this.getVolume());
return result;
}
@ -239,7 +235,7 @@ public abstract class AbstractRegion extends AbstractSet<BlockVector3> implement
if(this.getWorld().equals(region.getWorld())
&& this.getMinimumPoint().equals(region.getMinimumPoint())
&& this.getMaximumPoint().equals(region.getMaximumPoint())
&& this.getArea() == region.getArea()){
&& this.getVolume() == region.getVolume()){
return true;
}
return false;

Datei anzeigen

@ -35,6 +35,9 @@ import com.sk89q.worldedit.math.geom.Polygons;
import com.sk89q.worldedit.regions.iterator.FlatRegion3DIterator;
import com.sk89q.worldedit.regions.iterator.FlatRegionIterator;
import com.sk89q.worldedit.world.World;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Iterator;
import java.util.List;
@ -196,9 +199,16 @@ public class CylinderRegion extends AbstractRegion implements FlatRegion {
return minY;
}
private static final BigDecimal PI = BigDecimal.valueOf(Math.PI);
@Override
public int getArea() {
return (int) Math.floor(radius.getX() * radius.getZ() * Math.PI * getHeight());
public long getVolume() {
return BigDecimal.valueOf(radius.getX())
.multiply(BigDecimal.valueOf(radius.getZ()))
.multiply(PI)
.multiply(BigDecimal.valueOf(getHeight()))
.setScale(0, RoundingMode.FLOOR)
.longValue();
}
@Override

Datei anzeigen

@ -32,6 +32,8 @@ import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.storage.ChunkStore;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashSet;
import java.util.Set;
@ -68,7 +70,7 @@ public class EllipsoidRegion extends AbstractRegion {
/**
* Construct a new instance of this ellipsoid region.
*
* @param world the world
* @param world the world
* @param center the center
* @param radius the radius
*/
@ -92,10 +94,16 @@ public class EllipsoidRegion extends AbstractRegion {
return center.toVector3().add(getRadius()).toBlockPoint();
}
private static final BigDecimal ELLIPSOID_BASE_MULTIPLIER = BigDecimal.valueOf((4.0 / 3.0) * Math.PI);
@Override
public int getArea() {
return (int) Math
.floor((4.0 / 3.0) * Math.PI * radius.getX() * radius.getY() * radius.getZ());
public long getVolume() {
return ELLIPSOID_BASE_MULTIPLIER
.multiply(BigDecimal.valueOf(radius.getX()))
.multiply(BigDecimal.valueOf(radius.getY()))
.multiply(BigDecimal.valueOf(radius.getZ()))
.setScale(0, RoundingMode.FLOOR)
.longValue();
}
@Override
@ -118,7 +126,7 @@ public class EllipsoidRegion extends AbstractRegion {
if ((diff.getBlockX() & 1) + (diff.getBlockY() & 1) + (diff.getBlockZ() & 1) != 0) {
throw new RegionOperationException(
"Ellipsoid changes must be even for each dimensions.");
"Ellipsoid changes must be even for each dimensions.");
}
return diff.divide(2).floor();

Datei anzeigen

@ -53,7 +53,7 @@ public class NullRegion implements Region {
}
@Override
public int getArea() {
public long getVolume() {
return 0;
}

Datei anzeigen

@ -25,6 +25,8 @@ import com.sk89q.worldedit.regions.iterator.FlatRegion3DIterator;
import com.sk89q.worldedit.regions.iterator.FlatRegionIterator;
import com.sk89q.worldedit.world.World;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
@ -197,18 +199,22 @@ public class Polygonal2DRegion extends AbstractRegion implements FlatRegion {
}
@Override
public int getArea() {
double area = 0;
public long getVolume() {
long area = 0;
int i, j = points.size() - 1;
for (i = 0; i < points.size(); ++i) {
area += (points.get(j).getBlockX() + points.get(i).getBlockX())
* (points.get(j).getBlockZ() - points.get(i).getBlockZ());
long x = points.get(j).getBlockX() + points.get(i).getBlockX();
long z = points.get(j).getBlockZ() - points.get(i).getBlockZ();
area += x * z;
j = i;
}
return (int) Math.floor(Math.abs(area * 0.5)
* (maxY - minY + 1));
return BigDecimal.valueOf(area)
.multiply(BigDecimal.valueOf(0.5))
.abs()
.setScale(0, RoundingMode.FLOOR)
.longValue() * (maxY - minY + 1);
}
@Override

Datei anzeigen

@ -32,8 +32,10 @@ import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.world.World;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
/**
@ -74,14 +76,40 @@ public interface Region extends Iterable<BlockVector3>, Cloneable, IBatchProcess
* Get the number of blocks in the region.
*
* @return number of blocks
* @deprecated use {@link Region#getVolume()} to prevent overflows
*/
@Deprecated
default int getArea() {
return (int) getVolume();
}
/**
* Get the number of blocks in the region.
*
* <p>Note: This method <b>must</b> be overridden.</p>
*
* @return number of blocks
*/
default long getVolume() {
// TODO Remove default status when getArea is removed.
try {
if (getClass().getMethod("getArea").getDeclaringClass().equals(Region.class)) {
throw new IllegalStateException("Class " + getClass().getName() + " must override getVolume.");
}
} catch (NoSuchMethodException e) {
throw new AssertionError(e);
}
return getArea();
}
/* FAWE code for getArea() before merge:
default int getArea() {
BlockVector3 min = getMinimumPoint();
BlockVector3 max = getMaximumPoint();
return (max.getX() - min.getX() + 1) * (max.getY() - min.getY() + 1) * (max.getZ() - min.getZ() + 1);
}
*/
/**
* Get X-size.
*

Datei anzeigen

@ -135,8 +135,31 @@ public interface RegionSelector {
* Get the number of blocks inside the region.
*
* @return number of blocks, or -1 if undefined
* @deprecated use {@link RegionSelector#getVolume()}
*/
int getArea();
@Deprecated
default int getArea() {
return (int) getVolume();
}
/**
* Get the number of blocks inside the region.
*
* <p>Note: This method <b>must</b> be overridden.</p>
*
* @return number of blocks, or -1 if undefined
*/
default long getVolume() {
// TODO Remove default once getArea is removed
try {
if (getClass().getMethod("getArea").getDeclaringClass().equals(RegionSelector.class)) {
throw new IllegalStateException("Class " + getClass().getName() + " must override getVolume.");
}
} catch (NoSuchMethodException e) {
throw new AssertionError(e);
}
return getArea();
}
/**
* Update the selector with changes to the region.

Datei anzeigen

@ -113,8 +113,8 @@ public class TransformRegion extends AbstractRegion {
}
@Override
public int getArea() {
return region.getArea(); // Cannot transform this
public long getVolume() {
return region.getVolume(); // Cannot transform this
}
@Override

Datei anzeigen

@ -170,8 +170,8 @@ public class ConvexPolyhedralRegionSelector implements RegionSelector, CUIRegion
}
@Override
public int getArea() {
return region.getArea();
public long getVolume() {
return region.getVolume();
}
@Override
@ -250,7 +250,7 @@ public class ConvexPolyhedralRegionSelector implements RegionSelector, CUIRegion
int lastVertexId = -1;
for (BlockVector3 vertex : vertices) {
vertexIds.put(vertex, ++lastVertexId);
session.dispatchCUIEvent(player, new SelectionPointEvent(lastVertexId, vertex, getArea()));
session.dispatchCUIEvent(player, new SelectionPointEvent(lastVertexId, vertex, getVolume()));
}
for (Triangle triangle : triangles) {
@ -273,8 +273,8 @@ public class ConvexPolyhedralRegionSelector implements RegionSelector, CUIRegion
checkNotNull(session);
if (isDefined()) {
session.dispatchCUIEvent(player, new SelectionPointEvent(0, region.getMinimumPoint(), getArea()));
session.dispatchCUIEvent(player, new SelectionPointEvent(1, region.getMaximumPoint(), getArea()));
session.dispatchCUIEvent(player, new SelectionPointEvent(0, region.getMinimumPoint(), getVolume()));
session.dispatchCUIEvent(player, new SelectionPointEvent(1, region.getMaximumPoint(), getVolume()));
}
}

Datei anzeigen

@ -161,13 +161,13 @@ public class CuboidRegionSelector implements RegionSelector, CUIRegion {
player.printInfo(TranslatableComponent.of(
"worldedit.selection.cuboid.explain.primary-area",
TextComponent.of(position1.toString()),
TextComponent.of(region.getArea())
TextComponent.of(region.getVolume())
));
} else if (position1 != null) {
player.printInfo(TranslatableComponent.of("worldedit.selection.cuboid.explain.primary", TextComponent.of(position1.toString())));
}
session.dispatchCUIEvent(player, new SelectionPointEvent(0, pos, getArea()));
session.dispatchCUIEvent(player, new SelectionPointEvent(0, pos, getVolume()));
}
@Override
@ -180,13 +180,13 @@ public class CuboidRegionSelector implements RegionSelector, CUIRegion {
player.printInfo(TranslatableComponent.of(
"worldedit.selection.cuboid.explain.secondary-area",
TextComponent.of(position2.toString()),
TextComponent.of(region.getArea())
TextComponent.of(region.getVolume())
));
} else if (position2 != null) {
player.printInfo(TranslatableComponent.of("worldedit.selection.cuboid.explain.secondary", TextComponent.of(position2.toString())));
}
session.dispatchCUIEvent(player, new SelectionPointEvent(1, pos, getArea()));
session.dispatchCUIEvent(player, new SelectionPointEvent(1, pos, getVolume()));
}
@Override
@ -197,11 +197,11 @@ public class CuboidRegionSelector implements RegionSelector, CUIRegion {
session.dispatchCUIEvent(player, new SelectionShapeEvent(getTypeID()));
if (position1 != null) {
session.dispatchCUIEvent(player, new SelectionPointEvent(0, position1, getArea()));
session.dispatchCUIEvent(player, new SelectionPointEvent(0, position1, getVolume()));
}
if (position2 != null) {
session.dispatchCUIEvent(player, new SelectionPointEvent(1, position2, getArea()));
session.dispatchCUIEvent(player, new SelectionPointEvent(1, position2, getVolume()));
}
}
@ -268,7 +268,7 @@ public class CuboidRegionSelector implements RegionSelector, CUIRegion {
}
@Override
public int getArea() {
public long getVolume() {
if (position1 == null) {
return -1;
}
@ -277,17 +277,17 @@ public class CuboidRegionSelector implements RegionSelector, CUIRegion {
return -1;
}
return region.getArea();
return region.getVolume();
}
@Override
public void describeCUI(LocalSession session, Actor player) {
if (position1 != null) {
session.dispatchCUIEvent(player, new SelectionPointEvent(0, position1, getArea()));
session.dispatchCUIEvent(player, new SelectionPointEvent(0, position1, getVolume()));
}
if (position2 != null) {
session.dispatchCUIEvent(player, new SelectionPointEvent(1, position2, getArea()));
session.dispatchCUIEvent(player, new SelectionPointEvent(1, position2, getVolume()));
}
}

Datei anzeigen

@ -188,7 +188,7 @@ public class CylinderRegionSelector implements RegionSelector, CUIRegion {
"worldedit.selection.cylinder.explain.secondary",
TextComponent.of(NUMBER_FORMAT.format(region.getRadius().getX())),
TextComponent.of(NUMBER_FORMAT.format(region.getRadius().getZ())),
TextComponent.of(region.getArea())
TextComponent.of(region.getVolume())
));
} else {
player.printError(TranslatableComponent.of("worldedit.selection.cylinder.explain.secondary-missing"));
@ -260,8 +260,8 @@ public class CylinderRegionSelector implements RegionSelector, CUIRegion {
}
@Override
public int getArea() {
return region.getArea();
public long getVolume() {
return region.getVolume();
}
@Override
@ -273,8 +273,8 @@ public class CylinderRegionSelector implements RegionSelector, CUIRegion {
@Override
public void describeLegacyCUI(LocalSession session, Actor player) {
if (isDefined()) {
session.dispatchCUIEvent(player, new SelectionPointEvent(0, region.getMinimumPoint(), getArea()));
session.dispatchCUIEvent(player, new SelectionPointEvent(1, region.getMaximumPoint(), getArea()));
session.dispatchCUIEvent(player, new SelectionPointEvent(0, region.getMinimumPoint(), getVolume()));
session.dispatchCUIEvent(player, new SelectionPointEvent(1, region.getMaximumPoint(), getVolume()));
}
}

Datei anzeigen

@ -151,7 +151,7 @@ public class EllipsoidRegionSelector implements RegionSelector, CUIRegion {
player.printInfo(TranslatableComponent.of(
"worldedit.selection.ellipsoid.explain.primary-area",
TextComponent.of(region.getCenter().toString()),
TextComponent.of(region.getArea())
TextComponent.of(region.getVolume())
));
} else {
player.printInfo(TranslatableComponent.of(
@ -169,7 +169,7 @@ public class EllipsoidRegionSelector implements RegionSelector, CUIRegion {
player.printInfo(TranslatableComponent.of(
"worldedit.selection.ellipsoid.explain.secondary-area",
TextComponent.of(region.getRadius().toString()),
TextComponent.of(region.getArea())
TextComponent.of(region.getVolume())
));
} else {
player.printInfo(TranslatableComponent.of(
@ -238,8 +238,8 @@ public class EllipsoidRegionSelector implements RegionSelector, CUIRegion {
}
@Override
public int getArea() {
return region.getArea();
public long getVolume() {
return region.getVolume();
}
@Override
@ -250,8 +250,8 @@ public class EllipsoidRegionSelector implements RegionSelector, CUIRegion {
@Override
public void describeLegacyCUI(LocalSession session, Actor player) {
session.dispatchCUIEvent(player, new SelectionPointEvent(0, region.getMinimumPoint(), getArea()));
session.dispatchCUIEvent(player, new SelectionPointEvent(1, region.getMaximumPoint(), getArea()));
session.dispatchCUIEvent(player, new SelectionPointEvent(0, region.getMinimumPoint(), getVolume()));
session.dispatchCUIEvent(player, new SelectionPointEvent(1, region.getMaximumPoint(), getVolume()));
}
@Override

Datei anzeigen

@ -134,7 +134,7 @@ public class ExtendingCuboidRegionSelector extends CuboidRegionSelector {
player.printInfo(TranslatableComponent.of(
"worldedit.selection.extend.explain.primary",
TextComponent.of(pos.toString()),
TextComponent.of(region.getArea())
TextComponent.of(region.getVolume())
));
explainRegionAdjust(player, session);
@ -145,7 +145,7 @@ public class ExtendingCuboidRegionSelector extends CuboidRegionSelector {
player.printInfo(TranslatableComponent.of(
"worldedit.selection.extend.explain.secondary",
TextComponent.of(pos.toString()),
TextComponent.of(region.getArea())
TextComponent.of(region.getVolume())
));
explainRegionAdjust(player, session);

Datei anzeigen

@ -174,7 +174,7 @@ public class Polygonal2DRegionSelector implements RegionSelector, CUIRegion {
player.printInfo(TranslatableComponent.of("worldedit.selection.polygon2d.explain.primary", TextComponent.of(pos.toString())));
session.dispatchCUIEvent(player, new SelectionShapeEvent(getTypeID()));
session.dispatchCUIEvent(player, new SelectionPoint2DEvent(0, pos, getArea()));
session.dispatchCUIEvent(player, new SelectionPoint2DEvent(0, pos, getVolume()));
session.dispatchCUIEvent(player, new SelectionMinMaxEvent(region.getMinimumY(), region.getMaximumY()));
}
@ -186,7 +186,7 @@ public class Polygonal2DRegionSelector implements RegionSelector, CUIRegion {
TextComponent.of(pos.toString())
));
session.dispatchCUIEvent(player, new SelectionPoint2DEvent(region.size() - 1, pos, getArea()));
session.dispatchCUIEvent(player, new SelectionPoint2DEvent(region.size() - 1, pos, getVolume()));
session.dispatchCUIEvent(player, new SelectionMinMaxEvent(region.getMinimumY(), region.getMaximumY()));
}
@ -247,8 +247,8 @@ public class Polygonal2DRegionSelector implements RegionSelector, CUIRegion {
}
@Override
public int getArea() {
return region.getArea();
public long getVolume() {
return region.getVolume();
}
/**
@ -264,7 +264,7 @@ public class Polygonal2DRegionSelector implements RegionSelector, CUIRegion {
public void describeCUI(LocalSession session, Actor player) {
final List<BlockVector2> points = region.getPoints();
for (int id = 0; id < points.size(); id++) {
session.dispatchCUIEvent(player, new SelectionPoint2DEvent(id, points.get(id), getArea()));
session.dispatchCUIEvent(player, new SelectionPoint2DEvent(id, points.get(id), getVolume()));
}
session.dispatchCUIEvent(player, new SelectionMinMaxEvent(region.getMinimumY(), region.getMaximumY()));

Datei anzeigen

@ -93,7 +93,7 @@ public class SphereRegionSelector extends EllipsoidRegionSelector {
player.printInfo(TranslatableComponent.of(
"worldedit.selection.sphere.explain.secondary-defined",
TextComponent.of(region.getRadius().getX()),
TextComponent.of(region.getArea())
TextComponent.of(region.getVolume())
));
} else {
player.printInfo(TranslatableComponent.of("worldedit.selection.sphere.explain.secondary", TextComponent.of(region.getRadius().getX())));

Datei anzeigen

@ -78,8 +78,8 @@ public class RequestSelection implements Region {
}
@Override
public int getArea() {
return getRegion().getArea();
public long getVolume() {
return getRegion().getVolume();
}
@Override