mirror of
https://github.com/Minestom/Minestom.git
synced 2025-03-02 11:21:15 +01:00
Removed deprecated parts, changed conditions and other minor changes
This commit is contained in:
parent
477b69b9e5
commit
a442603067
@ -6,25 +6,18 @@ import net.kyori.adventure.text.format.NamedTextColor;
|
|||||||
import net.minestom.server.command.CommandSender;
|
import net.minestom.server.command.CommandSender;
|
||||||
import net.minestom.server.command.builder.Command;
|
import net.minestom.server.command.builder.Command;
|
||||||
import net.minestom.server.command.builder.CommandContext;
|
import net.minestom.server.command.builder.CommandContext;
|
||||||
|
import net.minestom.server.command.builder.condition.Conditions;
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
|
|
||||||
public class BookCommand extends Command {
|
public class BookCommand extends Command {
|
||||||
public BookCommand() {
|
public BookCommand() {
|
||||||
super("book");
|
super("book");
|
||||||
|
|
||||||
setCondition(this::playerCondition);
|
setCondition(Conditions::playerOnly);
|
||||||
|
|
||||||
setDefaultExecutor(this::execute);
|
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) {
|
private void execute(CommandSender sender, CommandContext context) {
|
||||||
Player player = sender.asPlayer();
|
Player player = sender.asPlayer();
|
||||||
|
|
||||||
|
@ -2,8 +2,12 @@ package demo.commands;
|
|||||||
|
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.minestom.server.MinecraftServer;
|
import net.minestom.server.MinecraftServer;
|
||||||
import net.minestom.server.command.CommandProcessor;
|
|
||||||
import net.minestom.server.command.CommandSender;
|
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.entity.Player;
|
||||||
import net.minestom.server.instance.Instance;
|
import net.minestom.server.instance.Instance;
|
||||||
import net.minestom.server.world.DimensionType;
|
import net.minestom.server.world.DimensionType;
|
||||||
@ -11,46 +15,39 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public class DimensionCommand implements CommandProcessor {
|
public class DimensionCommand extends Command {
|
||||||
@NotNull
|
|
||||||
@Override
|
private final ArgumentWord dimension_type;
|
||||||
public String getCommandName() {
|
|
||||||
return "dimensiontest";
|
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
|
private void execute(@NotNull CommandSender commandSender, @NotNull CommandContext commandContext) {
|
||||||
public String[] getAliases() {
|
final Player player = commandSender.asPlayer();
|
||||||
return new String[0];
|
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();
|
||||||
@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();
|
|
||||||
if (targetInstance.isPresent()) {
|
if (targetInstance.isPresent()) {
|
||||||
player.sendMessage(Component.text("You were in " + instance.getDimensionType()));
|
if (instance != null) {
|
||||||
player.setInstance(targetInstance.get());
|
if (targetInstance.get() != instance) {
|
||||||
player.sendMessage(Component.text("You are now in " + targetDimensionType));
|
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 {
|
} 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) {
|
private void executor(CommandSender commandSender, CommandContext context) {
|
||||||
Instance instance = commandSender.asPlayer().getInstance();
|
|
||||||
EntityFinder entityFinder = context.get("entities");
|
EntityFinder entityFinder = context.get("entities");
|
||||||
List<Entity> entities = entityFinder.find(commandSender);
|
List<Entity> entities = entityFinder.find(commandSender);
|
||||||
System.out.println("found " + entities.size() + " entities");
|
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.CommandContext;
|
||||||
import net.minestom.server.command.builder.arguments.ArgumentEnum;
|
import net.minestom.server.command.builder.arguments.ArgumentEnum;
|
||||||
import net.minestom.server.command.builder.arguments.ArgumentType;
|
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.command.builder.exception.ArgumentSyntaxException;
|
||||||
import net.minestom.server.entity.GameMode;
|
import net.minestom.server.entity.GameMode;
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
@ -19,7 +20,7 @@ public class GamemodeCommand extends Command {
|
|||||||
public GamemodeCommand() {
|
public GamemodeCommand() {
|
||||||
super("gamemode", "g", "gm");
|
super("gamemode", "g", "gm");
|
||||||
|
|
||||||
setCondition(this::isAllowed);
|
setCondition(Conditions::playerOnly);
|
||||||
|
|
||||||
setDefaultExecutor(this::usage);
|
setDefaultExecutor(this::usage);
|
||||||
|
|
||||||
@ -69,12 +70,4 @@ public class GamemodeCommand extends Command {
|
|||||||
private void gameModeCallback(CommandSender sender, ArgumentSyntaxException exception) {
|
private void gameModeCallback(CommandSender sender, ArgumentSyntaxException exception) {
|
||||||
sender.sendMessage(Component.text("'" + exception.getInput() + "' is not a valid gamemode!"));
|
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.CommandContext;
|
||||||
import net.minestom.server.command.builder.arguments.ArgumentType;
|
import net.minestom.server.command.builder.arguments.ArgumentType;
|
||||||
import net.minestom.server.command.builder.arguments.number.ArgumentNumber;
|
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.command.builder.exception.ArgumentSyntaxException;
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ public class HealthCommand extends Command {
|
|||||||
public HealthCommand() {
|
public HealthCommand() {
|
||||||
super("health");
|
super("health");
|
||||||
|
|
||||||
setCondition(this::condition);
|
setCondition(Conditions::playerOnly);
|
||||||
|
|
||||||
setDefaultExecutor(this::defaultExecutor);
|
setDefaultExecutor(this::defaultExecutor);
|
||||||
|
|
||||||
@ -29,16 +30,8 @@ public class HealthCommand extends Command {
|
|||||||
addSyntax(this::onHealthCommand, modeArg, valueArg);
|
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) {
|
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) {
|
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.Command;
|
||||||
import net.minestom.server.command.builder.CommandContext;
|
import net.minestom.server.command.builder.CommandContext;
|
||||||
import net.minestom.server.command.builder.arguments.ArgumentType;
|
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.command.builder.exception.ArgumentSyntaxException;
|
||||||
import net.minestom.server.entity.EntityCreature;
|
import net.minestom.server.entity.EntityCreature;
|
||||||
import net.minestom.server.entity.EntityType;
|
import net.minestom.server.entity.EntityType;
|
||||||
@ -19,7 +20,7 @@ public class HorseCommand extends Command {
|
|||||||
|
|
||||||
public HorseCommand() {
|
public HorseCommand() {
|
||||||
super("horse");
|
super("horse");
|
||||||
setCondition(this::condition);
|
setCondition(Conditions::playerOnly);
|
||||||
setDefaultExecutor(this::defaultExecutor);
|
setDefaultExecutor(this::defaultExecutor);
|
||||||
var babyArg = ArgumentType.Boolean("baby");
|
var babyArg = ArgumentType.Boolean("baby");
|
||||||
var markingArg = ArgumentType.Enum("marking", HorseMeta.Marking.class);
|
var markingArg = ArgumentType.Enum("marking", HorseMeta.Marking.class);
|
||||||
@ -30,16 +31,8 @@ public class HorseCommand extends Command {
|
|||||||
addSyntax(this::onHorseCommand, babyArg, markingArg, colorArg);
|
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) {
|
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) {
|
private void onBabyError(CommandSender sender, ArgumentSyntaxException exception) {
|
||||||
@ -70,6 +63,7 @@ public class HorseCommand extends Command {
|
|||||||
var meta = (HorseMeta) horse.getEntityMeta();
|
var meta = (HorseMeta) horse.getEntityMeta();
|
||||||
meta.setBaby(baby);
|
meta.setBaby(baby);
|
||||||
meta.setVariant(new HorseMeta.Variant(marking, color));
|
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());
|
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.CommandSender;
|
||||||
import net.minestom.server.command.builder.Command;
|
import net.minestom.server.command.builder.Command;
|
||||||
import net.minestom.server.command.builder.CommandContext;
|
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.arguments.ArgumentType;
|
||||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||||
import net.minestom.server.extensions.ExtensionManager;
|
import net.minestom.server.extensions.ExtensionManager;
|
||||||
@ -14,16 +14,18 @@ import java.io.IOException;
|
|||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
|
||||||
public class LoadExtensionCommand extends Command {
|
public class LoadExtensionCommand extends Command {
|
||||||
|
|
||||||
|
private final ArgumentString extensionName;
|
||||||
|
|
||||||
public LoadExtensionCommand() {
|
public LoadExtensionCommand() {
|
||||||
super("load");
|
super("load");
|
||||||
|
|
||||||
setDefaultExecutor(this::usage);
|
setDefaultExecutor(this::usage);
|
||||||
|
|
||||||
var extension = ArgumentType.DynamicStringArray("extensionName");
|
extensionName = ArgumentType.String("extensionName");
|
||||||
|
|
||||||
setArgumentCallback(this::extensionCallback, extension);
|
setArgumentCallback(this::extensionCallback, extensionName);
|
||||||
|
addSyntax(this::execute, extensionName);
|
||||||
addSyntax(this::execute, extension);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void usage(CommandSender sender, CommandContext context) {
|
private void usage(CommandSender sender, CommandContext context) {
|
||||||
@ -31,49 +33,37 @@ public class LoadExtensionCommand extends Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void execute(CommandSender sender, CommandContext context) {
|
private void execute(CommandSender sender, CommandContext context) {
|
||||||
String name = join(context.getStringArray("extensionName"));
|
final String name = context.get(extensionName);
|
||||||
sender.sendMessage(Component.text("extensionFile = "+name+"...."));
|
sender.sendMessage(Component.text("extensionFile = " + name + "...."));
|
||||||
|
|
||||||
ExtensionManager extensionManager = MinecraftServer.getExtensionManager();
|
ExtensionManager extensionManager = MinecraftServer.getExtensionManager();
|
||||||
Path extensionFolder = extensionManager.getExtensionFolder().toPath().toAbsolutePath();
|
Path extensionFolder = extensionManager.getExtensionFolder().toPath().toAbsolutePath();
|
||||||
Path extensionJar = extensionFolder.resolve(name);
|
Path extensionJar = extensionFolder.resolve(name);
|
||||||
try {
|
try {
|
||||||
if(!extensionJar.toFile().getCanonicalPath().startsWith(extensionFolder.toFile().getCanonicalPath())) {
|
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"));
|
sender.sendMessage(Component.text("File name '" + name + "' does not represent a file inside the extensions folder. Will not load"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
sender.sendMessage(Component.text("Failed to load extension: "+e.getMessage()));
|
sender.sendMessage(Component.text("Failed to load extension: " + e.getMessage()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
boolean managed = extensionManager.loadDynamicExtension(extensionJar.toFile());
|
boolean managed = extensionManager.loadDynamicExtension(extensionJar.toFile());
|
||||||
if(managed) {
|
if (managed) {
|
||||||
sender.sendMessage(Component.text("Extension loaded!"));
|
sender.sendMessage(Component.text("Extension loaded!"));
|
||||||
} else {
|
} else {
|
||||||
sender.sendMessage(Component.text("Failed to load extension, check your logs."));
|
sender.sendMessage(Component.text("Failed to load extension, check your logs."));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
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) {
|
private void extensionCallback(CommandSender sender, ArgumentSyntaxException exception) {
|
||||||
sender.sendMessage(Component.text("'" + exception.getInput() + "' is not a valid extension name!"));
|
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.Command;
|
||||||
import net.minestom.server.command.builder.CommandContext;
|
import net.minestom.server.command.builder.CommandContext;
|
||||||
import net.minestom.server.command.builder.arguments.ArgumentType;
|
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.entity.Player;
|
||||||
import net.minestom.server.potion.Potion;
|
import net.minestom.server.potion.Potion;
|
||||||
import net.minestom.server.potion.PotionEffect;
|
import net.minestom.server.potion.PotionEffect;
|
||||||
|
|
||||||
public class PotionCommand extends Command {
|
public class PotionCommand extends Command {
|
||||||
|
|
||||||
|
private final ArgumentPotionEffect potion;
|
||||||
|
private final ArgumentInteger duration;
|
||||||
|
|
||||||
public PotionCommand() {
|
public PotionCommand() {
|
||||||
super("potion");
|
super("potion");
|
||||||
|
|
||||||
setCondition(this::condition);
|
setCondition(Conditions::playerOnly);
|
||||||
|
|
||||||
setDefaultExecutor(((sender, args) -> {
|
setDefaultExecutor(((sender, args) -> sender.sendMessage(Component.text("Usage: /potion <type> <duration (seconds)>"))));
|
||||||
sender.sendMessage(Component.text("Usage: /potion [type] [duration (seconds)]"));
|
|
||||||
}));
|
|
||||||
|
|
||||||
var potionArg = ArgumentType.Potion("potion");
|
potion = ArgumentType.Potion("potion");
|
||||||
var durationArg = ArgumentType.Integer("duration");
|
duration = ArgumentType.Integer("duration");
|
||||||
|
|
||||||
addSyntax(this::onPotionCommand, potionArg, durationArg);
|
addSyntax(this::onPotionCommand, potion, duration);
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onPotionCommand(CommandSender sender, CommandContext context) {
|
private void onPotionCommand(CommandSender sender, CommandContext context) {
|
||||||
final Player player = (Player) sender;
|
final Player player = (Player) sender;
|
||||||
final PotionEffect potion = context.get("potion");
|
final PotionEffect potionEffect = context.get(potion);
|
||||||
final int duration = context.get("duration");
|
final Integer duration = context.get(this.duration);
|
||||||
|
|
||||||
player.sendMessage(Component.text(player.getActiveEffects().toString()));
|
player.sendMessage(Component.text(player.getActiveEffects().toString()));
|
||||||
player.addEffect(new Potion(
|
player.addEffect(new Potion(
|
||||||
potion,
|
potionEffect,
|
||||||
(byte) 0,
|
(byte) 0,
|
||||||
duration * MinecraftServer.TICK_PER_SECOND
|
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.Command;
|
||||||
import net.minestom.server.command.builder.CommandContext;
|
import net.minestom.server.command.builder.CommandContext;
|
||||||
import net.minestom.server.command.builder.arguments.Argument;
|
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.arguments.ArgumentType;
|
||||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||||
import net.minestom.server.extensions.Extension;
|
import net.minestom.server.extensions.Extension;
|
||||||
@ -30,16 +31,18 @@ public class ReloadExtensionCommand extends Command {
|
|||||||
.toArray(String[]::new);
|
.toArray(String[]::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final ArgumentString extensionName;
|
||||||
|
|
||||||
public ReloadExtensionCommand() {
|
public ReloadExtensionCommand() {
|
||||||
super("reload");
|
super("reload");
|
||||||
|
|
||||||
setDefaultExecutor(this::usage);
|
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) {
|
private void usage(CommandSender sender, CommandContext context) {
|
||||||
@ -47,7 +50,7 @@ public class ReloadExtensionCommand extends Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void execute(CommandSender sender, CommandContext context) {
|
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 + "...."));
|
sender.sendMessage(Component.text("extensionName = " + name + "...."));
|
||||||
|
|
||||||
ExtensionManager extensionManager = MinecraftServer.getExtensionManager();
|
ExtensionManager extensionManager = MinecraftServer.getExtensionManager();
|
||||||
@ -63,7 +66,7 @@ public class ReloadExtensionCommand extends Command {
|
|||||||
baos.flush();
|
baos.flush();
|
||||||
baos.close();
|
baos.close();
|
||||||
String contents = new String(baos.toByteArray(), StandardCharsets.UTF_8);
|
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) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -82,16 +85,4 @@ public class ReloadExtensionCommand extends Command {
|
|||||||
public String[] onDynamicWrite(@NotNull CommandSender sender, @NotNull String text) {
|
public String[] onDynamicWrite(@NotNull CommandSender sender, @NotNull String text) {
|
||||||
return extensionsName;
|
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import net.minestom.server.command.CommandSender;
|
|||||||
import net.minestom.server.command.builder.Command;
|
import net.minestom.server.command.builder.Command;
|
||||||
import net.minestom.server.command.builder.CommandContext;
|
import net.minestom.server.command.builder.CommandContext;
|
||||||
import net.minestom.server.command.builder.arguments.ArgumentType;
|
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.command.builder.exception.ArgumentSyntaxException;
|
||||||
import net.minestom.server.entity.EntityType;
|
import net.minestom.server.entity.EntityType;
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
@ -17,21 +18,13 @@ public class ShootCommand extends Command {
|
|||||||
|
|
||||||
public ShootCommand() {
|
public ShootCommand() {
|
||||||
super("shoot");
|
super("shoot");
|
||||||
setCondition(this::condition);
|
setCondition(Conditions::playerOnly);
|
||||||
setDefaultExecutor(this::defaultExecutor);
|
setDefaultExecutor(this::defaultExecutor);
|
||||||
var typeArg = ArgumentType.Word("type").from("default", "spectral", "colored");
|
var typeArg = ArgumentType.Word("type").from("default", "spectral", "colored");
|
||||||
setArgumentCallback(this::onTypeError, typeArg);
|
setArgumentCallback(this::onTypeError, typeArg);
|
||||||
addSyntax(this::onShootCommand, 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) {
|
private void defaultExecutor(CommandSender sender, CommandContext context) {
|
||||||
sender.sendMessage(Component.text("Correct usage: shoot [default/spectral/colored]"));
|
sender.sendMessage(Component.text("Correct usage: shoot [default/spectral/colored]"));
|
||||||
}
|
}
|
||||||
@ -60,6 +53,7 @@ public class ShootCommand extends Command {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var pos = player.getPosition().clone().add(0D, player.getEyeHeight(), 0D);
|
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);
|
projectile.setInstance(player.getInstance(), pos);
|
||||||
var dir = pos.getDirection().multiply(30D);
|
var dir = pos.getDirection().multiply(30D);
|
||||||
pos = pos.clone().add(dir.getX(), dir.getY(), dir.getZ());
|
pos = pos.clone().add(dir.getX(), dir.getY(), dir.getZ());
|
||||||
|
@ -1,35 +1,22 @@
|
|||||||
package demo.commands;
|
package demo.commands;
|
||||||
|
|
||||||
import net.minestom.server.MinecraftServer;
|
import net.minestom.server.MinecraftServer;
|
||||||
import net.minestom.server.command.CommandProcessor;
|
|
||||||
import net.minestom.server.command.CommandSender;
|
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;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple shutdown command.
|
* A simple shutdown command.
|
||||||
*/
|
*/
|
||||||
public class ShutdownCommand implements CommandProcessor {
|
public class ShutdownCommand extends Command {
|
||||||
|
|
||||||
@NotNull
|
public ShutdownCommand() {
|
||||||
@Override
|
super("shutdown");
|
||||||
public String getCommandName() {
|
addSyntax(this::execute);
|
||||||
return "shutdown";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void execute(@NotNull CommandSender commandSender, @NotNull CommandContext commandContext) {
|
||||||
public String[] getAliases() {
|
|
||||||
return new String[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean process(@NotNull CommandSender sender, @NotNull String command, @NotNull String[] args) {
|
|
||||||
MinecraftServer.stopCleanly();
|
MinecraftServer.stopCleanly();
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean hasAccess(@NotNull Player player) {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,11 @@
|
|||||||
package demo.commands;
|
package demo.commands;
|
||||||
|
|
||||||
import net.minestom.server.chat.JsonMessage;
|
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.minestom.server.command.CommandSender;
|
import net.minestom.server.command.CommandSender;
|
||||||
import net.minestom.server.command.builder.Command;
|
import net.minestom.server.command.builder.Command;
|
||||||
import net.minestom.server.command.builder.CommandContext;
|
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.ResourceLocation;
|
||||||
import static net.minestom.server.command.builder.arguments.ArgumentType.*;
|
|
||||||
|
|
||||||
public class TestCommand extends Command {
|
public class TestCommand extends Command {
|
||||||
|
|
||||||
@ -18,9 +15,7 @@ public class TestCommand extends Command {
|
|||||||
|
|
||||||
var test = ResourceLocation("msg");
|
var test = ResourceLocation("msg");
|
||||||
|
|
||||||
addSyntax((sender, context) -> {
|
addSyntax((sender, context) -> System.out.println("executed"),test);
|
||||||
System.out.println("executed");
|
|
||||||
},test);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void usage(CommandSender sender, CommandContext context) {
|
private void usage(CommandSender sender, CommandContext context) {
|
||||||
|
@ -1,24 +1,19 @@
|
|||||||
package demo.commands;
|
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.text.Component;
|
||||||
import net.kyori.adventure.title.Title;
|
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.CommandSender;
|
||||||
import net.minestom.server.command.builder.Command;
|
import net.minestom.server.command.builder.Command;
|
||||||
import net.minestom.server.command.builder.CommandContext;
|
import net.minestom.server.command.builder.CommandContext;
|
||||||
import net.minestom.server.command.builder.arguments.ArgumentType;
|
import net.minestom.server.command.builder.arguments.ArgumentType;
|
||||||
|
import net.minestom.server.command.builder.condition.Conditions;
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
|
|
||||||
public class TitleCommand extends Command {
|
public class TitleCommand extends Command {
|
||||||
public TitleCommand() {
|
public TitleCommand() {
|
||||||
super("title");
|
super("title");
|
||||||
setDefaultExecutor((source, args) -> {
|
setDefaultExecutor((source, args) -> source.sendMessage(Component.text("Unknown syntax (note: title must be quoted)")));
|
||||||
source.sendMessage(Component.text("Unknown syntax (note: title must be quoted)"));
|
setCondition(Conditions::playerOnly);
|
||||||
});
|
|
||||||
|
|
||||||
var content = ArgumentType.String("content");
|
var content = ArgumentType.String("content");
|
||||||
|
|
||||||
@ -26,11 +21,6 @@ public class TitleCommand extends Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void handleTitle(CommandSender source, CommandContext context) {
|
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();
|
Player player = source.asPlayer();
|
||||||
String titleContent = context.get("content");
|
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.Command;
|
||||||
import net.minestom.server.command.builder.CommandContext;
|
import net.minestom.server.command.builder.CommandContext;
|
||||||
import net.minestom.server.command.builder.arguments.Argument;
|
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.arguments.ArgumentType;
|
||||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||||
import net.minestom.server.extensions.Extension;
|
import net.minestom.server.extensions.Extension;
|
||||||
@ -18,16 +19,19 @@ import java.io.PrintStream;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
public class UnloadExtensionCommand extends Command {
|
public class UnloadExtensionCommand extends Command {
|
||||||
|
|
||||||
|
private final ArgumentString extensionName;
|
||||||
|
|
||||||
public UnloadExtensionCommand() {
|
public UnloadExtensionCommand() {
|
||||||
super("unload");
|
super("unload");
|
||||||
|
|
||||||
setDefaultExecutor(this::usage);
|
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) {
|
private void usage(CommandSender sender, CommandContext context) {
|
||||||
@ -35,7 +39,7 @@ public class UnloadExtensionCommand extends Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void execute(CommandSender sender, CommandContext context) {
|
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 + "...."));
|
sender.sendMessage(Component.text("extensionName = " + name + "...."));
|
||||||
|
|
||||||
ExtensionManager extensionManager = MinecraftServer.getExtensionManager();
|
ExtensionManager extensionManager = MinecraftServer.getExtensionManager();
|
||||||
@ -51,7 +55,7 @@ public class UnloadExtensionCommand extends Command {
|
|||||||
baos.flush();
|
baos.flush();
|
||||||
baos.close();
|
baos.close();
|
||||||
String contents = baos.toString(StandardCharsets.UTF_8);
|
String contents = baos.toString(StandardCharsets.UTF_8);
|
||||||
contents.lines().forEach(sender::sendMessage);
|
contents.lines().map(Component::text).forEach(sender::sendMessage);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -64,16 +68,4 @@ public class UnloadExtensionCommand extends Command {
|
|||||||
private void extensionCallback(CommandSender sender, ArgumentSyntaxException exception) {
|
private void extensionCallback(CommandSender sender, ArgumentSyntaxException exception) {
|
||||||
sender.sendMessage(Component.text("'" + exception.getInput() + "' is not a valid extension name!"));
|
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