update sync
This commit is contained in:
parent
3a9bd966aa
commit
a2bee9f1d9
|
|
@ -3,4 +3,4 @@
|
||||||
org.gradle.jvmargs=-Xmx3G
|
org.gradle.jvmargs=-Xmx3G
|
||||||
org.gradle.daemon=false
|
org.gradle.daemon=false
|
||||||
|
|
||||||
mod_version=1.16.5-1.3.0
|
mod_version=1.16.5-1.3.1
|
||||||
|
|
@ -38,7 +38,7 @@ public class PlayerSync
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public void onServerStarting(FMLServerStartingEvent event) throws SQLException {
|
public void onServerStarting(FMLServerStartingEvent event) throws SQLException {
|
||||||
JDBCsetUp.executeUpdate("CREATE DATABASE IF NOT EXISTS `playersync`;",1);
|
JDBCsetUp.executeUpdate("CREATE DATABASE IF NOT EXISTS "+JdbcConfig.DATABASE_NAME.get(),1);
|
||||||
|
|
||||||
JDBCsetUp.executeUpdate("CREATE TABLE IF NOT EXISTS player_data (uuid CHAR(36) NOT NULL," +
|
JDBCsetUp.executeUpdate("CREATE TABLE IF NOT EXISTS player_data (uuid CHAR(36) NOT NULL," +
|
||||||
"inventory MEDIUMBLOB,armor BLOB,advancements BLOB,enderchest MEDIUMBLOB,effects BLOB," +
|
"inventory MEDIUMBLOB,armor BLOB,advancements BLOB,enderchest MEDIUMBLOB,effects BLOB," +
|
||||||
|
|
@ -51,6 +51,7 @@ public class PlayerSync
|
||||||
"VALUES(" + JdbcConfig.SERVER_ID.get() + ",true," + current + ") " +
|
"VALUES(" + JdbcConfig.SERVER_ID.get() + ",true," + current + ") " +
|
||||||
"ON DUPLICATE KEY UPDATE id= " + JdbcConfig.SERVER_ID.get() +",enable = 1," +
|
"ON DUPLICATE KEY UPDATE id= " + JdbcConfig.SERVER_ID.get() +",enable = 1," +
|
||||||
"last_update=" + current + ";");
|
"last_update=" + current + ";");
|
||||||
|
JDBCsetUp.executeUpdate("UPDATE server_info SET enable= 1 WHERE id= "+ JdbcConfig.SERVER_ID.get());
|
||||||
|
|
||||||
if(ModList.get().isLoaded("curios")) {
|
if(ModList.get().isLoaded("curios")) {
|
||||||
JDBCsetUp.executeUpdate("CREATE TABLE IF NOT EXISTS curios (uuid CHAR(36) NOT NULL,curios_item BLOB, PRIMARY KEY (uuid))");
|
JDBCsetUp.executeUpdate("CREATE TABLE IF NOT EXISTS curios (uuid CHAR(36) NOT NULL,curios_item BLOB, PRIMARY KEY (uuid))");
|
||||||
|
|
|
||||||
|
|
@ -14,9 +14,13 @@ public class JdbcConfig {
|
||||||
public static ForgeConfigSpec.IntValue PORT;
|
public static ForgeConfigSpec.IntValue PORT;
|
||||||
public static ForgeConfigSpec.ConfigValue<String> USERNAME;
|
public static ForgeConfigSpec.ConfigValue<String> USERNAME;
|
||||||
public static ForgeConfigSpec.ConfigValue<String> PASSWORD;
|
public static ForgeConfigSpec.ConfigValue<String> PASSWORD;
|
||||||
|
public static ForgeConfigSpec.ConfigValue<String> DATABASE_NAME;
|
||||||
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.ConfigValue<Integer> SERVER_ID;
|
public static ForgeConfigSpec.ConfigValue<Integer> SERVER_ID;
|
||||||
|
|
||||||
|
|
@ -29,9 +33,13 @@ public class JdbcConfig {
|
||||||
USE_SSL = COMMON_BUILDER.comment("whether use SSL").define("use_ssl", false);
|
USE_SSL = COMMON_BUILDER.comment("whether use SSL").define("use_ssl", false);
|
||||||
USERNAME = COMMON_BUILDER.comment("username").define("user_name", "root");
|
USERNAME = COMMON_BUILDER.comment("username").define("user_name", "root");
|
||||||
PASSWORD = COMMON_BUILDER.comment("password").define("password", "password");
|
PASSWORD = COMMON_BUILDER.comment("password").define("password", "password");
|
||||||
SERVER_ID = COMMON_BUILDER.comment("the server id should be unique").define("Server_id", (int) (Math.random()*Integer.MAX_VALUE));
|
DATABASE_NAME = COMMON_BUILDER.comment("database name").define("db_name","playersync");
|
||||||
|
SERVER_ID = COMMON_BUILDER.comment("the server id should be unique").define("Server_id", new Random().nextInt());
|
||||||
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);
|
||||||
COMMON_BUILDER.pop();
|
COMMON_BUILDER.pop();
|
||||||
COMMON_CONFIG = COMMON_BUILDER.build();
|
COMMON_CONFIG = COMMON_BUILDER.build();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,61 +3,124 @@ package vip.fubuki.playersync.sync;
|
||||||
import net.minecraft.server.management.PlayerList;
|
import net.minecraft.server.management.PlayerList;
|
||||||
import net.minecraft.util.text.ChatType;
|
import net.minecraft.util.text.ChatType;
|
||||||
import net.minecraft.util.text.ITextComponent;
|
import net.minecraft.util.text.ITextComponent;
|
||||||
|
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 net.minecraftforge.fml.common.Mod;
|
import vip.fubuki.playersync.config.JdbcConfig;
|
||||||
import vip.fubuki.playersync.util.JDBCsetUp;
|
|
||||||
|
|
||||||
import java.sql.ResultSet;
|
import java.io.IOException;
|
||||||
import java.sql.SQLException;
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
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;
|
||||||
|
|
||||||
@Mod.EventBusSubscriber
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeEvent
|
|
||||||
public static void onPlayerChat(net.minecraftforge.event.ServerChatEvent event) throws SQLException {
|
|
||||||
JDBCsetUp.executeUpdate("INSERT INTO chat (player, message, timestamp) VALUES ('" + event.getUsername() + "', '" + event.getMessage() + "', '" + current + "');");
|
|
||||||
}
|
|
||||||
|
|
||||||
@SubscribeEvent
|
private static void ServerSocket() {
|
||||||
public static void Tick(net.minecraftforge.event.TickEvent.ServerTickEvent event) throws SQLException {
|
try {
|
||||||
tick++;
|
serverSocket = new ServerSocket(JdbcConfig.CHAT_SERVER_PORT.get());
|
||||||
if(tick == 20) {
|
while (true) {
|
||||||
ReadMessage(playerList);
|
Socket newSocket = serverSocket.accept();
|
||||||
|
SocketList.add(newSocket);
|
||||||
|
executorService.submit(() -> handleClient(newSocket));
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
serverSocket.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void handleClient(Socket socket) {
|
||||||
|
try (InputStream inputStream = socket.getInputStream()) {
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
int bytesRead;
|
||||||
|
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();
|
||||||
|
playerList.broadcastMessage(ITextComponent.nullToEmpty(line), 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.getEntity().getServer()).getPlayerList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
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.getEntity().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");
|
|
||||||
ITextComponent textComponents = ITextComponent.nullToEmpty(player+": "+message);
|
|
||||||
playerList.broadcastMessage(textComponents, ChatType.CHAT, UUID.nameUUIDFromBytes(player.getBytes()));
|
|
||||||
}
|
|
||||||
resultSet.close();
|
|
||||||
queryResult.getConnection().close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -67,9 +67,9 @@ public class ModsSupport {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if(init) {
|
if(init) {
|
||||||
JDBCsetUp.executeUpdate("INSERT INTO curios (uuid,curios_item) VALUES ('"+player.getUUID()+"','"+ curios+"');");
|
JDBCsetUp.executeUpdate("INSERT INTO curios (uuid,curios_item) VALUES ('"+player.getUUID()+"','"+ curios+"')");
|
||||||
} else {
|
} else {
|
||||||
JDBCsetUp.executeUpdate("UPDATE curios SET curios_item = '"+ curios+"' WHERE uuid = '"+player.getUUID()+"';");
|
JDBCsetUp.executeUpdate("UPDATE curios SET curios_item = '"+ curios+"' WHERE uuid = '"+player.getUUID()+"'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -64,15 +64,15 @@ public class VanillaSync {
|
||||||
serverPlayer.connection.disconnect(new StringTextComponent("playersync.already_online"));
|
serverPlayer.connection.disconnect(new StringTextComponent("playersync.already_online"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
JDBCsetUp.executeUpdate("UPDATE server_info SET enable=false WHERE id=" + lastServer+";");
|
JDBCsetUp.executeUpdate("UPDATE server_info SET enable= '0' WHERE id=" + lastServer);
|
||||||
}
|
}
|
||||||
|
|
||||||
getServerInfo.close();
|
getServerInfo.close();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
JDBCsetUp.executeUpdate("UPDATE server_info SET last_update=" + System.currentTimeMillis() + " WHERE id=" + JdbcConfig.SERVER_ID.get()+";");
|
JDBCsetUp.executeUpdate("UPDATE server_info SET last_update=" + System.currentTimeMillis() + " WHERE id=" + JdbcConfig.SERVER_ID.get());
|
||||||
JDBCsetUp.executeUpdate("UPDATE player_data SET online=true,last_server=" + JdbcConfig.SERVER_ID.get() + " WHERE uuid='"+player_uuid+"';");
|
JDBCsetUp.executeUpdate("UPDATE player_data SET online= '1',last_server=" + JdbcConfig.SERVER_ID.get() + " WHERE uuid='"+player_uuid+"'");
|
||||||
if(resultSet.next()) {
|
if(resultSet.next()) {
|
||||||
//Easy Part
|
//Easy Part
|
||||||
serverPlayer.setHealth(resultSet.getInt("health"));
|
serverPlayer.setHealth(resultSet.getInt("health"));
|
||||||
|
|
|
||||||
|
|
@ -13,32 +13,48 @@ public class JDBCsetUp {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static QueryResult executeQuery(String sql) throws SQLException{
|
public static QueryResult executeQuery(String sql) throws SQLException{
|
||||||
Connection connection = getConnection();
|
Connection connection = getConnection();
|
||||||
PreparedStatement useStatement = connection.prepareStatement("USE `playersync`");
|
|
||||||
useStatement.executeUpdate();
|
|
||||||
|
|
||||||
|
try (Statement useStatement = connection.createStatement()) {
|
||||||
|
useStatement.execute("USE " + JdbcConfig.DATABASE_NAME.get());
|
||||||
|
}
|
||||||
PreparedStatement queryStatement = connection.prepareStatement(sql);
|
PreparedStatement queryStatement = connection.prepareStatement(sql);
|
||||||
ResultSet resultSet = queryStatement.executeQuery();
|
ResultSet resultSet = queryStatement.executeQuery();
|
||||||
return new QueryResult(connection,resultSet);
|
return new QueryResult(connection,resultSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int executeUpdate(String sql) throws SQLException{
|
public static void executeUpdate(String sql) throws SQLException{
|
||||||
try (Connection connection = getConnection()) {
|
try (Connection connection = getConnection()) {
|
||||||
|
|
||||||
PreparedStatement useStatement = connection.prepareStatement("USE `playersync`");
|
try (Statement useStatement = connection.createStatement()) {
|
||||||
useStatement.executeUpdate();
|
useStatement.execute("USE " + JdbcConfig.DATABASE_NAME.get());
|
||||||
|
}
|
||||||
|
|
||||||
try (PreparedStatement updateStatement = connection.prepareStatement(sql)) {
|
try (PreparedStatement updateStatement = connection.prepareStatement(sql)) {
|
||||||
return updateStatement.executeUpdate();
|
updateStatement.executeUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int executeUpdate(String sql,int i) throws SQLException{
|
public static void Update(String sql, String... argument) throws SQLException{
|
||||||
|
Connection connection = getConnection();
|
||||||
|
|
||||||
|
try (Statement useStatement = connection.createStatement()) {
|
||||||
|
useStatement.execute("USE " + JdbcConfig.DATABASE_NAME.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
PreparedStatement updateStatement = connection.prepareStatement(sql);
|
||||||
|
for (int i = 1; i <= argument.length; i++) {
|
||||||
|
updateStatement.setString(i,argument[i]);
|
||||||
|
}
|
||||||
|
updateStatement.executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void executeUpdate(String sql, int i) throws SQLException{
|
||||||
try (Connection connection = getConnection()) {
|
try (Connection connection = getConnection()) {
|
||||||
|
|
||||||
try (PreparedStatement updateStatement = connection.prepareStatement(sql)) {
|
try (PreparedStatement updateStatement = connection.prepareStatement(sql)) {
|
||||||
return updateStatement.executeUpdate();
|
updateStatement.executeUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user