Add PlayerWelcome event

This commit is contained in:
Alessandro Proto 2024-08-06 11:19:39 +02:00
parent d40b2d1b3b
commit c16722fc8e
3 changed files with 24 additions and 4 deletions

View file

@ -9,7 +9,7 @@ yarn_mappings=1.21+build.9
loader_version=0.15.11 loader_version=0.15.11
# Mod Properties # Mod Properties
mod_version=1.4.0 mod_version=1.4.1
maven_group=ct maven_group=ct
archives_base_name=ct-server archives_base_name=ct-server

View file

@ -3,6 +3,7 @@ package ct.server;
import ct.server.database.DatabaseClient; import ct.server.database.DatabaseClient;
import ct.server.database.PlayerData; import ct.server.database.PlayerData;
import ct.server.database.PlayerTable; import ct.server.database.PlayerTable;
import ct.server.events.PlayerWelcome;
import ct.server.http.ServiceServer; import ct.server.http.ServiceServer;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents;
@ -51,7 +52,7 @@ public class CtServer implements ModInitializer {
return database; return database;
} }
private PlayerTable playerTable; private final PlayerTable playerTable = new PlayerTable();
public PlayerTable playerTable() { public PlayerTable playerTable() {
return playerTable; return playerTable;
@ -65,7 +66,6 @@ public class CtServer implements ModInitializer {
try { try {
database = new DatabaseClient(); database = new DatabaseClient();
playerTable = new PlayerTable();
playerTable.ensureDatabaseCreated(); playerTable.ensureDatabaseCreated();
} catch (SQLException e) { } catch (SQLException e) {
@ -95,6 +95,9 @@ public class CtServer implements ModInitializer {
playerData.firstJoinedDate(new Date()); playerData.firstJoinedDate(new Date());
playerData.name(player.getName().getString()); playerData.name(player.getName().getString());
playerTable.updatePlayerData(playerData); playerTable.updatePlayerData(playerData);
PlayerWelcome.PLAYER_WELCOME.invoker().playerWelcome(player, playerData, server);
broadcastMessage(server, Text.literal("Welcome " + player.getName().getString() + " to the server!").formatted(Formatting.GREEN)); broadcastMessage(server, Text.literal("Welcome " + player.getName().getString() + " to the server!").formatted(Formatting.GREEN));
} else { } else {
if (!playerData.name().equals(player.getName().getString())) { if (!playerData.name().equals(player.getName().getString())) {
@ -105,7 +108,6 @@ public class CtServer implements ModInitializer {
}); });
ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> { ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> {
currentPlayerCount = server.getCurrentPlayerCount() - 1; currentPlayerCount = server.getCurrentPlayerCount() - 1;
}); });
} }

View file

@ -0,0 +1,18 @@
package ct.server.events;
import ct.server.database.PlayerData;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
public interface PlayerWelcome {
Event<PlayerWelcome> PLAYER_WELCOME = EventFactory.createArrayBacked(PlayerWelcome.class,
(listeners) -> (player, playerData, server) -> {
for (PlayerWelcome listener : listeners) {
listener.playerWelcome(player, playerData, server);
}
});
void playerWelcome(ServerPlayerEntity player, PlayerData playerData, MinecraftServer server);
}