From 0af44e240658f59a6e17f6be7a033140345560f1 Mon Sep 17 00:00:00 2001 From: Zach Levis Date: Sun, 3 Jan 2021 23:22:02 -0800 Subject: [PATCH] fabric: Add builders for many of the basic argument types --- .../arguments/CommandArgument.java | 88 +++++++++++ .../fabric/FabricCommandManager.java | 10 +- .../fabric/argument/AngleArgument.java | 131 +++++++++++++++++ .../fabric/argument/AxisArgument.java | 137 +++++++++++++++++ .../fabric/argument/ColorArgument.java | 126 ++++++++++++++++ .../fabric/argument/CompoundTagArgument.java | 129 ++++++++++++++++ .../fabric/argument/EnchantmentArgument.java | 130 ++++++++++++++++ .../fabric/argument/EntityAnchorArgument.java | 128 ++++++++++++++++ .../fabric/argument/FloatRangeArgument.java | 139 ++++++++++++++++++ .../fabric/argument/IdentifierArgument.java | 129 ++++++++++++++++ .../fabric/argument/IntRangeArgument.java | 139 ++++++++++++++++++ .../fabric/argument/ItemDataArgument.java | 137 +++++++++++++++++ .../fabric/argument/NbtPathArgument.java | 128 ++++++++++++++++ .../fabric/argument/NbtTagArgument.java | 129 ++++++++++++++++ .../argument/ParticleEffectArgument.java | 131 +++++++++++++++++ .../argument/ScoreboardCriterionArgument.java | 134 +++++++++++++++++ .../argument/ScoreboardOperationArgument.java | 135 +++++++++++++++++ .../fabric/argument/StatusEffectArgument.java | 137 +++++++++++++++++ .../fabric/argument/package-info.java | 28 ++++ 19 files changed, 2242 insertions(+), 3 deletions(-) create mode 100644 cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/AngleArgument.java create mode 100644 cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/AxisArgument.java create mode 100644 cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ColorArgument.java create mode 100644 cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/CompoundTagArgument.java create mode 100644 cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/EnchantmentArgument.java create mode 100644 cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/EntityAnchorArgument.java create mode 100644 cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/FloatRangeArgument.java create mode 100644 cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/IdentifierArgument.java create mode 100644 cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/IntRangeArgument.java create mode 100644 cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ItemDataArgument.java create mode 100644 cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/NbtPathArgument.java create mode 100644 cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/NbtTagArgument.java create mode 100644 cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ParticleEffectArgument.java create mode 100644 cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ScoreboardCriterionArgument.java create mode 100644 cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ScoreboardOperationArgument.java create mode 100644 cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/StatusEffectArgument.java create mode 100644 cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/package-info.java diff --git a/cloud-core/src/main/java/cloud/commandframework/arguments/CommandArgument.java b/cloud-core/src/main/java/cloud/commandframework/arguments/CommandArgument.java index 0a78a82f..16ab4575 100644 --- a/cloud-core/src/main/java/cloud/commandframework/arguments/CommandArgument.java +++ b/cloud-core/src/main/java/cloud/commandframework/arguments/CommandArgument.java @@ -741,4 +741,92 @@ public class CommandArgument implements Comparable>, } + /** + * A variant of builders designed for subclassing, that returns a self type . + * + * @param sender type + * @param argument value type + * @param the subclass type + * @since 1.4.0 + */ + public abstract static class TypedBuilder> extends Builder { + + protected TypedBuilder( + final @NonNull TypeToken valueType, + final @NonNull String name + ) { + super(valueType, name); + } + + protected TypedBuilder( + final @NonNull Class valueType, + final @NonNull String name + ) { + super(valueType, name); + } + + @SuppressWarnings("unchecked") + protected final B self() { + return (B) this; + } + + /** + * {@inheritDoc} + */ + @Override + public @NonNull B manager(final @NonNull CommandManager manager) { + super.manager(manager); + return this.self(); + } + + /** + * {@inheritDoc} + */ + @Override + public @NonNull B asRequired() { + super.asRequired(); + return this.self(); + } + + /** + * {@inheritDoc} + */ + @Override + public @NonNull B asOptional() { + super.asOptional(); + return this.self(); + } + + /** + * {@inheritDoc} + */ + @Override + public @NonNull B asOptionalWithDefault(final @NonNull String defaultValue) { + super.asOptionalWithDefault(defaultValue); + return this.self(); + } + + /** + * {@inheritDoc} + */ + @Override + public @NonNull B withParser(final @NonNull ArgumentParser<@NonNull C, @NonNull T> parser) { + super.withParser(parser); + return this.self(); + } + + /** + * {@inheritDoc} + */ + @Override + public @NonNull Builder<@NonNull C, @NonNull T> withSuggestionsProvider( + final @NonNull BiFunction<@NonNull CommandContext, + @NonNull String, @NonNull List> suggestionsProvider + ) { + super.withSuggestionsProvider(suggestionsProvider); + return this.self(); + } + + } + } diff --git a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/FabricCommandManager.java b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/FabricCommandManager.java index 2518d6a5..bd1f9fc5 100644 --- a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/FabricCommandManager.java +++ b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/FabricCommandManager.java @@ -50,6 +50,7 @@ import net.minecraft.command.argument.MessageArgumentType; import net.minecraft.command.argument.MobEffectArgumentType; import net.minecraft.command.argument.NbtCompoundTagArgumentType; import net.minecraft.command.argument.NbtPathArgumentType; +import net.minecraft.command.argument.NbtTagArgumentType; import net.minecraft.command.argument.NumberRangeArgumentType; import net.minecraft.command.argument.ObjectiveCriteriaArgumentType; import net.minecraft.command.argument.OperationArgumentType; @@ -59,6 +60,7 @@ import net.minecraft.command.argument.UuidArgumentType; import net.minecraft.enchantment.Enchantment; import net.minecraft.entity.effect.StatusEffect; import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.Tag; import net.minecraft.particle.ParticleEffect; import net.minecraft.predicate.NumberRange; import net.minecraft.scoreboard.ScoreboardCriterion; @@ -84,6 +86,7 @@ import java.util.function.Supplier; * @param the manager's sender type * @param the platform sender type * @see FabricServerCommandManager for server commands + * @since 1.4.0 */ public abstract class FabricCommandManager extends CommandManager implements BrigadierManagerHolder { @@ -139,10 +142,8 @@ public abstract class FabricCommandManager extends C /* Wrapped/Constant Brigadier types, native value type */ this.registerConstantNativeParserSupplier(Formatting.class, ColorArgumentType.color()); - this.registerConstantNativeParserSupplier(BlockPredicateArgumentType.BlockPredicate.class, - BlockPredicateArgumentType.blockPredicate()); - this.registerConstantNativeParserSupplier(MessageArgumentType.MessageFormat.class, MessageArgumentType.message()); this.registerConstantNativeParserSupplier(CompoundTag.class, NbtCompoundTagArgumentType.nbtCompound()); + this.registerConstantNativeParserSupplier(Tag.class, NbtTagArgumentType.nbtTag()); this.registerConstantNativeParserSupplier(NbtPathArgumentType.NbtPath.class, NbtPathArgumentType.nbtPath()); this.registerConstantNativeParserSupplier(ScoreboardCriterion.class, ObjectiveCriteriaArgumentType.objectiveCriteria()); this.registerConstantNativeParserSupplier(OperationArgumentType.Operation.class, OperationArgumentType.operation()); @@ -159,6 +160,9 @@ public abstract class FabricCommandManager extends C this.registerConstantNativeParserSupplier(ItemStackArgument.class, ItemStackArgumentType.itemStack()); /* Wrapped/Constant Brigadier types, mapped value type */ + this.registerConstantNativeParserSupplier(BlockPredicateArgumentType.BlockPredicate.class, + BlockPredicateArgumentType.blockPredicate()); + this.registerConstantNativeParserSupplier(MessageArgumentType.MessageFormat.class, MessageArgumentType.message()); /*this.registerConstantNativeParserSupplier(GameProfile.class, GameProfileArgumentType.gameProfile()); this.registerConstantNativeParserSupplier(BlockPos.class, BlockPosArgumentType.blockPos()); this.registerConstantNativeParserSupplier(ColumnPos.class, ColumnPosArgumentType.columnPos()); diff --git a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/AngleArgument.java b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/AngleArgument.java new file mode 100644 index 00000000..36064129 --- /dev/null +++ b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/AngleArgument.java @@ -0,0 +1,131 @@ +// +// 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.fabric.argument; + +import cloud.commandframework.arguments.CommandArgument; +import cloud.commandframework.brigadier.argument.WrappedBrigadierParser; +import cloud.commandframework.context.CommandContext; +import net.minecraft.command.argument.AngleArgumentType; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.List; +import java.util.function.BiFunction; + +/** + * An argument for an angle, specified in degrees. + * + * @param the sender type + * @since 1.4.0 + */ +public final class AngleArgument extends CommandArgument { + + // todo: This angle is relative to a certain entity + // Vanilla only supports this on the server, we should be able to make it work on the client just fine too. + + AngleArgument( + final boolean required, + final @NonNull String name, + final @NonNull String defaultValue, + final @Nullable BiFunction, String, List> suggestionsProvider + ) { + super( + required, + name, + new WrappedBrigadierParser<>(AngleArgumentType.angle()), + defaultValue, + AngleArgumentType.Angle.class, + suggestionsProvider + ); + } + + /** + * Create a new builder. + * + * @param name Name of the argument + * @param Command sender type + * @return Created builder + */ + public static AngleArgument.@NonNull Builder newBuilder(final @NonNull String name) { + return new AngleArgument.Builder<>(name); + } + + /** + * Create a new required command argument. + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull AngleArgument of(final @NonNull String name) { + return AngleArgument.newBuilder(name).asRequired().build(); + } + + /** + * Create a new optional command argument + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull AngleArgument optional(final @NonNull String name) { + return AngleArgument.newBuilder(name).asOptional().build(); + } + + /** + * Create a new optional command argument with a default value + * + * @param name Argument name + * @param defaultAngle Default angle, in degrees + * @param Command sender type + * @return Created argument + */ + public static @NonNull AngleArgument optional( + final @NonNull String name, + final float defaultAngle + ) { + return AngleArgument.newBuilder(name).asOptionalWithDefault(Float.toString(defaultAngle)).build(); + } + + + public static final class Builder extends TypedBuilder> { + + Builder(final @NonNull String name) { + super(AngleArgumentType.Angle.class, name); + } + + /** + * Build a new criterion argument + * + * @return Constructed argument + */ + @Override + public @NonNull AngleArgument build() { + return new AngleArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(), this.getSuggestionsProvider()); + } + + } + +} diff --git a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/AxisArgument.java b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/AxisArgument.java new file mode 100644 index 00000000..4de4790c --- /dev/null +++ b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/AxisArgument.java @@ -0,0 +1,137 @@ +// +// 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.fabric.argument; + +import cloud.commandframework.arguments.CommandArgument; +import cloud.commandframework.brigadier.argument.WrappedBrigadierParser; +import cloud.commandframework.context.CommandContext; +import io.leangen.geantyref.TypeToken; +import net.minecraft.command.argument.SwizzleArgumentType; +import net.minecraft.util.math.Direction; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.EnumSet; +import java.util.List; +import java.util.Set; +import java.util.function.BiFunction; + +/** + * An argument for a set of axes, described in Vanilla as a "swizzle". + * + * @param the sender type + * @since 1.4.0 + */ +public final class AxisArgument extends CommandArgument> { + private static final TypeToken> TYPE = new TypeToken>() {}; + + AxisArgument( + final boolean required, + final @NonNull String name, + final @NonNull String defaultValue, + final @Nullable BiFunction, String, List> suggestionsProvider + ) { + super( + required, + name, + new WrappedBrigadierParser<>(SwizzleArgumentType.swizzle()), + defaultValue, + TYPE, + suggestionsProvider + ); + } + + /** + * Create a new builder. + * + * @param name Name of the argument + * @param Command sender type + * @return Created builder + */ + public static AxisArgument.@NonNull Builder newBuilder(final @NonNull String name) { + return new AxisArgument.Builder<>(name); + } + + /** + * Create a new required command argument. + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull AxisArgument of(final @NonNull String name) { + return AxisArgument.newBuilder(name).asRequired().build(); + } + + /** + * Create a new optional command argument + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull AxisArgument optional(final @NonNull String name) { + return AxisArgument.newBuilder(name).asOptional().build(); + } + + /** + * Create a new optional command argument with a default value + * + * @param name Argument name + * @param defaultValues Default axes to include + * @param Command sender type + * @return Created argument + */ + public static @NonNull AxisArgument optional( + final @NonNull String name, + final @NonNull Set defaultValues + ) { + final StringBuilder builder = new StringBuilder(); + for (final Direction.Axis axis : defaultValues) { + builder.append(axis.getName()); + } + return AxisArgument.newBuilder(name).asOptionalWithDefault(builder.toString()).build(); + } + + + public static final class Builder extends TypedBuilder, Builder> { + + Builder(final @NonNull String name) { + super(TYPE, name); + } + + /** + * Build a new criterion argument + * + * @return Constructed argument + */ + @Override + public @NonNull AxisArgument build() { + return new AxisArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(), this.getSuggestionsProvider()); + } + + } + +} diff --git a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ColorArgument.java b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ColorArgument.java new file mode 100644 index 00000000..3f2e6133 --- /dev/null +++ b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ColorArgument.java @@ -0,0 +1,126 @@ +// +// 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.fabric.argument; + +import cloud.commandframework.arguments.CommandArgument; +import cloud.commandframework.brigadier.argument.WrappedBrigadierParser; +import cloud.commandframework.context.CommandContext; +import net.minecraft.command.argument.ColorArgumentType; +import net.minecraft.util.Formatting; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.List; +import java.util.function.BiFunction; + +/** + * An argument for named colors in the {@link Formatting} enum. + * + * @param the sender type + * @since 1.4.0 + */ +public final class ColorArgument extends CommandArgument { + + ColorArgument( + final boolean required, + final @NonNull String name, + final @NonNull String defaultValue, + final @Nullable BiFunction, String, List> suggestionsProvider + ) { + super(required, name, new WrappedBrigadierParser<>(ColorArgumentType.color()), defaultValue, Formatting.class, + suggestionsProvider); + } + + /** + * Create a new builder + * + * @param name Name of the component + * @param Command sender type + * @return Created builder + */ + public static ColorArgument.@NonNull Builder newBuilder(final @NonNull String name) { + return new ColorArgument.Builder<>(name); + } + + /** + * Create a new required command component + * + * @param name Component name + * @param Command sender type + * @return Created component + */ + public static @NonNull ColorArgument of(final @NonNull String name) { + return ColorArgument.newBuilder(name).asRequired().build(); + } + + /** + * Create a new optional command component + * + * @param name Component name + * @param Command sender type + * @return Created component + */ + public static @NonNull ColorArgument optional(final @NonNull String name) { + return ColorArgument.newBuilder(name).asOptional().build(); + } + + /** + * Create a new optional command argument with a default value + * + * @param name Component name + * @param defaultColor Default colour, must be {@link Formatting#isColor() a color} + * @param Command sender type + * @return Created component + */ + public static @NonNull ColorArgument optional( + final @NonNull String name, + final @NonNull Formatting defaultColor + ) { + if (!defaultColor.isColor()) { + throw new IllegalArgumentException("Only color types are allowed but " + defaultColor + " was provided"); + } + return ColorArgument.newBuilder(name).asOptionalWithDefault(defaultColor.toString()).build(); + } + + + public static final class Builder extends CommandArgument.TypedBuilder> { + + Builder(final @NonNull String name) { + super(Formatting.class, name); + } + + /** + * Build a new argument + * + * @return Constructed argument + */ + @Override + public @NonNull ColorArgument build() { + return new ColorArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(), this.getSuggestionsProvider()); + } + + } + +} diff --git a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/CompoundTagArgument.java b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/CompoundTagArgument.java new file mode 100644 index 00000000..4f229d59 --- /dev/null +++ b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/CompoundTagArgument.java @@ -0,0 +1,129 @@ +// +// 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.fabric.argument; + +import cloud.commandframework.arguments.CommandArgument; +import cloud.commandframework.brigadier.argument.WrappedBrigadierParser; +import cloud.commandframework.context.CommandContext; +import net.minecraft.command.argument.NbtCompoundTagArgumentType; +import net.minecraft.nbt.CompoundTag; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.List; +import java.util.function.BiFunction; + +/** + * An argument for the string representation of an NBT {@link CompoundTag}. + * + * @param the sender type + * @since 1.4.0 + */ +public final class CompoundTagArgument extends CommandArgument { + + CompoundTagArgument( + final boolean required, + final @NonNull String name, + final @NonNull String defaultValue, + final @Nullable BiFunction, String, List> suggestionsProvider + ) { + super( + required, + name, + new WrappedBrigadierParser<>(NbtCompoundTagArgumentType.nbtCompound()), + defaultValue, + CompoundTag.class, + suggestionsProvider + ); + } + + /** + * Create a new builder + * + * @param name Name of the component + * @param Command sender type + * @return Created builder + */ + public static CompoundTagArgument.@NonNull Builder newBuilder(final @NonNull String name) { + return new CompoundTagArgument.Builder<>(name); + } + + /** + * Create a new required command argument + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull CompoundTagArgument of(final @NonNull String name) { + return CompoundTagArgument.newBuilder(name).asRequired().build(); + } + + /** + * Create a new optional command argument + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull CompoundTagArgument optional(final @NonNull String name) { + return CompoundTagArgument.newBuilder(name).asOptional().build(); + } + + /** + * Create a new optional command argument with a default value + * + * @param name Component name + * @param defaultTag Default tag value + * @param Command sender type + * @return Created component + */ + public static @NonNull CompoundTagArgument optional( + final @NonNull String name, + final @NonNull CompoundTag defaultTag + ) { + return CompoundTagArgument.newBuilder(name).asOptionalWithDefault(defaultTag.toString()).build(); + } + + + public static final class Builder extends TypedBuilder> { + + Builder(final @NonNull String name) { + super(CompoundTag.class, name); + } + + /** + * Builder a new example component + * + * @return Constructed component + */ + @Override + public @NonNull CompoundTagArgument build() { + return new CompoundTagArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(), this.getSuggestionsProvider()); + } + + } + +} diff --git a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/EnchantmentArgument.java b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/EnchantmentArgument.java new file mode 100644 index 00000000..4d1d4da9 --- /dev/null +++ b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/EnchantmentArgument.java @@ -0,0 +1,130 @@ +// +// 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.fabric.argument; + +import cloud.commandframework.arguments.CommandArgument; +import cloud.commandframework.brigadier.argument.WrappedBrigadierParser; +import cloud.commandframework.context.CommandContext; +import net.minecraft.command.argument.ItemEnchantmentArgumentType; +import net.minecraft.enchantment.Enchantment; +import net.minecraft.util.registry.Registry; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.List; +import java.util.function.BiFunction; + +/** + * An argument parsing enchantment identifier from the {@link Registry#ENCHANTMENT enchantments registry}. + * + * @param the sender type + * @since 1.4.0 + */ +public final class EnchantmentArgument extends CommandArgument { + + EnchantmentArgument( + final boolean required, + final @NonNull String name, + final @NonNull String defaultValue, + final @Nullable BiFunction, String, List> suggestionsProvider + ) { + super( + required, + name, + new WrappedBrigadierParser<>(ItemEnchantmentArgumentType.itemEnchantment()), + defaultValue, + Enchantment.class, + suggestionsProvider + ); + } + + /** + * Create a new builder. + * + * @param name Name of the argument + * @param Command sender type + * @return Created builder + */ + public static EnchantmentArgument.@NonNull Builder newBuilder(final @NonNull String name) { + return new EnchantmentArgument.Builder<>(name); + } + + /** + * Create a new required command argument. + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull EnchantmentArgument of(final @NonNull String name) { + return EnchantmentArgument.newBuilder(name).asRequired().build(); + } + + /** + * Create a new optional command argument + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull EnchantmentArgument optional(final @NonNull String name) { + return EnchantmentArgument.newBuilder(name).asOptional().build(); + } + + /** + * Create a new optional command argument with a default value + * + * @param name Argument name + * @param defaultValue Default value + * @param Command sender type + * @return Created argument + */ + public static @NonNull EnchantmentArgument optional( + final @NonNull String name, + final Enchantment defaultValue + ) { + return EnchantmentArgument.newBuilder(name).asOptionalWithDefault(Registry.ENCHANTMENT.getId(defaultValue).toString()).build(); + } + + + public static final class Builder extends TypedBuilder> { + + Builder(final @NonNull String name) { + super(Enchantment.class, name); + } + + /** + * Build a new criterion argument + * + * @return Constructed argument + */ + @Override + public @NonNull EnchantmentArgument build() { + return new EnchantmentArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(), this.getSuggestionsProvider()); + } + + } + +} diff --git a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/EntityAnchorArgument.java b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/EntityAnchorArgument.java new file mode 100644 index 00000000..9ba7fa40 --- /dev/null +++ b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/EntityAnchorArgument.java @@ -0,0 +1,128 @@ +// +// 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.fabric.argument; + +import cloud.commandframework.arguments.CommandArgument; +import cloud.commandframework.brigadier.argument.WrappedBrigadierParser; +import cloud.commandframework.context.CommandContext; +import net.minecraft.command.argument.EntityAnchorArgumentType; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.List; +import java.util.function.BiFunction; + +/** + * An argument parsing an entity anchor. + * + * @param the sender type + * @since 1.4.0 + */ +public final class EntityAnchorArgument extends CommandArgument { + + EntityAnchorArgument( + final boolean required, + final @NonNull String name, + final @NonNull String defaultValue, + final @Nullable BiFunction, String, List> suggestionsProvider + ) { + super( + required, + name, + new WrappedBrigadierParser<>(EntityAnchorArgumentType.entityAnchor()), + defaultValue, + EntityAnchorArgumentType.EntityAnchor.class, + suggestionsProvider + ); + } + + /** + * Create a new builder. + * + * @param name Name of the argument + * @param Command sender type + * @return Created builder + */ + public static EntityAnchorArgument.@NonNull Builder newBuilder(final @NonNull String name) { + return new EntityAnchorArgument.Builder<>(name); + } + + /** + * Create a new required command argument. + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull EntityAnchorArgument of(final @NonNull String name) { + return EntityAnchorArgument.newBuilder(name).asRequired().build(); + } + + /** + * Create a new optional command argument + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull EntityAnchorArgument optional(final @NonNull String name) { + return EntityAnchorArgument.newBuilder(name).asOptional().build(); + } + + /** + * Create a new optional command argument with a default value + * + * @param name Argument name + * @param defaultValue Default value + * @param Command sender type + * @return Created argument + */ + public static @NonNull EntityAnchorArgument optional( + final @NonNull String name, + final EntityAnchorArgumentType.@NonNull EntityAnchor defaultValue + ) { + return EntityAnchorArgument.newBuilder(name).asOptionalWithDefault(defaultValue.name()).build(); + } + + + public static final class Builder extends TypedBuilder> { + + Builder(final @NonNull String name) { + super(EntityAnchorArgumentType.EntityAnchor.class, name); + } + + /** + * Build a new criterion argument + * + * @return Constructed argument + */ + @Override + public @NonNull EntityAnchorArgument build() { + return new EntityAnchorArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(), this.getSuggestionsProvider()); + } + + } + +} diff --git a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/FloatRangeArgument.java b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/FloatRangeArgument.java new file mode 100644 index 00000000..84e0e598 --- /dev/null +++ b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/FloatRangeArgument.java @@ -0,0 +1,139 @@ +// +// 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.fabric.argument; + +import cloud.commandframework.arguments.CommandArgument; +import cloud.commandframework.brigadier.argument.WrappedBrigadierParser; +import cloud.commandframework.context.CommandContext; +import net.minecraft.command.argument.NumberRangeArgumentType; +import net.minecraft.predicate.NumberRange; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.List; +import java.util.function.BiFunction; + +/** + * An argument parsing an unbounded float range, in the form {@code [min]..[max] }, where both lower and upper bounds are + * optional. + * + * @param the sender type + * @since 1.4.0 + */ +public final class FloatRangeArgument extends CommandArgument { + + FloatRangeArgument( + final boolean required, + final @NonNull String name, + final @NonNull String defaultValue, + final @Nullable BiFunction, String, List> suggestionsProvider + ) { + super( + required, + name, + new WrappedBrigadierParser<>(NumberRangeArgumentType.method_30918()), + defaultValue, + NumberRange.FloatRange.class, + suggestionsProvider + ); + } + + /** + * Create a new builder. + * + * @param name Name of the argument + * @param Command sender type + * @return Created builder + */ + public static FloatRangeArgument.@NonNull Builder newBuilder(final @NonNull String name) { + return new FloatRangeArgument.Builder<>(name); + } + + /** + * Create a new required command argument. + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull FloatRangeArgument of(final @NonNull String name) { + return FloatRangeArgument.newBuilder(name).asRequired().build(); + } + + /** + * Create a new optional command argument + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull FloatRangeArgument optional(final @NonNull String name) { + return FloatRangeArgument.newBuilder(name).asOptional().build(); + } + + /** + * Create a new optional command argument with a default value + * + * @param name Argument name + * @param defaultValue Default value + * @param Command sender type + * @return Created argument + */ + public static @NonNull FloatRangeArgument optional( + final @NonNull String name, + final NumberRange.FloatRange defaultValue + ) { + final StringBuilder value = new StringBuilder(6); + if (defaultValue.getMin() != null) { + value.append(defaultValue.getMin()); + } + value.append(".."); + if (defaultValue.getMax() != null) { + value.append(defaultValue.getMax()); + } + + return FloatRangeArgument.newBuilder(name).asOptionalWithDefault(value.toString()).build(); + } + + + public static final class Builder extends TypedBuilder> { + + Builder(final @NonNull String name) { + super(NumberRange.FloatRange.class, name); + } + + /** + * Build a new criterion argument + * + * @return Constructed argument + */ + @Override + public @NonNull FloatRangeArgument build() { + return new FloatRangeArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(), this.getSuggestionsProvider()); + } + + } + +} diff --git a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/IdentifierArgument.java b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/IdentifierArgument.java new file mode 100644 index 00000000..4b3ace5f --- /dev/null +++ b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/IdentifierArgument.java @@ -0,0 +1,129 @@ +// +// 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.fabric.argument; + +import cloud.commandframework.arguments.CommandArgument; +import cloud.commandframework.brigadier.argument.WrappedBrigadierParser; +import cloud.commandframework.context.CommandContext; +import net.minecraft.command.argument.IdentifierArgumentType; +import net.minecraft.util.Identifier; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.List; +import java.util.function.BiFunction; + +/** + * An argument parsing an identifier, or "resource location". + * + * @param the sender type + * @since 1.4.0 + */ +public final class IdentifierArgument extends CommandArgument { + + IdentifierArgument( + final boolean required, + final @NonNull String name, + final @NonNull String defaultValue, + final @Nullable BiFunction, String, List> suggestionsProvider + ) { + super( + required, + name, + new WrappedBrigadierParser<>(IdentifierArgumentType.identifier()), + defaultValue, + Identifier.class, + suggestionsProvider + ); + } + + /** + * Create a new builder. + * + * @param name Name of the argument + * @param Command sender type + * @return Created builder + */ + public static IdentifierArgument.@NonNull Builder newBuilder(final @NonNull String name) { + return new IdentifierArgument.Builder<>(name); + } + + /** + * Create a new required command argument. + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull IdentifierArgument of(final @NonNull String name) { + return IdentifierArgument.newBuilder(name).asRequired().build(); + } + + /** + * Create a new optional command argument + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull IdentifierArgument optional(final @NonNull String name) { + return IdentifierArgument.newBuilder(name).asOptional().build(); + } + + /** + * Create a new optional command argument with a default value + * + * @param name Argument name + * @param defaultValue Default value + * @param Command sender type + * @return Created argument + */ + public static @NonNull IdentifierArgument optional( + final @NonNull String name, + final @NonNull Identifier defaultValue + ) { + return IdentifierArgument.newBuilder(name).asOptionalWithDefault(defaultValue.toString()).build(); + } + + + public static final class Builder extends TypedBuilder> { + + Builder(final @NonNull String name) { + super(Identifier.class, name); + } + + /** + * Build a new criterion argument + * + * @return Constructed argument + */ + @Override + public @NonNull IdentifierArgument build() { + return new IdentifierArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(), this.getSuggestionsProvider()); + } + + } + +} diff --git a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/IntRangeArgument.java b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/IntRangeArgument.java new file mode 100644 index 00000000..028de9d4 --- /dev/null +++ b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/IntRangeArgument.java @@ -0,0 +1,139 @@ +// +// 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.fabric.argument; + +import cloud.commandframework.arguments.CommandArgument; +import cloud.commandframework.brigadier.argument.WrappedBrigadierParser; +import cloud.commandframework.context.CommandContext; +import net.minecraft.command.argument.NumberRangeArgumentType; +import net.minecraft.predicate.NumberRange; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.List; +import java.util.function.BiFunction; + +/** + * An argument parsing an unbounded number range, in the form {@code [min]..[max] }, where both lower and upper bounds are + * optional. + * + * @param the sender type + * @since 1.4.0 + */ +public final class IntRangeArgument extends CommandArgument { + + IntRangeArgument( + final boolean required, + final @NonNull String name, + final @NonNull String defaultValue, + final @Nullable BiFunction, String, List> suggestionsProvider + ) { + super( + required, + name, + new WrappedBrigadierParser<>(NumberRangeArgumentType.numberRange()), + defaultValue, + NumberRange.IntRange.class, + suggestionsProvider + ); + } + + /** + * Create a new builder. + * + * @param name Name of the argument + * @param Command sender type + * @return Created builder + */ + public static IntRangeArgument.@NonNull Builder newBuilder(final @NonNull String name) { + return new IntRangeArgument.Builder<>(name); + } + + /** + * Create a new required command argument. + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull IntRangeArgument of(final @NonNull String name) { + return IntRangeArgument.newBuilder(name).asRequired().build(); + } + + /** + * Create a new optional command argument + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull IntRangeArgument optional(final @NonNull String name) { + return IntRangeArgument.newBuilder(name).asOptional().build(); + } + + /** + * Create a new optional command argument with a default value + * + * @param name Argument name + * @param defaultValue Default value + * @param Command sender type + * @return Created argument + */ + public static @NonNull IntRangeArgument optional( + final @NonNull String name, + final NumberRange.IntRange defaultValue + ) { + final StringBuilder value = new StringBuilder(6); + if (defaultValue.getMin() != null) { + value.append(defaultValue.getMin()); + } + value.append(".."); + if (defaultValue.getMax() != null) { + value.append(defaultValue.getMax()); + } + + return IntRangeArgument.newBuilder(name).asOptionalWithDefault(value.toString()).build(); + } + + + public static final class Builder extends TypedBuilder> { + + Builder(final @NonNull String name) { + super(NumberRange.IntRange.class, name); + } + + /** + * Build a new criterion argument + * + * @return Constructed argument + */ + @Override + public @NonNull IntRangeArgument build() { + return new IntRangeArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(), this.getSuggestionsProvider()); + } + + } + +} diff --git a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ItemDataArgument.java b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ItemDataArgument.java new file mode 100644 index 00000000..fb5f7e19 --- /dev/null +++ b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ItemDataArgument.java @@ -0,0 +1,137 @@ +// +// 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.fabric.argument; + +import cloud.commandframework.arguments.CommandArgument; +import cloud.commandframework.brigadier.argument.WrappedBrigadierParser; +import cloud.commandframework.context.CommandContext; +import net.minecraft.command.argument.ItemStackArgument; +import net.minecraft.command.argument.ItemStackArgumentType; +import net.minecraft.item.ItemStack; +import net.minecraft.util.registry.Registry; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.List; +import java.util.function.BiFunction; + +/** + * An argument parsing an item identifier and optional NBT data + * + * @param the sender type + * @since 1.4.0 + */ +public final class ItemDataArgument extends CommandArgument { + + ItemDataArgument( + final boolean required, + final @NonNull String name, + final @NonNull String defaultValue, + final @Nullable BiFunction, String, List> suggestionsProvider + ) { + super( + required, + name, + new WrappedBrigadierParser<>(ItemStackArgumentType.itemStack()), + defaultValue, + ItemStackArgument.class, + suggestionsProvider + ); + } + + /** + * Create a new builder. + * + * @param name Name of the argument + * @param Command sender type + * @return Created builder + */ + public static ItemDataArgument.@NonNull Builder newBuilder(final @NonNull String name) { + return new ItemDataArgument.Builder<>(name); + } + + /** + * Create a new required command argument. + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull ItemDataArgument of(final @NonNull String name) { + return ItemDataArgument.newBuilder(name).asRequired().build(); + } + + /** + * Create a new optional command argument + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull ItemDataArgument optional(final @NonNull String name) { + return ItemDataArgument.newBuilder(name).asOptional().build(); + } + + /** + * Create a new optional command argument with a default value + * + * @param name Argument name + * @param defaultValue Default value + * @param Command sender type + * @return Created argument + */ + public static @NonNull ItemDataArgument optional( + final @NonNull String name, + final ItemStack defaultValue + ) { + final String serializedDefault; + if (defaultValue.hasTag()) { + serializedDefault = Registry.ITEM.getId(defaultValue.getItem()).toString() + defaultValue.getTag().toString(); + } else { + serializedDefault = Registry.ITEM.getId(defaultValue.getItem()).toString(); + } + return ItemDataArgument.newBuilder(name).asOptionalWithDefault(serializedDefault).build(); + + } + + public static final class Builder extends TypedBuilder> { + + Builder(final @NonNull String name) { + super(ItemStackArgument.class, name); + } + + /** + * Build a new criterion argument + * + * @return Constructed argument + */ + @Override + public @NonNull ItemDataArgument build() { + return new ItemDataArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(), this.getSuggestionsProvider()); + } + + } + +} diff --git a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/NbtPathArgument.java b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/NbtPathArgument.java new file mode 100644 index 00000000..132b4ea0 --- /dev/null +++ b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/NbtPathArgument.java @@ -0,0 +1,128 @@ +// +// 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.fabric.argument; + +import cloud.commandframework.arguments.CommandArgument; +import cloud.commandframework.brigadier.argument.WrappedBrigadierParser; +import cloud.commandframework.context.CommandContext; +import net.minecraft.command.argument.NbtPathArgumentType; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.List; +import java.util.function.BiFunction; + +/** + * An argument for NBT paths to locations within {@link net.minecraft.nbt.Tag Tags}. + * + * @param the sender type + * @since 1.4.0 + */ +public final class NbtPathArgument extends CommandArgument { + + NbtPathArgument( + final boolean required, + final @NonNull String name, + final @NonNull String defaultValue, + final @Nullable BiFunction, String, List> suggestionsProvider + ) { + super( + required, + name, + new WrappedBrigadierParser<>(NbtPathArgumentType.nbtPath()), + defaultValue, + NbtPathArgumentType.NbtPath.class, + suggestionsProvider + ); + } + + /** + * Create a new builder + * + * @param name Name of the component + * @param Command sender type + * @return Created builder + */ + public static NbtPathArgument.@NonNull Builder newBuilder(final @NonNull String name) { + return new NbtPathArgument.Builder<>(name); + } + + /** + * Create a new required command argument + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull NbtPathArgument of(final @NonNull String name) { + return NbtPathArgument.newBuilder(name).asRequired().build(); + } + + /** + * Create a new optional command argument + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull NbtPathArgument optional(final @NonNull String name) { + return NbtPathArgument.newBuilder(name).asOptional().build(); + } + + /** + * Create a new optional command argument with a default value + * + * @param name Component name + * @param defaultTag Default tag value + * @param Command sender type + * @return Created component + */ + public static @NonNull NbtPathArgument optional( + final @NonNull String name, + final NbtPathArgumentType.@NonNull NbtPath defaultTag + ) { + return NbtPathArgument.newBuilder(name).asOptionalWithDefault(defaultTag.toString()).build(); + } + + + public static final class Builder extends TypedBuilder> { + + Builder(final @NonNull String name) { + super(NbtPathArgumentType.NbtPath.class, name); + } + + /** + * Builder a new example component + * + * @return Constructed component + */ + @Override + public @NonNull NbtPathArgument build() { + return new NbtPathArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(), this.getSuggestionsProvider()); + } + + } + +} diff --git a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/NbtTagArgument.java b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/NbtTagArgument.java new file mode 100644 index 00000000..413fb148 --- /dev/null +++ b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/NbtTagArgument.java @@ -0,0 +1,129 @@ +// +// 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.fabric.argument; + +import cloud.commandframework.arguments.CommandArgument; +import cloud.commandframework.brigadier.argument.WrappedBrigadierParser; +import cloud.commandframework.context.CommandContext; +import net.minecraft.command.argument.NbtTagArgumentType; +import net.minecraft.nbt.Tag; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.List; +import java.util.function.BiFunction; + +/** + * An argument for the string representation of an NBT {@link Tag}. + * + * @param the sender type + * @since 1.4.0 + */ +public final class NbtTagArgument extends CommandArgument { + + NbtTagArgument( + final boolean required, + final @NonNull String name, + final @NonNull String defaultValue, + final @Nullable BiFunction, String, List> suggestionsProvider + ) { + super( + required, + name, + new WrappedBrigadierParser<>(NbtTagArgumentType.nbtTag()), + defaultValue, + Tag.class, + suggestionsProvider + ); + } + + /** + * Create a new builder + * + * @param name Name of the component + * @param Command sender type + * @return Created builder + */ + public static NbtTagArgument.@NonNull Builder newBuilder(final @NonNull String name) { + return new NbtTagArgument.Builder<>(name); + } + + /** + * Create a new required command argument + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull NbtTagArgument of(final @NonNull String name) { + return NbtTagArgument.newBuilder(name).asRequired().build(); + } + + /** + * Create a new optional command argument + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull NbtTagArgument optional(final @NonNull String name) { + return NbtTagArgument.newBuilder(name).asOptional().build(); + } + + /** + * Create a new optional command argument with a default value + * + * @param name Component name + * @param defaultTag Default tag value + * @param Command sender type + * @return Created component + */ + public static @NonNull NbtTagArgument optional( + final @NonNull String name, + final @NonNull Tag defaultTag + ) { + return NbtTagArgument.newBuilder(name).asOptionalWithDefault(defaultTag.toString()).build(); + } + + + public static final class Builder extends TypedBuilder> { + + Builder(final @NonNull String name) { + super(Tag.class, name); + } + + /** + * Builder a new example component + * + * @return Constructed component + */ + @Override + public @NonNull NbtTagArgument build() { + return new NbtTagArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(), this.getSuggestionsProvider()); + } + + } + +} diff --git a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ParticleEffectArgument.java b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ParticleEffectArgument.java new file mode 100644 index 00000000..d7cf8f96 --- /dev/null +++ b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ParticleEffectArgument.java @@ -0,0 +1,131 @@ +// +// 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.fabric.argument; + +import cloud.commandframework.arguments.CommandArgument; +import cloud.commandframework.brigadier.argument.WrappedBrigadierParser; +import cloud.commandframework.context.CommandContext; +import net.minecraft.command.argument.ParticleArgumentType; +import net.minecraft.particle.ParticleEffect; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.List; +import java.util.function.BiFunction; + +/** + * An argument for any {@link net.minecraft.particle.ParticleEffect} + * + *

