Merge pull request #78 from mlus-asuka/1.20.4-dev
Full tested ChatSync Feature
This commit is contained in:
commit
4bdad9f625
|
|
@ -34,7 +34,7 @@ mod_name=PlayerSync
|
||||||
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
|
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
|
||||||
mod_license=GPL-3.0 license
|
mod_license=GPL-3.0 license
|
||||||
# The mod version. See https://semver.org/
|
# The mod version. See https://semver.org/
|
||||||
mod_version=2.1.1
|
mod_version=2.1.2
|
||||||
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
|
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
|
||||||
# This should match the base package used for the mod sources.
|
# This should match the base package used for the mod sources.
|
||||||
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
|
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
|
||||||
|
|
|
||||||
28
src/main/java/vip/fubuki/playersync/CommandInit.java
Normal file
28
src/main/java/vip/fubuki/playersync/CommandInit.java
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
package vip.fubuki.playersync;
|
||||||
|
|
||||||
|
import com.mojang.brigadier.CommandDispatcher;
|
||||||
|
import net.minecraft.commands.CommandSourceStack;
|
||||||
|
import net.minecraft.commands.Commands;
|
||||||
|
import net.neoforged.bus.api.SubscribeEvent;
|
||||||
|
import net.neoforged.fml.common.Mod;
|
||||||
|
import net.neoforged.neoforge.event.RegisterCommandsEvent;
|
||||||
|
import vip.fubuki.playersync.sync.chat.ChatSyncClient;
|
||||||
|
|
||||||
|
@Mod.EventBusSubscriber()
|
||||||
|
public class CommandInit {
|
||||||
|
|
||||||
|
@SubscribeEvent
|
||||||
|
public static void registerCommand(RegisterCommandsEvent event){
|
||||||
|
CommandDispatcher<CommandSourceStack> dispatcher=event.getDispatcher();
|
||||||
|
dispatcher.register(Commands.literal("playersync")
|
||||||
|
.requires(cs->cs.hasPermission(2))
|
||||||
|
.then(Commands.literal("reconnect")
|
||||||
|
.executes(context -> {
|
||||||
|
new ChatSyncClient().run();
|
||||||
|
// context.getSource().sendSuccess(()->MutableComponent.create(new TranslatableContents("playersync.command.reconnect")),true);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,140 +1,38 @@
|
||||||
package vip.fubuki.playersync.sync;
|
package vip.fubuki.playersync.sync;
|
||||||
|
|
||||||
import net.minecraft.network.chat.Component;
|
import com.mojang.logging.LogUtils;
|
||||||
import net.minecraft.server.players.PlayerList;
|
|
||||||
import net.neoforged.bus.api.SubscribeEvent;
|
|
||||||
import net.neoforged.neoforge.common.NeoForge;
|
import net.neoforged.neoforge.common.NeoForge;
|
||||||
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
|
import org.slf4j.Logger;
|
||||||
import vip.fubuki.playersync.config.JdbcConfig;
|
import vip.fubuki.playersync.config.JdbcConfig;
|
||||||
|
import vip.fubuki.playersync.sync.chat.ChatSyncClient;
|
||||||
|
import vip.fubuki.playersync.sync.chat.ChatSyncServer;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.net.ServerSocket;
|
|
||||||
import java.net.Socket;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Scanner;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
|
|
||||||
import com.mojang.logging.LogUtils;
|
|
||||||
|
|
||||||
public class ChatSync {
|
public class ChatSync {
|
||||||
private static final Logger LOGGER = LogUtils.getLogger();
|
public static final Logger LOGGER = LogUtils.getLogger();
|
||||||
|
|
||||||
static PlayerList playerList;
|
|
||||||
|
|
||||||
static ServerSocket serverSocket;
|
|
||||||
static Socket clientSocket;
|
|
||||||
static Set<Socket> SocketList = ConcurrentHashMap.newKeySet();
|
|
||||||
static ExecutorService executorService = Executors.newCachedThreadPool();
|
|
||||||
|
|
||||||
public static void register(){
|
public static void register(){
|
||||||
if(JdbcConfig.IS_CHAT_SERVER.get()) {
|
if(JdbcConfig.IS_CHAT_SERVER.get()) {
|
||||||
LOGGER.info("Launching chat server thread.");
|
|
||||||
new Thread(ChatSync::ServerSocket).start();
|
|
||||||
}
|
|
||||||
ClientSocket();
|
|
||||||
NeoForge.EVENT_BUS.register(ChatSync.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private static void ServerSocket() {
|
|
||||||
try {
|
|
||||||
LOGGER.info("Trying to setup chat server at port " + JdbcConfig.CHAT_SERVER_PORT.get());
|
LOGGER.info("Trying to setup chat server at port " + JdbcConfig.CHAT_SERVER_PORT.get());
|
||||||
serverSocket = new ServerSocket(JdbcConfig.CHAT_SERVER_PORT.get());
|
new Thread(()->{
|
||||||
while (true) {
|
ChatSyncServer chatSyncServer = new ChatSyncServer();
|
||||||
Socket newSocket = serverSocket.accept();
|
|
||||||
SocketList.add(newSocket);
|
|
||||||
executorService.submit(() -> handleClient(newSocket));
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOGGER.error("Unable to start chat server");
|
|
||||||
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 {
|
try {
|
||||||
OutputStream outputStream = socket.getOutputStream();
|
chatSyncServer.run();
|
||||||
outputStream.write(message.getBytes());
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
LOGGER.error("Unable to start chat server", e);
|
||||||
}
|
}
|
||||||
}
|
}).start();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private static void ClientSocket() {
|
new Thread(()->{
|
||||||
try {
|
|
||||||
LOGGER.info("Trying to connect to chat server "
|
LOGGER.info("Trying to connect to chat server "
|
||||||
+ JdbcConfig.CHAT_SERVER_IP.get()
|
+ JdbcConfig.CHAT_SERVER_IP.get()
|
||||||
+ ":"
|
+ ":"
|
||||||
+ JdbcConfig.CHAT_SERVER_PORT.get());
|
+ JdbcConfig.CHAT_SERVER_PORT.get());
|
||||||
clientSocket = new Socket(JdbcConfig.CHAT_SERVER_IP.get(), JdbcConfig.CHAT_SERVER_PORT.get());
|
ChatSyncClient chatSyncClient = new ChatSyncClient();
|
||||||
Scanner scanner = new Scanner(clientSocket.getInputStream());
|
chatSyncClient.run();
|
||||||
while (scanner.hasNextLine()) {
|
}).start();
|
||||||
String line = scanner.nextLine();
|
NeoForge.EVENT_BUS.register(ChatSyncClient.class);
|
||||||
Component textComponents = Component.nullToEmpty(line);
|
|
||||||
playerList.broadcastSystemMessage(textComponents,true);
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
reconnectClient();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void reconnectClient() {
|
|
||||||
LOGGER.warn("TODO: implement reconnectClient()");
|
|
||||||
//TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
@SubscribeEvent
|
|
||||||
public static void onPlayerChat(net.neoforged.neoforge.event.ServerChatEvent event) throws IOException {
|
|
||||||
String message= event.getUsername()+":"+event.getMessage();
|
|
||||||
OutputStream outputStream = clientSocket.getOutputStream();
|
|
||||||
outputStream.write(message.getBytes());
|
|
||||||
}
|
|
||||||
|
|
||||||
@SubscribeEvent
|
|
||||||
public static void onPlayerJoin(PlayerEvent.PlayerLoggedInEvent event){
|
|
||||||
playerList= Objects.requireNonNull(event.getEntity().getServer()).getPlayerList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@SubscribeEvent
|
|
||||||
public static void onPlayerLeave(PlayerEvent.PlayerLoggedOutEvent event){
|
|
||||||
playerList= Objects.requireNonNull(event.getEntity().getServer()).getPlayerList();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,9 @@
|
||||||
package vip.fubuki.playersync.sync;
|
package vip.fubuki.playersync.sync;
|
||||||
|
|
||||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||||
|
|
||||||
import net.minecraft.ChatFormatting;
|
import net.minecraft.ChatFormatting;
|
||||||
import net.minecraft.nbt.CompoundTag;
|
import net.minecraft.core.registries.BuiltInRegistries;
|
||||||
import net.minecraft.nbt.ListTag;
|
import net.minecraft.nbt.*;
|
||||||
import net.minecraft.nbt.NbtUtils;
|
|
||||||
import net.minecraft.nbt.StringTag;
|
|
||||||
import net.minecraft.nbt.Tag;
|
|
||||||
import net.minecraft.network.chat.Component;
|
import net.minecraft.network.chat.Component;
|
||||||
import net.minecraft.network.chat.Style;
|
import net.minecraft.network.chat.Style;
|
||||||
import net.minecraft.resources.ResourceLocation;
|
import net.minecraft.resources.ResourceLocation;
|
||||||
|
|
@ -29,7 +25,6 @@ import net.neoforged.neoforge.event.OnDatapackSyncEvent;
|
||||||
import net.neoforged.neoforge.event.TickEvent;
|
import net.neoforged.neoforge.event.TickEvent;
|
||||||
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
|
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
|
||||||
import net.neoforged.neoforge.event.server.ServerStoppedEvent;
|
import net.neoforged.neoforge.event.server.ServerStoppedEvent;
|
||||||
import net.minecraft.core.registries.BuiltInRegistries;
|
|
||||||
import net.neoforged.neoforge.server.ServerLifecycleHooks;
|
import net.neoforged.neoforge.server.ServerLifecycleHooks;
|
||||||
import vip.fubuki.playersync.PlayerSync;
|
import vip.fubuki.playersync.PlayerSync;
|
||||||
import vip.fubuki.playersync.config.JdbcConfig;
|
import vip.fubuki.playersync.config.JdbcConfig;
|
||||||
|
|
@ -43,11 +38,7 @@ import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Base64;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,66 @@
|
||||||
|
package vip.fubuki.playersync.sync.chat;
|
||||||
|
|
||||||
|
import net.minecraft.network.chat.Component;
|
||||||
|
import net.minecraft.server.players.PlayerList;
|
||||||
|
import net.neoforged.bus.api.SubscribeEvent;
|
||||||
|
import net.neoforged.neoforge.event.ServerChatEvent;
|
||||||
|
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
|
||||||
|
import vip.fubuki.playersync.PlayerSync;
|
||||||
|
import vip.fubuki.playersync.config.JdbcConfig;
|
||||||
|
import vip.fubuki.playersync.sync.ChatSync;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class ChatSyncClient {
|
||||||
|
static PlayerList playerList;
|
||||||
|
static Socket clientSocket;
|
||||||
|
static PrintWriter out;
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
clientSocket = new Socket(JdbcConfig.CHAT_SERVER_IP.get(), JdbcConfig.CHAT_SERVER_PORT.get());
|
||||||
|
out = new PrintWriter(clientSocket.getOutputStream(),true);
|
||||||
|
|
||||||
|
BufferedReader in = new BufferedReader(
|
||||||
|
new InputStreamReader(clientSocket.getInputStream()));
|
||||||
|
|
||||||
|
String serverMessage;
|
||||||
|
while ((serverMessage = in.readLine()) != null) {
|
||||||
|
PlayerSync.LOGGER.info("Received message from chat server: " + serverMessage);
|
||||||
|
Component textComponents = Component.nullToEmpty(serverMessage);
|
||||||
|
playerList.broadcastSystemMessage(textComponents,false);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
reconnectClient();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reconnectClient() {
|
||||||
|
ChatSync.LOGGER.warn("TODO: implement reconnectClient()");
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
@SubscribeEvent
|
||||||
|
public static void onPlayerChat(ServerChatEvent event) {
|
||||||
|
String message= "<"+event.getUsername()+"> "+event.getMessage().getString();
|
||||||
|
if (out != null) {
|
||||||
|
out.println(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SubscribeEvent
|
||||||
|
public static void onPlayerJoin(PlayerEvent.PlayerLoggedInEvent event){
|
||||||
|
playerList = Objects.requireNonNull(event.getEntity().getServer()).getPlayerList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@SubscribeEvent
|
||||||
|
public static void onPlayerLeave(PlayerEvent.PlayerLoggedOutEvent event){
|
||||||
|
playerList = Objects.requireNonNull(event.getEntity().getServer()).getPlayerList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
package vip.fubuki.playersync.sync.chat;
|
||||||
|
|
||||||
|
import vip.fubuki.playersync.config.JdbcConfig;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
public class ChatSyncServer {
|
||||||
|
static ServerSocket serverSocket;
|
||||||
|
static final Set<Socket> SocketList = ConcurrentHashMap.newKeySet();
|
||||||
|
static final ExecutorService executorService = Executors.newCachedThreadPool();
|
||||||
|
|
||||||
|
public void run() throws IOException {
|
||||||
|
serverSocket = new ServerSocket(JdbcConfig.CHAT_SERVER_PORT.get());
|
||||||
|
while (!Thread.currentThread().isInterrupted()) {
|
||||||
|
Socket newSocket = serverSocket.accept();
|
||||||
|
SocketList.add(newSocket);
|
||||||
|
executorService.submit(() -> handleClient(newSocket));
|
||||||
|
}
|
||||||
|
serverSocket.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private 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 void broadcastMessage(Socket sender, String message) {
|
||||||
|
for (Socket socket : SocketList) {
|
||||||
|
if (!socket.equals(sender)) {
|
||||||
|
try {
|
||||||
|
OutputStream outputStream = socket.getOutputStream();
|
||||||
|
outputStream.write(message.getBytes());
|
||||||
|
outputStream.flush();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user