Fixed a null pointer. Merged in a bunch of CLI stuff.

This commit is contained in:
MattBDev
2019-09-02 15:22:43 -04:00
parent aa4c443358
commit c20f4c6b7f
28 changed files with 240 additions and 352 deletions

View File

@ -135,7 +135,7 @@ public final class StringUtil {
}
StringBuilder buffer = new StringBuilder(Integer.toString(str[initialIndex]));
for (int i = initialIndex + 1; i < str.length; ++i) {
buffer.append(delimiter).append(str[i]);
buffer.append(delimiter).append(Integer.toString(str[i]));
}
return buffer.toString();
}

View File

@ -1010,7 +1010,7 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
@Override
@Nullable
public Entity createEntity(Location location, final BaseEntity entity) {
public Entity createEntity(com.sk89q.worldedit.util.Location location, BaseEntity entity) {
return getExtent().createEntity(location, entity);
}
@ -1233,7 +1233,7 @@ public class EditSession extends PassthroughExtent implements AutoCloseable {
* @throws MaxChangedBlocksException thrown if too many blocks are changed
*/
public <B extends BlockStateHolder<B>> int fillXZ(BlockVector3 origin, B block, double radius, int depth, boolean recursive) throws MaxChangedBlocksException {
return fillXZ(origin, block, radius, depth, recursive);
return fillXZ(origin, (Pattern) block, radius, depth, recursive);
}
/**

View File

@ -23,6 +23,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.event.extent.EditSessionEvent;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.util.eventbus.EventBus;
import com.sk89q.worldedit.world.World;
@ -65,10 +66,10 @@ public class EditSessionFactory {
*
* @param world the world
* @param maxBlocks the maximum number of blocks that can be changed, or -1 to use no limit
* @param player the player that the {@link EditSession} is for
* @param actor the actor that the {@link EditSession} is for
* @return an instance
*/
public EditSession getEditSession(World world, int maxBlocks, Player player) {
public EditSession getEditSession(World world, int maxBlocks, Actor actor) {
// ============ READ ME ============
@ -114,10 +115,10 @@ public class EditSessionFactory {
* @param world the world
* @param maxBlocks the maximum number of blocks that can be changed, or -1 to use no limit
* @param blockBag an optional {@link BlockBag} to use, otherwise null
* @param player the player that the {@link EditSession} is for
* @param actor the actor that the {@link EditSession} is for
* @return an instance
*/
public EditSession getEditSession(World world, int maxBlocks, BlockBag blockBag, Player player) {
public EditSession getEditSession(World world, int maxBlocks, BlockBag blockBag, Actor actor) {
// ============ READ ME ============
@ -156,8 +157,8 @@ public class EditSessionFactory {
}
@Override
public EditSession getEditSession(World world, int maxBlocks, Player player) {
return getEditSession(world, maxBlocks, null, player);
public EditSession getEditSession(World world, int maxBlocks, Actor actor) {
return getEditSession(world, maxBlocks, null, actor);
}
@Override
@ -166,11 +167,11 @@ public class EditSessionFactory {
}
@Override
public EditSession getEditSession(World world, int maxBlocks, BlockBag blockBag, Player player) {
public EditSession getEditSession(World world, int maxBlocks, BlockBag blockBag, Actor actor) {
if (WorldEdit.getInstance().getConfiguration().traceUnflushedSessions) {
return new TracedEditSession(eventBus, world, maxBlocks, blockBag, new EditSessionEvent(world, player, maxBlocks, null));
return new TracedEditSession(eventBus, world, maxBlocks, blockBag, new EditSessionEvent(world, actor, maxBlocks, null));
}
return new EditSession(eventBus, world, maxBlocks, blockBag, new EditSessionEvent(world, player, maxBlocks, null));
return new EditSession(eventBus, world, maxBlocks, blockBag, new EditSessionEvent(world, actor, maxBlocks, null));
}
}

View File

@ -152,6 +152,7 @@ public class LocalSession implements TextureHolder {
private transient VirtualWorld virtual;
private transient BlockVector3 cuiTemporaryBlock;
private transient List<Countable<BlockState>> lastDistribution;
private transient World worldOverride;
// Saved properties
private String lastScript;
@ -569,6 +570,19 @@ public class LocalSession implements TextureHolder {
return null;
}
public boolean hasWorldOverride() {
return this.worldOverride != null;
}
@Nullable
public World getWorldOverride() {
return this.worldOverride;
}
public void setWorldOverride(@Nullable World worldOverride) {
this.worldOverride = worldOverride;
}
public void unregisterTools(Player player) {
for (Tool tool : tools) {
if (tool instanceof BrushTool) {
@ -614,6 +628,9 @@ public class LocalSession implements TextureHolder {
if (selector.getWorld() == null || !selector.getWorld().equals(world)) {
selector.setWorld(world);
selector.clear();
if (hasWorldOverride() && !world.equals(getWorldOverride())) {
setWorldOverride(null);
}
}
} catch (Throwable ignore) {
selector.clear();
@ -632,6 +649,9 @@ public class LocalSession implements TextureHolder {
checkNotNull(selector);
selector.setWorld(world);
this.selector = selector;
if (hasWorldOverride() && !world.equals(getWorldOverride())) {
setWorldOverride(null);
}
}
/**
@ -1328,17 +1348,23 @@ public class LocalSession implements TextureHolder {
/**
* Construct a new edit session.
*
* @param player the actor
* @param actor the actor
* @return an edit session
*/
public EditSession createEditSession(Player player) {
checkNotNull(player);
BlockBag blockBag = getBlockBag(player);
World world = player.getWorld();
public EditSession createEditSession(Actor actor) {
checkNotNull(actor);
BlockBag blockBag = null;
if (actor.isPlayer() && actor instanceof Player) {
blockBag = getBlockBag((Player) actor);
}
World world = null;
if (hasWorldOverride()) {
world = getWorldOverride();
} else if (actor instanceof Locatable && ((Locatable) actor).getExtent() instanceof World) {
world = (World) ((Locatable) actor).getExtent();
}
EditSessionBuilder builder = new EditSessionBuilder(world);
if (player.isPlayer()) builder.player(FawePlayer.wrap(player));
if (actor.isPlayer() && actor instanceof Player) builder.player(FawePlayer.wrap(actor));
builder.blockBag(blockBag);
builder.fastmode(fastMode);

View File

@ -72,6 +72,27 @@ public class ApplyBrushCommands {
.ofTypes(ImmutableList.of(Key.of(double.class)))
.build();
public static void register(CommandManagerService service, CommandManager commandManager, CommandRegistrationHandler registration) {
commandManager.register("apply", builder -> {
builder.description(TextComponent.of("Apply brush, apply a function to every block"));
builder.action(org.enginehub.piston.Command.Action.NULL_ACTION);
CommandManager manager = service.newCommandManager();
registration.register(
manager,
ApplyBrushCommandsRegistration.builder(),
new ApplyBrushCommands()
);
builder.condition(new PermissionCondition(ImmutableSet.of("worldedit.brush.apply")));
builder.addParts(REGION_FACTORY, RADIUS);
builder.addPart(SubCommandPart.builder(TranslatableComponent.of("type"), TextComponent.of("Type of brush to use"))
.withCommands(manager.getAllCommands().collect(Collectors.toList()))
.required()
.build());
});
}
private void setApplyBrush(CommandParameters parameters, Player player, LocalSession localSession,
Contextual<? extends RegionFunction> generatorFactory) throws WorldEditException {
double radius = requireNonNull(RADIUS.value(parameters).asSingle(double.class));

View File

@ -30,6 +30,7 @@ import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.command.util.CommandPermissions;
import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator;
import com.sk89q.worldedit.command.util.Logging;
import com.sk89q.worldedit.command.util.WorldEditAsyncCommandBuilder;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.internal.anvil.ChunkDeleter;
@ -39,6 +40,7 @@ import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.util.formatting.component.PaginationBox;
import com.sk89q.worldedit.util.formatting.text.Component;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.event.ClickEvent;
import com.sk89q.worldedit.util.formatting.text.format.TextColor;
@ -51,8 +53,8 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.enginehub.piston.annotation.Command;
import org.enginehub.piston.annotation.CommandContainer;
import org.enginehub.piston.annotation.param.ArgFlag;
@ -94,10 +96,11 @@ public class ChunkCommands {
@CommandPermissions("worldedit.listchunks")
public void listChunks(Actor actor, World world, LocalSession session,
@ArgFlag(name = 'p', desc = "Page number.", def = "1") int page) throws WorldEditException {
Set<BlockVector2> chunks = session.getSelection(world).getChunks();
final Region region = session.getSelection(world);
PaginationBox paginationBox = PaginationBox.fromStrings("Selected Chunks", "/listchunks -p %page%", chunks);
actor.print(paginationBox.create(page));
WorldEditAsyncCommandBuilder.createAndSendMessage(actor,
() -> new ChunkListPaginationBox(region).create(page),
"Listing chunks for " + actor.getName());
}
@Command(
@ -168,4 +171,27 @@ public class ChunkCommands {
.clickEvent(ClickEvent.of(ClickEvent.Action.SUGGEST_COMMAND, "/stop"))));
}
private static class ChunkListPaginationBox extends PaginationBox {
//private final Region region;
private final List<BlockVector2> chunks;
ChunkListPaginationBox(Region region) {
super("Selected Chunks", "/listchunks -p %page%");
// TODO make efficient/streamable/calculable implementations of this
// for most region types, so we can just store the region and random-access get one page of chunks
// (this is non-trivial for some types of selections...)
//this.region = region.clone();
this.chunks = new ArrayList<>(region.getChunks());
}
@Override
public Component getComponent(int number) {
return TextComponent.of(chunks.get(number).toString());
}
@Override
public int getComponentsSize() {
return chunks.size();
}
}
}

View File

@ -29,6 +29,7 @@ import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.command.util.Logging;
import com.sk89q.worldedit.command.util.PermissionCondition;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.internal.annotation.Direction;
import com.sk89q.worldedit.internal.annotation.MultiDirection;
import com.sk89q.worldedit.internal.command.CommandRegistrationHandler;
@ -37,6 +38,7 @@ import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.RegionOperationException;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
import com.sk89q.worldedit.world.World;
import java.util.List;
import org.enginehub.piston.Command;
import org.enginehub.piston.CommandManager;
@ -119,7 +121,7 @@ public class ExpandCommands {
desc = "Expand the selection area"
)
@Logging(REGION)
public void expand(Player player, LocalSession session,
public void expand(Actor actor, World world, LocalSession session,
@Arg(desc = "Amount to expand the selection by, can be `vert` to expand to the whole vertical column")
int amount,
@Arg(desc = "Amount to expand the selection by in the other direction", def = "0")
@ -127,7 +129,7 @@ public class ExpandCommands {
@Arg(desc = "Direction to expand", def = Direction.AIM)
@MultiDirection
List<BlockVector3> direction) throws WorldEditException {
Region region = session.getSelection(player.getWorld());
Region region = session.getSelection(world);
int oldSize = region.getArea();
if (reverseAmount == 0) {
@ -140,12 +142,12 @@ public class ExpandCommands {
}
}
session.getRegionSelector(player.getWorld()).learnChanges();
session.getRegionSelector(world).learnChanges();
int newSize = region.getArea();
session.getRegionSelector(player.getWorld()).explainRegionAdjust(player, session);
session.getRegionSelector(world).explainRegionAdjust(actor, session);
player.print("Region expanded " + (newSize - oldSize) + " block(s).");
actor.print("Region expanded " + (newSize - oldSize) + " block(s).");
}
}

View File

@ -85,23 +85,23 @@ public class GeneralCommands {
desc = "Modify block change limit"
)
@CommandPermissions("worldedit.limit")
public void limit(Player player, LocalSession session,
public void limit(Actor actor, LocalSession session,
@Arg(desc = "The limit to set", def = "")
Integer limit) {
LocalConfiguration config = worldEdit.getConfiguration();
boolean mayDisable = player.hasPermission("worldedit.limit.unrestricted");
boolean mayDisable = actor.hasPermission("worldedit.limit.unrestricted");
limit = limit == null ? config.defaultChangeLimit : Math.max(-1, limit);
if (!mayDisable && config.maxChangeLimit > -1) {
if (limit > config.maxChangeLimit) {
player.printError("Your maximum allowable limit is " + config.maxChangeLimit + ".");
actor.printError("Your maximum allowable limit is " + config.maxChangeLimit + ".");
return;
}
}
session.setBlockChangeLimit(limit);
player.print("Block change limit set to " + limit + "."
actor.print("Block change limit set to " + limit + "."
+ (limit == config.defaultChangeLimit ? "" : " (Use //limit to go back to the default.)"));
}
@ -110,22 +110,22 @@ public class GeneralCommands {
desc = "Modify evaluation timeout time."
)
@CommandPermissions("worldedit.timeout")
public void timeout(Player player, LocalSession session,
public void timeout(Actor actor, LocalSession session,
@Arg(desc = "The timeout time to set", def = "")
Integer limit) {
LocalConfiguration config = worldEdit.getConfiguration();
boolean mayDisable = player.hasPermission("worldedit.timeout.unrestricted");
boolean mayDisable = actor.hasPermission("worldedit.timeout.unrestricted");
limit = limit == null ? config.calculationTimeout : Math.max(-1, limit);
if (!mayDisable && config.maxCalculationTimeout > -1) {
if (limit > config.maxCalculationTimeout) {
player.printError("Your maximum allowable timeout is " + config.maxCalculationTimeout + " ms.");
actor.printError("Your maximum allowable timeout is " + config.maxCalculationTimeout + " ms.");
return;
}
}
session.setTimeout(limit);
player.print("Timeout time set to " + limit + " ms."
actor.print("Timeout time set to " + limit + " ms."
+ (limit == config.calculationTimeout ? "" : " (Use //timeout to go back to the default.)"));
}
@ -134,18 +134,20 @@ public class GeneralCommands {
desc = "Toggle fast mode"
)
@CommandPermissions("worldedit.fast")
public void fast(Player player, LocalSession session, @Arg(desc = "The new fast mode state", def = "") Boolean fastMode) {
public void fast(Actor actor, LocalSession session,
@Arg(desc = "The new fast mode state", def = "")
Boolean fastMode) {
boolean hasFastMode = session.hasFastMode();
if (fastMode != null && fastMode == hasFastMode) {
player.printError("Fast mode already " + (fastMode ? "enabled" : "disabled") + ".");
actor.printError("Fast mode already " + (fastMode ? "enabled" : "disabled") + ".");
return;
}
if (hasFastMode) {
session.setFastMode(false);
BBC.FAST_DISABLED.send(player);
BBC.FAST_DISABLED.send(actor);
} else {
session.setFastMode(true);
BBC.FAST_ENABLED.send(player);
BBC.FAST_ENABLED.send(actor);
}
}
@ -154,14 +156,14 @@ public class GeneralCommands {
desc = "Sets the reorder mode of WorldEdit"
)
@CommandPermissions("worldedit.reorder")
public void reorderMode(Player player, LocalSession session,
public void reorderMode(Actor actor, LocalSession session,
@Arg(desc = "The reorder mode", def = "")
EditSession.ReorderMode reorderMode) {
if (reorderMode == null) {
player.print("The reorder mode is " + session.getReorderMode().getDisplayName());
actor.print("The reorder mode is " + session.getReorderMode().getDisplayName());
} else {
session.setReorderMode(reorderMode);
player.print("The reorder mode is now " + session.getReorderMode().getDisplayName());
actor.print("The reorder mode is now " + session.getReorderMode().getDisplayName());
}
}
@ -193,18 +195,33 @@ public class GeneralCommands {
}
@Command(
name = "/gmask",
aliases = {"gmask", "globalmask", "/globalmask"},
name = "/world",
desc = "Sets the world override"
)
@CommandPermissions("worldedit.world")
public void world(Actor actor, LocalSession session,
@Arg(desc = "The world override", def = "") World world) {
session.setWorldOverride(world);
if (world == null) {
actor.print("Removed world override.");
} else {
actor.print("Set the world override to " + world.getId() + ". (Use //world to go back to default)");
}
}
@Command(
name = "gmask",
aliases = {"/gmask"},
descFooter = "The global destination mask applies to all edits you do and masks based on the destination blocks (i.e. the blocks in the world).",
desc = "Set the global mask"
)
@CommandPermissions({"worldedit.global-mask", "worldedit.mask.global"})
public void gmask(Player player, LocalSession session, @Arg(desc = "The mask to set", def = "") Mask maskOpt) {
session.setMask(maskOpt);
if (maskOpt == null) {
BBC.MASK_DISABLED.send(player);
public void gmask(Actor actor, LocalSession session, @Arg(desc = "The mask to set", def = "") Mask mask) {
session.setMask(mask);
if (mask == null) {
BBC.MASK_DISABLED.send(actor);
} else {
BBC.MASK.send(player);
BBC.MASK.send(actor);
}
}

View File

@ -54,7 +54,7 @@ public final class RegistryConverter<V extends Keyed> implements ArgumentConvert
@SuppressWarnings("unchecked")
public static void register(CommandManager commandManager) {
ImmutableList.of(
BlockTypes.class,
BlockType.class,
BlockCategory.class,
ItemType.class,
ItemCategory.class,

View File

@ -27,6 +27,7 @@ import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.util.Location;
@ -91,8 +92,13 @@ public class LongRangeBuildTool extends BrushTool implements DoubleActionTraceTo
}
private Location getTargetFace(Player player) {
Location target = player.getBlockTraceFace(getRange(), true);
target = player.getBlockTrace(getRange(), true);
Location target;
Mask mask = getTraceMask();
if (this.range > -1) {
target = player.getBlockTrace(getRange(), true, mask);
} else {
target = player.getBlockTrace(MAX_RANGE, false, mask);
}
if (target == null) {
BBC.NO_BLOCK.send(player);
return null;

View File

@ -37,7 +37,7 @@ public class HollowCylinderBrush implements Brush {
@Override
public void build(EditSession editSession, BlockVector3 position, Pattern pattern, double size) throws MaxChangedBlocksException {
if (pattern == null) {
pattern = (BlockTypes.COBBLESTONE.getDefaultState());
pattern = BlockTypes.COBBLESTONE.getDefaultState();
}
editSession.makeCylinder(position, pattern, size, size, height, false);
}

View File

@ -31,7 +31,7 @@ public class HollowSphereBrush implements Brush {
@Override
public void build(EditSession editSession, BlockVector3 position, Pattern pattern, double size) throws MaxChangedBlocksException {
if (pattern == null) {
pattern = (BlockTypes.COBBLESTONE.getDefaultState());
pattern = BlockTypes.COBBLESTONE.getDefaultState();
}
editSession.makeSphere(position, pattern, size, size, size, false);
}

View File

@ -383,7 +383,8 @@ public abstract class AbstractPlayerActor implements Actor, Player, Cloneable {
@Override
public Location getBlockOn() {
return getLocation().setPosition(getLocation().setY(getLocation().getY() - 1).toVector().floor());
final Location location = getLocation();
return location.setPosition(location.setY(location.getY() - 1).toVector().floor());
}
@Override
@ -524,7 +525,8 @@ public abstract class AbstractPlayerActor implements Actor, Player, Cloneable {
@Override
public void setPosition(Vector3 pos) {
setPosition(pos, getLocation().getPitch(), getLocation().getYaw());
final Location location = getLocation();
setPosition(pos, location.getPitch(), location.getYaw());
}
@Override

View File

@ -393,7 +393,7 @@ public final class PlatformCommandManager {
registerSubCommands(
"brush",
ImmutableList.of("br", "/brush", "/br", "/tool", "tool"),
"Tool commands",
"Brushing commands",
BrushOptionsCommandsRegistration.builder(),
new BrushOptionsCommands(worldEdit)
);
@ -729,6 +729,7 @@ public final class PlatformCommandManager {
// exceptions without writing a hook into every dispatcher, we need to unwrap these
// exceptions and rethrow their converted form, if their is one.
try {
//Why the hell do we need to return an object if we aren't doing anything with it?
Object result = task.get();
} catch (Throwable t) {
// Use the exception converter to convert the exception if any of its causes

View File

@ -125,7 +125,7 @@ public class CuboidRegionSelector implements RegionSelector, CUIRegion {
public boolean selectPrimary(BlockVector3 position, SelectorLimits limits) {
checkNotNull(position);
if (position.equals(position1)) {
if (position1 != null && position.equals(position1)) {
return false;
}
@ -138,7 +138,7 @@ public class CuboidRegionSelector implements RegionSelector, CUIRegion {
public boolean selectSecondary(BlockVector3 position, SelectorLimits limits) {
checkNotNull(position);
if (position.equals(position2)) {
if (position2 != null && position.equals(position2)) {
return false;
}

View File

@ -34,6 +34,8 @@ import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.weather.WeatherType;
import com.sk89q.worldedit.world.weather.WeatherTypes;
import javax.annotation.Nullable;
import java.nio.file.Path;
import java.util.PriorityQueue;
@ -132,6 +134,23 @@ public abstract class AbstractWorld implements World {
return null;
}
@Override
public WeatherType getWeather() {
return WeatherTypes.CLEAR;
}
@Override
public long getRemainingWeatherDuration() {
return 0;
}
@Override
public void setWeather(WeatherType weatherType) {
}
@Override
public void setWeather(WeatherType weatherType, long duration) {
}
private class QueuedEffect implements Comparable<QueuedEffect> {
private final Vector3 position;
private final BlockType blockType;

View File

@ -63,6 +63,10 @@ public class NullWorld extends AbstractWorld {
return "null";
}
@Override
public String getId() {
return "null";
}
@Override
public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 position, B block, boolean notifyAndLight) throws WorldEditException {
return false;

View File

@ -106,10 +106,6 @@ public class ItemType implements RegistryItem, Keyed {
return this.blockType;
}
public void setBlockType(BlockType blockType) {
this.blockType = blockType;
}
public BaseItem getDefaultState() {
if (defaultState == null) {
this.defaultState = new BaseItemStack(this);

View File

@ -37,5 +37,7 @@ public interface CategoryRegistry<T extends Keyed> {
*/
Set<T> getCategorisedByName(String category);
Set<T> getAll(final Category<T> category);
default Set<T> getAll(final Category<T> category) {
return getCategorisedByName(category.getId());
}
}