mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-07-04 03:56:41 +00:00
Major command changes that don't work yet.
This commit is contained in:
@ -0,0 +1,119 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.command.argument;
|
||||
|
||||
import com.google.auto.value.AutoAnnotation;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.sk89q.worldedit.UnknownDirectionException;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.entity.Player;
|
||||
import com.sk89q.worldedit.internal.annotation.Direction;
|
||||
import com.sk89q.worldedit.internal.annotation.MultiDirection;
|
||||
import com.sk89q.worldedit.util.formatting.text.Component;
|
||||
import com.sk89q.worldedit.util.formatting.text.TextComponent;
|
||||
import org.enginehub.piston.CommandManager;
|
||||
import org.enginehub.piston.converter.ArgumentConverter;
|
||||
import org.enginehub.piston.converter.ConversionResult;
|
||||
import org.enginehub.piston.converter.FailedConversion;
|
||||
import org.enginehub.piston.converter.SuccessfulConversion;
|
||||
import org.enginehub.piston.inject.InjectedValueAccess;
|
||||
import org.enginehub.piston.inject.Key;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
|
||||
import static org.enginehub.piston.converter.SuggestionHelper.limitByPrefix;
|
||||
|
||||
public abstract class AbstractDirectionConverter<D> implements ArgumentConverter<D> {
|
||||
|
||||
@AutoAnnotation
|
||||
private static Direction direction(boolean includeDiagonals) {
|
||||
return new AutoAnnotation_AbstractDirectionConverter_direction(includeDiagonals);
|
||||
}
|
||||
|
||||
@AutoAnnotation
|
||||
private static MultiDirection multiDirection(boolean includeDiagonals) {
|
||||
return new AutoAnnotation_AbstractDirectionConverter_multiDirection(includeDiagonals);
|
||||
}
|
||||
|
||||
protected static <D> void register(CommandManager commandManager, AbstractDirectionConverter<D> converter,
|
||||
Class<D> keyClass, boolean includeDiagonals) {
|
||||
commandManager.registerConverter(
|
||||
Key.of(keyClass, direction(includeDiagonals)),
|
||||
converter
|
||||
);
|
||||
commandManager.registerConverter(
|
||||
Key.of(keyClass, multiDirection(includeDiagonals)),
|
||||
CommaSeparatedValuesConverter.wrap(converter)
|
||||
);
|
||||
}
|
||||
|
||||
private static final ImmutableSet<String> ORTHOGONAL = ImmutableSet.of(
|
||||
"north", "south", "east", "west", "up", "down"
|
||||
);
|
||||
private static final ImmutableSet<String> RELATIVE = ImmutableSet.of(
|
||||
"me", "forward", "back", "left", "right"
|
||||
);
|
||||
private static final ImmutableSet<String> DIAGONAL = ImmutableSet.of(
|
||||
"northeast", "northwest", "southeast", "southwest"
|
||||
);
|
||||
|
||||
private final WorldEdit worldEdit;
|
||||
private final boolean includeDiagonals;
|
||||
private final ImmutableList<String> suggestions;
|
||||
|
||||
protected AbstractDirectionConverter(WorldEdit worldEdit, boolean includeDiagonals) {
|
||||
this.worldEdit = worldEdit;
|
||||
this.includeDiagonals = includeDiagonals;
|
||||
suggestions = ImmutableList.<String>builder()
|
||||
.addAll(ORTHOGONAL)
|
||||
.addAll(RELATIVE)
|
||||
.addAll(includeDiagonals ? DIAGONAL : ImmutableList.of())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConversionResult<D> convert(String argument, InjectedValueAccess context) {
|
||||
Player player = context.injectedValue(Key.of(Player.class)).orElse(null);
|
||||
try {
|
||||
return SuccessfulConversion.fromSingle(convertDirection(argument, player, includeDiagonals));
|
||||
} catch (Exception e) {
|
||||
return FailedConversion.from(e);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract D convertDirection(String argument, @Nullable Player player, boolean includeDiagonals) throws UnknownDirectionException;
|
||||
|
||||
@Override
|
||||
public Component describeAcceptableArguments() {
|
||||
return TextComponent.of("`me` to use facing direction, or any "
|
||||
+ (includeDiagonals ? "direction" : "non-diagonal direction"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getSuggestions(String input) {
|
||||
return limitByPrefix(suggestions.stream(), input);
|
||||
}
|
||||
|
||||
protected WorldEdit getWorldEdit() {
|
||||
return worldEdit;
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.command.argument;
|
||||
|
||||
|
||||
import org.enginehub.piston.inject.InjectedValueAccess;
|
||||
|
||||
/**
|
||||
* Key-interface for {@link InjectedValueAccess} for the String arguments.
|
||||
*/
|
||||
public interface Arguments {
|
||||
|
||||
String get();
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.command.argument;
|
||||
|
||||
import com.google.common.collect.ImmutableSetMultimap;
|
||||
import org.enginehub.piston.CommandManager;
|
||||
import org.enginehub.piston.converter.MultiKeyConverter;
|
||||
import org.enginehub.piston.inject.Key;
|
||||
|
||||
public class BooleanConverter {
|
||||
|
||||
public static void register(CommandManager commandManager) {
|
||||
commandManager.registerConverter(Key.of(Boolean.class),
|
||||
MultiKeyConverter.builder(
|
||||
ImmutableSetMultimap.<Boolean, String>builder()
|
||||
.putAll(false, "off", "f", "false", "n", "no")
|
||||
.putAll(true, "on", "t", "true", "y", "yes")
|
||||
.build()
|
||||
)
|
||||
.errorMessage(arg -> "Not a boolean value: " + arg)
|
||||
.build()
|
||||
);
|
||||
}
|
||||
|
||||
private BooleanConverter() {
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.command.argument;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.sk89q.worldedit.util.formatting.text.TextComponent.space;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.sk89q.worldedit.util.formatting.text.Component;
|
||||
import com.sk89q.worldedit.util.formatting.text.TextComponent;
|
||||
import java.util.List;
|
||||
import org.enginehub.piston.converter.ArgumentConverter;
|
||||
import org.enginehub.piston.converter.ConversionResult;
|
||||
import org.enginehub.piston.converter.SuccessfulConversion;
|
||||
import org.enginehub.piston.inject.InjectedValueAccess;
|
||||
|
||||
public class CommaSeparatedValuesConverter<T> implements ArgumentConverter<T> {
|
||||
|
||||
public static <T> CommaSeparatedValuesConverter<T> wrap(ArgumentConverter<T> delegate) {
|
||||
return wrapAndLimit(delegate, -1);
|
||||
}
|
||||
|
||||
public static <T> CommaSeparatedValuesConverter<T> wrapAndLimit(ArgumentConverter<T> delegate, int maximum) {
|
||||
return new CommaSeparatedValuesConverter<>(delegate, maximum);
|
||||
}
|
||||
|
||||
private static final Splitter COMMA = Splitter.on(',');
|
||||
|
||||
private final ArgumentConverter<T> delegate;
|
||||
private final int maximum;
|
||||
|
||||
private CommaSeparatedValuesConverter(ArgumentConverter<T> delegate, int maximum) {
|
||||
checkArgument(maximum == -1 || maximum > 1,
|
||||
"Maximum must be bigger than 1, or exactly -1");
|
||||
this.delegate = delegate;
|
||||
this.maximum = maximum;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component describeAcceptableArguments() {
|
||||
TextComponent.Builder result = TextComponent.builder("");
|
||||
if (maximum > -1) {
|
||||
result.append(TextComponent.of("up to "))
|
||||
.append(TextComponent.of(maximum))
|
||||
.append(space());
|
||||
}
|
||||
result.append(TextComponent.of("comma separated values of: "))
|
||||
.append(delegate.describeAcceptableArguments());
|
||||
return result.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getSuggestions(String input) {
|
||||
String lastInput = Iterables.getLast(COMMA.split(input), "");
|
||||
return delegate.getSuggestions(lastInput);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConversionResult<T> convert(String argument, InjectedValueAccess context) {
|
||||
ImmutableList.Builder<T> result = ImmutableList.builder();
|
||||
for (String input : COMMA.split(argument)) {
|
||||
ConversionResult<T> temp = delegate.convert(input, context);
|
||||
if (!temp.isSuccessful()) {
|
||||
return temp;
|
||||
}
|
||||
result.addAll(temp.get());
|
||||
}
|
||||
return SuccessfulConversion.from(result.build());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.command.argument;
|
||||
|
||||
import com.sk89q.worldedit.UnknownDirectionException;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.entity.Player;
|
||||
import com.sk89q.worldedit.math.BlockVector3;
|
||||
import com.sk89q.worldedit.util.Direction;
|
||||
import org.enginehub.piston.CommandManager;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Optional;
|
||||
|
||||
public final class DirectionConverter extends AbstractDirectionConverter<Direction> {
|
||||
|
||||
private DirectionConverter(WorldEdit worldEdit, boolean includeDiagonals) {
|
||||
super(worldEdit, includeDiagonals);
|
||||
}
|
||||
|
||||
public static void register(WorldEdit worldEdit, CommandManager commandManager) {
|
||||
for (boolean includeDiagonals : new boolean[] { false, true }) {
|
||||
DirectionConverter directionConverter = new DirectionConverter(worldEdit, includeDiagonals);
|
||||
register(commandManager, directionConverter, Direction.class, includeDiagonals);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Direction convertDirection(String argument, @Nullable Player player, boolean includeDiagonals) throws UnknownDirectionException {
|
||||
final BlockVector3 vec = includeDiagonals
|
||||
? getWorldEdit().getDiagonalDirection(player, argument)
|
||||
: getWorldEdit().getDirection(player, argument);
|
||||
return Optional.ofNullable(Direction.findClosest(vec.toVector3(), Direction.Flag.ALL))
|
||||
.orElseThrow(() -> new UnknownDirectionException(argument));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.command.argument;
|
||||
|
||||
import com.sk89q.worldedit.UnknownDirectionException;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.entity.Player;
|
||||
import com.sk89q.worldedit.math.BlockVector3;
|
||||
import org.enginehub.piston.CommandManager;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public final class DirectionVectorConverter extends AbstractDirectionConverter<BlockVector3> {
|
||||
|
||||
private DirectionVectorConverter(WorldEdit worldEdit, boolean includeDiagonals) {
|
||||
super(worldEdit, includeDiagonals);
|
||||
}
|
||||
|
||||
public static void register(WorldEdit worldEdit, CommandManager commandManager) {
|
||||
for (boolean includeDiagonals : new boolean[] { false, true }) {
|
||||
DirectionVectorConverter directionConverter = new DirectionVectorConverter(worldEdit, includeDiagonals);
|
||||
register(commandManager, directionConverter, BlockVector3.class, includeDiagonals);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BlockVector3 convertDirection(String argument, @Nullable Player player, boolean includeDiagonals) throws UnknownDirectionException {
|
||||
return includeDiagonals
|
||||
? getWorldEdit().getDiagonalDirection(player, argument)
|
||||
: getWorldEdit().getDirection(player, argument);
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.command.argument;
|
||||
|
||||
import com.sk89q.worldedit.EditSession;
|
||||
import com.sk89q.worldedit.LocalSession;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.entity.Player;
|
||||
|
||||
import java.util.concurrent.locks.StampedLock;
|
||||
|
||||
/**
|
||||
* Lazily-created {@link EditSession}.
|
||||
*/
|
||||
public class EditSessionHolder {
|
||||
|
||||
private final StampedLock lock = new StampedLock();
|
||||
private final WorldEdit worldEdit;
|
||||
private final Player player;
|
||||
|
||||
public EditSessionHolder(WorldEdit worldEdit, Player player) {
|
||||
this.worldEdit = worldEdit;
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
private EditSession session;
|
||||
|
||||
/**
|
||||
* Get the session, but does not create it if it doesn't exist.
|
||||
*/
|
||||
public EditSession getSession() {
|
||||
long stamp = lock.tryOptimisticRead();
|
||||
EditSession result = session;
|
||||
if (!lock.validate(stamp)) {
|
||||
stamp = lock.readLock();
|
||||
try {
|
||||
result = session;
|
||||
} finally {
|
||||
lock.unlockRead(stamp);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public EditSession getOrCreateSession() {
|
||||
// use the already-generated result if possible
|
||||
EditSession result = getSession();
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
// otherwise, acquire write lock
|
||||
long stamp = lock.writeLock();
|
||||
try {
|
||||
// check session field again -- maybe another writer hit it in between
|
||||
result = session;
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
// now we can do the actual creation
|
||||
LocalSession localSession = worldEdit.getSessionManager().get(player);
|
||||
EditSession editSession = localSession.createEditSession(player);
|
||||
editSession.enableStandardMode();
|
||||
localSession.tellVersion(player);
|
||||
return session = editSession;
|
||||
} finally {
|
||||
lock.unlockWrite(stamp);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.command.argument;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.sk89q.worldedit.command.util.EntityRemover;
|
||||
import com.sk89q.worldedit.util.formatting.text.Component;
|
||||
import com.sk89q.worldedit.util.formatting.text.TextComponent;
|
||||
import org.enginehub.piston.CommandManager;
|
||||
import org.enginehub.piston.converter.ArgumentConverter;
|
||||
import org.enginehub.piston.converter.ConversionResult;
|
||||
import org.enginehub.piston.converter.FailedConversion;
|
||||
import org.enginehub.piston.converter.SuccessfulConversion;
|
||||
import static org.enginehub.piston.converter.SuggestionHelper.limitByPrefix;
|
||||
import org.enginehub.piston.inject.InjectedValueAccess;
|
||||
import org.enginehub.piston.inject.Key;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class EntityRemoverConverter implements ArgumentConverter<EntityRemover> {
|
||||
|
||||
public static void register(CommandManager commandManager) {
|
||||
commandManager.registerConverter(Key.of(EntityRemover.class), new EntityRemoverConverter());
|
||||
}
|
||||
|
||||
private final List<String> suggestions
|
||||
= ImmutableList.of("projectiles", "items", "paintings", "itemframes", "boats", "minecarts", "tnt", "xp", "all");
|
||||
|
||||
private EntityRemoverConverter() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component describeAcceptableArguments() {
|
||||
return TextComponent.of(
|
||||
"projectiles, items, paintings, itemframes, boats, minecarts, tnt, xp, or all"
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getSuggestions(String input) {
|
||||
return limitByPrefix(suggestions.stream(), input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConversionResult<EntityRemover> convert(String argument, InjectedValueAccess context) {
|
||||
try {
|
||||
return SuccessfulConversion.fromSingle(EntityRemover.fromString(argument));
|
||||
} catch (Exception e) {
|
||||
return FailedConversion.from(e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.command.argument;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.sk89q.worldedit.EditSession;
|
||||
import com.sk89q.worldedit.util.TreeGenerator;
|
||||
import org.enginehub.piston.CommandManager;
|
||||
import org.enginehub.piston.converter.ArgumentConverter;
|
||||
import org.enginehub.piston.converter.MultiKeyConverter;
|
||||
import org.enginehub.piston.inject.Key;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
public final class EnumConverter {
|
||||
|
||||
public static void register(CommandManager commandManager) {
|
||||
commandManager.registerConverter(Key.of(SelectorChoice.class),
|
||||
basic(SelectorChoice.class));
|
||||
commandManager.registerConverter(Key.of(TreeGenerator.TreeType.class),
|
||||
full(TreeGenerator.TreeType.class,
|
||||
t -> ImmutableSet.copyOf(t.lookupKeys),
|
||||
null));
|
||||
commandManager.registerConverter(Key.of(EditSession.ReorderMode.class),
|
||||
full(EditSession.ReorderMode.class,
|
||||
r -> ImmutableSet.of(r.getDisplayName()),
|
||||
null));
|
||||
}
|
||||
|
||||
private static <E extends Enum<E>> ArgumentConverter<E> basic(Class<E> enumClass) {
|
||||
return full(enumClass, e -> ImmutableSet.of(e.name().toLowerCase(Locale.ROOT)), null);
|
||||
}
|
||||
|
||||
private static <E extends Enum<E>> ArgumentConverter<E> basic(Class<E> enumClass, @Nullable E unknownValue) {
|
||||
return full(enumClass, e -> ImmutableSet.of(e.name().toLowerCase(Locale.ROOT)), unknownValue);
|
||||
}
|
||||
|
||||
private static <E extends Enum<E>> ArgumentConverter<E> full(Class<E> enumClass,
|
||||
Function<E, Set<String>> lookupKeys,
|
||||
@Nullable E unknownValue) {
|
||||
return MultiKeyConverter.from(
|
||||
EnumSet.allOf(enumClass),
|
||||
lookupKeys,
|
||||
unknownValue
|
||||
);
|
||||
}
|
||||
|
||||
private EnumConverter() {
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,105 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.command.argument;
|
||||
|
||||
import com.sk89q.worldedit.LocalSession;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.blocks.BaseItem;
|
||||
import com.sk89q.worldedit.entity.Entity;
|
||||
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.function.mask.Mask;
|
||||
import com.sk89q.worldedit.function.pattern.Pattern;
|
||||
import com.sk89q.worldedit.internal.registry.AbstractFactory;
|
||||
import com.sk89q.worldedit.util.formatting.text.Component;
|
||||
import com.sk89q.worldedit.util.formatting.text.TextComponent;
|
||||
import com.sk89q.worldedit.world.World;
|
||||
import org.enginehub.piston.CommandManager;
|
||||
import org.enginehub.piston.converter.ArgumentConverter;
|
||||
import org.enginehub.piston.converter.ConversionResult;
|
||||
import org.enginehub.piston.converter.FailedConversion;
|
||||
import org.enginehub.piston.converter.SuccessfulConversion;
|
||||
import org.enginehub.piston.inject.InjectedValueAccess;
|
||||
import org.enginehub.piston.inject.Key;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class FactoryConverter<T> implements ArgumentConverter<T> {
|
||||
|
||||
public static void register(WorldEdit worldEdit, CommandManager commandManager) {
|
||||
commandManager.registerConverter(Key.of(Pattern.class),
|
||||
new FactoryConverter<>(worldEdit, WorldEdit::getPatternFactory, "pattern"));
|
||||
commandManager.registerConverter(Key.of(Mask.class),
|
||||
new FactoryConverter<>(worldEdit, WorldEdit::getMaskFactory, "mask"));
|
||||
commandManager.registerConverter(Key.of(BaseItem.class),
|
||||
new FactoryConverter<>(worldEdit, WorldEdit::getItemFactory, "item"));
|
||||
}
|
||||
|
||||
private final WorldEdit worldEdit;
|
||||
private final Function<WorldEdit, AbstractFactory<T>> factoryExtractor;
|
||||
private final String description;
|
||||
|
||||
private FactoryConverter(WorldEdit worldEdit,
|
||||
Function<WorldEdit, AbstractFactory<T>> factoryExtractor,
|
||||
String description) {
|
||||
this.worldEdit = worldEdit;
|
||||
this.factoryExtractor = factoryExtractor;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConversionResult<T> convert(String argument, InjectedValueAccess context) {
|
||||
Actor actor = context.injectedValue(Key.of(Actor.class))
|
||||
.orElseThrow(() -> new IllegalStateException("No actor"));
|
||||
LocalSession session = WorldEdit.getInstance().getSessionManager().get(actor);
|
||||
|
||||
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(session);
|
||||
parserContext.setRestricted(true);
|
||||
|
||||
try {
|
||||
return SuccessfulConversion.fromSingle(
|
||||
factoryExtractor.apply(worldEdit).parseFromInput(argument, parserContext)
|
||||
);
|
||||
} catch (InputParseException e) {
|
||||
return FailedConversion.from(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getSuggestions(String input) {
|
||||
return factoryExtractor.apply(worldEdit).getSuggestions(input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component describeAcceptableArguments() {
|
||||
return TextComponent.of("any " + description);
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.command.argument;
|
||||
|
||||
import com.google.common.collect.ImmutableSetMultimap;
|
||||
import com.sk89q.worldedit.regions.factory.CuboidRegionFactory;
|
||||
import com.sk89q.worldedit.regions.factory.CylinderRegionFactory;
|
||||
import com.sk89q.worldedit.regions.factory.RegionFactory;
|
||||
import com.sk89q.worldedit.regions.factory.SphereRegionFactory;
|
||||
import org.enginehub.piston.CommandManager;
|
||||
import org.enginehub.piston.converter.MultiKeyConverter;
|
||||
import org.enginehub.piston.inject.Key;
|
||||
|
||||
public class RegionFactoryConverter {
|
||||
|
||||
public static void register(CommandManager commandManager) {
|
||||
commandManager.registerConverter(Key.of(RegionFactory.class),
|
||||
MultiKeyConverter.builder(
|
||||
ImmutableSetMultimap.<RegionFactory, String>builder()
|
||||
.put(new CuboidRegionFactory(), "cuboid")
|
||||
.put(new SphereRegionFactory(), "sphere")
|
||||
.putAll(new CylinderRegionFactory(1), "cyl", "cylinder")
|
||||
.build()
|
||||
)
|
||||
.errorMessage(arg -> "Not a known region type: " + arg)
|
||||
.build()
|
||||
);
|
||||
}
|
||||
|
||||
private RegionFactoryConverter() {
|
||||
}
|
||||
}
|
@ -0,0 +1,111 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.command.argument;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.sk89q.worldedit.command.util.SuggestionHelper;
|
||||
import com.sk89q.worldedit.registry.Keyed;
|
||||
import com.sk89q.worldedit.registry.Registry;
|
||||
import com.sk89q.worldedit.util.formatting.text.Component;
|
||||
import com.sk89q.worldedit.util.formatting.text.TextComponent;
|
||||
import com.sk89q.worldedit.world.biome.BiomeType;
|
||||
import com.sk89q.worldedit.world.block.BlockCategory;
|
||||
import com.sk89q.worldedit.world.block.BlockType;
|
||||
import com.sk89q.worldedit.world.entity.EntityType;
|
||||
import com.sk89q.worldedit.world.fluid.FluidCategory;
|
||||
import com.sk89q.worldedit.world.fluid.FluidType;
|
||||
import com.sk89q.worldedit.world.gamemode.GameMode;
|
||||
import com.sk89q.worldedit.world.item.ItemCategory;
|
||||
import com.sk89q.worldedit.world.item.ItemType;
|
||||
import com.sk89q.worldedit.world.weather.WeatherType;
|
||||
import org.enginehub.piston.CommandManager;
|
||||
import org.enginehub.piston.converter.ArgumentConverter;
|
||||
import org.enginehub.piston.converter.ConversionResult;
|
||||
import org.enginehub.piston.converter.FailedConversion;
|
||||
import org.enginehub.piston.converter.SuccessfulConversion;
|
||||
import org.enginehub.piston.inject.InjectedValueAccess;
|
||||
import org.enginehub.piston.inject.Key;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public final class RegistryConverter<V extends Keyed> implements ArgumentConverter<V> {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void register(CommandManager commandManager) {
|
||||
ImmutableList.of(
|
||||
BlockType.class,
|
||||
BlockCategory.class,
|
||||
ItemType.class,
|
||||
ItemCategory.class,
|
||||
BiomeType.class,
|
||||
EntityType.class,
|
||||
FluidType.class,
|
||||
FluidCategory.class,
|
||||
GameMode.class,
|
||||
WeatherType.class
|
||||
).stream()
|
||||
.map(c -> (Class<Keyed>) c)
|
||||
.forEach(registryType ->
|
||||
commandManager.registerConverter(Key.of(registryType), from(registryType))
|
||||
);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <V extends Keyed> RegistryConverter<V> from(Class<Keyed> registryType) {
|
||||
try {
|
||||
Field registryField = registryType.getDeclaredField("REGISTRY");
|
||||
Registry<V> registry = (Registry<V>) registryField.get(null);
|
||||
return new RegistryConverter<>(registry);
|
||||
} catch (NoSuchFieldException e) {
|
||||
throw new IllegalArgumentException("Not a registry-backed type: " + registryType.getName());
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new IllegalStateException("Registry field inaccessible on " + registryType.getName());
|
||||
}
|
||||
}
|
||||
|
||||
private final Registry<V> registry;
|
||||
private final TextComponent choices;
|
||||
|
||||
private RegistryConverter(Registry<V> registry) {
|
||||
this.registry = registry;
|
||||
this.choices = TextComponent.of("any " + registry.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component describeAcceptableArguments() {
|
||||
return this.choices;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConversionResult<V> convert(String argument, InjectedValueAccess injectedValueAccess) {
|
||||
V result = registry.get(argument);
|
||||
return result == null
|
||||
? FailedConversion.from(new IllegalArgumentException(
|
||||
"Not a valid " + registry.getName() + ": " + argument))
|
||||
: SuccessfulConversion.fromSingle(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getSuggestions(String input) {
|
||||
return SuggestionHelper.getRegistrySuggestions(registry, input).collect(Collectors.toList());
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.command.argument;
|
||||
|
||||
public enum SelectorChoice {
|
||||
CUBOID,
|
||||
EXTEND,
|
||||
POLY,
|
||||
ELLIPSOID,
|
||||
SPHERE,
|
||||
CYL,
|
||||
CONVEX,
|
||||
HULL,
|
||||
POLYHEDRON,
|
||||
LIST,
|
||||
FUZZY,
|
||||
MAGIC,
|
||||
POLYHEDRAL
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.command.argument;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.reflect.TypeToken;
|
||||
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.util.formatting.text.Component;
|
||||
import com.sk89q.worldedit.util.formatting.text.TextComponent;
|
||||
import org.enginehub.piston.CommandManager;
|
||||
import org.enginehub.piston.converter.ArgumentConverter;
|
||||
import org.enginehub.piston.converter.ArgumentConverters;
|
||||
import org.enginehub.piston.converter.ConversionResult;
|
||||
import org.enginehub.piston.converter.FailedConversion;
|
||||
import org.enginehub.piston.converter.SuccessfulConversion;
|
||||
import org.enginehub.piston.inject.InjectedValueAccess;
|
||||
import org.enginehub.piston.inject.Key;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class VectorConverter<C, T> implements ArgumentConverter<T> {
|
||||
public static void register(CommandManager commandManager) {
|
||||
CommaSeparatedValuesConverter<Integer> intConverter = CommaSeparatedValuesConverter.wrap(ArgumentConverters.get(TypeToken.of(int.class)));
|
||||
CommaSeparatedValuesConverter<Double> doubleConverter = CommaSeparatedValuesConverter.wrap(ArgumentConverters.get(TypeToken.of(double.class)));
|
||||
commandManager.registerConverter(Key.of(BlockVector2.class),
|
||||
new VectorConverter<>(
|
||||
intConverter,
|
||||
2,
|
||||
cmps -> BlockVector2.at(cmps.get(0), cmps.get(1)),
|
||||
"block vector with x and z"
|
||||
));
|
||||
commandManager.registerConverter(Key.of(Vector2.class),
|
||||
new VectorConverter<>(
|
||||
doubleConverter,
|
||||
2,
|
||||
cmps -> Vector2.at(cmps.get(0), cmps.get(1)),
|
||||
"vector with x and z"
|
||||
));
|
||||
commandManager.registerConverter(Key.of(BlockVector3.class),
|
||||
new VectorConverter<>(
|
||||
intConverter,
|
||||
3,
|
||||
cmps -> BlockVector3.at(cmps.get(0), cmps.get(1), cmps.get(2)),
|
||||
"block vector with x, y, and z"
|
||||
));
|
||||
commandManager.registerConverter(Key.of(Vector3.class),
|
||||
new VectorConverter<>(
|
||||
doubleConverter,
|
||||
3,
|
||||
cmps -> Vector3.at(cmps.get(0), cmps.get(1), cmps.get(2)),
|
||||
"vector with x, y, and z"
|
||||
));
|
||||
}
|
||||
|
||||
private final ArgumentConverter<C> componentConverter;
|
||||
private final int componentCount;
|
||||
private final Function<List<C>, T> vectorConstructor;
|
||||
private final String acceptableArguments;
|
||||
|
||||
|
||||
private VectorConverter(ArgumentConverter<C> componentConverter,
|
||||
int componentCount,
|
||||
Function<List<C>, T> vectorConstructor,
|
||||
String acceptableArguments) {
|
||||
this.componentConverter = componentConverter;
|
||||
this.componentCount = componentCount;
|
||||
this.vectorConstructor = vectorConstructor;
|
||||
this.acceptableArguments = acceptableArguments;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component describeAcceptableArguments() {
|
||||
return TextComponent.of("any " + acceptableArguments);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConversionResult<T> convert(String argument, InjectedValueAccess context) {
|
||||
ConversionResult<C> components = componentConverter.convert(argument, context);
|
||||
if (!components.isSuccessful()) {
|
||||
return components.failureAsAny();
|
||||
}
|
||||
if (components.get().size() != componentCount) {
|
||||
return FailedConversion.from(new IllegalArgumentException(
|
||||
"Must have exactly " + componentCount + " vector components"));
|
||||
}
|
||||
T vector = vectorConstructor.apply(ImmutableList.copyOf(components.get()));
|
||||
return SuccessfulConversion.fromSingle(vector);
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.command.argument;
|
||||
|
||||
import com.sk89q.worldedit.LocalSession;
|
||||
import com.sk89q.worldedit.util.formatting.text.Component;
|
||||
import com.sk89q.worldedit.util.formatting.text.TextComponent;
|
||||
import org.enginehub.piston.CommandManager;
|
||||
import org.enginehub.piston.converter.ArgumentConverter;
|
||||
import org.enginehub.piston.converter.ConversionResult;
|
||||
import org.enginehub.piston.converter.FailedConversion;
|
||||
import org.enginehub.piston.converter.SuccessfulConversion;
|
||||
import org.enginehub.piston.inject.InjectedValueAccess;
|
||||
import org.enginehub.piston.inject.Key;
|
||||
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Calendar;
|
||||
|
||||
public class ZonedDateTimeConverter implements ArgumentConverter<ZonedDateTime> {
|
||||
|
||||
public static void register(CommandManager commandManager) {
|
||||
commandManager.registerConverter(Key.of(ZonedDateTime.class), new ZonedDateTimeConverter());
|
||||
}
|
||||
|
||||
private ZonedDateTimeConverter() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component describeAcceptableArguments() {
|
||||
return TextComponent.of("any date");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConversionResult<ZonedDateTime> convert(String argument, InjectedValueAccess context) {
|
||||
LocalSession session = context.injectedValue(Key.of(LocalSession.class))
|
||||
.orElseThrow(() -> new IllegalStateException("Need a local session"));
|
||||
Calendar date = session.detectDate(argument);
|
||||
if (date == null) {
|
||||
return FailedConversion.from(new IllegalArgumentException("Not a date: " + argument));
|
||||
}
|
||||
return SuccessfulConversion.fromSingle(date.toInstant().atZone(ZoneOffset.UTC));
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
@org.enginehub.piston.util.NonnullByDefault
|
||||
package com.sk89q.worldedit.command.argument;
|
Reference in New Issue
Block a user