diff --git a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentEnum.java b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentEnum.java index 7d91f157a..3d0a5d8a0 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentEnum.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentEnum.java @@ -23,8 +23,9 @@ public class ArgumentEnum extends Argument { this.values = enumClass.getEnumConstants(); } - public void setFormat(@NotNull Format format) { + public ArgumentEnum setFormat(@NotNull Format format) { this.format = format; + return this; } @NotNull diff --git a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentType.java b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentType.java index a9f399895..9d3942014 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentType.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentType.java @@ -18,67 +18,87 @@ import org.jetbrains.annotations.NotNull; */ public class ArgumentType { + /** + * @see ArgumentLiteral + */ public static ArgumentLiteral Literal(@NotNull String id) { return new ArgumentLiteral(id); } + /** + * @see ArgumentGroup + */ public static ArgumentGroup Group(@NotNull String id, @NotNull Argument... arguments) { return new ArgumentGroup(id, arguments); } + /** + * @see ArgumentLoop + */ @SafeVarargs public static ArgumentLoop Loop(@NotNull String id, @NotNull Argument... arguments) { return new ArgumentLoop<>(id, arguments); } + /** + * @see ArgumentBoolean + */ public static ArgumentBoolean Boolean(@NotNull String id) { return new ArgumentBoolean(id); } - public static ArgumentLong Long(@NotNull String id) { - return new ArgumentLong(id); - } - + /** + * @see ArgumentInteger + */ public static ArgumentInteger Integer(@NotNull String id) { return new ArgumentInteger(id); } + /** + * @see ArgumentDouble + */ public static ArgumentDouble Double(@NotNull String id) { return new ArgumentDouble(id); } + /** + * @see ArgumentFloat + */ public static ArgumentFloat Float(@NotNull String id) { return new ArgumentFloat(id); } + /** + * @see ArgumentString + */ public static ArgumentString String(@NotNull String id) { return new ArgumentString(id); } + /** + * @see ArgumentWord + */ public static ArgumentWord Word(@NotNull String id) { return new ArgumentWord(id); } - public static ArgumentDynamicWord DynamicWord(@NotNull String id, @NotNull SuggestionType suggestionType) { - return new ArgumentDynamicWord(id, suggestionType); - } - - public static ArgumentDynamicWord DynamicWord(@NotNull String id) { - return DynamicWord(id, SuggestionType.ASK_SERVER); - } - + /** + * @see ArgumentStringArray + */ public static ArgumentStringArray StringArray(@NotNull String id) { return new ArgumentStringArray(id); } - public static ArgumentDynamicStringArray DynamicStringArray(@NotNull String id) { - return new ArgumentDynamicStringArray(id); - } - + /** + * @see ArgumentCommand + */ public static ArgumentCommand Command(@NotNull String id) { return new ArgumentCommand(id); } + /** + * @see ArgumentEnum + */ @SuppressWarnings("rawtypes") public static ArgumentEnum Enum(@NotNull String id, @NotNull Class enumClass) { return new ArgumentEnum<>(id, enumClass); @@ -86,72 +106,167 @@ public class ArgumentType { // Minecraft specific arguments + /** + * @see ArgumentColor + */ public static ArgumentColor Color(@NotNull String id) { return new ArgumentColor(id); } + /** + * @see ArgumentTime + */ public static ArgumentTime Time(@NotNull String id) { return new ArgumentTime(id); } + /** + * @see ArgumentEnchantment + */ public static ArgumentEnchantment Enchantment(@NotNull String id) { return new ArgumentEnchantment(id); } + /** + * @see ArgumentParticle + */ public static ArgumentParticle Particle(@NotNull String id) { return new ArgumentParticle(id); } + /** + * @see ArgumentPotionEffect + */ public static ArgumentPotionEffect Potion(@NotNull String id) { return new ArgumentPotionEffect(id); } + /** + * @see ArgumentEntityType + */ public static ArgumentEntityType EntityType(@NotNull String id) { return new ArgumentEntityType(id); } + /** + * @see ArgumentBlockState + */ public static ArgumentBlockState BlockState(@NotNull String id) { return new ArgumentBlockState(id); } + /** + * @see ArgumentIntRange + */ public static ArgumentIntRange IntRange(@NotNull String id) { return new ArgumentIntRange(id); } + /** + * @see ArgumentFloatRange + */ public static ArgumentFloatRange FloatRange(@NotNull String id) { return new ArgumentFloatRange(id); } - public static ArgumentEntity Entities(@NotNull String id) { + /** + * @see ArgumentEntity + */ + public static ArgumentEntity Entity(@NotNull String id) { return new ArgumentEntity(id); } + /** + * @see ArgumentItemStack + */ public static ArgumentItemStack ItemStack(@NotNull String id) { return new ArgumentItemStack(id); } + /** + * @see ArgumentComponent + */ public static ArgumentComponent Component(@NotNull String id) { return new ArgumentComponent(id); } + /** + * @see ArgumentNbtTag + */ public static ArgumentNbtTag NBT(@NotNull String id) { return new ArgumentNbtTag(id); } + /** + * @see ArgumentNbtCompoundTag + */ public static ArgumentNbtCompoundTag NbtCompound(@NotNull String id) { return new ArgumentNbtCompoundTag(id); } + /** + * @see ArgumentRelativeBlockPosition + */ public static ArgumentRelativeBlockPosition RelativeBlockPosition(@NotNull String id) { return new ArgumentRelativeBlockPosition(id); } + /** + * @see ArgumentRelativeVec3 + */ public static ArgumentRelativeVec3 RelativeVec3(@NotNull String id) { return new ArgumentRelativeVec3(id); } + /** + * @see ArgumentRelativeVec2 + */ public static ArgumentRelativeVec2 RelativeVec2(@NotNull String id) { return new ArgumentRelativeVec2(id); } + /** + * @see ArgumentLoop + * @deprecated brigadier does not support long + */ + @Deprecated + public static ArgumentLong Long(@NotNull String id) { + return new ArgumentLong(id); + } + + /** + * @see ArgumentEntity + * @deprecated use {@link #Entity(String)} + */ + @Deprecated + public static ArgumentEntity Entities(@NotNull String id) { + return new ArgumentEntity(id); + } + + /** + * @see ArgumentDynamicWord + * @deprecated will be replaced soon + */ + @Deprecated + public static ArgumentDynamicWord DynamicWord(@NotNull String id, @NotNull SuggestionType suggestionType) { + return new ArgumentDynamicWord(id, suggestionType); + } + + /** + * @see ArgumentDynamicWord + * @deprecated will be replaced soon + */ + @Deprecated + public static ArgumentDynamicWord DynamicWord(@NotNull String id) { + return DynamicWord(id, SuggestionType.ASK_SERVER); + } + + /** + * @see ArgumentDynamicStringArray + * @deprecated will be replaced soon + */ + @Deprecated + public static ArgumentDynamicStringArray DynamicStringArray(@NotNull String id) { + return new ArgumentDynamicStringArray(id); + } } diff --git a/src/test/java/demo/commands/EntitySelectorCommand.java b/src/test/java/demo/commands/EntitySelectorCommand.java index 31a61d26e..60a3b8495 100644 --- a/src/test/java/demo/commands/EntitySelectorCommand.java +++ b/src/test/java/demo/commands/EntitySelectorCommand.java @@ -6,8 +6,8 @@ import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.arguments.ArgumentType; import net.minestom.server.command.builder.arguments.minecraft.ArgumentEntity; import net.minestom.server.entity.Entity; -import net.minestom.server.entity.Player; import net.minestom.server.instance.Instance; +import net.minestom.server.utils.entity.EntityFinder; import java.util.List; @@ -18,7 +18,7 @@ public class EntitySelectorCommand extends Command { setDefaultExecutor((sender, args) -> System.out.println("DEFAULT")); - ArgumentEntity argumentEntity = ArgumentType.Entities("entities").onlyPlayers(true); + ArgumentEntity argumentEntity = ArgumentType.Entity("entities").onlyPlayers(true); setArgumentCallback((sender, exception) -> exception.printStackTrace(), argumentEntity); @@ -28,7 +28,8 @@ public class EntitySelectorCommand extends Command { private void executor(CommandSender commandSender, Arguments arguments) { Instance instance = commandSender.asPlayer().getInstance(); - List entities = arguments.getEntities("entities").find(instance, null); - System.out.println("test " + ((Player) entities.get(0)).getUsername()); + EntityFinder entityFinder = arguments.get("entities"); + List entities = entityFinder.find(commandSender); + System.out.println("found " + entities.size() + " entities"); } } diff --git a/src/test/java/demo/commands/GamemodeCommand.java b/src/test/java/demo/commands/GamemodeCommand.java index 7902eb6bd..46518d1dc 100644 --- a/src/test/java/demo/commands/GamemodeCommand.java +++ b/src/test/java/demo/commands/GamemodeCommand.java @@ -3,13 +3,12 @@ package demo.commands; import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.Arguments; import net.minestom.server.command.builder.Command; -import net.minestom.server.command.builder.arguments.Argument; +import net.minestom.server.command.builder.arguments.ArgumentEnum; import net.minestom.server.command.builder.arguments.ArgumentType; import net.minestom.server.command.builder.exception.ArgumentSyntaxException; import net.minestom.server.entity.GameMode; import net.minestom.server.entity.Player; - -import java.util.Optional; +import net.minestom.server.utils.entity.EntityFinder; /** * Command that make a player change gamemode @@ -22,15 +21,14 @@ public class GamemodeCommand extends Command { setDefaultExecutor(this::usage); - Argument player = ArgumentType.Word("player"); + var player = ArgumentType.Entity("player") + .onlyPlayers(true) + .singleEntity(true); - GameMode[] gameModes = GameMode.values(); - String[] names = new String[gameModes.length]; - for (int i = 0; i < gameModes.length; i++) { - names[i] = gameModes[i].name().toLowerCase(); - } - Argument mode = ArgumentType.Word("mode").from(names); + var mode = ArgumentType.Enum("gamemode", GameMode.class) + .setFormat(ArgumentEnum.Format.LOWER_CASED); + setArgumentCallback(this::targetCallback, player); setArgumentCallback(this::gameModeCallback, mode); addSyntax(this::executeOnSelf, mode); @@ -44,27 +42,24 @@ public class GamemodeCommand extends Command { private void executeOnSelf(CommandSender sender, Arguments arguments) { Player player = (Player) sender; - String gamemodeName = arguments.getWord("mode"); - GameMode mode = GameMode.valueOf(gamemodeName.toUpperCase()); - assert mode != null; // mode is not supposed to be null, because gamemodeName will be valid - player.setGameMode(mode); - player.sendMessage("You are now playing in " + gamemodeName); + GameMode gamemode = arguments.get("gamemode"); + assert gamemode != null; // mode is not supposed to be null, because gamemodeName will be valid + player.setGameMode(gamemode); + player.sendMessage("You are now playing in " + gamemode.toString().toLowerCase()); } private void executeOnOther(CommandSender sender, Arguments arguments) { - Player player = (Player) sender; + GameMode gamemode = arguments.get("gamemode"); + EntityFinder targetFinder = arguments.get("player"); + Player target = targetFinder.findFirstPlayer(sender); + assert gamemode != null; // mode is not supposed to be null, because gamemodeName will be valid + assert target != null; + target.setGameMode(gamemode); + target.sendMessage("You are now playing in " + gamemode.toString().toLowerCase()); + } - String gamemodeName = arguments.getWord("mode"); - String targetName = arguments.getWord("player"); - GameMode mode = GameMode.valueOf(gamemodeName.toUpperCase()); - assert mode != null; // mode is not supposed to be null, because gamemodeName will be valid - Optional target = player.getInstance().getPlayers().stream().filter(p -> p.getUsername().equalsIgnoreCase(targetName)).findFirst(); - if (target.isPresent()) { - target.get().setGameMode(mode); - target.get().sendMessage("You are now playing in " + gamemodeName); - } else { - player.sendMessage("'" + targetName + "' is not a valid player name."); - } + private void targetCallback(CommandSender sender, ArgumentSyntaxException exception) { + sender.sendMessage("'" + exception.getInput() + "' is not a valid player name."); } private void gameModeCallback(CommandSender sender, ArgumentSyntaxException exception) { diff --git a/src/test/java/demo/commands/HealthCommand.java b/src/test/java/demo/commands/HealthCommand.java index b3eeaf89c..3b37c60d5 100644 --- a/src/test/java/demo/commands/HealthCommand.java +++ b/src/test/java/demo/commands/HealthCommand.java @@ -58,13 +58,13 @@ public class HealthCommand extends Command { } private void sendSuggestionMessage(CommandSender sender, Arguments args) { - sender.sendMessage("/health " + args.getWord("mode") + " [Integer]"); + sender.sendMessage("/health " + args.get("mode") + " [Integer]"); } private void onHealthCommand(CommandSender sender, Arguments args) { final Player player = (Player) sender; - final String mode = args.getWord("mode"); - final int value = args.getInteger("value"); + final String mode = args.get("mode"); + final int value = args.get("value"); switch (mode.toLowerCase()) { case "set": diff --git a/src/test/java/demo/commands/HorseCommand.java b/src/test/java/demo/commands/HorseCommand.java index cc5bd7cce..cdf0d7b74 100644 --- a/src/test/java/demo/commands/HorseCommand.java +++ b/src/test/java/demo/commands/HorseCommand.java @@ -65,11 +65,11 @@ public class HorseCommand extends Command { boolean baby = args.get("baby"); HorseMeta.Marking marking = args.get("marking"); HorseMeta.Color color = args.get("color"); - var horse = new EntityCreature(EntityType.HORSE, player.getPosition()); + var horse = new EntityCreature(EntityType.HORSE); var meta = (HorseMeta) horse.getEntityMeta(); meta.setBaby(baby); meta.setVariant(new HorseMeta.Variant(marking, color)); - horse.setInstance(player.getInstance()); + horse.setInstance(player.getInstance(), player.getPosition()); } } diff --git a/src/test/java/demo/commands/PotionCommand.java b/src/test/java/demo/commands/PotionCommand.java index 33c05d062..fb66169e4 100644 --- a/src/test/java/demo/commands/PotionCommand.java +++ b/src/test/java/demo/commands/PotionCommand.java @@ -4,7 +4,6 @@ import net.minestom.server.MinecraftServer; import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.Arguments; import net.minestom.server.command.builder.Command; -import net.minestom.server.command.builder.arguments.Argument; import net.minestom.server.command.builder.arguments.ArgumentType; import net.minestom.server.entity.Player; import net.minestom.server.potion.Potion; @@ -21,8 +20,8 @@ public class PotionCommand extends Command { sender.sendMessage("Usage: /potion [type] [duration (seconds)]"); })); - Argument potionArg = ArgumentType.Potion("potion"); - Argument durationArg = ArgumentType.Integer("duration"); + var potionArg = ArgumentType.Potion("potion"); + var durationArg = ArgumentType.Integer("duration"); addSyntax(this::onPotionCommand, potionArg, durationArg); } @@ -37,8 +36,8 @@ public class PotionCommand extends Command { private void onPotionCommand(CommandSender sender, Arguments args) { final Player player = (Player) sender; - final PotionEffect potion = args.getPotionEffect("potion"); - final int duration = args.getInteger("duration"); + final PotionEffect potion = args.get("potion"); + final int duration = args.get("duration"); player.sendMessage(player.getActiveEffects().toString()); player.addEffect(new Potion( diff --git a/src/test/java/demo/commands/ShootCommand.java b/src/test/java/demo/commands/ShootCommand.java index 071295f49..10571404f 100644 --- a/src/test/java/demo/commands/ShootCommand.java +++ b/src/test/java/demo/commands/ShootCommand.java @@ -41,7 +41,7 @@ public class ShootCommand extends Command { private void onShootCommand(CommandSender sender, Arguments args) { Player player = (Player) sender; - String mode = args.getWord("type"); + String mode = args.get("type"); EntityProjectile projectile; switch (mode) { case "default": diff --git a/src/test/java/demo/commands/TeleportCommand.java b/src/test/java/demo/commands/TeleportCommand.java index 562efa677..826f5fadd 100644 --- a/src/test/java/demo/commands/TeleportCommand.java +++ b/src/test/java/demo/commands/TeleportCommand.java @@ -4,7 +4,6 @@ import net.minestom.server.MinecraftServer; import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.Arguments; import net.minestom.server.command.builder.Command; -import net.minestom.server.command.builder.arguments.Argument; import net.minestom.server.command.builder.arguments.ArgumentType; import net.minestom.server.entity.Player; import net.minestom.server.utils.Position; @@ -17,27 +16,27 @@ public class TeleportCommand extends Command { setDefaultExecutor((source, args) -> source.sendMessage("Usage: /tp x y z")); - Argument posArg = ArgumentType.RelativeVec3("pos"); - Argument playerArg = ArgumentType.Word("player"); + var posArg = ArgumentType.RelativeVec3("pos"); + var playerArg = ArgumentType.Word("player"); addSyntax(this::onPlayerTeleport, playerArg); addSyntax(this::onPositionTeleport, posArg); } private void onPlayerTeleport(CommandSender sender, Arguments args) { - final String playerName = args.getWord("player"); + final String playerName = args.get("player"); Player pl = MinecraftServer.getConnectionManager().getPlayer(playerName); if (pl != null && sender.isPlayer()) { Player player = (Player) sender; player.teleport(pl.getPosition()); } - sender.sendMessage("Teleported to player "+playerName); + sender.sendMessage("Teleported to player " + playerName); } private void onPositionTeleport(CommandSender sender, Arguments args) { final Player player = sender.asPlayer(); - final RelativeVec relativeVec = args.getRelativeVector("pos"); + final RelativeVec relativeVec = args.get("pos"); final Position position = relativeVec.from(player).toPosition(); player.teleport(position); diff --git a/src/test/java/demo/commands/TestCommand.java b/src/test/java/demo/commands/TestCommand.java index 3ec28f1e2..dadf8ce62 100644 --- a/src/test/java/demo/commands/TestCommand.java +++ b/src/test/java/demo/commands/TestCommand.java @@ -1,14 +1,12 @@ package demo.commands; +import net.minestom.server.chat.JsonMessage; import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.Arguments; import net.minestom.server.command.builder.Command; -import net.minestom.server.command.builder.CommandResult; - -import java.util.List; +import static net.minestom.server.command.builder.arguments.ArgumentType.Component; import static net.minestom.server.command.builder.arguments.ArgumentType.Integer; -import static net.minestom.server.command.builder.arguments.ArgumentType.*; public class TestCommand extends Command { @@ -16,17 +14,11 @@ public class TestCommand extends Command { super("testcmd"); setDefaultExecutor(this::usage); - addSyntax((sender, args) -> { - final CommandResult result = args.get("command"); - System.out.println("test " + result.getType() + " " + result.getInput()); - }, Literal("cmd"), Command("command")); + var number = Integer("number2"); addSyntax((sender, args) -> { - List groups = args.get("groups"); - System.out.println("size " + groups.size()); - }, Literal("loop"), Loop("groups", - Group("group", Literal("name"), Word("word1")), - Group("group2", Literal("delay"), Integer("number2")))); + sender.sendMessage((JsonMessage) args.get("msg")); + }, Component("msg")); } private void usage(CommandSender sender, Arguments arguments) { diff --git a/src/test/java/demo/commands/TitleCommand.java b/src/test/java/demo/commands/TitleCommand.java index 77d238e1f..a89ba00dc 100644 --- a/src/test/java/demo/commands/TitleCommand.java +++ b/src/test/java/demo/commands/TitleCommand.java @@ -31,7 +31,7 @@ public class TitleCommand extends Command { } Player player = source.asPlayer(); - String titleContent = args.getString("content"); + String titleContent = args.get("content"); player.sendTitleTime(10, 100, 10); try { diff --git a/src/test/java/demo/entity/ChickenCreature.java b/src/test/java/demo/entity/ChickenCreature.java index 89883e023..7d8fe1222 100644 --- a/src/test/java/demo/entity/ChickenCreature.java +++ b/src/test/java/demo/entity/ChickenCreature.java @@ -2,24 +2,18 @@ package demo.entity; import com.google.common.collect.ImmutableList; import net.minestom.server.attribute.Attributes; +import net.minestom.server.entity.EntityCreature; +import net.minestom.server.entity.EntityType; import net.minestom.server.entity.LivingEntity; -import net.minestom.server.entity.ai.EntityAIGroupBuilder; -import net.minestom.server.entity.ai.goal.DoNothingGoal; -import net.minestom.server.entity.ai.goal.MeleeAttackGoal; import net.minestom.server.entity.ai.goal.RandomStrollGoal; -import net.minestom.server.entity.ai.target.ClosestEntityTarget; -import net.minestom.server.entity.ai.target.LastEntityDamagerTarget; import net.minestom.server.entity.damage.DamageType; -import net.minestom.server.entity.type.animal.EntityChicken; import net.minestom.server.event.entity.EntityAttackEvent; -import net.minestom.server.utils.Position; import net.minestom.server.utils.Vector; -import net.minestom.server.utils.time.TimeUnit; -public class ChickenCreature extends EntityChicken { +public class ChickenCreature extends EntityCreature { - public ChickenCreature(Position defaultPosition) { - super(defaultPosition); + public ChickenCreature() { + super(EntityType.CHICKEN); addAIGroup( ImmutableList.of( diff --git a/src/test/java/demo/entity/ZombieCreature.java b/src/test/java/demo/entity/ZombieCreature.java index 0a0bea1f8..bbff4565b 100644 --- a/src/test/java/demo/entity/ZombieCreature.java +++ b/src/test/java/demo/entity/ZombieCreature.java @@ -1,14 +1,14 @@ package demo.entity; +import net.minestom.server.entity.EntityCreature; +import net.minestom.server.entity.EntityType; import net.minestom.server.entity.ai.EntityAIGroupBuilder; import net.minestom.server.entity.ai.goal.RandomLookAroundGoal; -import net.minestom.server.entity.type.monster.EntityZombie; -import net.minestom.server.utils.Position; -public class ZombieCreature extends EntityZombie { +public class ZombieCreature extends EntityCreature { - public ZombieCreature(Position spawnPosition) { - super(spawnPosition); + public ZombieCreature() { + super(EntityType.ZOMBIE); addAIGroup( new EntityAIGroupBuilder() .addGoalSelector(new RandomLookAroundGoal(this, 20)) diff --git a/src/test/java/improveextensions/unloadcallbacks/UnloadCallbacksExtension.java b/src/test/java/improveextensions/unloadcallbacks/UnloadCallbacksExtension.java index a0815d861..5289438a6 100644 --- a/src/test/java/improveextensions/unloadcallbacks/UnloadCallbacksExtension.java +++ b/src/test/java/improveextensions/unloadcallbacks/UnloadCallbacksExtension.java @@ -1,7 +1,8 @@ package improveextensions.unloadcallbacks; import net.minestom.server.MinecraftServer; -import net.minestom.server.entity.type.monster.EntityZombie; +import net.minestom.server.entity.EntityCreature; +import net.minestom.server.entity.EntityType; import net.minestom.server.event.EventCallback; import net.minestom.server.event.GlobalEventHandler; import net.minestom.server.event.entity.EntityTickEvent; @@ -42,14 +43,14 @@ public class UnloadCallbacksExtension extends Extension { // add an event callback on an instance instance.addEventCallback(InstanceTickEvent.class, e -> instanceTicked = true); - instance.loadChunk(0,0); + instance.loadChunk(0, 0); // add an event callback on an entity - EntityZombie zombie = new EntityZombie(new Position(8,64,8) /* middle of chunk */); + EntityCreature zombie = new EntityCreature(EntityType.ZOMBIE); zombie.addEventCallback(EntityTickEvent.class, e -> { zombieTicked = true; }); - zombie.setInstance(instance); + zombie.setInstance(instance, new Position(8, 64, 8) /* middle of chunk */); // this callback will be cancelled MinecraftServer.getSchedulerManager().buildTask(() -> {