|
| 1 | +package me.drex.essentials.command.impl.misc; |
| 2 | + |
| 3 | +import com.mojang.brigadier.builder.LiteralArgumentBuilder; |
| 4 | +import eu.pb4.placeholders.api.PlaceholderContext; |
| 5 | +import me.drex.essentials.command.Command; |
| 6 | +import me.drex.essentials.command.CommandProperties; |
| 7 | +import net.minecraft.commands.CommandBuildContext; |
| 8 | +import net.minecraft.commands.CommandSourceStack; |
| 9 | +import net.minecraft.commands.Commands; |
| 10 | +import net.minecraft.server.level.ServerPlayer; |
| 11 | +import net.minecraft.world.item.ItemStack; |
| 12 | + |
| 13 | +import static me.drex.message.api.LocalizedMessage.localized; |
| 14 | +import static net.minecraft.commands.Commands.argument; |
| 15 | +import static net.minecraft.commands.arguments.EntityArgument.getPlayer; |
| 16 | +import static net.minecraft.commands.arguments.EntityArgument.player; |
| 17 | + |
| 18 | +public class RepairCommand extends Command { |
| 19 | + |
| 20 | + public RepairCommand() { |
| 21 | + super(CommandProperties.create("repair", 2)); |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + protected void registerArguments(LiteralArgumentBuilder<CommandSourceStack> literal, CommandBuildContext commandBuildContext) { |
| 26 | + literal.then( |
| 27 | + Commands.literal("all") |
| 28 | + .requires(require("all")) |
| 29 | + .executes(ctx -> { |
| 30 | + CommandSourceStack src = ctx.getSource(); |
| 31 | + ServerPlayer player = src.getPlayerOrException(); |
| 32 | + for (ItemStack itemStack : player.getInventory()) { |
| 33 | + if (itemStack.isDamaged()) { |
| 34 | + itemStack.setDamageValue(0); |
| 35 | + } |
| 36 | + } |
| 37 | + src.sendSuccess(() -> localized("fabric-essentials.commands.repair.all"), false); |
| 38 | + return SUCCESS; |
| 39 | + }) |
| 40 | + ) |
| 41 | + .executes(ctx -> { |
| 42 | + CommandSourceStack src = ctx.getSource(); |
| 43 | + ServerPlayer player = src.getPlayerOrException(); |
| 44 | + ItemStack itemStack = player.getMainHandItem(); |
| 45 | + if (itemStack.isEmpty() || !itemStack.isDamaged()) { |
| 46 | + src.sendFailure(localized("fabric-essentials.commands.repair.missing")); |
| 47 | + return FAILURE; |
| 48 | + } |
| 49 | + itemStack.setDamageValue(0); |
| 50 | + src.sendSuccess(() -> localized("fabric-essentials.commands.repair"), false); |
| 51 | + return SUCCESS; |
| 52 | + }); |
| 53 | + } |
| 54 | +} |
0 commit comments