Skip to content

Commit 5c252b9

Browse files
committed
CM root and eject commands
1 parent 0144c92 commit 5c252b9

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package dev.compactmods.machines.command;
2+
3+
import com.mojang.brigadier.CommandDispatcher;
4+
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
5+
import dev.compactmods.machines.CompactMachines;
6+
import net.minecraft.command.CommandSource;
7+
8+
public class CMCommandRoot {
9+
10+
public static void register(CommandDispatcher<CommandSource> dispatcher) {
11+
final LiteralArgumentBuilder<CommandSource> root = LiteralArgumentBuilder.literal(CompactMachines.MOD_ID);
12+
root.then(CMEjectSubcommand.register());
13+
dispatcher.register(root);
14+
}
15+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package dev.compactmods.machines.command;
2+
3+
import java.util.Collection;
4+
import com.mojang.brigadier.builder.ArgumentBuilder;
5+
import com.mojang.brigadier.context.CommandContext;
6+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
7+
import dev.compactmods.machines.rooms.capability.CapabilityRoomHistory;
8+
import dev.compactmods.machines.rooms.capability.IRoomHistory;
9+
import dev.compactmods.machines.util.PlayerUtil;
10+
import net.minecraft.command.CommandSource;
11+
import net.minecraft.command.Commands;
12+
import net.minecraft.command.arguments.EntityArgument;
13+
import net.minecraft.entity.player.ServerPlayerEntity;
14+
15+
public class CMEjectSubcommand {
16+
public static ArgumentBuilder<CommandSource, ?> register() {
17+
return Commands.literal("eject")
18+
.requires(cs -> cs.hasPermission(2))
19+
.executes(CMEjectSubcommand::execExecutingPlayer)
20+
.then(Commands.argument("player", EntityArgument.player())
21+
.executes(CMEjectSubcommand::execSpecificPlayer));
22+
}
23+
24+
private static int execSpecificPlayer(CommandContext<CommandSource> ctx) throws CommandSyntaxException {
25+
Collection<ServerPlayerEntity> ent = EntityArgument.getPlayers(ctx, "player");
26+
ent.forEach(player -> {
27+
player.getCapability(CapabilityRoomHistory.HISTORY_CAPABILITY).ifPresent(IRoomHistory::clear);
28+
PlayerUtil.teleportPlayerToRespawnOrOverworld(ctx.getSource().getServer(), player);
29+
});
30+
31+
return 0;
32+
}
33+
34+
private static int execExecutingPlayer(CommandContext<CommandSource> ctx) throws CommandSyntaxException {
35+
final ServerPlayerEntity player = ctx.getSource().getPlayerOrException();
36+
37+
player.getCapability(CapabilityRoomHistory.HISTORY_CAPABILITY).ifPresent(IRoomHistory::clear);
38+
PlayerUtil.teleportPlayerToRespawnOrOverworld(ctx.getSource().getServer(), player);
39+
40+
return 0;
41+
}
42+
}

src/main/java/dev/compactmods/machines/core/ServerEventHandler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package dev.compactmods.machines.core;
22

33
import dev.compactmods.machines.CompactMachines;
4+
import dev.compactmods.machines.command.CMCommandRoot;
45
import net.minecraft.server.MinecraftServer;
6+
import net.minecraftforge.event.RegisterCommandsEvent;
57
import net.minecraftforge.eventbus.api.SubscribeEvent;
68
import net.minecraftforge.fml.common.Mod;
79
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
@@ -14,4 +16,9 @@ public static void onServerStarting(final FMLServerStartingEvent evt) {
1416
MinecraftServer server = evt.getServer();
1517
// SavedMachineDataMigrator.migrate(server);
1618
}
19+
20+
@SubscribeEvent
21+
public static void onCommandsRegister(final RegisterCommandsEvent event) {
22+
CMCommandRoot.register(event.getDispatcher());
23+
}
1724
}

0 commit comments

Comments
 (0)