new method to sync chat
This commit is contained in:
parent
e75a793abd
commit
fcfd299807
|
|
@ -18,6 +18,10 @@ public class JdbcConfig {
|
||||||
public static ForgeConfigSpec.ConfigValue<List<String>> SYNC_WORLD;
|
public static ForgeConfigSpec.ConfigValue<List<String>> SYNC_WORLD;
|
||||||
public static ForgeConfigSpec.BooleanValue USE_SSL;
|
public static ForgeConfigSpec.BooleanValue USE_SSL;
|
||||||
public static ForgeConfigSpec.BooleanValue SYNC_CHAT;
|
public static ForgeConfigSpec.BooleanValue SYNC_CHAT;
|
||||||
|
public static ForgeConfigSpec.BooleanValue IS_CHAT_SERVER;
|
||||||
|
public static ForgeConfigSpec.ConfigValue<String> CHAT_SERVER_IP;
|
||||||
|
public static ForgeConfigSpec.IntValue CHAT_SERVER_PORT;
|
||||||
|
public static ForgeConfigSpec.IntValue CHAT_CLIENT_PORT;
|
||||||
|
|
||||||
public static ForgeConfigSpec.ConfigValue<Integer> SERVER_ID;
|
public static ForgeConfigSpec.ConfigValue<Integer> SERVER_ID;
|
||||||
|
|
||||||
|
|
@ -34,6 +38,10 @@ public class JdbcConfig {
|
||||||
SERVER_ID = COMMON_BUILDER.comment("the server id should be unique").define("Server_id", new Random().nextInt(1,Integer.MAX_VALUE-1));
|
SERVER_ID = COMMON_BUILDER.comment("the server id should be unique").define("Server_id", new Random().nextInt(1,Integer.MAX_VALUE-1));
|
||||||
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<>());
|
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<>());
|
||||||
SYNC_CHAT= COMMON_BUILDER.comment("Whether synchronize chat").define("sync_chat", true);
|
SYNC_CHAT= COMMON_BUILDER.comment("Whether synchronize chat").define("sync_chat", true);
|
||||||
|
IS_CHAT_SERVER = COMMON_BUILDER.comment("Whether recieve messages from other servers as host").define("IsChatServer",false);
|
||||||
|
CHAT_SERVER_IP = COMMON_BUILDER.define("ChatServerIP","127.0.0.1");
|
||||||
|
CHAT_SERVER_PORT = COMMON_BUILDER.defineInRange("ChatServerPort",7900,0,65535);
|
||||||
|
CHAT_CLIENT_PORT = COMMON_BUILDER.defineInRange("ChatClientPort",7980,0,655535);
|
||||||
COMMON_BUILDER.pop();
|
COMMON_BUILDER.pop();
|
||||||
COMMON_CONFIG = COMMON_BUILDER.build();
|
COMMON_CONFIG = COMMON_BUILDER.build();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,45 +6,115 @@ import net.minecraft.server.players.PlayerList;
|
||||||
import net.minecraftforge.common.MinecraftForge;
|
import net.minecraftforge.common.MinecraftForge;
|
||||||
import net.minecraftforge.event.entity.player.PlayerEvent;
|
import net.minecraftforge.event.entity.player.PlayerEvent;
|
||||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||||
import vip.fubuki.playersync.util.JDBCsetUp;
|
import vip.fubuki.playersync.config.JdbcConfig;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.io.IOException;
|
||||||
import java.sql.PreparedStatement;
|
import java.io.InputStream;
|
||||||
import java.sql.ResultSet;
|
import java.io.OutputStream;
|
||||||
import java.sql.SQLException;
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
public class ChatSync {
|
public class ChatSync {
|
||||||
static int tick = 0;
|
|
||||||
static long current = System.currentTimeMillis();
|
|
||||||
|
|
||||||
static PlayerList playerList;
|
static PlayerList playerList;
|
||||||
|
|
||||||
|
static ServerSocket serverSocket;
|
||||||
|
static Socket clientSocket;
|
||||||
|
static Set<Socket> SocketList;
|
||||||
|
static ExecutorService executorService = Executors.newCachedThreadPool();
|
||||||
|
|
||||||
public static void register(){
|
public static void register(){
|
||||||
|
if(JdbcConfig.IS_CHAT_SERVER.get())
|
||||||
|
new Thread(ChatSync::ServerSocket).start();
|
||||||
|
ClientSocket();
|
||||||
MinecraftForge.EVENT_BUS.register(ChatSync.class);
|
MinecraftForge.EVENT_BUS.register(ChatSync.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeEvent
|
|
||||||
public static void onPlayerChat(net.minecraftforge.event.ServerChatEvent event) throws SQLException {
|
private static void ServerSocket() {
|
||||||
String sql = "INSERT INTO chat (player, message, timestamp) VALUES (?, ?, ?)";
|
try {
|
||||||
try (Connection connection = JDBCsetUp.getConnection();
|
serverSocket = new ServerSocket(JdbcConfig.CHAT_SERVER_PORT.get());
|
||||||
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
|
while (true) {
|
||||||
preparedStatement.setString(1, event.getUsername());
|
Socket newSocket = serverSocket.accept();
|
||||||
preparedStatement.setString(2, event.getMessage());
|
SocketList.add(newSocket);
|
||||||
preparedStatement.setLong(3, current);
|
executorService.submit(() -> handleClient(newSocket));
|
||||||
preparedStatement.executeUpdate();
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
serverSocket.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeEvent
|
private static void handleClient(Socket socket) {
|
||||||
public static void Tick(net.minecraftforge.event.TickEvent.ServerTickEvent event) throws SQLException {
|
try (InputStream inputStream = socket.getInputStream()) {
|
||||||
tick++;
|
byte[] buffer = new byte[1024];
|
||||||
if(tick == 20) {
|
int bytesRead;
|
||||||
ReadMessage(playerList);
|
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||||
|
String message = new String(buffer, 0, bytesRead);
|
||||||
|
broadcastMessage(socket, message);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
SocketList.remove(socket);
|
||||||
|
try {
|
||||||
|
socket.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void broadcastMessage(Socket sender, String message) {
|
||||||
|
for (Socket socket : SocketList) {
|
||||||
|
if (!socket.equals(sender)) {
|
||||||
|
try {
|
||||||
|
OutputStream outputStream = socket.getOutputStream();
|
||||||
|
outputStream.write(message.getBytes());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ClientSocket() {
|
||||||
|
try {
|
||||||
|
clientSocket = new Socket(JdbcConfig.CHAT_SERVER_IP.get(), JdbcConfig.CHAT_SERVER_PORT.get());
|
||||||
|
Scanner scanner = new Scanner(clientSocket.getInputStream());
|
||||||
|
while (scanner.hasNextLine()) {
|
||||||
|
String line = scanner.nextLine();
|
||||||
|
Component textComponents = Component.nullToEmpty(line);
|
||||||
|
playerList.broadcastMessage(textComponents, ChatType.CHAT, UUID.randomUUID());
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
reconnectClient();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void reconnectClient() {
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
@SubscribeEvent
|
||||||
|
public static void onPlayerChat(net.minecraftforge.event.ServerChatEvent event) throws IOException {
|
||||||
|
String message= event.getUsername()+":"+event.getMessage();
|
||||||
|
OutputStream outputStream = clientSocket.getOutputStream();
|
||||||
|
outputStream.write(message.getBytes());
|
||||||
|
}
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public static void onPlayerJoin(PlayerEvent.PlayerLoggedInEvent event){
|
public static void onPlayerJoin(PlayerEvent.PlayerLoggedInEvent event){
|
||||||
playerList= Objects.requireNonNull(event.getPlayer().getServer()).getPlayerList();
|
playerList= Objects.requireNonNull(event.getPlayer().getServer()).getPlayerList();
|
||||||
|
|
@ -54,19 +124,4 @@ public class ChatSync {
|
||||||
public static void onPlayerLeave(PlayerEvent.PlayerLoggedOutEvent event){
|
public static void onPlayerLeave(PlayerEvent.PlayerLoggedOutEvent event){
|
||||||
playerList= Objects.requireNonNull(event.getPlayer().getServer()).getPlayerList();
|
playerList= Objects.requireNonNull(event.getPlayer().getServer()).getPlayerList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ReadMessage(PlayerList playerList) throws SQLException {
|
|
||||||
JDBCsetUp.QueryResult queryResult=JDBCsetUp.executeQuery("SELECT * FROM chat WHERE timestamp > " + current);
|
|
||||||
ResultSet resultSet= queryResult.getResultSet();
|
|
||||||
current = System.currentTimeMillis();
|
|
||||||
tick = 0;
|
|
||||||
while(resultSet.next()) {
|
|
||||||
String player = resultSet.getString("player");
|
|
||||||
String message = resultSet.getString("message");
|
|
||||||
Component textComponents = Component.nullToEmpty(player+": "+message);
|
|
||||||
playerList.broadcastMessage(textComponents, ChatType.CHAT, UUID.nameUUIDFromBytes(player.getBytes()));
|
|
||||||
}
|
|
||||||
resultSet.close();
|
|
||||||
queryResult.getConnection().close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user