Minestom/demo/src/main/java/net/minestom/demo/commands/PotionCommand.java

43 lines
1.7 KiB
Java
Raw Normal View History

2022-01-01 18:27:52 +01:00
package net.minestom.demo.commands;
2020-12-31 00:12:03 +01:00
2021-03-04 14:41:41 +01:00
import net.kyori.adventure.text.Component;
2020-12-31 00:12:03 +01:00
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;
2020-12-31 00:12:03 +01:00
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;
2020-12-31 00:12:03 +01:00
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;
2020-12-31 00:12:03 +01:00
public PotionCommand() {
super("potion");
setCondition(Conditions::playerOnly);
2020-12-31 00:12:03 +01:00
setDefaultExecutor(((sender, args) -> sender.sendMessage(Component.text("Usage: /potion <type> <duration (seconds)>"))));
2020-12-31 00:12:03 +01:00
potion = ArgumentType.Potion("potion");
duration = ArgumentType.Integer("duration");
2020-12-31 00:12:03 +01:00
addSyntax(this::onPotionCommand, potion, duration);
2020-12-31 00:12:03 +01:00
}
private void onPotionCommand(CommandSender sender, CommandContext context) {
2020-12-31 00:12:03 +01:00
final Player player = (Player) sender;
final PotionEffect potionEffect = context.get(potion);
final Integer duration = context.get(this.duration);
2020-12-31 00:12:03 +01:00
2021-03-04 14:41:41 +01:00
player.sendMessage(Component.text(player.getActiveEffects().toString()));
2021-11-30 17:49:41 +01:00
player.addEffect(new Potion(potionEffect, (byte) 0, duration * MinecraftServer.TICK_PER_SECOND, (byte) 0));
2020-12-31 00:12:03 +01:00
}
}