mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-09 01:47:54 +01:00
commit
ea3f558d8a
@ -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;
|
||||
}
|
||||
}
|
@ -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)));
|
||||
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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<Instance> 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<Instance> 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;
|
||||
}
|
||||
}
|
||||
|
@ -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<Entity> entities = entityFinder.find(commandSender);
|
||||
System.out.println("found " + entities.size() + " entities");
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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 <number>"));
|
||||
}
|
||||
|
||||
private void onModeError(CommandSender sender, ArgumentSyntaxException exception) {
|
||||
|
@ -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 <baby> <marking> <color>"));
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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 <type> <duration (seconds)>"))));
|
||||
|
||||
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
|
||||
));
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
34
src/test/java/demo/commands/RemoveCommand.java
Normal file
34
src/test/java/demo/commands/RemoveCommand.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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());
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
33
src/test/java/demo/commands/SummonCommand.java
Normal file
33
src/test/java/demo/commands/SummonCommand.java
Normal file
@ -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());
|
||||
}
|
||||
}
|
@ -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) {
|
||||
|
@ -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");
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user