Skip to content
This repository was archived by the owner on Dec 5, 2020. It is now read-only.

Commit 6951b54

Browse files
committed
Fixed the reattach firing on shutdown and reload
1 parent 95a3655 commit 6951b54

File tree

3 files changed

+48
-30
lines changed

3 files changed

+48
-30
lines changed

src/club/moddedminecraft/polychat/bukkitclient/BukkitClient.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public final class BukkitClient extends JavaPlugin implements Listener{
2828
public static ActivePlayerThread playerThread;
2929
public static String idJson = null;
3030
public static String idJsonNoColor = null;
31+
public static boolean reattachKill = false;
3132
public static String serverIdText = null;
3233
public static ArrayList<String> commands = new ArrayList<>();
3334

@@ -63,17 +64,23 @@ public void onEnable() {
6364
//TODO: check if the folder exists
6465
handleConfiguration(this.getDataFolder());
6566
handlePrefix();
66-
reattachThread = new ReattachThread(5000);
6767

68+
reattachThread = new ReattachThread(5000);
6869
playerThread = new ActivePlayerThread(30000, properties.getProperty("server_id", "DEFAULT_ID"));
70+
6971
handleClientConnection();
70-
72+
7173
new EventListener(this);
7274

7375
Runtime.getRuntime().addShutdownHook(new Thread(this::shutdownHook));
74-
ServerInfoMessage infoMessage = new ServerInfoMessage(BukkitClient.properties.getProperty("server_id", "DEFAULT_ID"),
76+
77+
ServerInfoMessage infoMessage = new ServerInfoMessage(
78+
BukkitClient.properties.getProperty("server_id", "DEFAULT_ID"),
7579
BukkitClient.properties.getProperty("server_name", "DEFAULT_NAME"),
76-
BukkitClient.properties.getProperty("server_address", "DEFAULT_ADDRESS"), BukkitClient.getMaxPlayers());
80+
BukkitClient.properties.getProperty("server_address", "DEFAULT_ADDRESS"),
81+
BukkitClient.getMaxPlayers()
82+
);
83+
7784
BukkitClient.sendMessage(infoMessage);
7885

7986
ServerStatusMessage onlineMsg = new ServerStatusMessage(properties.getProperty("server_id"), idJson, (short) 1);
@@ -93,13 +100,18 @@ public void run() {
93100
}
94101
}
95102
}, 0L, 20L);
103+
if(!reattachKill) { //only start it on a fresh start, not on a reload
104+
reattachThread.start();//actually start the thread at the end so the main thread is running already
105+
}else{
96106

97-
reattachThread.start(); //actually start the thread at the end so the main thread is running already
107+
}
98108
}
99109

