fix chat message sync

This commit is contained in:
mlus-Asuka 2023-01-10 13:49:02 +08:00
parent 2578834a0f
commit 34e16313b6
4 changed files with 14 additions and 10 deletions

View File

@ -31,8 +31,9 @@ public class PlayerSync
modEventBus.addListener(this::commonSetup);
MinecraftForge.EVENT_BUS.register(this);
MinecraftForge.EVENT_BUS.register(new VanillaSync());
MinecraftForge.EVENT_BUS.register(new ChatSync());
if(JdbcConfig.SYNC_CHAT.get()){
MinecraftForge.EVENT_BUS.register(new ChatSync());
}
}
private void commonSetup(final FMLCommonSetupEvent event) {}

View File

@ -17,6 +17,7 @@ public class JdbcConfig {
public static ForgeConfigSpec.ConfigValue<String> PASSWORD;
public static ForgeConfigSpec.ConfigValue<List<String>> SYNC_WORLD;
public static ForgeConfigSpec.BooleanValue USE_SSL;
public static ForgeConfigSpec.BooleanValue SYNC_CHAT;
static {
@ -29,6 +30,7 @@ public class JdbcConfig {
USERNAME = COMMON_BUILDER.comment("username").define("user_name", "root");
PASSWORD = COMMON_BUILDER.comment("password").define("password", "password");
SYNC_WORLD = COMMON_BUILDER.comment("The worlds that will be synchronized.If running in server it is supposed to have only one").define("sync_world", new ArrayList<String>());
SYNC_CHAT= COMMON_BUILDER.comment("Whether synchronize chat").define("sync_chat", true);
COMMON_BUILDER.pop();
COMMON_CONFIG = COMMON_BUILDER.build();
}

View File

@ -10,23 +10,23 @@ import java.sql.SQLException;
import java.util.Objects;
public class ChatSync {
int tick = 0;
long current = System.currentTimeMillis();
static int tick = 0;
static long current = System.currentTimeMillis();
@SubscribeEvent
public void onPlayerChat(net.minecraftforge.event.ServerChatEvent event) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
public static 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 {
public static 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 {
public static void ReadMessage(PlayerList playerList) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
ResultSet resultSet= JDBCsetUp.executeQuery("SELECT * FROM chat WHERE timestamp > " + current);
current = System.currentTimeMillis();
tick = 0;
@ -36,5 +36,6 @@ public class ChatSync {
Component textComponents = Component.literal(player+": "+message);
playerList.broadcastSystemMessage(textComponents, true);
}
resultSet.close();
}
}

View File

@ -108,7 +108,7 @@ public class VanillaSync {
return ItemStack.of(compoundTag);
}
@SubscribeEvent
public void OnPlayerLogout(PlayerEvent.PlayerLoggedOutEvent event) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
public static void OnPlayerLogout(PlayerEvent.PlayerLoggedOutEvent event) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
String player_uuid = event.getEntity().getUUID().toString();
JDBCsetUp.executeUpdate("UPDATE player_data SET online=false WHERE uuid='"+player_uuid+"'");
if(!event.getEntity().getTags().contains("player_synced")) return;
@ -119,7 +119,7 @@ public class VanillaSync {
event.getEntity().removeTag("player_synced");
}
public void Store(Player player, boolean init,boolean isServer) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
public static void Store(Player player, boolean init,boolean isServer) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
String player_uuid = player.getUUID().toString();
//Easy part
int XP = player.totalExperience;
@ -183,7 +183,7 @@ public class VanillaSync {
}else JDBCsetUp.executeUpdate("UPDATE player_data SET inventory = '"+inventoryMap+"',armor='"+equipment+"' ,xp='"+XP+"',effects='"+effectMap+"',enderchest='"+ender_chest+"',score='"+score+"',food_level='"+food_level+"',health='"+health+"',advancements='"+json+"' WHERE uuid = '"+player_uuid+"'");
}
private File[] ScanAdvancementsFile(String player_uuid, File gameDir) {
private static File[] ScanAdvancementsFile(String player_uuid, File gameDir) {
File[] files = new File[JdbcConfig.SYNC_WORLD.get().size()];
for (int i = 0; i < JdbcConfig.SYNC_WORLD.get().size(); i++) {
File advanceFile=new File(gameDir, "saves/"+JdbcConfig.SYNC_WORLD.get().get(i)+"/advancements"+"/"+player_uuid+".json");