diff options
3 files changed, 51 insertions, 31 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 00112ed..2c9a603 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 @@ -16,9 +16,9 @@ public class BBsentials { 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 Thread bbthread; public static Chat chat = new Chat(); + private static boolean initialised = false; public static Config getConfig() { return config; @@ -56,7 +56,6 @@ public class BBsentials { else { connection.connect(config.getBBServerURL(), 5000); } - connection.setMessageReceivedCallback(message -> executionService.execute(() -> connection.onMessageReceived(message))); }); bbthread.start(); }); 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 0bc1978..56aa052 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 @@ -33,16 +33,17 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import static de.hype.bbsentials.common.client.BBsentials.bbthread; + public class BBsentialConnection { + public Thread messageReceiverThread; + public Thread messageSenderThread; private Socket socket; private BufferedReader reader; private PrintWriter writer; private LinkedBlockingQueue<String> messageQueue; - private MessageReceivedCallback messageReceivedCallback; private String itemName = "Hub #0"; private PacketManager packetManager; - public Thread messageReceiverThread; - public Thread messageSenderThread; public BBsentialConnection() { packetManager = new PacketManager(this); @@ -142,12 +143,10 @@ public class BBsentialConnection { // Start message receiver thread messageReceiverThread = new Thread(() -> { try { - while (true) { + while (!Thread.interrupted()) { String message = reader.readLine(); if (message != null) { - if (messageReceivedCallback != null) { - messageReceivedCallback.onMessageReceived(message); - } + onMessageReceived(message); // else { // Chat.sendPrivateMessageToSelfError("BB: It seemed like you disconnected. Reconnecting..."); // BBsentials.connectToBBserver(); @@ -167,9 +166,10 @@ public class BBsentialConnection { // Start message sender thread messageSenderThread = new Thread(() -> { try { - while (true) { + while (!Thread.interrupted()) { String message = messageQueue.take(); - if (BBsentials.config.isDetailedDevModeEnabled()) Chat.sendPrivateMessageToSelfDebug("BBs: "+message); + if (BBsentials.config.isDetailedDevModeEnabled()) + Chat.sendPrivateMessageToSelfDebug("BBs: " + message); writer.println(message); } } catch (InterruptedException | NullPointerException ignored) { @@ -178,14 +178,11 @@ public class BBsentialConnection { messageSenderThread.start(); messageSenderThread.setName("bb sender thread"); - } catch (IOException | NoSuchAlgorithmException | - KeyManagementException e) { + } catch (IOException | NoSuchAlgorithmException | KeyManagementException e) { e.printStackTrace(); - } catch ( - CertificateException e) { + } catch (CertificateException e) { throw new RuntimeException(e); } - } public void sendMessage(String message) { @@ -205,7 +202,8 @@ public class BBsentialConnection { } try { if (socket.isConnected() && writer != null) { - if (BBsentials.config.isDetailedDevModeEnabled()) Chat.sendPrivateMessageToSelfDebug("BBHs: "+message); + if (BBsentials.config.isDetailedDevModeEnabled()) + Chat.sendPrivateMessageToSelfDebug("BBHs: " + message); writer.println(message); } } catch (NullPointerException ignored) { @@ -246,9 +244,6 @@ public class BBsentialConnection { } //TODO search - public void setMessageReceivedCallback(MessageReceivedCallback callback) { - this.messageReceivedCallback = callback; - } public <E extends AbstractPacket> void sendPacket(E packet) { String packetName = packet.getClass().getSimpleName(); @@ -559,13 +554,34 @@ public class BBsentialConnection { public void close() { try { - Thread.currentThread().interrupt(); - messageReceiverThread.interrupt(); - messageSenderThread.interrupt(); - messageQueue.clear(); - socket.close(); + if (messageReceiverThread != null) { + messageReceiverThread.interrupt(); + } + if (messageSenderThread != null) { + messageSenderThread.interrupt(); + } + if (bbthread != null) { + bbthread.interrupt(); + } writer.close(); reader.close(); + socket.close(); + messageQueue.clear(); + if (bbthread != null) { + bbthread.join(); + bbthread = null; + } + if (messageSenderThread != null) { + messageSenderThread.join(); + messageSenderThread = null; + } + if (messageReceiverThread != null) { + messageReceiverThread.join(); + messageReceiverThread = null; + } + writer = null; + reader = null; + socket = null; } catch (Exception e) { Chat.sendPrivateMessageToSelfError(e.getMessage()); e.printStackTrace(); diff --git a/common/src/main/java/de/hype/bbsentials/common/packets/PacketManager.java b/common/src/main/java/de/hype/bbsentials/common/packets/PacketManager.java index 8d8eaec..2cc672d 100644 --- a/common/src/main/java/de/hype/bbsentials/common/packets/PacketManager.java +++ b/common/src/main/java/de/hype/bbsentials/common/packets/PacketManager.java @@ -7,7 +7,9 @@ import java.util.ArrayList; import java.util.List; public class PacketManager { - private static List<Packet<? extends AbstractPacket>> packets = new ArrayList<>(); + + private static PacketManager lastPacketManager = null; + List<Packet<? extends AbstractPacket>> packets = new ArrayList<>(); public List<Packet<? extends AbstractPacket>> getPackets() { return packets; @@ -20,9 +22,10 @@ public class PacketManager { public PacketManager(BBsentialConnection connection) { this.connection = connection; initializePacketActions(connection); + lastPacketManager = this; } - public static void initializePacketActions(BBsentialConnection connection) { + public void initializePacketActions(BBsentialConnection connection) { packets.add(new Packet<>(BingoChatMessagePacket.class, connection::onBingoChatMessagePacket)); packets.add(new Packet<>(BroadcastMessagePacket.class, connection::onBroadcastMessagePacket)); packets.add(new Packet<>(ChChestPacket.class, connection::onChChestPacket)); @@ -44,10 +47,12 @@ public class PacketManager { // method to get a list of all packets public static List<Class<? extends AbstractPacket>> getAllPacketClasses() { - initializePacketActions(null); + if (lastPacketManager == null) { + lastPacketManager = new PacketManager(null); + } List<Class<? extends AbstractPacket>> allPackets = new ArrayList<>(); - for (int i = 0; i < allPackets.size(); i++) { - allPackets.add(packets.get(i).getClazz()); + for (int i = 0; i < lastPacketManager.packets.size(); i++) { + allPackets.add(lastPacketManager.packets.get(i).getClazz()); } return allPackets; } |