100110
@Override
101111
public void onDisable() {
102112
shutdownClean = true;
113+
reattachKill = true;
114+
103115
ServerStatusMessage offlineMsg = new ServerStatusMessage(properties.getProperty("server_id"), idJson, (short) 2);
104116
sendMessage(offlineMsg);
105117

@@ -117,8 +129,6 @@ public void onDisable() {
117129

118130

119131
public void shutdownHook() {
120-
reattachThread.interrupt();
121-
playerThread.interrupt();
122132
//Sends either crashed or offline depending on if shutdown happened cleanly
123133
if (!shutdownClean) {
124134
ServerStatusMessage crashMsg = new ServerStatusMessage(properties.getProperty("server_id"), idJson, (short) 3);
@@ -129,6 +139,7 @@ public void shutdownHook() {
129139
Thread.sleep(1000);
130140
} catch (InterruptedException ignored) {
131141
}
142+
messageBus.stop();
132143
}
133144

134145
public static ArrayList<String> getOnlinePlayersNames() { //Might have to fix return type

src/club/moddedminecraft/polychat/bukkitclient/EventListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public EventListener(BukkitClient plugin) {
2424

2525
@EventHandler
2626
public void onChat(AsyncPlayerChatEvent event) {
27-
event.setFormat(BukkitClient.idJson + " " + event.getFormat());
27+
//event.setFormat(BukkitClient.idJson + " " + event.getPlayer().getDisplayName() + event.getMessage());
2828
String id = BukkitClient.properties.getProperty("server_id");
2929
String json = BukkitClient.idJson + " " +event.getPlayer().getDisplayName() + ": "+ event.getMessage(); //TODO: Fix this JSON encoding
3030
ChatMessage chatMessage = new ChatMessage(BukkitClient.idJsonNoColor + " " + event.getPlayer().getDisplayName(), event.getMessage(), json);

src/club/moddedminecraft/polychat/bukkitclient/threads/ReattachThread.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import java.util.ArrayList;
1515
import java.util.Collections;
1616

17+
import static club.moddedminecraft.polychat.bukkitclient.BukkitClient.reattachKill;
18+
1719
public class ReattachThread extends HeartbeatThread {
1820

1921
private boolean isConnected = true;
@@ -25,31 +27,36 @@ public ReattachThread(int interval) {
2527
@Override
2628
protected void run() throws InterruptedException, IOException {
2729
try {
28-
if (BukkitClient.messageBus == null || (BukkitClient.messageBus.isSocketClosed())) {
29-
//Tells players ingame that the connection failed
30-
if (isConnected) {
31-
isConnected = false; //TODO
32-
BukkitClient.sendGameMessage("[PolyChat] Lost connection to main server, attempting reconnect...");
33-
}
30+
if(!reattachKill) {
31+
if (BukkitClient.messageBus == null || (BukkitClient.messageBus.isSocketClosed())) {
3432

35-
//Stops threads if they are still running
36-
if (BukkitClient.messageBus != null) BukkitClient.messageBus.stop();
37-
38-
//Attempts to start the connection //TODO
39-
BukkitClient.messageBus = new MessageBus(new Socket(BukkitClient.properties.getProperty("address"), Integer.parseInt(BukkitClient.properties.getProperty("port"))), EventListener::handleMessage);
40-
BukkitClient.messageBus.start();
41-
42-
//If the socket was reopened, wait 3 seconds to make sure sending online message works
43-
if (!BukkitClient.messageBus.isSocketClosed()) {
44-
Thread.sleep(2000); //TODO
45-
BukkitClient.sendGameMessage("[PolyChat] Connection re-established!");
46-
sendServerOnline();
47-
Thread.sleep(1000);
48-
sendOnlinePlayers();
49-
isConnected = true;
50-
}
33+
System.out.println("Firing reattach");
34+
35+
//Tells players ingame that the connection failed
36+
if (isConnected) {
37+
isConnected = false; //TODO
38+
BukkitClient.sendGameMessage("[PolyChat] Lost connection to main server, attempting reconnect...");
39+
}
5140

41+
//Stops threads if they are still running
42+
if (BukkitClient.messageBus != null) BukkitClient.messageBus.stop();
43+
44+
//Attempts to start the connection //TODO
45+
BukkitClient.messageBus = new MessageBus(new Socket(BukkitClient.properties.getProperty("address"), Integer.parseInt(BukkitClient.properties.getProperty("port"))), EventListener::handleMessage);
46+
BukkitClient.messageBus.start();
47+
48+
//If the socket was reopened, wait 3 seconds to make sure sending online message works
49+
if (!BukkitClient.messageBus.isSocketClosed()) {
50+
Thread.sleep(2000); //TODO
51+
BukkitClient.sendGameMessage("[PolyChat] Connection re-established!");
52+
sendServerOnline();
53+
Thread.sleep(1000);
54+
sendOnlinePlayers();
55+
isConnected = true;
56+
}
57+
}
5258
}
59+
5360
} catch (UnknownHostException e) {
5461
System.out.println("Unknown host exception on reattach");
5562
} catch (IOException e) {

0 commit comments

Comments
 (0)