diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/LocationConverter.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/LocationConverter.java new file mode 100644 index 000000000..5cf84a152 --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/LocationConverter.java @@ -0,0 +1,88 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * 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 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.command.argument; + +import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldedit.util.Location; +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.Collections; +import java.util.List; + +public class LocationConverter implements ArgumentConverter { + + public static void register(CommandManager commandManager) { + commandManager.registerConverter(Key.of(Location.class), LOCATION_CONVERTER); + } + + public static final LocationConverter LOCATION_CONVERTER = new LocationConverter(); + + private final WorldConverter worldConverter = WorldConverter.WORLD_CONVERTER; + private final VectorConverter vectorConverter = VectorConverter.BLOCK_VECTOR_3_CONVERTER; + private final Component desc = TextComponent.of("any world, x, y, and z"); + + private LocationConverter() { + } + + @Override + public Component describeAcceptableArguments() { + return desc; + } + + @Override + public List getSuggestions(String input, InjectedValueAccess context) { + if (input.contains(",")) { + return Collections.emptyList(); + } + return worldConverter.getSuggestions(input, context); + } + + @Override + public ConversionResult convert(String s, InjectedValueAccess injectedValueAccess) { + String[] split4 = s.split(",", 4); + if (split4.length != 4) { + return FailedConversion.from(new IllegalArgumentException( + "Must have exactly 1 world and 3 vector components")); + } + + String[] split2 = s.split(",", 2); + + ConversionResult world = worldConverter.convert(split2[0], injectedValueAccess); + if (!world.isSuccessful()) { + return (FailedConversion) world; + } + ConversionResult vector = vectorConverter.convert(split2[1], injectedValueAccess); + if (!vector.isSuccessful()) { + return (FailedConversion) vector; + } + + Location location = new Location(world.get().iterator().next(), vector.get().iterator().next().toVector3()); + return SuccessfulConversion.fromSingle(location); + } +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/WorldConverter.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/WorldConverter.java index af6ce8b41..d9243cba4 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/WorldConverter.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/WorldConverter.java @@ -39,10 +39,10 @@ import java.util.stream.Stream; public class WorldConverter implements ArgumentConverter { public static void register(CommandManager commandManager) { - commandManager.registerConverter(Key.of(World.class), - new WorldConverter() - ); + commandManager.registerConverter(Key.of(World.class), WORLD_CONVERTER); } + + public static final WorldConverter WORLD_CONVERTER = new WorldConverter(); private final TextComponent choices; diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java index 3a66dc276..81df98434 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/extension/platform/PlatformCommandManager.java @@ -90,6 +90,7 @@ import com.sk89q.worldedit.command.argument.EntityRemoverConverter; import com.sk89q.worldedit.command.argument.EnumConverter; import com.sk89q.worldedit.command.argument.ExpressionConverter; import com.sk89q.worldedit.command.argument.FactoryConverter; +import com.sk89q.worldedit.command.argument.LocationConverter; import com.sk89q.worldedit.command.argument.RegionFactoryConverter; import com.sk89q.worldedit.command.argument.RegistryConverter; import com.sk89q.worldedit.command.argument.SideEffectConverter; @@ -256,6 +257,7 @@ public final class PlatformCommandManager { EntityRemoverConverter.register(commandManager); RegionFactoryConverter.register(commandManager); WorldConverter.register(commandManager); + LocationConverter.register(commandManager); ExpressionConverter.register(commandManager); SideEffectConverter.register(commandManager);