From 4dba5f3c41566dd11c5061fb23f564cd88a5f7a7 Mon Sep 17 00:00:00 2001 From: Len <40720638+destro174@users.noreply.github.com> Date: Fri, 4 Oct 2024 20:59:36 +0200 Subject: [PATCH] Add RepairCommand --- .../commands/admin/RepairCommand.java | 79 +++++++++++++++++++ .../argumement/EquipmentArgumentType.java | 49 ++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 plugin/src/main/java/com/alttd/essentia/commands/admin/RepairCommand.java create mode 100644 plugin/src/main/java/com/alttd/essentia/commands/argumement/EquipmentArgumentType.java diff --git a/plugin/src/main/java/com/alttd/essentia/commands/admin/RepairCommand.java b/plugin/src/main/java/com/alttd/essentia/commands/admin/RepairCommand.java new file mode 100644 index 0000000..19df28d --- /dev/null +++ b/plugin/src/main/java/com/alttd/essentia/commands/admin/RepairCommand.java @@ -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 command() { + final LiteralArgumentBuilder 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 ." : " has repaired your .", placeholders); + if (target != sender) + target.sendRichMessage("You repaired 's .", placeholders); + } + +} diff --git a/plugin/src/main/java/com/alttd/essentia/commands/argumement/EquipmentArgumentType.java b/plugin/src/main/java/com/alttd/essentia/commands/argumement/EquipmentArgumentType.java new file mode 100644 index 0000000..d750e88 --- /dev/null +++ b/plugin/src/main/java/com/alttd/essentia/commands/argumement/EquipmentArgumentType.java @@ -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 { + + @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 getNativeType() { + return StringArgumentType.word(); + } + + @Override + public CompletableFuture listSuggestions(CommandContext context, SuggestionsBuilder builder) { + for (EquipmentSlot equipmentSlot : EquipmentSlot.values()) { + builder.suggest(equipmentSlot.name().toLowerCase()); + } + + return CompletableFuture.completedFuture( + builder.build() + ); + } + +}