mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-07-10 07:08:35 +00:00
some bindings
This commit is contained in:
@ -22,55 +22,29 @@ package com.sk89q.worldedit.extension.platform;
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
import com.boydti.fawe.Fawe;
|
||||
import com.boydti.fawe.command.AnvilCommands;
|
||||
import com.boydti.fawe.config.BBC;
|
||||
import com.boydti.fawe.config.Settings;
|
||||
import com.boydti.fawe.object.FawePlayer;
|
||||
import com.boydti.fawe.object.task.ThrowableSupplier;
|
||||
import com.boydti.fawe.util.TaskManager;
|
||||
import com.boydti.fawe.wrappers.LocationMaskedPlayerWrapper;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import com.sk89q.minecraft.util.commands.CommandLocals;
|
||||
import com.sk89q.worldedit.EditSession;
|
||||
import com.sk89q.worldedit.IncompleteRegionException;
|
||||
import com.sk89q.worldedit.LocalConfiguration;
|
||||
import com.sk89q.worldedit.LocalSession;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.command.ApplyBrushCommands;
|
||||
import com.sk89q.worldedit.command.BiomeCommands;
|
||||
//import com.sk89q.worldedit.command.BiomeCommandsRegistration;
|
||||
import com.sk89q.worldedit.command.BrushCommands;
|
||||
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.ExpandCommands;
|
||||
import com.sk89q.worldedit.command.GeneralCommands;
|
||||
//import com.sk89q.worldedit.command.GeneralCommandsRegistration;
|
||||
import com.sk89q.worldedit.command.GenerationCommands;
|
||||
import com.sk89q.worldedit.command.HistoryCommands;
|
||||
//import com.sk89q.worldedit.command.HistoryCommandsRegistration;
|
||||
import com.sk89q.worldedit.command.NavigationCommands;
|
||||
//import com.sk89q.worldedit.command.NavigationCommandsRegistration;
|
||||
import com.sk89q.worldedit.command.PaintBrushCommands;
|
||||
import com.sk89q.worldedit.command.RegionCommands;
|
||||
import com.sk89q.worldedit.command.SchematicCommands;
|
||||
//import com.sk89q.worldedit.command.SchematicCommandsRegistration;
|
||||
import com.sk89q.worldedit.command.ScriptingCommands;
|
||||
import com.sk89q.worldedit.command.SelectionCommands;
|
||||
import com.sk89q.worldedit.command.SnapshotCommands;
|
||||
//import com.sk89q.worldedit.command.SnapshotCommandsRegistration;
|
||||
import com.sk89q.worldedit.command.SnapshotUtilCommands;
|
||||
import com.sk89q.worldedit.command.SuperPickaxeCommands;
|
||||
//import com.sk89q.worldedit.command.SuperPickaxeCommandsRegistration;
|
||||
import com.sk89q.worldedit.command.ToolCommands;
|
||||
import com.sk89q.worldedit.command.ToolUtilCommands;
|
||||
import com.sk89q.worldedit.command.UtilityCommands;
|
||||
import com.sk89q.worldedit.command.WorldEditCommands;
|
||||
//import com.sk89q.worldedit.command.WorldEditCommandsRegistration;
|
||||
import com.sk89q.worldedit.command.argument.Arguments;
|
||||
import com.sk89q.worldedit.command.argument.BooleanConverter;
|
||||
@ -84,9 +58,7 @@ import com.sk89q.worldedit.command.argument.RegionFactoryConverter;
|
||||
import com.sk89q.worldedit.command.argument.RegistryConverter;
|
||||
import com.sk89q.worldedit.command.argument.VectorConverter;
|
||||
import com.sk89q.worldedit.command.argument.ZonedDateTimeConverter;
|
||||
import com.sk89q.worldedit.command.tool.brush.Brush;
|
||||
import com.sk89q.worldedit.command.util.CommandPermissions;
|
||||
import com.sk89q.worldedit.command.util.CommandQueued;
|
||||
import com.sk89q.worldedit.command.util.CommandQueuedCondition;
|
||||
import com.sk89q.worldedit.command.util.PermissionCondition;
|
||||
import com.sk89q.worldedit.command.util.SubCommandPermissionCondition;
|
||||
@ -94,15 +66,17 @@ import com.sk89q.worldedit.entity.Entity;
|
||||
import com.sk89q.worldedit.entity.Player;
|
||||
import com.sk89q.worldedit.event.platform.CommandEvent;
|
||||
import com.sk89q.worldedit.event.platform.CommandSuggestionEvent;
|
||||
import com.sk89q.worldedit.extension.platform.binding.AnnotatedBindings;
|
||||
import com.sk89q.worldedit.extension.platform.binding.CommandBindings;
|
||||
import com.sk89q.worldedit.extension.platform.binding.ConsumeBindings;
|
||||
import com.sk89q.worldedit.extension.platform.binding.ProvideBindings;
|
||||
import com.sk89q.worldedit.extent.Extent;
|
||||
import com.sk89q.worldedit.internal.annotation.Selection;
|
||||
import com.sk89q.worldedit.internal.command.CommandArgParser;
|
||||
import com.sk89q.worldedit.internal.command.CommandLoggingHandler;
|
||||
import com.sk89q.worldedit.internal.command.CommandRegistrationHandler;
|
||||
import com.sk89q.worldedit.internal.command.exception.ExceptionConverter;
|
||||
import com.sk89q.worldedit.internal.command.exception.WorldEditExceptionConverter;
|
||||
import com.sk89q.worldedit.internal.util.Substring;
|
||||
import com.sk89q.worldedit.regions.Region;
|
||||
import com.sk89q.worldedit.scripting.CommandScriptLoader;
|
||||
import com.sk89q.worldedit.session.request.Request;
|
||||
import com.sk89q.worldedit.util.auth.AuthorizationException;
|
||||
@ -152,7 +126,6 @@ import org.enginehub.piston.part.SubCommandPart;
|
||||
import org.enginehub.piston.suggestion.Suggestion;
|
||||
import org.enginehub.piston.util.HelpGenerator;
|
||||
import org.enginehub.piston.util.ValueProvider;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -246,32 +219,16 @@ public final class PlatformCommandManager {
|
||||
}
|
||||
|
||||
private void registerAlwaysInjectedValues() {
|
||||
globalInjectedValues.injectValue(Key.of(Region.class, Selection.class),
|
||||
context -> {
|
||||
LocalSession localSession = context.injectedValue(Key.of(LocalSession.class))
|
||||
.orElseThrow(() -> new IllegalStateException("No LocalSession"));
|
||||
return context.injectedValue(Key.of(Player.class))
|
||||
.map(player -> {
|
||||
try {
|
||||
return localSession.getSelection(player.getWorld());
|
||||
} catch (IncompleteRegionException e) {
|
||||
exceptionConverter.convert(e);
|
||||
throw new AssertionError("Should have thrown a new exception.");
|
||||
}
|
||||
});
|
||||
});
|
||||
globalInjectedValues.injectValue(Key.of(EditSession.class),
|
||||
context -> {
|
||||
LocalSession localSession = context.injectedValue(Key.of(LocalSession.class))
|
||||
.orElseThrow(() -> new IllegalStateException("No LocalSession"));
|
||||
return context.injectedValue(Key.of(Player.class))
|
||||
.map(player -> {
|
||||
EditSession editSession = localSession.createEditSession(player);
|
||||
editSession.enableStandardMode();
|
||||
return editSession;
|
||||
});
|
||||
});
|
||||
globalInjectedValues.injectValue(Key.of(InjectedValueAccess.class), context -> Optional.of(context));
|
||||
register(new AnnotatedBindings(worldEdit));
|
||||
register(new CommandBindings(worldEdit));
|
||||
register(new ConsumeBindings(worldEdit));
|
||||
register(new ProvideBindings(worldEdit));
|
||||
register(new ProvideBindings(worldEdit));
|
||||
}
|
||||
|
||||
public void register(Object classWithMethods) {
|
||||
// TODO NOT IMPLEMENTED - register the following using a custom processor / annotations
|
||||
}
|
||||
|
||||
private <CI> void registerSubCommands(String name, List<String> aliases, String desc,
|
||||
@ -306,7 +263,7 @@ public final class PlatformCommandManager {
|
||||
});
|
||||
}
|
||||
|
||||
private void registerAllCommands() {
|
||||
public void registerAllCommands() {
|
||||
if (Settings.IMP.ENABLED_COMPONENTS.COMMANDS) {
|
||||
// TODO NOT IMPLEMENTED dunno why these have issues generating
|
||||
// registerSubCommands(
|
||||
|
@ -0,0 +1,49 @@
|
||||
package com.sk89q.worldedit.extension.platform.binding;
|
||||
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.extension.input.InputParseException;
|
||||
import com.sk89q.worldedit.internal.annotation.Validate;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
public class AnnotatedBindings extends Bindings {
|
||||
|
||||
private final WorldEdit worldEdit;
|
||||
|
||||
public AnnotatedBindings(WorldEdit worldEdit) {
|
||||
this.worldEdit = worldEdit;
|
||||
}
|
||||
|
||||
@Validate()
|
||||
public String getText(String argument, Validate modifier) {
|
||||
return validate(argument, modifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a string value using relevant modifiers.
|
||||
*
|
||||
* @param string the string
|
||||
* @param modifiers the list of modifiers to scan
|
||||
* @throws InputParseException on a validation error
|
||||
*/
|
||||
private static String validate(String string, Annotation... modifiers)
|
||||
{
|
||||
if (string != null) {
|
||||
for (Annotation modifier : modifiers) {
|
||||
if (modifier instanceof Validate) {
|
||||
Validate validate = (Validate) modifier;
|
||||
|
||||
if (!validate.value().isEmpty()) {
|
||||
if (!string.matches(validate.value())) {
|
||||
throw new InputParseException(
|
||||
String.format(
|
||||
"The given text doesn't match the right format (technically speaking, the 'format' is %s)",
|
||||
validate.value()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return string;
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package com.sk89q.worldedit.extension.platform.binding;
|
||||
|
||||
public class Bindings {
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.sk89q.worldedit.extension.platform.binding;
|
||||
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
|
||||
public class CommandBindings extends Bindings {
|
||||
private final WorldEdit worldEdit;
|
||||
|
||||
public CommandBindings(WorldEdit worldEdit) {
|
||||
this.worldEdit = worldEdit;
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.sk89q.worldedit.extension.platform.binding;
|
||||
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.internal.annotation.Validate;
|
||||
|
||||
public class ConsumeBindings extends Bindings {
|
||||
private final WorldEdit worldEdit;
|
||||
|
||||
public ConsumeBindings(WorldEdit worldEdit) {
|
||||
this.worldEdit = worldEdit;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,394 @@
|
||||
package com.sk89q.worldedit.extension.platform.binding;
|
||||
|
||||
import com.boydti.fawe.Fawe;
|
||||
import com.boydti.fawe.object.extent.NullExtent;
|
||||
import com.boydti.fawe.object.extent.ResettableExtent;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.entity.Entity;
|
||||
import com.sk89q.worldedit.extension.factory.DefaultTransformParser;
|
||||
import com.sk89q.worldedit.extension.input.InputParseException;
|
||||
import com.sk89q.worldedit.extension.input.ParserContext;
|
||||
import com.sk89q.worldedit.extension.platform.Actor;
|
||||
import com.sk89q.worldedit.extent.Extent;
|
||||
import com.sk89q.worldedit.internal.annotation.Range;
|
||||
import com.sk89q.worldedit.internal.expression.Expression;
|
||||
import com.sk89q.worldedit.internal.expression.ExpressionException;
|
||||
import com.sk89q.worldedit.internal.expression.runtime.EvaluationException;
|
||||
import com.sk89q.worldedit.math.BlockVector2;
|
||||
import com.sk89q.worldedit.math.BlockVector3;
|
||||
import com.sk89q.worldedit.math.Vector2;
|
||||
import com.sk89q.worldedit.math.Vector3;
|
||||
import com.sk89q.worldedit.world.World;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Locale;
|
||||
|
||||
public class PrimitiveBindings extends Bindings {
|
||||
|
||||
/*
|
||||
Parsers
|
||||
*/
|
||||
public Expression getExpression(String argument) throws ExpressionException {
|
||||
try {
|
||||
return new Expression(Double.parseDouble(argument));
|
||||
} catch (NumberFormatException e1) {
|
||||
try {
|
||||
Expression expression = Expression.compile(argument);
|
||||
expression.optimize();
|
||||
return expression;
|
||||
} catch (EvaluationException e) {
|
||||
throw new InputParseException(String.format(
|
||||
"Expected '%s' to be a valid number (or a valid mathematical expression)", argument));
|
||||
} catch (ExpressionException e) {
|
||||
throw new InputParseException(String.format(
|
||||
"Expected '%s' to be a number or valid math expression (error: %s)", argument, e.getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an {@link com.sk89q.worldedit.extent.Extent} from a {@link ArgumentStack}.
|
||||
*
|
||||
* @param context the context
|
||||
* @return an extent
|
||||
* @throws InputParseException on other error
|
||||
*/
|
||||
public ResettableExtent getResettableExtent(Actor actor, String argument) throws InputParseException {
|
||||
if (argument.equalsIgnoreCase("#null")) {
|
||||
return new NullExtent();
|
||||
}
|
||||
DefaultTransformParser parser = Fawe.get().getTransformParser();
|
||||
ParserContext parserContext = new ParserContext();
|
||||
if (actor instanceof Entity) {
|
||||
Extent extent = ((Entity) actor).getExtent();
|
||||
if (extent instanceof World) {
|
||||
parserContext.setWorld((World) extent);
|
||||
}
|
||||
}
|
||||
parserContext.setSession(WorldEdit.getInstance().getSessionManager().get(actor));
|
||||
return parser.parseFromInput(argument, parserContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a type from a {@link ArgumentStack}.
|
||||
*
|
||||
* @param context the context
|
||||
* @return the requested type
|
||||
* @throws InputParseException on error
|
||||
*/
|
||||
public Boolean getBoolean(String argument) {
|
||||
switch (argument.toLowerCase(Locale.ROOT)) {
|
||||
case "":
|
||||
return null;
|
||||
case "true":
|
||||
case "yes":
|
||||
case "on":
|
||||
case "y":
|
||||
case "1":
|
||||
case "t":
|
||||
return true;
|
||||
case "false":
|
||||
case "no":
|
||||
case "off":
|
||||
case "f":
|
||||
case "n":
|
||||
case "0":
|
||||
return false;
|
||||
default:
|
||||
throw new InputParseException("Invalid boolean " + argument);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a type from a {@link ArgumentStack}.
|
||||
*
|
||||
* @param context the context
|
||||
* @return the requested type
|
||||
* @throws InputParseException on error
|
||||
*/
|
||||
public Vector3 getVector3(String argument) {
|
||||
String radiusString = argument;
|
||||
String[] radii = radiusString.split(",");
|
||||
final double radiusX, radiusY, radiusZ;
|
||||
switch (radii.length) {
|
||||
case 1:
|
||||
radiusX = radiusY = radiusZ = Math.max(1, PrimitiveBindings.parseNumericInput(radii[0]));
|
||||
break;
|
||||
|
||||
case 3:
|
||||
radiusX = Math.max(1, PrimitiveBindings.parseNumericInput(radii[0]));
|
||||
radiusY = Math.max(1, PrimitiveBindings.parseNumericInput(radii[1]));
|
||||
radiusZ = Math.max(1, PrimitiveBindings.parseNumericInput(radii[2]));
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new InputParseException("You must either specify 1 or 3 radius values.");
|
||||
}
|
||||
return Vector3.at(radiusX, radiusY, radiusZ);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets a type from a {@link ArgumentStack}.
|
||||
*
|
||||
* @param context the context
|
||||
* @return the requested type
|
||||
* @throws InputParseException on error
|
||||
*/
|
||||
public Vector2 getVector2(String argument) {
|
||||
String radiusString = argument;
|
||||
String[] radii = radiusString.split(",");
|
||||
final double radiusX, radiusZ;
|
||||
switch (radii.length) {
|
||||
case 1:
|
||||
radiusX = radiusZ = Math.max(1, PrimitiveBindings.parseNumericInput(radii[0]));
|
||||
break;
|
||||
|
||||
case 2:
|
||||
radiusX = Math.max(1, PrimitiveBindings.parseNumericInput(radii[0]));
|
||||
radiusZ = Math.max(1, PrimitiveBindings.parseNumericInput(radii[1]));
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new InputParseException("You must either specify 1 or 2 radius values.");
|
||||
}
|
||||
return Vector2.at(radiusX, radiusZ);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a type from a {@link ArgumentStack}.
|
||||
*
|
||||
* @param context the context
|
||||
* @return the requested type
|
||||
* @throws InputParseException on error
|
||||
*/
|
||||
public BlockVector3 getBlockVector3(String argument) {
|
||||
String radiusString = argument;
|
||||
String[] radii = radiusString.split(",");
|
||||
final double radiusX, radiusY, radiusZ;
|
||||
switch (radii.length) {
|
||||
case 1:
|
||||
radiusX = radiusY = radiusZ = Math.max(1, PrimitiveBindings.parseNumericInput(radii[0]));
|
||||
break;
|
||||
|
||||
case 3:
|
||||
radiusX = Math.max(1, PrimitiveBindings.parseNumericInput(radii[0]));
|
||||
radiusY = Math.max(1, PrimitiveBindings.parseNumericInput(radii[1]));
|
||||
radiusZ = Math.max(1, PrimitiveBindings.parseNumericInput(radii[2]));
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new InputParseException("You must either specify 1 or 3 radius values.");
|
||||
}
|
||||
return BlockVector3.at(radiusX, radiusY, radiusZ);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets a type from a {@link ArgumentStack}.
|
||||
*
|
||||
* @param context the context
|
||||
* @return the requested type
|
||||
* @throws InputParseException on error
|
||||
*/
|
||||
public BlockVector2 getBlockVector2(String argument) {
|
||||
String radiusString = argument;
|
||||
String[] radii = radiusString.split(",");
|
||||
final double radiusX, radiusZ;
|
||||
switch (radii.length) {
|
||||
case 1:
|
||||
radiusX = radiusZ = Math.max(1, PrimitiveBindings.parseNumericInput(radii[0]));
|
||||
break;
|
||||
|
||||
case 2:
|
||||
radiusX = Math.max(1, PrimitiveBindings.parseNumericInput(radii[0]));
|
||||
radiusZ = Math.max(1, PrimitiveBindings.parseNumericInput(radii[1]));
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new InputParseException("You must either specify 1 or 2 radius values.");
|
||||
}
|
||||
return BlockVector2.at(radiusX, radiusZ);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
*/
|
||||
|
||||
public Long getLong(String argument, Annotation... modifiers) {
|
||||
try {
|
||||
Long v = Long.parseLong(argument);
|
||||
validate(v, modifiers);
|
||||
return v;
|
||||
|
||||
} catch (NumberFormatException ignore) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static void validate(long number, Annotation... modifiers) {
|
||||
for (Annotation modifier : modifiers) {
|
||||
if (modifier instanceof Range) {
|
||||
Range range = (Range) modifier;
|
||||
if (number < range.min()) {
|
||||
throw new InputParseException(
|
||||
String.format(
|
||||
"A valid value is greater than or equal to %s " +
|
||||
"(you entered %s)", range.min(), number));
|
||||
} else if (number > range.max()) {
|
||||
throw new InputParseException(
|
||||
String.format(
|
||||
"A valid value is less than or equal to %s " +
|
||||
"(you entered %s)", range.max(), number));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to parse numeric input as either a number or a mathematical expression.
|
||||
*
|
||||
* @param input input
|
||||
* @return a number
|
||||
* @throws InputParseException thrown on parse error
|
||||
*/
|
||||
public static
|
||||
@Nullable
|
||||
Double parseNumericInput(@Nullable String input) {
|
||||
if (input == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return Double.parseDouble(input);
|
||||
} catch (NumberFormatException e1) {
|
||||
try {
|
||||
Expression expression = Expression.compile(input);
|
||||
return expression.evaluate();
|
||||
} catch (EvaluationException e) {
|
||||
throw new InputParseException(String.format(
|
||||
"Expected '%s' to be a valid number (or a valid mathematical expression)", input));
|
||||
} catch (ExpressionException e) {
|
||||
throw new InputParseException(String.format(
|
||||
"Expected '%s' to be a number or valid math expression (error: %s)", input, e.getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a type from a {@link ArgumentStack}.
|
||||
*
|
||||
* @param context the context
|
||||
* @param modifiers a list of modifiers
|
||||
* @return the requested type
|
||||
* @throws InputParseException on error
|
||||
*/
|
||||
public Integer getInteger(String argument, Annotation[] modifiers) {
|
||||
Double v = parseNumericInput(argument);
|
||||
if (v != null) {
|
||||
int intValue = v.intValue();
|
||||
validate(intValue, modifiers);
|
||||
return intValue;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a type from a {@link ArgumentStack}.
|
||||
*
|
||||
* @param context the context
|
||||
* @param modifiers a list of modifiers
|
||||
* @return the requested type
|
||||
* @throws InputParseException on error
|
||||
*/
|
||||
public Short getShort(String argument, Annotation[] modifiers) {
|
||||
Integer v = getInteger(argument, modifiers);
|
||||
if (v != null) {
|
||||
return v.shortValue();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a type from a {@link ArgumentStack}.
|
||||
*
|
||||
* @param context the context
|
||||
* @param modifiers a list of modifiers
|
||||
* @return the requested type
|
||||
* @throws InputParseException on error
|
||||
*/
|
||||
public Double getDouble(String argument, Annotation[] modifiers) {
|
||||
Double v = parseNumericInput(argument);
|
||||
if (v != null) {
|
||||
validate(v, modifiers);
|
||||
return v;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a type from a {@link ArgumentStack}.
|
||||
*
|
||||
* @param context the context
|
||||
* @param modifiers a list of modifiers
|
||||
* @return the requested type
|
||||
* @throws InputParseException on error
|
||||
*/
|
||||
public Float getFloat(String argument, Annotation[] modifiers) {
|
||||
Double v = getDouble(argument, modifiers);
|
||||
if (v != null) {
|
||||
return v.floatValue();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a number value using relevant modifiers.
|
||||
*
|
||||
* @param number the number
|
||||
* @param modifiers the list of modifiers to scan
|
||||
* @throws InputParseException on a validation error
|
||||
*/
|
||||
private static void validate(double number, Annotation[] modifiers)
|
||||
{
|
||||
for (Annotation modifier : modifiers) {
|
||||
if (modifier instanceof Range) {
|
||||
Range range = (Range) modifier;
|
||||
if (number < range.min()) {
|
||||
throw new InputParseException(
|
||||
String.format("A valid value is greater than or equal to %s (you entered %s)", range.min(), number));
|
||||
} else if (number > range.max()) {
|
||||
throw new InputParseException(
|
||||
String.format("A valid value is less than or equal to %s (you entered %s)", range.max(), number));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a number value using relevant modifiers.
|
||||
*
|
||||
* @param number the number
|
||||
* @param modifiers the list of modifiers to scan
|
||||
* @throws InputParseException on a validation error
|
||||
*/
|
||||
private static void validate(int number, Annotation[] modifiers)
|
||||
{
|
||||
for (Annotation modifier : modifiers) {
|
||||
if (modifier instanceof Range) {
|
||||
Range range = (Range) modifier;
|
||||
if (number < range.min()) {
|
||||
throw new InputParseException(
|
||||
String.format(
|
||||
"A valid value is greater than or equal to %s (you entered %s)", range.min(), number));
|
||||
} else if (number > range.max()) {
|
||||
throw new InputParseException(
|
||||
String.format(
|
||||
"A valid value is less than or equal to %s (you entered %s)", range.max(), number));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,231 @@
|
||||
package com.sk89q.worldedit.extension.platform.binding;
|
||||
|
||||
import com.boydti.fawe.object.FawePlayer;
|
||||
import com.boydti.fawe.util.MathMan;
|
||||
import com.boydti.fawe.util.TextureUtil;
|
||||
import com.boydti.fawe.util.image.ImageUtil;
|
||||
import com.sk89q.worldedit.EditSession;
|
||||
import com.sk89q.worldedit.LocalSession;
|
||||
import com.sk89q.worldedit.UnknownDirectionException;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.WorldEditException;
|
||||
import com.sk89q.worldedit.entity.Entity;
|
||||
import com.sk89q.worldedit.entity.Player;
|
||||
import com.sk89q.worldedit.extension.input.InputParseException;
|
||||
import com.sk89q.worldedit.extension.input.NoMatchException;
|
||||
import com.sk89q.worldedit.extension.input.ParserContext;
|
||||
import com.sk89q.worldedit.extension.platform.Actor;
|
||||
import com.sk89q.worldedit.extension.platform.Capability;
|
||||
import com.sk89q.worldedit.extent.Extent;
|
||||
import com.sk89q.worldedit.internal.annotation.Direction;
|
||||
import com.sk89q.worldedit.internal.annotation.Selection;
|
||||
import com.sk89q.worldedit.math.BlockVector3;
|
||||
import com.sk89q.worldedit.regions.Region;
|
||||
import com.sk89q.worldedit.session.request.Request;
|
||||
import com.sk89q.worldedit.util.TreeGenerator;
|
||||
import com.sk89q.worldedit.world.World;
|
||||
import com.sk89q.worldedit.world.biome.BiomeType;
|
||||
import com.sk89q.worldedit.world.biome.BiomeTypes;
|
||||
import com.sk89q.worldedit.world.biome.Biomes;
|
||||
import com.sk89q.worldedit.world.block.BaseBlock;
|
||||
import com.sk89q.worldedit.world.block.BlockState;
|
||||
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
||||
import com.sk89q.worldedit.world.block.BlockType;
|
||||
import com.sk89q.worldedit.world.registry.BiomeRegistry;
|
||||
import org.enginehub.piston.inject.InjectedValueAccess;
|
||||
import org.enginehub.piston.inject.InjectedValueStore;
|
||||
import org.enginehub.piston.inject.Key;
|
||||
import org.enginehub.piston.util.ValueProvider;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.net.URI;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ProvideBindings extends Bindings {
|
||||
private final WorldEdit worldEdit;
|
||||
|
||||
public ProvideBindings(WorldEdit worldEdit) {
|
||||
this.worldEdit = worldEdit;
|
||||
}
|
||||
|
||||
/*
|
||||
Provided
|
||||
*/
|
||||
|
||||
public Player getPlayer(Actor actor) {
|
||||
if (actor.isPlayer()) {
|
||||
return (Player) actor;
|
||||
}
|
||||
throw new InputParseException("This command must be used with a player.");
|
||||
}
|
||||
|
||||
public LocalSession getLocalSession(Player player) {
|
||||
return worldEdit.getSessionManager().get(player);
|
||||
}
|
||||
|
||||
public EditSession editSession(LocalSession localSession, Player player) {
|
||||
EditSession editSession = localSession.createEditSession(player);
|
||||
editSession.enableStandardMode();
|
||||
return editSession;
|
||||
}
|
||||
|
||||
@Selection
|
||||
public Region selection(LocalSession localSession, Player player) {
|
||||
return localSession.getSelection(player.getWorld());
|
||||
}
|
||||
|
||||
public TextureUtil getTexture(LocalSession session) {
|
||||
return session.getTextureUtil();
|
||||
}
|
||||
|
||||
public class ImageUri {
|
||||
public final URI uri;
|
||||
private BufferedImage image;
|
||||
|
||||
ImageUri(URI uri) {
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
public BufferedImage load() {
|
||||
if (image != null) {
|
||||
return image;
|
||||
}
|
||||
return image = ImageUtil.load(uri);
|
||||
}
|
||||
}
|
||||
|
||||
public Extent getExtent(Actor actor, InjectedValueAccess access, InjectedValueStore store) {
|
||||
Optional<EditSession> editSessionOpt = access.injectedValue(Key.of(EditSession.class));
|
||||
if (editSessionOpt.isPresent()) {
|
||||
return editSessionOpt.get();
|
||||
}
|
||||
Extent extent = Request.request().getExtent();
|
||||
if (extent != null) {
|
||||
return extent;
|
||||
}
|
||||
LocalSession session = WorldEdit.getInstance().getSessionManager().get(actor);
|
||||
Player plr = getPlayer(actor);
|
||||
EditSession editSession = editSession(getLocalSession(plr), plr);
|
||||
store.injectValue(Key.of(EditSession.class), ValueProvider.constant(editSession));
|
||||
return editSession;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an {@link com.boydti.fawe.object.FawePlayer} from a {@link ArgumentStack}.
|
||||
*
|
||||
* @param context the context
|
||||
* @return a FawePlayer
|
||||
* @throws ParameterException on other error
|
||||
*/
|
||||
public FawePlayer getFawePlayer(Actor actor) throws InputParseException {
|
||||
return FawePlayer.wrap(actor);
|
||||
}
|
||||
|
||||
/*
|
||||
Parsed
|
||||
*/
|
||||
public ImageUri getImage(String argument) {
|
||||
return new ImageUri(ImageUtil.getImageURI(argument));
|
||||
}
|
||||
|
||||
public BlockType blockType(Actor actor, String argument) {
|
||||
return blockState(actor, argument).getBlockType();
|
||||
}
|
||||
|
||||
public BlockStateHolder blockStateHolder(Actor actor, String argument) {
|
||||
return blockState(actor, argument);
|
||||
}
|
||||
|
||||
public BlockState blockState(Actor actor, String argument) {
|
||||
return baseBlock(actor, argument).toBlockState();
|
||||
}
|
||||
|
||||
public BaseBlock baseBlock(Actor actor, String argument) {
|
||||
ParserContext parserContext = new ParserContext();
|
||||
parserContext.setActor(actor);
|
||||
if (actor instanceof Entity) {
|
||||
Extent extent = ((Entity) actor).getExtent();
|
||||
if (extent instanceof World) {
|
||||
parserContext.setWorld((World) extent);
|
||||
}
|
||||
}
|
||||
parserContext.setSession(worldEdit.getSessionManager().get(actor));
|
||||
try {
|
||||
return worldEdit.getBlockFactory().parseFromInput(argument, parserContext);
|
||||
} catch (NoMatchException e) {
|
||||
throw new InputParseException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a direction from the player.
|
||||
*
|
||||
* @param context the context
|
||||
* @param direction the direction annotation
|
||||
* @return a pattern
|
||||
* @throws ParameterException on error
|
||||
* @throws UnknownDirectionException on an unknown direction
|
||||
*/
|
||||
@Direction
|
||||
public BlockVector3 getDirection(Player player, Direction direction, String argument) throws UnknownDirectionException {
|
||||
if (direction.includeDiagonals()) {
|
||||
return worldEdit.getDiagonalDirection(player, argument);
|
||||
} else {
|
||||
return worldEdit.getDirection(player, argument);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an {@link TreeType} from a {@link ArgumentStack}.
|
||||
*
|
||||
* @param context the context
|
||||
* @return a pattern
|
||||
* @throws ParameterException on error
|
||||
* @throws WorldEditException on error
|
||||
*/
|
||||
public TreeGenerator.TreeType getTreeType(String argument) throws WorldEditException {
|
||||
if (argument != null) {
|
||||
TreeGenerator.TreeType type = TreeGenerator.lookup(argument);
|
||||
if (type != null) {
|
||||
return type;
|
||||
} else {
|
||||
throw new InputParseException(
|
||||
String.format("Can't recognize tree type '%s' -- choose from: %s", argument,
|
||||
TreeGenerator.TreeType.getPrimaryAliases()));
|
||||
}
|
||||
} else {
|
||||
return TreeGenerator.TreeType.TREE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an {@link BiomeType} from a {@link ArgumentStack}.
|
||||
*
|
||||
* @param context the context
|
||||
* @return a pattern
|
||||
* @throws ParameterException on error
|
||||
* @throws WorldEditException on error
|
||||
*/
|
||||
public BiomeType getBiomeType(String argument) throws WorldEditException {
|
||||
if (argument != null) {
|
||||
|
||||
if (MathMan.isInteger(argument)) return BiomeTypes.getLegacy(Integer.parseInt(argument));
|
||||
BiomeRegistry biomeRegistry = WorldEdit.getInstance().getPlatformManager()
|
||||
.queryCapability(Capability.GAME_HOOKS).getRegistries().getBiomeRegistry();
|
||||
Collection<BiomeType> knownBiomes = BiomeType.REGISTRY.values();
|
||||
BiomeType biome = Biomes.findBiomeByName(knownBiomes, argument, biomeRegistry);
|
||||
if (biome != null) {
|
||||
return biome;
|
||||
} else {
|
||||
throw new InputParseException(
|
||||
String.format("Can't recognize biome type '%s' -- use /biomelist to list available types", argument));
|
||||
}
|
||||
} else {
|
||||
throw new InputParseException(
|
||||
"This command takes a 'default' biome if one is not set, except there is no particular " +
|
||||
"biome that should be 'default', so the command should not be taking a default biome");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user