These operations can be used to compare scores on a {@link net.minecraft.scoreboard.Scoreboard}.

+ * + * @param the sender type + * @since 1.4.0 + */ +public final class ParticleEffectArgument extends CommandArgument { + + ParticleEffectArgument( + final boolean required, + final @NonNull String name, + final @NonNull String defaultValue, + final @Nullable BiFunction, String, List> suggestionsProvider + ) { + super( + required, + name, + new WrappedBrigadierParser<>(ParticleArgumentType.particle()), + defaultValue, + ParticleEffect.class, + suggestionsProvider + ); + } + + /** + * Create a new builder. + * + * @param name Name of the argument + * @param Command sender type + * @return Created builder + */ + public static ParticleEffectArgument.@NonNull Builder newBuilder(final @NonNull String name) { + return new ParticleEffectArgument.Builder<>(name); + } + + /** + * Create a new required command argument. + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull ParticleEffectArgument of(final @NonNull String name) { + return ParticleEffectArgument.newBuilder(name).asRequired().build(); + } + + /** + * Create a new optional command argument + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull ParticleEffectArgument optional(final @NonNull String name) { + return ParticleEffectArgument.newBuilder(name).asOptional().build(); + } + + /** + * Create a new optional command argument with a default value + * + * @param name Argument name + * @param defaultValue Default particle effect value + * @param Command sender type + * @return Created argument + */ + public static @NonNull ParticleEffectArgument optional( + final @NonNull String name, + final ParticleEffect defaultValue + ) { + return ParticleEffectArgument.newBuilder(name).asOptionalWithDefault(defaultValue.asString()).build(); + } + + + public static final class Builder extends TypedBuilder> { + + Builder(final @NonNull String name) { + super(ParticleEffect.class, name); + } + + /** + * Build a new criterion argument + * + * @return Constructed argument + */ + @Override + public @NonNull ParticleEffectArgument build() { + return new ParticleEffectArgument<>(this.isRequired(), this.getName(), this.getDefaultValue(), this.getSuggestionsProvider()); + } + + } + +} diff --git a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ScoreboardCriterionArgument.java b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ScoreboardCriterionArgument.java new file mode 100644 index 00000000..e52ab1e6 --- /dev/null +++ b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ScoreboardCriterionArgument.java @@ -0,0 +1,134 @@ +// +// 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.fabric.argument; + +import cloud.commandframework.arguments.CommandArgument; +import cloud.commandframework.brigadier.argument.WrappedBrigadierParser; +import cloud.commandframework.context.CommandContext; +import net.minecraft.command.argument.ObjectiveCriteriaArgumentType; +import net.minecraft.scoreboard.ScoreboardCriterion; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.List; +import java.util.function.BiFunction; + +/** + * An argument for a {@linkplain ScoreboardCriterion criterion} in a scoreboard. + * + * @param the sender type + * @since 1.4.0 + */ +public final class ScoreboardCriterionArgument extends CommandArgument { + + ScoreboardCriterionArgument( + final boolean required, + final @NonNull String name, + final @NonNull String defaultValue, + final @Nullable BiFunction, String, List> suggestionsProvider + ) { + super( + required, + name, + new WrappedBrigadierParser<>(ObjectiveCriteriaArgumentType.objectiveCriteria()), + defaultValue, + ScoreboardCriterion.class, + suggestionsProvider + ); + } + + /** + * Create a new builder. + * + * @param name Name of the argument + * @param Command sender type + * @return Created builder + */ + public static ScoreboardCriterionArgument.@NonNull Builder newBuilder(final @NonNull String name) { + return new ScoreboardCriterionArgument.Builder<>(name); + } + + /** + * Create a new required command argument. + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull ScoreboardCriterionArgument of(final @NonNull String name) { + return ScoreboardCriterionArgument.newBuilder(name).asRequired().build(); + } + + /** + * Create a new optional command argument + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull ScoreboardCriterionArgument optional(final @NonNull String name) { + return ScoreboardCriterionArgument.newBuilder(name).asOptional().build(); + } + + /** + * Create a new optional command argument with a default value + * + * @param name Argument name + * @param defaultCriterion Default criterion + * @param Command sender type + * @return Created argument + */ + public static @NonNull ScoreboardCriterionArgument optional( + final @NonNull String name, + final ScoreboardCriterion defaultCriterion + ) { + return ScoreboardCriterionArgument.newBuilder(name).asOptionalWithDefault(defaultCriterion.getName()).build(); + } + + + public static final class Builder extends TypedBuilder> { + + Builder(final @NonNull String name) { + super(ScoreboardCriterion.class, name); + } + + /** + * Build a new criterion argument + * + * @return Constructed argument + */ + @Override + public @NonNull ScoreboardCriterionArgument build() { + return new ScoreboardCriterionArgument<>( + this.isRequired(), + this.getName(), + this.getDefaultValue(), + this.getSuggestionsProvider() + ); + } + + } + +} diff --git a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ScoreboardOperationArgument.java b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ScoreboardOperationArgument.java new file mode 100644 index 00000000..648bb0b8 --- /dev/null +++ b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/ScoreboardOperationArgument.java @@ -0,0 +1,135 @@ +// +// 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.fabric.argument; + +import cloud.commandframework.arguments.CommandArgument; +import cloud.commandframework.brigadier.argument.WrappedBrigadierParser; +import cloud.commandframework.context.CommandContext; +import net.minecraft.command.argument.OperationArgumentType; +import net.minecraft.command.argument.OperationArgumentType.Operation; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.List; +import java.util.function.BiFunction; + +/** + * An argument for selecting any of the logical operations in {@link Operation}. + * + *

