Skip to content

Commit 710b2d1

Browse files
committed
Add /extinguish and /ignite commands
1 parent 36d0222 commit 710b2d1

3 files changed

Lines changed: 117 additions & 0 deletions

File tree

src/main/java/me/alexdevs/solstice/commands/CommandInitializer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ public static void register() {
8585
InfoCommand.register(dispatcher);
8686
RulesCommand.register(dispatcher);
8787
SuicideCommand.register(dispatcher);
88+
ExtinguishCommand.register(dispatcher);
89+
IgniteCommand.register(dispatcher);
8890

8991
AnvilCommand.register(dispatcher);
9092
CartographyCommand.register(dispatcher);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package me.alexdevs.solstice.commands.misc;
2+
3+
import com.mojang.brigadier.CommandDispatcher;
4+
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
5+
import com.mojang.brigadier.context.CommandContext;
6+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
7+
import me.lucko.fabric.api.permissions.v0.Permissions;
8+
import net.minecraft.command.argument.EntityArgumentType;
9+
import net.minecraft.server.command.ServerCommandSource;
10+
import net.minecraft.server.network.ServerPlayerEntity;
11+
import net.minecraft.text.Text;
12+
import org.jetbrains.annotations.Nullable;
13+
14+
import java.util.Collection;
15+
16+
import static net.minecraft.server.command.CommandManager.argument;
17+
import static net.minecraft.server.command.CommandManager.literal;
18+
19+
public class ExtinguishCommand {
20+
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
21+
dispatcher.register(command("extinguish"));
22+
dispatcher.register(command("ex"));
23+
}
24+
25+
private static LiteralArgumentBuilder<ServerCommandSource> command(String command) {
26+
return literal(command)
27+
.requires(Permissions.require("solstice.command.extinguish", 2))
28+
.executes(context -> execute(context, null))
29+
.then(argument("players", EntityArgumentType.players())
30+
.executes(context -> execute(context, EntityArgumentType.getPlayers(context, "players"))));
31+
}
32+
33+
private static int execute(CommandContext<ServerCommandSource> context, @Nullable Collection<ServerPlayerEntity> players) throws CommandSyntaxException {
34+
var source = context.getSource();
35+
if (players == null) {
36+
extinguish(source, source.getPlayerOrThrow());
37+
return 1;
38+
} else {
39+
for (ServerPlayerEntity player : players) {
40+
extinguish(source, player);
41+
}
42+
43+
return players.size();
44+
}
45+
}
46+
47+
private static void extinguish(ServerCommandSource source, ServerPlayerEntity player) {
48+
player.extinguish();
49+
source.sendFeedback(() -> Text.literal("Extinguished ").append(source.getDisplayName()), true);
50+
}
51+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)