Add chat message sync
This commit is contained in:
parent
1283ede9d4
commit
89e74813e2
|
|
@ -13,6 +13,7 @@ import net.minecraftforge.event.server.ServerStartingEvent;
|
|||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||
import org.slf4j.Logger;
|
||||
import vip.fubuki.playersync.config.JdbcConfig;
|
||||
import vip.fubuki.playersync.sync.ChatSync;
|
||||
import vip.fubuki.playersync.sync.VanillaSync;
|
||||
import vip.fubuki.playersync.util.JDBCsetUp;
|
||||
|
||||
|
|
@ -30,6 +31,8 @@ public class PlayerSync
|
|||
modEventBus.addListener(this::commonSetup);
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
MinecraftForge.EVENT_BUS.register(new VanillaSync());
|
||||
MinecraftForge.EVENT_BUS.register(new ChatSync());
|
||||
|
||||
}
|
||||
|
||||
private void commonSetup(final FMLCommonSetupEvent event) {}
|
||||
|
|
@ -38,6 +41,7 @@ public class PlayerSync
|
|||
public void onServerStarting(ServerStartingEvent event) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
|
||||
JDBCsetUp.executeUpdate("CREATE DATABASE IF NOT EXISTS "+JdbcConfig.DATABASE_NAME.get(),true);
|
||||
JDBCsetUp.executeUpdate("CREATE TABLE IF NOT EXISTS player_data (uuid CHAR(36) NOT NULL,inventory BLOB,armor BLOB,advancements BLOB,enderchest BLOB,effects BLOB,xp int,food_level int,score int,health int,online boolean, PRIMARY KEY (uuid))");
|
||||
JDBCsetUp.executeUpdate("CREATE TABLE IF NOT EXISTS chat (player CHAR(36) NOT NULL,message TEXT,timestamp BIGINT)");
|
||||
if(ModList.get().isLoaded("curios")) {
|
||||
JDBCsetUp.executeUpdate("CREATE TABLE IF NOT EXISTS curios (uuid CHAR(36) NOT NULL,curios_item BLOB, PRIMARY KEY (uuid))");
|
||||
}
|
||||
|
|
|
|||
40
src/main/java/vip/fubuki/playersync/sync/ChatSync.java
Normal file
40
src/main/java/vip/fubuki/playersync/sync/ChatSync.java
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package vip.fubuki.playersync.sync;
|
||||
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.server.players.PlayerList;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import vip.fubuki.playersync.util.JDBCsetUp;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ChatSync {
|
||||
int tick = 0;
|
||||
long current = System.currentTimeMillis();
|
||||
@SubscribeEvent
|
||||
public void onPlayerChat(net.minecraftforge.event.ServerChatEvent event) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
|
||||
ReadMessage(Objects.requireNonNull(event.getPlayer().getServer()).getPlayerList());
|
||||
JDBCsetUp.executeUpdate("INSERT INTO chat (player, message, timestamp) VALUES ('" + event.getUsername() + "', '" + event.getMessage() + "', '" + current + "')");
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void Tick(net.minecraftforge.event.TickEvent.ServerTickEvent event) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
|
||||
tick++;
|
||||
if(tick == 20) {
|
||||
ReadMessage(event.getServer().getPlayerList());
|
||||
}
|
||||
}
|
||||
|
||||
public void ReadMessage(PlayerList playerList) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
|
||||
ResultSet resultSet= JDBCsetUp.executeQuery("SELECT * FROM chat WHERE timestamp > " + current);
|
||||
current = System.currentTimeMillis();
|
||||
tick = 0;
|
||||
while(resultSet.next()) {
|
||||
String player = resultSet.getString("player");
|
||||
String message = resultSet.getString("message");
|
||||
Component textComponents = Component.literal(player+": "+message);
|
||||
playerList.broadcastSystemMessage(textComponents, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -28,8 +28,7 @@ public class JDBCsetUp {
|
|||
|
||||
public static ResultSet executeQuery(String sql) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
|
||||
Statement statement= getConnection(false).createStatement();
|
||||
ResultSet resultSet = statement.executeQuery(sql);
|
||||
return resultSet;
|
||||
return statement.executeQuery(sql);
|
||||
}
|
||||
|
||||
public static void executeUpdate(String sql) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user