These operations can be used to compare scores on a {@link net.minecraft.scoreboard.Scoreboard}.

+ * + * @param the sender type + * @since 1.4.0 + */ +public final class ScoreboardOperationArgument extends CommandArgument { + + ScoreboardOperationArgument( + final boolean required, + final @NonNull String name, + final @NonNull String defaultValue, + final @Nullable BiFunction, String, List> suggestionsProvider + ) { + super( + required, + name, + new WrappedBrigadierParser<>(OperationArgumentType.operation()), + defaultValue, + Operation.class, + suggestionsProvider + ); + } + + /** + * Create a new builder. + * + * @param name Name of the argument + * @param Command sender type + * @return Created builder + */ + public static ScoreboardOperationArgument.@NonNull Builder newBuilder(final @NonNull String name) { + return new ScoreboardOperationArgument.Builder<>(name); + } + + /** + * Create a new required command argument. + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull ScoreboardOperationArgument of(final @NonNull String name) { + return ScoreboardOperationArgument.newBuilder(name).asRequired().build(); + } + + /** + * Create a new optional command argument + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull ScoreboardOperationArgument optional(final @NonNull String name) { + return ScoreboardOperationArgument.newBuilder(name).asOptional().build(); + } + + /* (todo: there's no way to get a parseable form from an unknown Operation) + * Create a new optional command argument with a default value + * + * @param name Argument name + * @param defaultTag Default tag value + * @param Command sender type + * @return Created argument + * + public static @NonNull ScoreboardOperationArgument optional( + final @NonNull String name, + final Operation defaultTag + ) { + return ScoreboardOperationArgument.newBuilder(name).asOptionalWithDefault(defaultTag.toString()).build(); + }*/ + + public static final class Builder extends TypedBuilder> { + + Builder(final @NonNull String name) { + super(Operation.class, name); + } + + /** + * Build a new criterion argument + * + * @return Constructed argument + */ + @Override + public @NonNull ScoreboardOperationArgument build() { + return new ScoreboardOperationArgument<>( + this.isRequired(), + this.getName(), + this.getDefaultValue(), + this.getSuggestionsProvider() + ); + } + + } + +} diff --git a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/StatusEffectArgument.java b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/StatusEffectArgument.java new file mode 100644 index 00000000..d4746027 --- /dev/null +++ b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/StatusEffectArgument.java @@ -0,0 +1,137 @@ +// +// 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.fabric.argument; + +import cloud.commandframework.arguments.CommandArgument; +import cloud.commandframework.brigadier.argument.WrappedBrigadierParser; +import cloud.commandframework.context.CommandContext; +import net.minecraft.command.argument.MobEffectArgumentType; +import net.minecraft.entity.effect.StatusEffect; +import net.minecraft.util.registry.Registry; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.List; +import java.util.function.BiFunction; + +/** + * An argument parsing a status effect from the {@link net.minecraft.util.registry.Registry#STATUS_EFFECT status effect registry} + * + * @param the sender type + * @since 1.4.0 + */ +public final class StatusEffectArgument extends CommandArgument { + + StatusEffectArgument( + final boolean required, + final @NonNull String name, + final @NonNull String defaultValue, + final @Nullable BiFunction, String, List> suggestionsProvider + ) { + super( + required, + name, + new WrappedBrigadierParser<>(MobEffectArgumentType.mobEffect()), + defaultValue, + StatusEffect.class, + suggestionsProvider + ); + } + + /** + * Create a new builder. + * + * @param name Name of the argument + * @param Command sender type + * @return Created builder + */ + public static StatusEffectArgument.@NonNull Builder newBuilder(final @NonNull String name) { + return new StatusEffectArgument.Builder<>(name); + } + + /** + * Create a new required command argument. + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull StatusEffectArgument of(final @NonNull String name) { + return StatusEffectArgument.newBuilder(name).asRequired().build(); + } + + /** + * Create a new optional command argument + * + * @param name Component name + * @param Command sender type + * @return Created argument + */ + public static @NonNull StatusEffectArgument optional(final @NonNull String name) { + return StatusEffectArgument.newBuilder(name).asOptional().build(); + } + + /** + * Create a new optional command argument with a default value + * + * @param name Argument name + * @param defaultValue Default value + * @param Command sender type + * @return Created argument + */ + public static @NonNull StatusEffectArgument optional( + final @NonNull String name, + final @NonNull StatusEffect defaultValue + ) { + return StatusEffectArgument.newBuilder(name) + .asOptionalWithDefault(Registry.STATUS_EFFECT.getId(defaultValue).toString()) + .build(); + } + + + public static final class Builder extends TypedBuilder> { + + Builder(final @NonNull String name) { + super(StatusEffect.class, name); + } + + /** + * Build a new criterion argument + * + * @return Constructed argument + */ + @Override + public @NonNull StatusEffectArgument build() { + return new StatusEffectArgument<>( + this.isRequired(), + this.getName(), + this.getDefaultValue(), + this.getSuggestionsProvider() + ); + } + + } + +} diff --git a/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/package-info.java b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/package-info.java new file mode 100644 index 00000000..051881d8 --- /dev/null +++ b/cloud-minecraft/cloud-fabric/src/main/java/cloud/commandframework/fabric/argument/package-info.java @@ -0,0 +1,28 @@ +// +// 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. +// + +/** + * Arguments for the Fabric environment. + */ +package cloud.commandframework.fabric.argument;