Add AFK messages

This commit is contained in:
Alessandro Proto 2024-10-17 20:26:43 +02:00
parent e2c0a350a9
commit abb2d985ca
4 changed files with 53 additions and 11 deletions

View file

@ -19,6 +19,7 @@ repositories {
maven { url 'https://maven.wispforest.io' } maven { url 'https://maven.wispforest.io' }
maven { url 'https://maven.nucleoid.xyz' } maven { url 'https://maven.nucleoid.xyz' }
maven { url 'https://maven.reconnected.cc/releases' }
} }
loom { loom {
@ -46,6 +47,8 @@ dependencies {
compileOnly "net.luckperms:api:${project.luckpermsapi_version}" compileOnly "net.luckperms:api:${project.luckpermsapi_version}"
include modImplementation("me.lucko:fabric-permissions-api:${project.permissions_api_version}") include modImplementation("me.lucko:fabric-permissions-api:${project.permissions_api_version}")
include modImplementation("net.kyori:adventure-platform-fabric:${project.adventure_version}")
} }
processResources { processResources {

View file

@ -20,3 +20,5 @@ owo_version=0.11.2+1.20
luckpermsapi_version=5.4 luckpermsapi_version=5.4
permissions_api_version=0.2-SNAPSHOT permissions_api_version=0.2-SNAPSHOT
adventure_version=5.9.1

View file

@ -14,6 +14,10 @@ import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.serializer.json.JSONComponentSerializer;
import net.luckperms.api.LuckPerms; import net.luckperms.api.LuckPerms;
import net.luckperms.api.LuckPermsProvider; import net.luckperms.api.LuckPermsProvider;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
@ -81,16 +85,18 @@ public class RccServer implements ModInitializer {
CommandRegistrationCallback.EVENT.register(RccCommand::register); CommandRegistrationCallback.EVENT.register(RccCommand::register);
CommandRegistrationCallback.EVENT.register(AfkCommand::register); CommandRegistrationCallback.EVENT.register(AfkCommand::register);
ServerLifecycleEvents.SERVER_STARTED.register(server -> {
luckPerms = LuckPermsProvider.get();
afkTracker = new AfkTracker();
Ready.READY.invoker().ready(server, luckPerms);
if(CONFIG.enableHttpApi()) {
try { try {
serviceServer = new ServiceServer(); serviceServer = new ServiceServer();
} catch (IOException e) { } catch (IOException e) {
LOGGER.error("Unable to start HTTP server", e); LOGGER.error("Unable to start HTTP server", e);
} }
}
ServerLifecycleEvents.SERVER_STARTED.register(server -> {
luckPerms = LuckPermsProvider.get();
afkTracker = new AfkTracker();
Ready.READY.invoker().ready(server, luckPerms);
}); });
ServerTickEvents.END_SERVER_TICK.register(server -> { ServerTickEvents.END_SERVER_TICK.register(server -> {
@ -101,8 +107,10 @@ public class RccServer implements ModInitializer {
}); });
ServerLifecycleEvents.SERVER_STOPPING.register(server -> { ServerLifecycleEvents.SERVER_STOPPING.register(server -> {
if(CONFIG.enableHttpApi()) {
LOGGER.info("Stopping HTTP services"); LOGGER.info("Stopping HTTP services");
serviceServer.httpServer().stop(0); serviceServer.httpServer().stop(0);
}
}); });
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
@ -127,11 +135,31 @@ public class RccServer implements ModInitializer {
}); });
PlayerActivityEvents.AFK.register((player, server) -> { PlayerActivityEvents.AFK.register((player, server) -> {
LOGGER.info("{} is AFK. Active time: {} seconds.", player, afkTracker.getActiveTime(player)); LOGGER.info("{} is AFK. Active time: {} seconds.", player.getGameProfile().getName(), afkTracker.getActiveTime(player));
var displayNameJson = Text.Serializer.toJson(player.getDisplayName());
var displayName = JSONComponentSerializer.json().deserialize(displayNameJson);
var message = MiniMessage.miniMessage().deserialize(CONFIG.afkMessage(),
Placeholder.component("name", displayName)
);
broadcastMessage(server, message);
}); });
PlayerActivityEvents.AFK_RETURN.register((player, server) -> { PlayerActivityEvents.AFK_RETURN.register((player, server) -> {
LOGGER.info("{} is no longer AFK. Active time: {} seconds.", player, afkTracker.getActiveTime(player)); LOGGER.info("{} is no longer AFK. Active time: {} seconds.", player.getGameProfile().getName(), afkTracker.getActiveTime(player));
var displayNameJson = Text.Serializer.toJson(player.getDisplayName());
var displayName = JSONComponentSerializer.json().deserialize(displayNameJson);
var message = MiniMessage.miniMessage().deserialize(CONFIG.afkReturnMessage(),
Placeholder.component("displayname", displayName),
Placeholder.unparsed("username", player.getGameProfile().getName()),
Placeholder.unparsed("uuid", player.getUuid().toString())
);
broadcastMessage(server, message);
}); });
} }
@ -141,6 +169,12 @@ public class RccServer implements ModInitializer {
} }
} }
public void broadcastMessage(MinecraftServer server, Component message) {
for(ServerPlayerEntity player : server.getPlayerManager().getPlayerList()) {
player.sendMessage(message);
}
}
public boolean isPlayerAfk(PlayerEntity player) { public boolean isPlayerAfk(PlayerEntity player) {
return afkTracker.isPlayerAfk(player.getUuid()); return afkTracker.isPlayerAfk(player.getUuid());
} }

View file

@ -8,4 +8,7 @@ public class RccServerConfigModel {
public int httpPort = 25581; public int httpPort = 25581;
public int afkTimeTrigger = 300; public int afkTimeTrigger = 300;
public String afkMessage = "<gray><displayname> is now AFK</gray>";
public String afkReturnMessage = "<gray><displayname> is no longer AFK</gray>";
} }