Port ClipboardCommands, update to new Key

This commit is contained in:
Kenzie Togami 2019-04-18 12:34:57 -07:00
parent f33140b327
commit 37c993be16
No known key found for this signature in database
GPG Key ID: 5D200B325E157A81
7 changed files with 93 additions and 107 deletions

View File

@ -20,7 +20,6 @@
package com.sk89q.worldedit.bukkit;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Key;
import com.sk89q.bukkit.util.CommandInspector;
import com.sk89q.worldedit.extension.platform.Actor;
import org.bukkit.command.Command;
@ -28,6 +27,7 @@ import org.bukkit.command.CommandSender;
import org.enginehub.piston.CommandManager;
import org.enginehub.piston.CommandParameters;
import org.enginehub.piston.NoInputCommandParameters;
import org.enginehub.piston.inject.Key;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -76,7 +76,7 @@ class BukkitCommandInspector implements CommandInspector {
if (mapping.isPresent()) {
CommandParameters parameters = NoInputCommandParameters.builder()
.injectedValues(ImmutableMap.of(
Key.get(Actor.class), plugin.wrapCommandSender(sender)
Key.of(Actor.class), plugin.wrapCommandSender(sender)
))
.build();
return mapping.get().getCondition().satisfied(parameters);

View File

@ -22,7 +22,6 @@
package com.sk89q.worldedit.bukkit;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Key;
import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.entity.Player;
@ -43,6 +42,7 @@ import org.bukkit.inventory.EquipmentSlot;
import org.enginehub.piston.CommandManager;
import org.enginehub.piston.CommandParameters;
import org.enginehub.piston.NoInputCommandParameters;
import org.enginehub.piston.inject.Key;
/**
* Handles all events thrown in relation to a Player
@ -110,7 +110,7 @@ public class WorldEditListener implements Listener {
public void onPlayerCommand(PlayerCommandSendEvent event) {
CommandParameters parameters = NoInputCommandParameters.builder()
.injectedValues(ImmutableMap.of(
Key.get(Actor.class), plugin.wrapCommandSender(event.getPlayer())
Key.of(Actor.class), plugin.wrapCommandSender(event.getPlayer())
))
.build();
CommandManager commandManager = plugin.getWorldEdit().getPlatformManager().getPlatformCommandMananger().getCommandManager();

View File

@ -19,16 +19,11 @@
package com.sk89q.worldedit.command;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.sk89q.worldedit.command.util.Logging.LogMode.PLACEMENT;
import static com.sk89q.worldedit.command.util.Logging.LogMode.REGION;
import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandPermissions;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
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.entity.Player;
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
@ -48,42 +43,38 @@ import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.RegionSelector;
import com.sk89q.worldedit.regions.selector.CuboidRegionSelector;
import com.sk89q.worldedit.session.ClipboardHolder;
import com.sk89q.worldedit.util.command.binding.Switch;
import com.sk89q.worldedit.util.command.parametric.Optional;
import org.enginehub.piston.annotation.Command;
import org.enginehub.piston.annotation.CommandContainer;
import org.enginehub.piston.annotation.param.Arg;
import org.enginehub.piston.annotation.param.ArgFlag;
import org.enginehub.piston.annotation.param.Switch;
import static com.sk89q.worldedit.command.util.Logging.LogMode.PLACEMENT;
import static com.sk89q.worldedit.command.util.Logging.LogMode.REGION;
/**
* Clipboard commands.
*/
@CommandContainer(superTypes = CommandPermissionsConditionGenerator.Registration.class)
public class ClipboardCommands {
private final WorldEdit worldEdit;
/**
* Create a new instance.
*
* @param worldEdit reference to WorldEdit
*/
public ClipboardCommands(WorldEdit worldEdit) {
checkNotNull(worldEdit);
this.worldEdit = worldEdit;
public ClipboardCommands() {
}
@Command(
aliases = { "/copy" },
flags = "em",
desc = "Copy the selection to the clipboard",
help = "Copy the selection to the clipboard\n" +
"Flags:\n" +
" -e will also copy entities\n" +
" -m sets a source mask so that excluded blocks become air",
min = 0,
max = 0
name = "/copy",
desc = "Copy the selection to the clipboard"
)
@CommandPermissions("worldedit.clipboard.copy")
public void copy(Player player, LocalSession session, EditSession editSession,
@Selection Region region, @Switch('e') boolean copyEntities,
@Switch('m') Mask mask) throws WorldEditException {
@Selection Region region,
@Switch(name = 'e', desc = "Also copy entities")
boolean copyEntities,
@ArgFlag(name = 'm', desc = "Set the exclude mask, matching blocks become air", def = "")
Mask mask) throws WorldEditException {
BlockArrayClipboard clipboard = new BlockArrayClipboard(region);
clipboard.setOrigin(session.getPlacementPosition(player));
ForwardExtentCopy copy = new ForwardExtentCopy(editSession, region, clipboard, region.getMinimumPoint());
@ -98,22 +89,20 @@ public class ClipboardCommands {
}
@Command(
aliases = { "/cut" },
flags = "em",
usage = "[leave-id]",
name = "/cut",
desc = "Cut the selection to the clipboard",
help = "Copy the selection to the clipboard\n" +
"Flags:\n" +
" -e will also cut entities\n" +
" -m sets a source mask so that excluded blocks become air\n" +
"WARNING: Cutting and pasting entities cannot yet be undone!",
max = 1
descFooter = "WARNING: Cutting and pasting entities cannot be undone!"
)
@CommandPermissions("worldedit.clipboard.cut")
@Logging(REGION)
public void cut(Player player, LocalSession session, EditSession editSession,
@Selection Region region, @Optional("air") Pattern leavePattern, @Switch('e') boolean copyEntities,
@Switch('m') Mask mask) throws WorldEditException {
@Selection Region region,
@Arg(desc = "Pattern to leave in place of the selection", def = "air")
Pattern leavePattern,
@Switch(name = 'e', desc = "Also cut entities")
boolean copyEntities,
@ArgFlag(name = 'm', desc = "Set the exclude mask, matching blocks become air", def = "")
Mask mask) throws WorldEditException {
BlockArrayClipboard clipboard = new BlockArrayClipboard(region);
clipboard.setOrigin(session.getPlacementPosition(player));
@ -131,24 +120,18 @@ public class ClipboardCommands {
}
@Command(
aliases = { "/paste" },
usage = "",
flags = "sao",
desc = "Paste the clipboard's contents",
help =
"Pastes the clipboard's contents.\n" +
"Flags:\n" +
" -a skips air blocks\n" +
" -o pastes at the original position\n" +
" -s selects the region after pasting",
min = 0,
max = 0
name = "/paste",
desc = "Paste the clipboard's contents"
)
@CommandPermissions("worldedit.clipboard.paste")
@Logging(PLACEMENT)
public void paste(Player player, LocalSession session, EditSession editSession,
@Switch('a') boolean ignoreAirBlocks, @Switch('o') boolean atOrigin,
@Switch('s') boolean selectPasted) throws WorldEditException {
@Switch(name = 'a', desc = "Skip air blocks")
boolean ignoreAirBlocks,
@Switch(name = 'o', desc = "Paste at the original position")
boolean atOrigin,
@Switch(name = 's', desc = "Select the region after pasting")
boolean selectPasted) throws WorldEditException {
ClipboardHolder holder = session.getClipboard();
Clipboard clipboard = holder.getClipboard();
@ -156,10 +139,10 @@ public class ClipboardCommands {
BlockVector3 to = atOrigin ? clipboard.getOrigin() : session.getPlacementPosition(player);
Operation operation = holder
.createPaste(editSession)
.to(to)
.ignoreAirBlocks(ignoreAirBlocks)
.build();
.createPaste(editSession)
.to(to)
.ignoreAirBlocks(ignoreAirBlocks)
.build();
Operations.completeLegacy(operation);
if (selectPasted) {
@ -176,42 +159,43 @@ public class ClipboardCommands {
}
@Command(
aliases = { "/rotate" },
usage = "<y-axis> [<x-axis>] [<z-axis>]",
name = "/rotate",
desc = "Rotate the contents of the clipboard",
help = "Non-destructively rotate the contents of the clipboard.\n" +
"Angles are provided in degrees and a positive angle will result in a clockwise rotation. " +
"Multiple rotations can be stacked. Interpolation is not performed so angles should be a multiple of 90 degrees.\n"
descFooter = "Non-destructively rotate the contents of the clipboard.\n" +
"Angles are provided in degrees and a positive angle will result in a clockwise rotation. " +
"Multiple rotations can be stacked. Interpolation is not performed so angles should be a multiple of 90 degrees.\n"
)
@CommandPermissions("worldedit.clipboard.rotate")
public void rotate(Player player, LocalSession session, Double yRotate, @Optional Double xRotate, @Optional Double zRotate) throws WorldEditException {
if ((yRotate != null && Math.abs(yRotate % 90) > 0.001) ||
xRotate != null && Math.abs(xRotate % 90) > 0.001 ||
zRotate != null && Math.abs(zRotate % 90) > 0.001) {
public void rotate(Player player, LocalSession session,
@Arg(desc = "Amount to rotate on the y-axis")
double yRotate,
@Arg(desc = "Amount to rotate on the x-axis", def = "0")
double xRotate,
@Arg(desc = "Amount to rotate on the z-axis", def = "0")
double zRotate) throws WorldEditException {
if (Math.abs(yRotate % 90) > 0.001 ||
Math.abs(xRotate % 90) > 0.001 ||
Math.abs(zRotate % 90) > 0.001) {
player.printDebug("Note: Interpolation is not yet supported, so angles that are multiples of 90 is recommended.");
}
ClipboardHolder holder = session.getClipboard();
AffineTransform transform = new AffineTransform();
transform = transform.rotateY(-(yRotate != null ? yRotate : 0));
transform = transform.rotateX(-(xRotate != null ? xRotate : 0));
transform = transform.rotateZ(-(zRotate != null ? zRotate : 0));
transform = transform.rotateY(-yRotate);
transform = transform.rotateX(-xRotate);
transform = transform.rotateZ(-zRotate);
holder.setTransform(holder.getTransform().combine(transform));
player.print("The clipboard copy has been rotated.");
}
@Command(
aliases = { "/flip" },
usage = "[<direction>]",
desc = "Flip the contents of the clipboard",
help =
"Flips the contents of the clipboard across the point from which the copy was made.\n",
min = 0,
max = 1
name = "/flip",
desc = "Flip the contents of the clipboard across the origin"
)
@CommandPermissions("worldedit.clipboard.flip")
public void flip(Player player, LocalSession session,
@Optional(Direction.AIM) @Direction BlockVector3 direction) throws WorldEditException {
@Arg(desc = "The direction to flip, defaults to look direction.", def = Direction.AIM)
@Direction BlockVector3 direction) throws WorldEditException {
ClipboardHolder holder = session.getClipboard();
AffineTransform transform = new AffineTransform();
transform = transform.scale(direction.abs().multiply(-2).add(1, 1, 1).toVector3());
@ -220,11 +204,8 @@ public class ClipboardCommands {
}
@Command(
aliases = { "clearclipboard" },
usage = "",
desc = "Clear your clipboard",
min = 0,
max = 0
name = "clearclipboard",
desc = "Clear your clipboard"
)
@CommandPermissions("worldedit.clipboard.clear")
public void clearClipboard(Player player, LocalSession session) throws WorldEditException {

View File

@ -19,16 +19,16 @@
package com.sk89q.worldedit.command.util;
import com.google.inject.Key;
import com.sk89q.worldedit.extension.platform.Actor;
import org.enginehub.piston.Command;
import org.enginehub.piston.CommandParameters;
import org.enginehub.piston.inject.Key;
import java.util.Set;
public final class PermissionCondition implements Command.Condition {
private static final Key<Actor> ACTOR_KEY = Key.get(Actor.class);
private static final Key<Actor> ACTOR_KEY = Key.of(Actor.class);
private final Set<String> permissions;

View File

@ -20,7 +20,6 @@
package com.sk89q.worldedit.extension.platform;
import com.google.common.collect.ImmutableList;
import com.google.inject.Key;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.LocalSession;
@ -31,10 +30,11 @@ import com.sk89q.worldedit.command.BrushCommands;
import com.sk89q.worldedit.command.BrushCommandsRegistration;
import com.sk89q.worldedit.command.ChunkCommands;
import com.sk89q.worldedit.command.ChunkCommandsRegistration;
import com.sk89q.worldedit.command.ClipboardCommands;
import com.sk89q.worldedit.command.ClipboardCommandsRegistration;
import com.sk89q.worldedit.command.SchematicCommands;
import com.sk89q.worldedit.command.SchematicCommandsRegistration;
import com.sk89q.worldedit.command.argument.Arguments;
import com.sk89q.worldedit.command.argument.EditSessionHolder;
import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator;
import com.sk89q.worldedit.command.util.PermissionCondition;
import com.sk89q.worldedit.entity.Entity;
@ -47,7 +47,6 @@ import com.sk89q.worldedit.internal.command.CommandLoggingHandler;
import com.sk89q.worldedit.internal.command.UserCommandCompleter;
import com.sk89q.worldedit.internal.command.WorldEditBinding;
import com.sk89q.worldedit.internal.command.WorldEditExceptionConverter;
import com.sk89q.worldedit.session.SessionOwner;
import com.sk89q.worldedit.session.request.Request;
import com.sk89q.worldedit.util.command.parametric.ExceptionConverter;
import com.sk89q.worldedit.util.command.parametric.LegacyCommandsHandler;
@ -65,8 +64,8 @@ import org.enginehub.piston.exception.ConditionFailedException;
import org.enginehub.piston.exception.UsageException;
import org.enginehub.piston.gen.CommandCallListener;
import org.enginehub.piston.gen.CommandRegistration;
import org.enginehub.piston.inject.InjectedValueAccess;
import org.enginehub.piston.inject.InjectedValueStore;
import org.enginehub.piston.inject.Key;
import org.enginehub.piston.inject.MapBackedValueStore;
import org.enginehub.piston.inject.MemoizingValueAccess;
import org.enginehub.piston.part.SubCommandPart;
@ -200,13 +199,17 @@ public final class PlatformCommandMananger {
ChunkCommandsRegistration.builder(),
new ChunkCommands(worldEdit)
);
register(
commandManager,
ClipboardCommandsRegistration.builder(),
new ClipboardCommands()
);
// Unported commands are below. Delete once they're added to the main manager above.
/*
dispatcher = new CommandGraph()
.builder(builder)
.commands()
.registerMethods(new ClipboardCommands(worldEdit))
.registerMethods(new GeneralCommands(worldEdit))
.registerMethods(new GenerationCommands(worldEdit))
.registerMethods(new HistoryCommands(worldEdit))
@ -334,22 +337,22 @@ public final class PlatformCommandMananger {
LocalConfiguration config = worldEdit.getConfiguration();
InjectedValueStore store = MapBackedValueStore.create();
store.injectValue(Key.get(Actor.class), ValueProvider.constant(actor));
store.injectValue(Key.of(Actor.class), ValueProvider.constant(actor));
if (actor instanceof Player) {
store.injectValue(Key.get(Player.class), ValueProvider.constant((Player) actor));
store.injectValue(Key.of(Player.class), ValueProvider.constant((Player) actor));
}
store.injectValue(Key.get(Arguments.class), ValueProvider.constant(event::getArguments));
store.injectValue(Key.get(LocalSession.class),
store.injectValue(Key.of(Arguments.class), ValueProvider.constant(event::getArguments));
store.injectValue(Key.of(LocalSession.class),
context -> {
LocalSession localSession = worldEdit.getSessionManager().get(actor);
localSession.tellVersion(actor);
return Optional.of(localSession);
});
store.injectValue(Key.get(EditSession.class),
store.injectValue(Key.of(EditSession.class),
context -> {
LocalSession localSession = context.injectedValue(Key.get(LocalSession.class))
LocalSession localSession = context.injectedValue(Key.of(LocalSession.class))
.orElseThrow(() -> new IllegalStateException("No LocalSession"));
return context.injectedValue(Key.get(Player.class))
return context.injectedValue(Key.of(Player.class))
.map(player -> {
EditSession editSession = localSession.createEditSession(player);
editSession.enableStandardMode();
@ -401,7 +404,7 @@ public final class PlatformCommandMananger {
}
} finally {
Optional<EditSession> editSessionOpt =
context.injectedValueIfMemoized(Key.get(EditSession.class));
context.injectedValueIfMemoized(Key.of(EditSession.class));
if (editSessionOpt.isPresent()) {
EditSession editSession = editSessionOpt.get();
@ -432,8 +435,8 @@ public final class PlatformCommandMananger {
@Subscribe
public void handleCommandSuggestion(CommandSuggestionEvent event) {
try {
commandManager.injectValue(Key.get(Actor.class), ValueProvider.constant(event.getActor()));
commandManager.injectValue(Key.get(Arguments.class), ValueProvider.constant(event::getArguments));
commandManager.injectValue(Key.of(Actor.class), ValueProvider.constant(event.getActor()));
commandManager.injectValue(Key.of(Arguments.class), ValueProvider.constant(event::getArguments));
// TODO suggestions
} catch (CommandException e) {
event.getActor().printError(e.getMessage());

View File

@ -19,6 +19,8 @@
package com.sk89q.worldedit.internal.annotation;
import org.enginehub.piston.inject.InjectAnnotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@ -29,6 +31,6 @@ import java.lang.annotation.Target;
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@InjectAnnotation
public @interface Selection {
}

View File

@ -19,16 +19,16 @@
package com.sk89q.worldedit.internal.command;
import com.google.inject.Key;
import com.sk89q.worldedit.command.util.Logging;
import com.sk89q.worldedit.IncompleteRegionException;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.command.util.Logging;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.math.Vector3;
import org.enginehub.piston.CommandParameters;
import org.enginehub.piston.gen.CommandCallListener;
import org.enginehub.piston.inject.Key;
import java.lang.reflect.Method;
import java.util.Optional;
@ -72,7 +72,7 @@ public class CommandLoggingHandler implements CommandCallListener, AutoCloseable
logMode = loggingAnnotation.value();
}
Optional<Player> playerOpt = parameters.injectedValue(Key.get(Actor.class))
Optional<Player> playerOpt = parameters.injectedValue(Key.of(Actor.class))
.filter(Player.class::isInstance)
.map(Player.class::cast);