-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: rename ChatStyleModule -> ChatModule + refactor: Position.o…
…f() + add: ReplyModule + add: AfkModule + refactor: FlyModule + refactor: GodModule + refactor: rename ChatStyleModule -> ChatModule +
- Loading branch information
1 parent
62b3307
commit 5b11ac0
Showing
18 changed files
with
261 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/io/github/sakurawald/mixin/afk/PlayerListMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/main/java/io/github/sakurawald/mixin/afk/ServerPlayerMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/main/java/io/github/sakurawald/module/afk/AfkModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/io/github/sakurawald/module/afk/ServerPlayerAccessor_afk.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.