Skip to content

Commit

Permalink
refactor: rename ChatStyleModule -> ChatModule + refactor: Position.o…
Browse files Browse the repository at this point in the history
…f() + add: ReplyModule + add: AfkModule + refactor: FlyModule + refactor: GodModule + refactor: rename ChatStyleModule -> ChatModule +
  • Loading branch information
sakurawald committed Oct 26, 2023
1 parent 62b3307 commit 5b11ac0
Show file tree
Hide file tree
Showing 18 changed files with 261 additions and 29 deletions.
16 changes: 15 additions & 1 deletion src/main/java/io/github/sakurawald/config/ConfigGSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public class Modules {
public Fly fly = new Fly();
public God god = new God();
public Language language = new Language();
public Reply reply = new Reply();
public Afk afk = new Afk();

public class ResourceWorld {
public boolean enable = false;
Expand Down Expand Up @@ -386,9 +388,21 @@ public class Language {
public boolean enable = false;
}

public Reply reply = new Reply();
public class Reply {
public boolean enable = false;
}

public class Afk {

public boolean enable = false;
public String format = "<gray>[AFK] <reset>%player_display_name%";

public AfkChecker afk_checker = new AfkChecker();

public class AfkChecker {
public String cron = "* 0 0 ? * * *";
public boolean kick_player = false;
}
}
}
}
21 changes: 21 additions & 0 deletions src/main/java/io/github/sakurawald/mixin/afk/PlayerListMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.sakurawald.mixin.afk;

