From 477b69b9e5975f13a3604457685f9282943d26a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Fri, 26 Mar 2021 21:13:45 +0100 Subject: [PATCH 1/3] Added common conditions --- .../command/builder/condition/Conditions.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/main/java/net/minestom/server/command/builder/condition/Conditions.java diff --git a/src/main/java/net/minestom/server/command/builder/condition/Conditions.java b/src/main/java/net/minestom/server/command/builder/condition/Conditions.java new file mode 100644 index 000000000..92b281a47 --- /dev/null +++ b/src/main/java/net/minestom/server/command/builder/condition/Conditions.java @@ -0,0 +1,25 @@ +package net.minestom.server.command.builder.condition; + + +import net.kyori.adventure.text.Component; +import net.minestom.server.command.CommandSender; + +/** + * Common command conditions + */ +public class Conditions { + public static boolean playerOnly(CommandSender sender, String commandString) { + if (!sender.isPlayer()) { + sender.sendMessage(Component.text("The command is only available for players")); + return false; + } + return true; + } + public static boolean consoleOnly(CommandSender sender, String commandString) { + if (!sender.isConsole()) { + sender.sendMessage(Component.text("The command is only available form the console")); + return false; + } + return true; + } +} From a442603067f46a5235587c47a7815ed78c15fc6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Fri, 26 Mar 2021 21:19:53 +0100 Subject: [PATCH 2/3] Removed deprecated parts, changed conditions and other minor changes --- src/test/java/demo/commands/BookCommand.java | 11 +-- .../java/demo/commands/DimensionCommand.java | 71 +++++++++---------- .../demo/commands/EntitySelectorCommand.java | 1 - .../java/demo/commands/GamemodeCommand.java | 11 +-- .../java/demo/commands/HealthCommand.java | 13 +--- src/test/java/demo/commands/HorseCommand.java | 14 ++-- .../demo/commands/LoadExtensionCommand.java | 38 ++++------ .../java/demo/commands/PotionCommand.java | 32 ++++----- .../demo/commands/ReloadExtensionCommand.java | 25 +++---- src/test/java/demo/commands/ShootCommand.java | 12 +--- .../java/demo/commands/ShutdownCommand.java | 27 ++----- src/test/java/demo/commands/TestCommand.java | 9 +-- src/test/java/demo/commands/TitleCommand.java | 16 +---- .../demo/commands/UnloadExtensionCommand.java | 26 +++---- 14 files changed, 105 insertions(+), 201 deletions(-) diff --git a/src/test/java/demo/commands/BookCommand.java b/src/test/java/demo/commands/BookCommand.java index 6381d1f4c..2f8cdc395 100644 --- a/src/test/java/demo/commands/BookCommand.java +++ b/src/test/java/demo/commands/BookCommand.java @@ -6,25 +6,18 @@ import net.kyori.adventure.text.format.NamedTextColor; import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.CommandContext; +import net.minestom.server.command.builder.condition.Conditions; import net.minestom.server.entity.Player; public class BookCommand extends Command { public BookCommand() { super("book"); - setCondition(this::playerCondition); + setCondition(Conditions::playerOnly); setDefaultExecutor(this::execute); } - private boolean playerCondition(CommandSender sender, String commandString) { - if (!sender.isPlayer()) { - sender.sendMessage(Component.text("The command is only available for players")); - return false; - } - return true; - } - private void execute(CommandSender sender, CommandContext context) { Player player = sender.asPlayer(); diff --git a/src/test/java/demo/commands/DimensionCommand.java b/src/test/java/demo/commands/DimensionCommand.java index f590a0adf..b69e96e67 100644 --- a/src/test/java/demo/commands/DimensionCommand.java +++ b/src/test/java/demo/commands/DimensionCommand.java @@ -2,8 +2,12 @@ package demo.commands; import net.kyori.adventure.text.Component; import net.minestom.server.MinecraftServer; -import net.minestom.server.command.CommandProcessor; import net.minestom.server.command.CommandSender; +import net.minestom.server.command.builder.Command; +import net.minestom.server.command.builder.CommandContext; +import net.minestom.server.command.builder.arguments.ArgumentType; +import net.minestom.server.command.builder.arguments.ArgumentWord; +import net.minestom.server.command.builder.condition.Conditions; import net.minestom.server.entity.Player; import net.minestom.server.instance.Instance; import net.minestom.server.world.DimensionType; @@ -11,46 +15,39 @@ import org.jetbrains.annotations.NotNull; import java.util.Optional; -public class DimensionCommand implements CommandProcessor { - @NotNull - @Override - public String getCommandName() { - return "dimensiontest"; +public class DimensionCommand extends Command { + + private final ArgumentWord dimension_type; + + public DimensionCommand() { + super("dimensiontest"); + setCondition(Conditions::playerOnly); + dimension_type = ArgumentType.Word("dimension type"); + dimension_type.from(MinecraftServer.getDimensionTypeManager().unmodifiableList().stream().map(DimensionType::getName).map(Object::toString).toArray(String[]::new)); + + addSyntax(this::execute, dimension_type); } - @Override - public String[] getAliases() { - return new String[0]; - } - - @Override - public boolean process(@NotNull CommandSender sender, @NotNull String command, @NotNull String[] args) { - - if (!sender.isPlayer()) - return false; - Player player = (Player) sender; - - Instance instance = player.getInstance(); - - DimensionType targetDimensionType = DimensionType.OVERWORLD; - //if (instance.getDimensionType() == targetDimensionType) { - // targetDimensionType = DimensionType.OVERWORLD; - //} - - Optional targetInstance = MinecraftServer.getInstanceManager().getInstances().stream().filter(in -> in.getDimensionType() == targetDimensionType).findFirst(); + private void execute(@NotNull CommandSender commandSender, @NotNull CommandContext commandContext) { + final Player player = commandSender.asPlayer(); + final Instance instance = player.getInstance(); + final String typeName = commandContext.get(dimension_type); + final Optional targetInstance = MinecraftServer.getInstanceManager().getInstances().stream().filter(in -> in.getDimensionType().toString().equals(typeName)).findFirst(); if (targetInstance.isPresent()) { - player.sendMessage(Component.text("You were in " + instance.getDimensionType())); - player.setInstance(targetInstance.get()); - player.sendMessage(Component.text("You are now in " + targetDimensionType)); + if (instance != null) { + if (targetInstance.get() != instance) { + player.sendMessage(Component.text("You were in " + instance.getDimensionType())); + player.setInstance(targetInstance.get()); + player.sendMessage(Component.text("You are now in " + typeName)); + } else { + player.sendMessage(Component.text("You are already in the instance")); + } + } else { + player.setInstance(targetInstance.get()); + player.sendMessage(Component.text("You did the impossible and you are now in " + typeName)); + } } else { - player.sendMessage(Component.text("Could not find instance with dimension " + targetDimensionType)); + player.sendMessage(Component.text("Could not find instance with dimension " + typeName)); } - - return true; - } - - @Override - public boolean hasAccess(@NotNull Player player) { - return true; } } diff --git a/src/test/java/demo/commands/EntitySelectorCommand.java b/src/test/java/demo/commands/EntitySelectorCommand.java index d56d1d0fe..178d63931 100644 --- a/src/test/java/demo/commands/EntitySelectorCommand.java +++ b/src/test/java/demo/commands/EntitySelectorCommand.java @@ -27,7 +27,6 @@ public class EntitySelectorCommand extends Command { } private void executor(CommandSender commandSender, CommandContext context) { - Instance instance = commandSender.asPlayer().getInstance(); EntityFinder entityFinder = context.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 540ceb5ae..8400c104a 100644 --- a/src/test/java/demo/commands/GamemodeCommand.java +++ b/src/test/java/demo/commands/GamemodeCommand.java @@ -7,6 +7,7 @@ import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.CommandContext; import net.minestom.server.command.builder.arguments.ArgumentEnum; import net.minestom.server.command.builder.arguments.ArgumentType; +import net.minestom.server.command.builder.condition.Conditions; import net.minestom.server.command.builder.exception.ArgumentSyntaxException; import net.minestom.server.entity.GameMode; import net.minestom.server.entity.Player; @@ -19,7 +20,7 @@ public class GamemodeCommand extends Command { public GamemodeCommand() { super("gamemode", "g", "gm"); - setCondition(this::isAllowed); + setCondition(Conditions::playerOnly); setDefaultExecutor(this::usage); @@ -69,12 +70,4 @@ public class GamemodeCommand extends Command { private void gameModeCallback(CommandSender sender, ArgumentSyntaxException exception) { sender.sendMessage(Component.text("'" + exception.getInput() + "' is not a valid gamemode!")); } - - private boolean isAllowed(CommandSender sender, String commandString) { - if (!sender.isPlayer()) { - sender.sendMessage(Component.text("The command is only available for player")); - return false; - } - return true; - } } diff --git a/src/test/java/demo/commands/HealthCommand.java b/src/test/java/demo/commands/HealthCommand.java index 171e5dac6..9cb9b102b 100644 --- a/src/test/java/demo/commands/HealthCommand.java +++ b/src/test/java/demo/commands/HealthCommand.java @@ -6,6 +6,7 @@ import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.CommandContext; import net.minestom.server.command.builder.arguments.ArgumentType; import net.minestom.server.command.builder.arguments.number.ArgumentNumber; +import net.minestom.server.command.builder.condition.Conditions; import net.minestom.server.command.builder.exception.ArgumentSyntaxException; import net.minestom.server.entity.Player; @@ -14,7 +15,7 @@ public class HealthCommand extends Command { public HealthCommand() { super("health"); - setCondition(this::condition); + setCondition(Conditions::playerOnly); setDefaultExecutor(this::defaultExecutor); @@ -29,16 +30,8 @@ public class HealthCommand extends Command { addSyntax(this::onHealthCommand, modeArg, valueArg); } - private boolean condition(CommandSender sender, String commandString) { - if (!sender.isPlayer()) { - sender.sendMessage(Component.text("The command is only available for player")); - return false; - } - return true; - } - private void defaultExecutor(CommandSender sender, CommandContext context) { - sender.sendMessage(Component.text("Correct usage: health [set/add] [number]")); + sender.sendMessage(Component.text("Correct usage: health set|add ")); } private void onModeError(CommandSender sender, ArgumentSyntaxException exception) { diff --git a/src/test/java/demo/commands/HorseCommand.java b/src/test/java/demo/commands/HorseCommand.java index 72485d8a1..3cb92c09d 100644 --- a/src/test/java/demo/commands/HorseCommand.java +++ b/src/test/java/demo/commands/HorseCommand.java @@ -5,6 +5,7 @@ import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.CommandContext; import net.minestom.server.command.builder.arguments.ArgumentType; +import net.minestom.server.command.builder.condition.Conditions; import net.minestom.server.command.builder.exception.ArgumentSyntaxException; import net.minestom.server.entity.EntityCreature; import net.minestom.server.entity.EntityType; @@ -19,7 +20,7 @@ public class HorseCommand extends Command { public HorseCommand() { super("horse"); - setCondition(this::condition); + setCondition(Conditions::playerOnly); setDefaultExecutor(this::defaultExecutor); var babyArg = ArgumentType.Boolean("baby"); var markingArg = ArgumentType.Enum("marking", HorseMeta.Marking.class); @@ -30,16 +31,8 @@ public class HorseCommand extends Command { addSyntax(this::onHorseCommand, babyArg, markingArg, colorArg); } - private boolean condition(CommandSender sender, String commandString) { - if (!sender.isPlayer()) { - sender.sendMessage(Component.text("The command is only available for player")); - return false; - } - return true; - } - private void defaultExecutor(CommandSender sender, CommandContext context) { - sender.sendMessage("Correct usage: horse [baby] [marking] [color]"); + sender.sendMessage(Component.text("Correct usage: /horse ")); } private void onBabyError(CommandSender sender, ArgumentSyntaxException exception) { @@ -70,6 +63,7 @@ public class HorseCommand extends Command { var meta = (HorseMeta) horse.getEntityMeta(); meta.setBaby(baby); meta.setVariant(new HorseMeta.Variant(marking, color)); + //noinspection ConstantConditions - It should be impossible to execute a command without being in an instance horse.setInstance(player.getInstance(), player.getPosition()); } diff --git a/src/test/java/demo/commands/LoadExtensionCommand.java b/src/test/java/demo/commands/LoadExtensionCommand.java index e5440920c..82dcc8df8 100644 --- a/src/test/java/demo/commands/LoadExtensionCommand.java +++ b/src/test/java/demo/commands/LoadExtensionCommand.java @@ -5,7 +5,7 @@ import net.minestom.server.MinecraftServer; import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.CommandContext; -import net.minestom.server.command.builder.arguments.Argument; +import net.minestom.server.command.builder.arguments.ArgumentString; import net.minestom.server.command.builder.arguments.ArgumentType; import net.minestom.server.command.builder.exception.ArgumentSyntaxException; import net.minestom.server.extensions.ExtensionManager; @@ -14,16 +14,18 @@ import java.io.IOException; import java.nio.file.Path; public class LoadExtensionCommand extends Command { + + private final ArgumentString extensionName; + public LoadExtensionCommand() { super("load"); setDefaultExecutor(this::usage); - var extension = ArgumentType.DynamicStringArray("extensionName"); + extensionName = ArgumentType.String("extensionName"); - setArgumentCallback(this::extensionCallback, extension); - - addSyntax(this::execute, extension); + setArgumentCallback(this::extensionCallback, extensionName); + addSyntax(this::execute, extensionName); } private void usage(CommandSender sender, CommandContext context) { @@ -31,49 +33,37 @@ public class LoadExtensionCommand extends Command { } private void execute(CommandSender sender, CommandContext context) { - String name = join(context.getStringArray("extensionName")); - sender.sendMessage(Component.text("extensionFile = "+name+"....")); + final String name = context.get(extensionName); + sender.sendMessage(Component.text("extensionFile = " + name + "....")); ExtensionManager extensionManager = MinecraftServer.getExtensionManager(); Path extensionFolder = extensionManager.getExtensionFolder().toPath().toAbsolutePath(); Path extensionJar = extensionFolder.resolve(name); try { - if(!extensionJar.toFile().getCanonicalPath().startsWith(extensionFolder.toFile().getCanonicalPath())) { - sender.sendMessage(Component.text("File name '"+name+"' does not represent a file inside the extensions folder. Will not load")); + if (!extensionJar.toFile().getCanonicalPath().startsWith(extensionFolder.toFile().getCanonicalPath())) { + sender.sendMessage(Component.text("File name '" + name + "' does not represent a file inside the extensions folder. Will not load")); return; } } catch (IOException e) { e.printStackTrace(); - sender.sendMessage(Component.text("Failed to load extension: "+e.getMessage())); + sender.sendMessage(Component.text("Failed to load extension: " + e.getMessage())); return; } try { boolean managed = extensionManager.loadDynamicExtension(extensionJar.toFile()); - if(managed) { + if (managed) { sender.sendMessage(Component.text("Extension loaded!")); } else { sender.sendMessage(Component.text("Failed to load extension, check your logs.")); } } catch (Exception e) { e.printStackTrace(); - sender.sendMessage(Component.text("Failed to load extension: "+e.getMessage())); + sender.sendMessage(Component.text("Failed to load extension: " + e.getMessage())); } } private void extensionCallback(CommandSender sender, ArgumentSyntaxException exception) { sender.sendMessage(Component.text("'" + exception.getInput() + "' is not a valid extension name!")); } - - private String join(String[] extensionNameParts) { - StringBuilder b = new StringBuilder(); - for (int i = 0; i < extensionNameParts.length; i++) { - String s = extensionNameParts[i]; - if (i != 0) { - b.append(" "); - } - b.append(s); - } - return b.toString(); - } } diff --git a/src/test/java/demo/commands/PotionCommand.java b/src/test/java/demo/commands/PotionCommand.java index e89ba2151..e16db5c56 100644 --- a/src/test/java/demo/commands/PotionCommand.java +++ b/src/test/java/demo/commands/PotionCommand.java @@ -6,43 +6,39 @@ import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.CommandContext; import net.minestom.server.command.builder.arguments.ArgumentType; +import net.minestom.server.command.builder.arguments.minecraft.registry.ArgumentPotionEffect; +import net.minestom.server.command.builder.arguments.number.ArgumentInteger; +import net.minestom.server.command.builder.condition.Conditions; import net.minestom.server.entity.Player; import net.minestom.server.potion.Potion; import net.minestom.server.potion.PotionEffect; public class PotionCommand extends Command { + private final ArgumentPotionEffect potion; + private final ArgumentInteger duration; + public PotionCommand() { super("potion"); - setCondition(this::condition); + setCondition(Conditions::playerOnly); - setDefaultExecutor(((sender, args) -> { - sender.sendMessage(Component.text("Usage: /potion [type] [duration (seconds)]")); - })); + setDefaultExecutor(((sender, args) -> sender.sendMessage(Component.text("Usage: /potion ")))); - var potionArg = ArgumentType.Potion("potion"); - var durationArg = ArgumentType.Integer("duration"); + potion = ArgumentType.Potion("potion"); + duration = ArgumentType.Integer("duration"); - addSyntax(this::onPotionCommand, potionArg, durationArg); - } - - private boolean condition(CommandSender sender, String commandString) { - if (!sender.isPlayer()) { - sender.sendMessage(Component.text("The command is only available for players")); - return false; - } - return true; + addSyntax(this::onPotionCommand, potion, duration); } private void onPotionCommand(CommandSender sender, CommandContext context) { final Player player = (Player) sender; - final PotionEffect potion = context.get("potion"); - final int duration = context.get("duration"); + final PotionEffect potionEffect = context.get(potion); + final Integer duration = context.get(this.duration); player.sendMessage(Component.text(player.getActiveEffects().toString())); player.addEffect(new Potion( - potion, + potionEffect, (byte) 0, duration * MinecraftServer.TICK_PER_SECOND )); diff --git a/src/test/java/demo/commands/ReloadExtensionCommand.java b/src/test/java/demo/commands/ReloadExtensionCommand.java index ba5bdd388..898251957 100644 --- a/src/test/java/demo/commands/ReloadExtensionCommand.java +++ b/src/test/java/demo/commands/ReloadExtensionCommand.java @@ -6,6 +6,7 @@ import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.CommandContext; import net.minestom.server.command.builder.arguments.Argument; +import net.minestom.server.command.builder.arguments.ArgumentString; import net.minestom.server.command.builder.arguments.ArgumentType; import net.minestom.server.command.builder.exception.ArgumentSyntaxException; import net.minestom.server.extensions.Extension; @@ -30,16 +31,18 @@ public class ReloadExtensionCommand extends Command { .toArray(String[]::new); } + private final ArgumentString extensionName; + public ReloadExtensionCommand() { super("reload"); setDefaultExecutor(this::usage); - Argument extension = ArgumentType.DynamicStringArray("extensionName"); + extensionName = ArgumentType.String("extensionName"); - setArgumentCallback(this::gameModeCallback, extension); + setArgumentCallback(this::gameModeCallback, extensionName); - addSyntax(this::execute, extension); + addSyntax(this::execute, extensionName); } private void usage(CommandSender sender, CommandContext context) { @@ -47,7 +50,7 @@ public class ReloadExtensionCommand extends Command { } private void execute(CommandSender sender, CommandContext context) { - String name = join(context.getStringArray("extensionName")); + final String name = context.get(extensionName); sender.sendMessage(Component.text("extensionName = " + name + "....")); ExtensionManager extensionManager = MinecraftServer.getExtensionManager(); @@ -63,7 +66,7 @@ public class ReloadExtensionCommand extends Command { baos.flush(); baos.close(); String contents = new String(baos.toByteArray(), StandardCharsets.UTF_8); - contents.lines().forEach(sender::sendMessage); + contents.lines().map(Component::text).forEach(sender::sendMessage); } catch (IOException e) { e.printStackTrace(); } @@ -82,16 +85,4 @@ public class ReloadExtensionCommand extends Command { public String[] onDynamicWrite(@NotNull CommandSender sender, @NotNull String text) { return extensionsName; } - - private String join(String[] extensionNameParts) { - StringBuilder b = new StringBuilder(); - for (int i = 0; i < extensionNameParts.length; i++) { - String s = extensionNameParts[i]; - if (i != 0) { - b.append(" "); - } - b.append(s); - } - return b.toString(); - } } diff --git a/src/test/java/demo/commands/ShootCommand.java b/src/test/java/demo/commands/ShootCommand.java index 8477de211..3968a5e16 100644 --- a/src/test/java/demo/commands/ShootCommand.java +++ b/src/test/java/demo/commands/ShootCommand.java @@ -5,6 +5,7 @@ import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.CommandContext; import net.minestom.server.command.builder.arguments.ArgumentType; +import net.minestom.server.command.builder.condition.Conditions; import net.minestom.server.command.builder.exception.ArgumentSyntaxException; import net.minestom.server.entity.EntityType; import net.minestom.server.entity.Player; @@ -17,21 +18,13 @@ public class ShootCommand extends Command { public ShootCommand() { super("shoot"); - setCondition(this::condition); + setCondition(Conditions::playerOnly); setDefaultExecutor(this::defaultExecutor); var typeArg = ArgumentType.Word("type").from("default", "spectral", "colored"); setArgumentCallback(this::onTypeError, typeArg); addSyntax(this::onShootCommand, typeArg); } - private boolean condition(CommandSender sender, String commandString) { - if (!sender.isPlayer()) { - sender.sendMessage(Component.text("The command is only available for player")); - return false; - } - return true; - } - private void defaultExecutor(CommandSender sender, CommandContext context) { sender.sendMessage(Component.text("Correct usage: shoot [default/spectral/colored]")); } @@ -60,6 +53,7 @@ public class ShootCommand extends Command { return; } var pos = player.getPosition().clone().add(0D, player.getEyeHeight(), 0D); + //noinspection ConstantConditions - It should be impossible to execute a command without being in an instance projectile.setInstance(player.getInstance(), pos); var dir = pos.getDirection().multiply(30D); pos = pos.clone().add(dir.getX(), dir.getY(), dir.getZ()); diff --git a/src/test/java/demo/commands/ShutdownCommand.java b/src/test/java/demo/commands/ShutdownCommand.java index 25000507e..12939dd88 100644 --- a/src/test/java/demo/commands/ShutdownCommand.java +++ b/src/test/java/demo/commands/ShutdownCommand.java @@ -1,35 +1,22 @@ package demo.commands; import net.minestom.server.MinecraftServer; -import net.minestom.server.command.CommandProcessor; import net.minestom.server.command.CommandSender; -import net.minestom.server.entity.Player; +import net.minestom.server.command.builder.Command; +import net.minestom.server.command.builder.CommandContext; import org.jetbrains.annotations.NotNull; /** * A simple shutdown command. */ -public class ShutdownCommand implements CommandProcessor { +public class ShutdownCommand extends Command { - @NotNull - @Override - public String getCommandName() { - return "shutdown"; + public ShutdownCommand() { + super("shutdown"); + addSyntax(this::execute); } - @Override - public String[] getAliases() { - return new String[0]; - } - - @Override - public boolean process(@NotNull CommandSender sender, @NotNull String command, @NotNull String[] args) { + private void execute(@NotNull CommandSender commandSender, @NotNull CommandContext commandContext) { MinecraftServer.stopCleanly(); - return true; - } - - @Override - public boolean hasAccess(@NotNull Player player) { - return true; } } diff --git a/src/test/java/demo/commands/TestCommand.java b/src/test/java/demo/commands/TestCommand.java index c339f12d2..767394682 100644 --- a/src/test/java/demo/commands/TestCommand.java +++ b/src/test/java/demo/commands/TestCommand.java @@ -1,14 +1,11 @@ package demo.commands; -import net.minestom.server.chat.JsonMessage; import net.kyori.adventure.text.Component; import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.CommandContext; -import net.minestom.server.command.builder.suggestion.SuggestionEntry; -import static net.minestom.server.command.builder.arguments.ArgumentType.Integer; -import static net.minestom.server.command.builder.arguments.ArgumentType.*; +import static net.minestom.server.command.builder.arguments.ArgumentType.ResourceLocation; public class TestCommand extends Command { @@ -18,9 +15,7 @@ public class TestCommand extends Command { var test = ResourceLocation("msg"); - addSyntax((sender, context) -> { - System.out.println("executed"); - },test); + addSyntax((sender, context) -> System.out.println("executed"),test); } private void usage(CommandSender sender, CommandContext context) { diff --git a/src/test/java/demo/commands/TitleCommand.java b/src/test/java/demo/commands/TitleCommand.java index 971009c76..cda7bc3dc 100644 --- a/src/test/java/demo/commands/TitleCommand.java +++ b/src/test/java/demo/commands/TitleCommand.java @@ -1,24 +1,19 @@ package demo.commands; -import com.google.gson.JsonElement; -import com.google.gson.JsonParseException; -import com.google.gson.JsonParser; import net.kyori.adventure.text.Component; import net.kyori.adventure.title.Title; -import net.minestom.server.chat.ColoredText; -import net.minestom.server.chat.JsonMessage; import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.CommandContext; import net.minestom.server.command.builder.arguments.ArgumentType; +import net.minestom.server.command.builder.condition.Conditions; import net.minestom.server.entity.Player; public class TitleCommand extends Command { public TitleCommand() { super("title"); - setDefaultExecutor((source, args) -> { - source.sendMessage(Component.text("Unknown syntax (note: title must be quoted)")); - }); + setDefaultExecutor((source, args) -> source.sendMessage(Component.text("Unknown syntax (note: title must be quoted)"))); + setCondition(Conditions::playerOnly); var content = ArgumentType.String("content"); @@ -26,11 +21,6 @@ public class TitleCommand extends Command { } private void handleTitle(CommandSender source, CommandContext context) { - if (!source.isPlayer()) { - source.sendMessage(Component.text("Only players can run this command!")); - return; - } - Player player = source.asPlayer(); String titleContent = context.get("content"); diff --git a/src/test/java/demo/commands/UnloadExtensionCommand.java b/src/test/java/demo/commands/UnloadExtensionCommand.java index e5223716c..115242638 100644 --- a/src/test/java/demo/commands/UnloadExtensionCommand.java +++ b/src/test/java/demo/commands/UnloadExtensionCommand.java @@ -6,6 +6,7 @@ import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.CommandContext; import net.minestom.server.command.builder.arguments.Argument; +import net.minestom.server.command.builder.arguments.ArgumentString; import net.minestom.server.command.builder.arguments.ArgumentType; import net.minestom.server.command.builder.exception.ArgumentSyntaxException; import net.minestom.server.extensions.Extension; @@ -18,16 +19,19 @@ import java.io.PrintStream; import java.nio.charset.StandardCharsets; public class UnloadExtensionCommand extends Command { + + private final ArgumentString extensionName; + public UnloadExtensionCommand() { super("unload"); setDefaultExecutor(this::usage); - Argument extension = ArgumentType.DynamicStringArray("extensionName"); + extensionName = ArgumentType.String("extensionName"); - setArgumentCallback(this::extensionCallback, extension); + setArgumentCallback(this::extensionCallback, extensionName); - addSyntax(this::execute, extension); + addSyntax(this::execute, extensionName); } private void usage(CommandSender sender, CommandContext context) { @@ -35,7 +39,7 @@ public class UnloadExtensionCommand extends Command { } private void execute(CommandSender sender, CommandContext context) { - String name = join(context.getStringArray("extensionName")); + final String name = context.get(extensionName); sender.sendMessage(Component.text("extensionName = " + name + "....")); ExtensionManager extensionManager = MinecraftServer.getExtensionManager(); @@ -51,7 +55,7 @@ public class UnloadExtensionCommand extends Command { baos.flush(); baos.close(); String contents = baos.toString(StandardCharsets.UTF_8); - contents.lines().forEach(sender::sendMessage); + contents.lines().map(Component::text).forEach(sender::sendMessage); } catch (IOException e) { e.printStackTrace(); } @@ -64,16 +68,4 @@ public class UnloadExtensionCommand extends Command { private void extensionCallback(CommandSender sender, ArgumentSyntaxException exception) { sender.sendMessage(Component.text("'" + exception.getInput() + "' is not a valid extension name!")); } - - private String join(String[] extensionNameParts) { - StringBuilder b = new StringBuilder(); - for (int i = 0; i < extensionNameParts.length; i++) { - String s = extensionNameParts[i]; - if (i != 0) { - b.append(StringUtils.SPACE); - } - b.append(s); - } - return b.toString(); - } } From 256ed45fab14ed075b0a4e791ff873299ca7dd72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9meth=20Noel?= Date: Fri, 26 Mar 2021 21:32:58 +0100 Subject: [PATCH 3/3] Added two new commands /summon and /remove entities --- src/test/java/demo/Main.java | 2 ++ .../java/demo/commands/RemoveCommand.java | 34 +++++++++++++++++++ .../java/demo/commands/SummonCommand.java | 33 ++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 src/test/java/demo/commands/RemoveCommand.java create mode 100644 src/test/java/demo/commands/SummonCommand.java diff --git a/src/test/java/demo/Main.java b/src/test/java/demo/Main.java index b584f4667..32a78c1e6 100644 --- a/src/test/java/demo/Main.java +++ b/src/test/java/demo/Main.java @@ -49,6 +49,8 @@ public class Main { commandManager.register(new ShootCommand()); commandManager.register(new HorseCommand()); commandManager.register(new EchoCommand()); + commandManager.register(new SummonCommand()); + commandManager.register(new RemoveCommand()); commandManager.setUnknownCommandCallback((sender, command) -> sender.sendMessage(Component.text("Unknown command", NamedTextColor.RED))); diff --git a/src/test/java/demo/commands/RemoveCommand.java b/src/test/java/demo/commands/RemoveCommand.java new file mode 100644 index 000000000..632579fc3 --- /dev/null +++ b/src/test/java/demo/commands/RemoveCommand.java @@ -0,0 +1,34 @@ +package demo.commands; + +import net.minestom.server.command.CommandSender; +import net.minestom.server.command.builder.Command; +import net.minestom.server.command.builder.CommandContext; +import net.minestom.server.command.builder.arguments.ArgumentType; +import net.minestom.server.command.builder.arguments.minecraft.ArgumentEntity; +import net.minestom.server.command.builder.condition.Conditions; +import net.minestom.server.entity.Entity; +import net.minestom.server.utils.entity.EntityFinder; + +public class RemoveCommand extends Command { + + public RemoveCommand() { + super("remove"); + addSubcommand(new RemoveEntities()); + } + + static class RemoveEntities extends Command { + private final ArgumentEntity entity; + + public RemoveEntities() { + super("entities"); + setCondition(Conditions::playerOnly); + entity = ArgumentType.Entity("entity"); + addSyntax(this::remove, entity); + } + + private void remove(CommandSender commandSender, CommandContext commandContext) { + final EntityFinder entityFinder = commandContext.get(entity); + entityFinder.find(commandSender).forEach(Entity::remove); + } + } +} \ No newline at end of file diff --git a/src/test/java/demo/commands/SummonCommand.java b/src/test/java/demo/commands/SummonCommand.java new file mode 100644 index 000000000..3ee4dd736 --- /dev/null +++ b/src/test/java/demo/commands/SummonCommand.java @@ -0,0 +1,33 @@ +package demo.commands; + +import net.minestom.server.command.CommandSender; +import net.minestom.server.command.builder.Command; +import net.minestom.server.command.builder.CommandContext; +import net.minestom.server.command.builder.arguments.ArgumentType; +import net.minestom.server.command.builder.arguments.minecraft.ArgumentEntity; +import net.minestom.server.command.builder.arguments.minecraft.registry.ArgumentEntityType; +import net.minestom.server.command.builder.arguments.relative.ArgumentRelativeVec3; +import net.minestom.server.command.builder.condition.Conditions; +import net.minestom.server.entity.Entity; +import org.jetbrains.annotations.NotNull; + +public class SummonCommand extends Command { + + private final ArgumentEntityType entity; + private final ArgumentRelativeVec3 pos; + + public SummonCommand() { + super("summon"); + setCondition(Conditions::playerOnly); + + entity = ArgumentType.EntityType("entity type"); + pos = ArgumentType.RelativeVec3("pos"); + addSyntax(this::execute, entity, pos); + } + + private void execute(@NotNull CommandSender commandSender, @NotNull CommandContext commandContext) { + final Entity entity = new Entity(commandContext.get(this.entity)); + //noinspection ConstantConditions - One couldn't possibly execute a command without being in an instance + entity.setInstance(commandSender.asPlayer().getInstance(), commandContext.get(pos).from(commandSender.asPlayer()).toPosition()); + } +}