|
| 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 | +} |
0 commit comments