Add RepairCommand

This commit is contained in:
Len 2024-10-04 20:59:36 +02:00
parent dbb3f45898
commit 4dba5f3c41
2 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,79 @@
package com.alttd.essentia.commands.admin;
import com.alttd.essentia.commands.EssentiaCommand;
import com.alttd.essentia.commands.argumement.EquipmentArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.tree.LiteralCommandNode;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.command.brigadier.argument.ArgumentTypes;
import io.papermc.paper.command.brigadier.argument.resolvers.selector.PlayerSelectorArgumentResolver;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.jetbrains.annotations.NotNull;
public class RepairCommand implements EssentiaCommand {
@Override
public String commandName() {
return "repair";
}
@Override
public @NotNull LiteralCommandNode<CommandSourceStack> command() {
final LiteralArgumentBuilder<CommandSourceStack> builder =
Commands.literal(commandName())
.requires(commandSourceStack -> commandSourceStack.getSender().hasPermission(adminCommandPermission()))
// .executes((source) -> {
// if (source.getSource().getSender() instanceof Player player)
// execute(player, player);
//
// return 1;
// })
.then(Commands.argument("equipmentslot", new EquipmentArgumentType())
.executes((source) -> {
if (!(source.getSource().getSender() instanceof Player player))
return 1;
EquipmentSlot equipmentSlot = source.getArgument("equipmentslot", EquipmentSlot.class);
execute(source.getSource().getSender(), player, equipmentSlot);
return 1;
})
.then(
Commands.argument("player", ArgumentTypes.player())
.requires(commandSourceStack -> commandSourceStack.getSender().hasPermission(adminOtherCommandPermission()))
.executes((source) -> {
CommandSourceStack sourceStack = source.getSource();
Player target = source.getArgument("player", PlayerSelectorArgumentResolver.class).resolve(sourceStack).getFirst();
EquipmentSlot equipmentSlot = source.getArgument("equipmentslot", EquipmentSlot.class);
execute(source.getSource().getSender(), target, equipmentSlot);
return 1;
})
)
);
return builder.build();
}
// TODO - placeholders and messages from config
public void execute(CommandSender sender, Player target, EquipmentSlot equipmentSlot) {
TagResolver placeholders = TagResolver.resolver(
Placeholder.component("requester", sender.name()),
Placeholder.component("target", target.displayName())
);
ItemStack itemStack = target.getInventory().getItem(equipmentSlot);
if (!(itemStack.getItemMeta() instanceof Damageable damageable))
return; // send message can not repair this item
damageable.setDamage(0);
sender.sendRichMessage(target == sender ? "You have repaired your <item>." : "<sender> has repaired your <item>.", placeholders);
if (target != sender)
target.sendRichMessage("You repaired <target>'s <item>.", placeholders);
}
}

View File

@ -0,0 +1,49 @@
package com.alttd.essentia.commands.argumement;
import com.mojang.brigadier.Message;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import io.papermc.paper.command.brigadier.MessageComponentSerializer;
import io.papermc.paper.command.brigadier.argument.CustomArgumentType;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.inventory.EquipmentSlot;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.CompletableFuture;
public class EquipmentArgumentType implements CustomArgumentType.Converted<EquipmentSlot, String> {
@Override
public @NotNull EquipmentSlot convert(String nativeType) throws CommandSyntaxException {
try {
return EquipmentSlot.valueOf(nativeType.toUpperCase());
} catch (Exception e) {
Message message = MessageComponentSerializer.message().serialize(Component.text("Invalid EquipmentSlot %s!".formatted(nativeType), NamedTextColor.RED));
throw new CommandSyntaxException(new SimpleCommandExceptionType(message), message);
}
}
@Override
public @NotNull ArgumentType<String> getNativeType() {
return StringArgumentType.word();
}
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
for (EquipmentSlot equipmentSlot : EquipmentSlot.values()) {
builder.suggest(equipmentSlot.name().toLowerCase());
}
return CompletableFuture.completedFuture(
builder.build()
);
}
}