|
| 1 | +package me.alexdevs.solstice.commands.misc; |
| 2 | + |
| 3 | +import com.mojang.brigadier.CommandDispatcher; |
| 4 | +import com.mojang.brigadier.arguments.IntegerArgumentType; |
| 5 | +import com.mojang.brigadier.builder.LiteralArgumentBuilder; |
| 6 | +import com.mojang.brigadier.context.CommandContext; |
| 7 | +import com.mojang.brigadier.exceptions.CommandSyntaxException; |
| 8 | +import me.lucko.fabric.api.permissions.v0.Permissions; |
| 9 | +import net.minecraft.command.argument.EntityArgumentType; |
| 10 | +import net.minecraft.server.command.ServerCommandSource; |
| 11 | +import net.minecraft.server.network.ServerPlayerEntity; |
| 12 | +import net.minecraft.text.Text; |
| 13 | +import org.jetbrains.annotations.Nullable; |
| 14 | + |
| 15 | +import java.util.Collection; |
| 16 | + |
| 17 | +import static net.minecraft.server.command.CommandManager.argument; |
| 18 | +import static net.minecraft.server.command.CommandManager.literal; |
| 19 | + |
| 20 | +public class IgniteCommand { |
| 21 | + public static final int defaultTicks = 200; // 10 seconds |
| 22 | + |
| 23 | + public static void register(CommandDispatcher<ServerCommandSource> dispatcher) { |
| 24 | + dispatcher.register(command("ignite")); |
| 25 | + } |
| 26 | + |
| 27 | + private static LiteralArgumentBuilder<ServerCommandSource> command(String command) { |
| 28 | + return literal(command) |
| 29 | + .requires(Permissions.require("solstice.command.ignite", 2)) |
| 30 | + .executes(context -> execute(context, null, null)) |
| 31 | + .then(argument("players", EntityArgumentType.players()) |
| 32 | + .executes(context -> execute(context, EntityArgumentType.getPlayers(context, "players"), null)) |
| 33 | + .then(argument("ticks", IntegerArgumentType.integer(0)) |
| 34 | + .executes(context -> |
| 35 | + execute(context, EntityArgumentType.getPlayers(context, "players"), IntegerArgumentType.getInteger(context, "ticks")) |
| 36 | + ) |
| 37 | + ) |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + private static int execute(CommandContext<ServerCommandSource> context, @Nullable Collection<ServerPlayerEntity> players, @Nullable Integer ticks) throws CommandSyntaxException { |
| 42 | + var source = context.getSource(); |
| 43 | + if (players == null) { |
| 44 | + ignite(source, source.getPlayerOrThrow(), ticks); |
| 45 | + return 1; |
| 46 | + } else { |
| 47 | + for (ServerPlayerEntity player : players) { |
| 48 | + ignite(source, player, ticks); |
| 49 | + } |
| 50 | + |
| 51 | + return players.size(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private static void ignite(ServerCommandSource source, ServerPlayerEntity player, @Nullable Integer ticks) { |
| 56 | + if (ticks == null) { |
| 57 | + player.setFireTicks(defaultTicks); |
| 58 | + source.sendFeedback(() -> Text.literal("Ignited ").append(source.getDisplayName()), true); |
| 59 | + } else { |
| 60 | + player.setFireTicks(ticks); |
| 61 | + source.sendFeedback(() -> Text.literal("Ignited ").append(source.getDisplayName()).append(Text.of(String.format(" for %d ticks", ticks))), true); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments