diff options
Diffstat (limited to 'common/src')
-rw-r--r-- | common/src/main/java/de/hype/bbsentials/common/client/BBsentials.java | 37 | ||||
-rw-r--r-- | common/src/main/java/de/hype/bbsentials/common/communication/BBsentialConnection.java | 23 |
2 files changed, 32 insertions, 28 deletions
diff --git a/common/src/main/java/de/hype/bbsentials/common/client/BBsentials.java b/common/src/main/java/de/hype/bbsentials/common/client/BBsentials.java index 7d0a1e6..9344c4c 100644 --- a/common/src/main/java/de/hype/bbsentials/common/client/BBsentials.java +++ b/common/src/main/java/de/hype/bbsentials/common/client/BBsentials.java @@ -15,9 +15,9 @@ public class BBsentials { public static Commands coms; public static ScheduledExecutorService executionService = Executors.newScheduledThreadPool(1000); public static boolean splashLobby; + public static SplashStatusUpdateListener splashStatusUpdateListener; private static Thread bbthread; private static boolean initialised = false; - public static SplashStatusUpdateListener splashStatusUpdateListener; public static Config getConfig() { return config; @@ -42,16 +42,18 @@ public class BBsentials { } public static void connectToBBserver(boolean beta) { - if (connection != null) { - connection.sendHiddenMessage("exit"); - connection.close(); - } - connection=null; if (bbthread != null) { - if (bbthread.isAlive()) { + try { bbthread.interrupt(); + connection.messageReceiverThread.interrupt(); + connection.messageSenderThread.interrupt(); + } catch (Exception ignored) { } } + connection = null; + if (connection != null) { + connection.close(); + } bbthread = new Thread(() -> { connection = new BBsentialConnection(); coms = new Commands(); @@ -62,7 +64,6 @@ public class BBsentials { else { connection.connect(config.getBBServerURL(), 5000); } - executionService.scheduleAtFixedRate(EnvironmentCore.debug, 0, 20, TimeUnit.SECONDS); }); bbthread.start(); } @@ -72,17 +73,19 @@ public class BBsentials { */ public static void onServerSwap() { - splashLobby = false; - if (!initialised) { - config = Config.load(); - if (config.doGammaOverride) EnvironmentCore.mcoptions.setGamma(10); - Chat chat = new Chat(); - if (Config.isBingoTime() || config.overrideBingoTime()) { - connectToBBserver(); - } - initialised = true; + splashLobby = false; + if (!initialised) { + config = Config.load(); + executionService.scheduleAtFixedRate(EnvironmentCore.debug, 0, 20, TimeUnit.SECONDS); + if (config.doGammaOverride) EnvironmentCore.mcoptions.setGamma(10); + Chat chat = new Chat(); + if (Config.isBingoTime() || config.overrideBingoTime()) { + connectToBBserver(); } + initialised = true; + } } + public void manualLoad() { initialised = true; config = Config.load(); diff --git a/common/src/main/java/de/hype/bbsentials/common/communication/BBsentialConnection.java b/common/src/main/java/de/hype/bbsentials/common/communication/BBsentialConnection.java index a524fb2..931252c 100644 --- a/common/src/main/java/de/hype/bbsentials/common/communication/BBsentialConnection.java +++ b/common/src/main/java/de/hype/bbsentials/common/communication/BBsentialConnection.java @@ -41,6 +41,8 @@ public class BBsentialConnection { private MessageReceivedCallback messageReceivedCallback; private String itemName = "Hub #0"; private PacketManager packetManager; + public Thread messageReceiverThread; + public Thread messageSenderThread; public BBsentialConnection() { packetManager = new PacketManager(this); @@ -133,13 +135,12 @@ public class BBsentialConnection { socket = sslSocketFactory.createSocket(serverIP, serverPort); socket.setKeepAlive(true); // Enable Keep-Alive - reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); writer = new PrintWriter(socket.getOutputStream(), true); messageQueue = new LinkedBlockingQueue<>(); // Start message receiver thread - Thread messageReceiverThread = new Thread(() -> { + messageReceiverThread = new Thread(() -> { try { while (true) { String message = reader.readLine(); @@ -164,7 +165,7 @@ public class BBsentialConnection { messageReceiverThread.start(); messageReceiverThread.setName("bb receiver thread"); // Start message sender thread - Thread messageSenderThread = new Thread(() -> { + messageSenderThread = new Thread(() -> { try { while (true) { String message = messageQueue.take(); @@ -215,8 +216,6 @@ public class BBsentialConnection { } - //The following onMessageReceived may or may not be modified - // or taken out of order in private/ non official versions of the mod! public void onMessageReceived(String message) { if (!PacketUtils.handleIfPacket(this, message)) { if (message.startsWith("H-")) { @@ -227,8 +226,8 @@ public class BBsentialConnection { } } - public void dummy(Object o) { - //this does absoloutely nothing + public <T extends AbstractPacket> void dummy(T o) { + //this does absolutely nothing. dummy for packet in packt manager } public void splashHighlightItem(String itemName, long displayTimeInMilliseconds) { @@ -256,14 +255,14 @@ public class BBsentialConnection { public <E extends AbstractPacket> void sendPacket(E packet) { String packetName = packet.getClass().getSimpleName(); String rawjson = PacketUtils.parsePacketToJson(packet); - if (BBsentials.getConfig().isDetailedDevModeEnabled() && !(packet.getClass().equals(RequestConnectPacket.class)&&BBsentials.config.devSecurity)) { - Chat.sendPrivateMessageToSelfDebug("BBDev-sP: " + packetName + ": " + rawjson); - } if (socket.isConnected() && writer != null) { + if (BBsentials.getConfig().isDetailedDevModeEnabled() && !(packet.getClass().equals(RequestConnectPacket.class) && BBsentials.config.devSecurity)) { + Chat.sendPrivateMessageToSelfDebug("BBDev-sP: " + packetName + ": " + rawjson); + } writer.println(packetName + "." + rawjson); } else { - Chat.sendPrivateMessageToSelfError("BB: Couldn't send a Packet? did you get disconnected?"); + Chat.sendPrivateMessageToSelfError("BB: Couldn't send a " + packetName + "! did you get disconnected?"); } } @@ -563,6 +562,8 @@ public class BBsentialConnection { public void close() { try { socket.close(); + reader = null; + writer = null; } catch (IOException ignored) { } } |