import io.github.sakurawald.module.afk.ServerPlayerAccessor_afk;
import lombok.extern.slf4j.Slf4j;
import net.minecraft.network.Connection;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.players.PlayerList;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(PlayerList.class)
@Slf4j
public abstract class PlayerListMixin {
@Inject(at = @At(value = "TAIL"), method = "placeNewPlayer")
private void $placeNewPlayer(Connection connection, ServerPlayer player, CallbackInfo info) {
ServerPlayerAccessor_afk afk_player = (ServerPlayerAccessor_afk) player;
afk_player.sakurawald$setLastLastActionTime(player.getLastActionTime());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package io.github.sakurawald.mixin.afk;

import io.github.sakurawald.config.ConfigManager;
import io.github.sakurawald.module.afk.ServerPlayerAccessor_afk;
import io.github.sakurawald.util.MessageUtil;
import lombok.extern.slf4j.Slf4j;
import net.kyori.adventure.text.TextReplacementConfig;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundPlayerInfoUpdatePacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import static io.github.sakurawald.util.MessageUtil.ofComponent;
import static io.github.sakurawald.util.MessageUtil.toVomponent;

@Mixin(ServerPlayer.class)
@Slf4j
public abstract class ServerPlayerMixin implements ServerPlayerAccessor_afk {

@Unique
private final ServerPlayer player = (ServerPlayer) (Object) this;
@Shadow
@Final
public MinecraftServer server;
@Unique
private boolean afk = false;

@Unique
private long lastLastActionTime = 0;

@Inject(method = "getTabListDisplayName", at = @At("HEAD"), cancellable = true)
public void getTabListDisplayName(CallbackInfoReturnable<Component> cir) {
ServerPlayerAccessor_afk accessor = (ServerPlayerAccessor_afk) player;

if (accessor.sakurawald$isAfk()) {
cir.setReturnValue(Component.literal("afk " + player.getGameProfile().getName()));
net.kyori.adventure.text.@NotNull Component component = ofComponent(ConfigManager.configWrapper.instance().modules.afk.format)
.replaceText(TextReplacementConfig.builder().match("%player_display_name%").replacement(player.getDisplayName()).build());
cir.setReturnValue(toVomponent(component));
} else {
cir.setReturnValue(null);
}
}


@Inject(method = "resetLastActionTime", at = @At("HEAD"))
public void resetLastActionTime(CallbackInfo ci) {
if (sakurawald$isAfk()) {
sakurawald$setAfk(false);
MessageUtil.sendBroadcast("afk.off.broadcast", player.getGameProfile().getName());
}
}

@Override
public void sakurawald$setAfk(boolean flag) {
this.afk = flag;
this.server.getPlayerList().broadcastAll(new ClientboundPlayerInfoUpdatePacket(ClientboundPlayerInfoUpdatePacket.Action.UPDATE_DISPLAY_NAME, (ServerPlayer) (Object) this));
}

@Override
public boolean sakurawald$isAfk() {
return this.afk;
}

@Override
public void sakurawald$setLastLastActionTime(long lastActionTime) {
this.lastLastActionTime = lastActionTime;
}

@Override
public long sakurawald$getLastLastActionTime() {
return this.lastLastActionTime;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.sakurawald.mixin.chat;

import io.github.sakurawald.module.ModuleManager;
import io.github.sakurawald.module.chat.ChatStyleModule;
import io.github.sakurawald.module.chat.ChatModule;
import io.github.sakurawald.util.CarpetUtil;
import net.minecraft.network.Connection;
import net.minecraft.server.level.ServerPlayer;
Expand All @@ -16,7 +16,7 @@
public abstract class PlayerListMixin {

@Unique
private static final ChatStyleModule module = ModuleManager.getOrNewInstance(ChatStyleModule.class);
private static final ChatModule module = ModuleManager.getOrNewInstance(ChatModule.class);

@Inject(at = @At(value = "TAIL"), method = "placeNewPlayer")
private void $placeNewPlayer(Connection connection, ServerPlayer player, CallbackInfo info) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.sakurawald.mixin.chat;

import io.github.sakurawald.module.ModuleManager;
import io.github.sakurawald.module.chat.ChatStyleModule;
import io.github.sakurawald.module.chat.ChatModule;
import net.minecraft.network.chat.PlayerChatMessage;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
Expand All @@ -15,7 +15,7 @@
@Mixin(value = ServerGamePacketListenerImpl.class, priority = 1001)
public abstract class ServerGamePacketListenerImplMixin {
@Unique
private static final ChatStyleModule module = ModuleManager.getOrNewInstance(ChatStyleModule.class);
private static final ChatModule module = ModuleManager.getOrNewInstance(ChatModule.class);
@Shadow
public ServerPlayer player;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public abstract class ServerPlayerMixin implements ServerPlayerAccessor {
if (!module.tickets.containsKey(playerName)) {
module.tickets.put(playerName,
new TeleportTicket(player
, new Position(player.level(), player.position().x, player.position().y, player.position().z, player.getYRot(), player.getXRot())
, new Position(targetWorld, x, y, z, yaw, pitch), false));
, Position.of(player), new Position(targetWorld, x, y, z, yaw, pitch), false));
ci.cancel();
return;
} else {
Expand Down
83 changes: 83 additions & 0 deletions src/main/java/io/github/sakurawald/module/afk/AfkModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package io.github.sakurawald.module.afk;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import io.github.sakurawald.ServerMain;
import io.github.sakurawald.config.ConfigManager;
import io.github.sakurawald.module.AbstractModule;
import io.github.sakurawald.util.MessageUtil;
import io.github.sakurawald.util.ScheduleUtil;
import lombok.extern.slf4j.Slf4j;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

@Slf4j
public class AfkModule extends AbstractModule {

@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register(this::registerCommand);
ServerLifecycleEvents.SERVER_STARTED.register(this::registerScheduleTask);
}

public void registerCommand(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext registryAccess, Commands.CommandSelection environment) {
dispatcher.register(Commands.literal("afk").executes(this::$afk));
}

@SuppressWarnings("SameReturnValue")
private int $afk(CommandContext<CommandSourceStack> ctx) {
ServerPlayer player = ctx.getSource().getPlayer();
if (player == null) return Command.SINGLE_SUCCESS;

boolean flag = !((ServerPlayerAccessor_afk) player).sakurawald$isAfk();
((ServerPlayerAccessor_afk) player).sakurawald$setAfk(flag);

MessageUtil.sendMessage(player, flag ? "afk.on" : "afk.off");
return Command.SINGLE_SUCCESS;
}

public void registerScheduleTask(MinecraftServer server) {
ScheduleUtil.addJob(AfkCheckerJob.class, ConfigManager.configWrapper.instance().modules.afk.afk_checker.cron, new JobDataMap());
}

public static class AfkCheckerJob implements Job {

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
for (ServerPlayer player : ServerMain.SERVER.getPlayerList().getPlayers()) {
ServerPlayerAccessor_afk afk_player = (ServerPlayerAccessor_afk) player;

// get last action time
long lastActionTime = player.getLastActionTime();
long lastLastActionTime = afk_player.sakurawald$getLastLastActionTime();
afk_player.sakurawald$setLastLastActionTime(lastActionTime);

// diff last action time
/* note:
when a player joins the server,
we'll set lastLastActionTime's initial value to Player#getLastActionTime(),
but there are a little difference even if you call Player#getLastActionTime() again
*/
if (lastActionTime - lastLastActionTime <= 3000) {
if (afk_player.sakurawald$isAfk()) continue;

afk_player.sakurawald$setAfk(true);
MessageUtil.sendBroadcast("afk.on.broadcast", player.getGameProfile().getName());
if (ConfigManager.configWrapper.instance().modules.afk.afk_checker.kick_player) {
player.connection.disconnect(MessageUtil.ofVomponent(player, "afk.kick"));
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.sakurawald.module.afk;

public interface ServerPlayerAccessor_afk {

void sakurawald$setAfk(boolean flag);

boolean sakurawald$isAfk();

void sakurawald$setLastLastActionTime(long lastActionTime);

long sakurawald$getLastLastActionTime();
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void updatePlayer(ServerPlayer player) {
|| (player.level() == lastPos.getLevel() && player.position().distanceToSqr(lastPos.getX(), lastPos.getY(), lastPos.getZ()) > ignoreDistance * ignoreDistance)
) {
player2lastPos.put(player.getGameProfile().getName(),
new Position(player.level(), player.position().x, player.position().y, player.position().z, player.getYRot(), player.getXRot()));
Position.of(player));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

@SuppressWarnings("UnstableApiUsage")
@Slf4j
public class ChatStyleModule extends AbstractModule {
public class ChatModule extends AbstractModule {

private final MiniMessage miniMessage = MiniMessage.builder().build();
private final MainStatsModule mainStatsModule = ModuleManager.getOrNewInstance(MainStatsModule.class);
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/io/github/sakurawald/module/fly/FlyModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ private int fly(CommandContext<CommandSourceStack> ctx) {
ServerPlayer player = ctx.getSource().getPlayer();
if (player == null) return Command.SINGLE_SUCCESS;

boolean flag = player.getAbilities().mayfly;
player.getAbilities().mayfly = !flag;
if (flag) {
boolean flag = !player.getAbilities().mayfly;
player.getAbilities().mayfly = flag;
player.onUpdateAbilities();

if (!flag) {
player.getAbilities().flying = false;
MessageUtil.sendMessage(player, "fly.off");
} else {
MessageUtil.sendMessage(player, "fly.on");
}

player.onUpdateAbilities();
MessageUtil.sendMessage(player, flag ? "fly.on" : "fly.off");
return Command.SINGLE_SUCCESS;
}

Expand Down
13 changes: 4 additions & 9 deletions src/main/java/io/github/sakurawald/module/god/GodModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,11 @@ private int god(CommandContext<CommandSourceStack> ctx) {
ServerPlayer player = ctx.getSource().getPlayer();
if (player == null) return Command.SINGLE_SUCCESS;

boolean flag = player.getAbilities().invulnerable;
player.getAbilities().invulnerable = !flag;

if (flag) {
MessageUtil.sendMessage(player, "god.off");
} else {
MessageUtil.sendMessage(player, "god.on");
}

boolean flag = !player.getAbilities().invulnerable;
player.getAbilities().invulnerable = flag;
player.onUpdateAbilities();

MessageUtil.sendMessage(player, flag ? "god.on" : "god.off");
return Command.SINGLE_SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ private static void kickPlayers(ServerLevel world) {
if (teleportWarmupModule != null) {
teleportWarmupModule.tickets.put(player.getGameProfile().getName(),
new TeleportTicket(player
, new Position(world, player.getX(), player.getY(), player.getZ(), player.getYRot(), player.getXRot())
, new Position(overworld, spawnPos.getX() + 0.5, spawnPos.getY() + 0.5, spawnPos.getZ() + 0.5, 0, 0)
, Position.of(player), new Position(overworld, spawnPos.getX() + 0.5, spawnPos.getY() + 0.5, spawnPos.getZ() + 0.5, 0, 0)
, true));
}
player.teleportTo(overworld, spawnPos.getX() + 0.5, spawnPos.getY(), spawnPos.getZ() + 0.5, overworld.getSharedSpawnAngle(), 0.0F);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.AllArgsConstructor;
import lombok.Data;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.Level;

@Data
Expand All @@ -14,4 +15,16 @@ public class Position {

private float yaw;
private float pitch;

public static Position of(ServerPlayer player) {
return new Position(player.level(), player.getX(), player.getY(), player.getZ(), player.getYRot(), player.getXRot());
}

public double distanceToSqr(Position position) {
if (this.level != position.level) return Double.MAX_VALUE;
double x = this.x - position.x;
double y = this.y - position.y;
double z = this.z - position.z;
return x * x + y * y + z * z;
}
}
1 change: 1 addition & 0 deletions src/main/java/io/github/sakurawald/util/MessageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public static Component ofComponent(String str) {
return miniMessage.deserialize(str);
}

// todo: auto add keys in language
public static net.minecraft.network.chat.Component ofVomponent(String str) {
return toVomponent(ofComponent(str));
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/resources/assets/sakurawald/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,10 @@
"fly.on": "<gold>Fly mode: on",
"fly.off": "<gold>Fly mode: off",
"god.on": "<gold>God mode: on",
"god.off": "<gold>God mode: off"
"god.off": "<gold>God mode: off",
"afk.on": "<gold>Afk mode: on",
"afk.off": "<gold>Afk mode: off",
"afk.on.broadcast": "<gold>Player %s is now afk",
"afk.off.broadcast": "<gold>Player %s is no longer afk",
"afk.kick": "<red>You have been kicked for being afk."
}
Loading

0 comments on commit 5b11ac0

Please sign in to comment.