diff --git a/src/main/java/land/chipmunk/tgbextras/Extras.java b/src/main/java/land/chipmunk/tgbextras/Extras.java index 5f25f63..045d559 100644 --- a/src/main/java/land/chipmunk/tgbextras/Extras.java +++ b/src/main/java/land/chipmunk/tgbextras/Extras.java @@ -22,6 +22,7 @@ public class Extras implements ModInitializer { } public static void registerCommands (CommandDispatcher dispatcher, CommandRegistryAccess commandRegistryAccess, CommandManager.RegistrationEnvironment environment) { + AFKCommand.register(dispatcher); ClearChatCommand.register(dispatcher); PingCommand.register(dispatcher); SpawnCoordinatesCommand.register(dispatcher); diff --git a/src/main/java/land/chipmunk/tgbextras/commands/AFKCommand.java b/src/main/java/land/chipmunk/tgbextras/commands/AFKCommand.java new file mode 100644 index 0000000..86938f2 --- /dev/null +++ b/src/main/java/land/chipmunk/tgbextras/commands/AFKCommand.java @@ -0,0 +1,54 @@ +package land.chipmunk.tgbextras.commands; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import static net.minecraft.server.command.CommandManager.literal; +import land.chipmunk.tgbextras.modules.PlayerExtensions; +import net.minecraft.entity.effect.StatusEffectInstance; +import net.minecraft.entity.effect.StatusEffects; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Text; + +public class AFKCommand { + public static void register (CommandDispatcher dispatcher) { + dispatcher.register( + literal("afk") + .executes(AFKCommand::AFKCommand) + ); + } + + public static int AFKCommand (CommandContext context) throws CommandSyntaxException { + final ServerCommandSource source = context.getSource(); + final ServerPlayerEntity player = source.getPlayerOrThrow(); + PlayerExtensions playerEx = (PlayerExtensions) player; + + playerEx.afk(!playerEx.afk()); + + if (playerEx.afk()) { + player.addStatusEffect(new StatusEffectInstance(StatusEffects.RESISTANCE, 2147483647, 4, true, false)); + player.addStatusEffect(new StatusEffectInstance(StatusEffects.BLINDNESS, 2147483647, 99, true, false)); + player.addStatusEffect(new StatusEffectInstance(StatusEffects.SLOWNESS, 2147483647, 99, true, false)); + player.addStatusEffect(new StatusEffectInstance(StatusEffects.WEAKNESS, 2147483647, 99, true, false)); + player.addStatusEffect(new StatusEffectInstance(StatusEffects.MINING_FATIGUE, 2147483647, 99, true, false)); + + source.getServer().getPlayerManager().broadcast( + Text.literal("* ") + .append(player.getDisplayName()) + .append(Text.literal(" is now afk")) + , false); + } else { + player.clearStatusEffects(); + + source.getServer().getPlayerManager().broadcast( + Text.literal("* ") + .append(player.getDisplayName()) + .append(Text.literal(" is no longer afk")) + , false); + } + + return Command.SINGLE_SUCCESS; + } +} \ No newline at end of file diff --git a/src/main/java/land/chipmunk/tgbextras/mixin/ServerPlayerEntityMixin.java b/src/main/java/land/chipmunk/tgbextras/mixin/ServerPlayerEntityMixin.java index 1a3b873..05df663 100644 --- a/src/main/java/land/chipmunk/tgbextras/mixin/ServerPlayerEntityMixin.java +++ b/src/main/java/land/chipmunk/tgbextras/mixin/ServerPlayerEntityMixin.java @@ -12,6 +12,7 @@ import land.chipmunk.tgbextras.modules.PlayerExtensions; @Mixin(ServerPlayerEntity.class) public abstract class ServerPlayerEntityMixin implements PlayerExtensions { private ServerPlayerEntity lastTeleportRequestor = null; + private boolean afk = false; public ServerPlayerEntity lastTeleportRequestor () { return lastTeleportRequestor; @@ -20,4 +21,12 @@ public abstract class ServerPlayerEntityMixin implements PlayerExtensions { public ServerPlayerEntity lastTeleportRequestor (ServerPlayerEntity lastTeleportRequestor) { return (this.lastTeleportRequestor = lastTeleportRequestor); } + + public boolean afk () { + return afk; + } + + public boolean afk (boolean afk) { + return (this.afk = afk); + } } \ No newline at end of file diff --git a/src/main/java/land/chipmunk/tgbextras/modules/PlayerExtensions.java b/src/main/java/land/chipmunk/tgbextras/modules/PlayerExtensions.java index fb8b029..01965f8 100644 --- a/src/main/java/land/chipmunk/tgbextras/modules/PlayerExtensions.java +++ b/src/main/java/land/chipmunk/tgbextras/modules/PlayerExtensions.java @@ -5,4 +5,6 @@ import net.minecraft.server.network.ServerPlayerEntity; public interface PlayerExtensions { ServerPlayerEntity lastTeleportRequestor (); ServerPlayerEntity lastTeleportRequestor (ServerPlayerEntity lastTeleportRequestor); + boolean afk (); + boolean afk (boolean afk); }