Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Location2DArgument, mapped to NMS Vec2I #201

Merged
merged 6 commits into from
Jan 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import cloud.commandframework.bukkit.arguments.selector.MultiplePlayerSelector;
import cloud.commandframework.bukkit.arguments.selector.SingleEntitySelector;
import cloud.commandframework.bukkit.arguments.selector.SinglePlayerSelector;
import cloud.commandframework.bukkit.parsers.location.Location2D;
import com.mojang.brigadier.arguments.ArgumentType;
import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand Down Expand Up @@ -88,6 +89,8 @@ public BukkitBrigadierMapper(
this.mapComplexNMS(MultiplePlayerSelector.class, this.getEntitySelectorArgument(false, true));
/* Map Vec3 */
this.mapComplexNMS(Location.class, this.getArgumentVec3());
/* Map Vec2I */
this.mapComplexNMS(Location2D.class, this.getArgumentVec2I());
} catch (final Exception e) {
this.commandManager.getOwningPlugin()
.getLogger()
Expand All @@ -100,7 +103,7 @@ public BukkitBrigadierMapper(
* @param playersOnly Whether the selector is for players only (true), or for all entities (false)
* @return The NMS ArgumentType
*/
private Supplier<ArgumentType<?>> getEntitySelectorArgument(
private @NonNull Supplier<ArgumentType<?>> getEntitySelectorArgument(
final boolean single,
final boolean playersOnly
) {
Expand All @@ -117,13 +120,25 @@ private Supplier<ArgumentType<?>> getEntitySelectorArgument(
}

@SuppressWarnings("UnnecessaryLambda")
private Supplier<ArgumentType<?>> getArgumentVec3() {
private @NonNull Supplier<ArgumentType<?>> getArgumentVec3() {
return () -> {
try {
return (ArgumentType<?>) this.getNMSArgument("Vec3").getDeclaredConstructor(boolean.class)
.newInstance(true);
} catch (final Exception e) {
this.commandManager.getOwningPlugin().getLogger().log(Level.INFO, "Failed to retrieve vector argument", e);
this.commandManager.getOwningPlugin().getLogger().log(Level.INFO, "Failed to retrieve Vec3D argument", e);
return null;
}
};
}

@SuppressWarnings("UnnecessaryLambda")
private @NonNull Supplier<ArgumentType<?>> getArgumentVec2I() {
return () -> {
try {
return (ArgumentType<?>) this.getNMSArgument("Vec2I").getDeclaredConstructor().newInstance();
} catch (final Exception e) {
this.commandManager.getOwningPlugin().getLogger().log(Level.INFO, "Failed to retrieve Vec2I argument", e);
return null;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
import cloud.commandframework.bukkit.parsers.OfflinePlayerArgument;
import cloud.commandframework.bukkit.parsers.PlayerArgument;
import cloud.commandframework.bukkit.parsers.WorldArgument;
import cloud.commandframework.bukkit.parsers.location.Location2D;
import cloud.commandframework.bukkit.parsers.location.LocationArgument;
import cloud.commandframework.bukkit.parsers.location.Location2DArgument;
import cloud.commandframework.bukkit.parsers.selector.MultipleEntitySelectorArgument;
import cloud.commandframework.bukkit.parsers.selector.MultiplePlayerSelectorArgument;
import cloud.commandframework.bukkit.parsers.selector.SingleEntitySelectorArgument;
Expand Down Expand Up @@ -172,6 +174,8 @@ public BukkitCommandManager(
new EnchantmentArgument.EnchantmentParser<>());
this.getParserRegistry().registerParserSupplier(TypeToken.get(Location.class), parserParameters ->
new LocationArgument.LocationParser<>());
this.getParserRegistry().registerParserSupplier(TypeToken.get(Location2D.class), parserParameters ->
new Location2DArgument.Location2DParser<>());
/* Register Entity Selector Parsers */
this.getParserRegistry().registerParserSupplier(TypeToken.get(SingleEntitySelector.class), parserParameters ->
new SingleEntitySelectorArgument.SingleEntitySelectorParser<>());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.bukkit.parsers.location;

import org.bukkit.Location;
import org.bukkit.World;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* {@link Location} projected onto the XZ-plane
*
* @since 1.4.0
*/
public class Location2D extends Location {

protected Location2D(final @Nullable World world, final double x, final double z) {
super(world, x, 0, z);
}

/**
* Get a new Location2D
*
* @param world World this location is in
* @param x X position for this location
* @param z Z position for this location
* @return Location2D
*/
public static @NonNull Location2D from(final @Nullable World world, final double x, final double z) {
return new Location2D(world, x, z);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.bukkit.parsers.location;

import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.bukkit.parsers.location.LocationArgument.LocationParseException;
import cloud.commandframework.context.CommandContext;
import io.leangen.geantyref.TypeToken;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.util.Vector;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.function.BiFunction;

/**
* Argument parser that parses {@link Location2D} from two doubles. This will use the command
* senders world when it exists, or else it'll use the first loaded Bukkit world
*
* @param <C> Command sender type
* @since 1.4.0
*/
public final class Location2DArgument<C> extends CommandArgument<C, Location2D> {

private Location2DArgument(
final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider,
final @NonNull Collection<@NonNull BiFunction<@NonNull CommandContext<C>,
@NonNull Queue<@NonNull String>, @NonNull ArgumentParseResult<Boolean>>> argumentPreprocessors
) {
super(
required,
name,
new Location2DParser<>(),
defaultValue,
TypeToken.get(Location2D.class),
suggestionsProvider,
argumentPreprocessors
);
}

/**
* Create a new argument builder
*
* @param name Argument name
* @param <C> Command sender type
* @return Builder instance
*/
public static <C> @NonNull Builder<C> newBuilder(
final @NonNull String name
) {
return new Builder<>(name);
}

/**
* Create a new required argument
*
* @param name Argument name
* @param <C> Command sender type
* @return Constructed argument
*/
public static <C> @NonNull CommandArgument<C, Location2D> of(
final @NonNull String name
) {
return Location2DArgument.<C>newBuilder(
name
).asRequired().build();
}

/**
* Create a new optional argument
*
* @param name Argument name
* @param <C> Command sender type
* @return Constructed argument
*/
public static <C> @NonNull CommandArgument<C, Location2D> optional(
final @NonNull String name
) {
return Location2DArgument.<C>newBuilder(
name
).asOptional().build();
}


public static final class Builder<C> extends CommandArgument.Builder<C, Location2D> {

private Builder(
final @NonNull String name
) {
super(
TypeToken.get(Location2D.class),
name
);
}

@Override
public @NonNull CommandArgument<@NonNull C, @NonNull Location2D> build() {
return new Location2DArgument<>(
this.isRequired(),
this.getName(),
this.getDefaultValue(),
this.getSuggestionsProvider(),
new LinkedList<>()
);
}

}


public static final class Location2DParser<C> implements ArgumentParser<C, Location2D> {

private static final int EXPECTED_PARAMETER_COUNT = 2;

private final LocationCoordinateParser<C> locationCoordinateParser = new LocationCoordinateParser<>();

@Override
public @NonNull ArgumentParseResult<@NonNull Location2D> parse(
final @NonNull CommandContext<@NonNull C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue
) {
if (inputQueue.size() < 2) {
final StringBuilder input = new StringBuilder();
for (int i = 0; i < inputQueue.size(); i++) {
input.append(((LinkedList<String>) inputQueue).get(i));
}
return ArgumentParseResult.failure(
new LocationParseException(
commandContext,
LocationParseException.FailureReason.WRONG_FORMAT,
input.toString()
)
);
}
final LocationCoordinate[] coordinates = new LocationCoordinate[2];
for (int i = 0; i < 2; i++) {
final ArgumentParseResult<@NonNull LocationCoordinate> coordinate = this.locationCoordinateParser.parse(
commandContext,
inputQueue
);
if (coordinate.getFailure().isPresent()) {
return ArgumentParseResult.failure(
coordinate.getFailure().get()
);
}
coordinates[i] = coordinate.getParsedValue().orElseThrow(NullPointerException::new);
}
final Location originalLocation;
final CommandSender bukkitSender = commandContext.get("BukkitCommandSender");

if (bukkitSender instanceof BlockCommandSender) {
originalLocation = ((BlockCommandSender) bukkitSender).getBlock().getLocation();
} else if (bukkitSender instanceof Entity) {
originalLocation = ((Entity) bukkitSender).getLocation();
} else {
originalLocation = new Location(Bukkit.getWorlds().get(0), 0, 0, 0);
}

if (coordinates[0].getType() == LocationCoordinateType.LOCAL && coordinates[1].getType() != LocationCoordinateType.LOCAL) {
return ArgumentParseResult.failure(
new LocationParseException(
commandContext,
LocationParseException.FailureReason.MIXED_LOCAL_ABSOLUTE,
""
)
);
}

if (coordinates[0].getType() == LocationCoordinateType.ABSOLUTE) {
originalLocation.setX(coordinates[0].getCoordinate());
} else if (coordinates[0].getType() == LocationCoordinateType.RELATIVE) {
originalLocation.add(coordinates[0].getCoordinate(), 0, 0);
}

if (coordinates[1].getType() == LocationCoordinateType.ABSOLUTE) {
originalLocation.setZ(coordinates[1].getCoordinate());
} else if (coordinates[1].getType() == LocationCoordinateType.RELATIVE) {
originalLocation.add(0, 0, coordinates[1].getCoordinate());
} else {
final Vector declaredPos = new Vector(
coordinates[0].getCoordinate(),
0,
coordinates[1].getCoordinate()
);
final Location local = LocationArgument.LocationParser.toLocalSpace(originalLocation, declaredPos);
return ArgumentParseResult.success(Location2D.from(
originalLocation.getWorld(),
local.getX(),
local.getZ()
));
}

return ArgumentParseResult.success(Location2D.from(
originalLocation.getWorld(),
originalLocation.getX(),
originalLocation.getZ()
));
}

@Override
public @NonNull List<@NonNull String> suggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull String input
) {
return LocationArgument.LocationParser.getSuggestions(commandContext, input);
}

@Override
public int getRequestedArgumentCount() {
return EXPECTED_PARAMETER_COUNT;
}

}

}
Loading