Skip to content

Commit

Permalink
Added multiple server cluster handling, Closes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed Feb 12, 2021
1 parent 95b762e commit e1a22e2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/main/java/me/william278/huskhomes2/Objects/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Settings {

// Bungee settings
boolean doBungee;
int clusterID;
String server;

// Data storage settings
Expand Down Expand Up @@ -84,6 +85,7 @@ private void setSettings(FileConfiguration configFile) {

this.doBungee = configFile.getBoolean("bungee_options.enable_bungee_mode");
this.server = configFile.getString("bungee_options.server_id");
this.clusterID = configFile.getInt("bungee_options.cluster_id");

this.storageType = configFile.getString("data_storage_options.storage_type");
this.playerDataTable = configFile.getString("data_storage_options.table_names.player_data");
Expand Down Expand Up @@ -233,6 +235,8 @@ public String getServerID() {
return server;
}

public int getServerClusterID() { return clusterID; }

public boolean doBungee() {
return doBungee;
}
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/me/william278/huskhomes2/messageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,6 @@ public class messageManager {

private static final HuskHomes plugin = HuskHomes.getInstance();

// Delete the file at the pointer specified
private static void deleteFile(File f) {
try {
if (!f.delete()) {
Bukkit.getLogger().severe("Failed to delete messages.yml file!");
}
} catch (Exception e) {
Bukkit.getLogger().severe("An error occurred while deleting the messages.yml file!");
e.printStackTrace();
}
}

// Create a new file, at the pointer specified
private static void createFile(File f) {
try {
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/me/william278/huskhomes2/pluginMessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ public static void sendPlayer(Player p, String targetServer) {
// Send a plugin message
public static void sendPluginMessage(Player sender, String targetPlayerName, String messageType, String messageData) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
int clusterID = HuskHomes.settings.getServerClusterID();

// Send a plugin message to the specified player name
out.writeUTF("ForwardToPlayer");
out.writeUTF(targetPlayerName);

// Send the HuskHomes message with a specific type
out.writeUTF("HuskHomes:" + messageType);
out.writeUTF("HuskHomes:" + clusterID + ":" + messageType);
ByteArrayOutputStream messageBytes = new ByteArrayOutputStream();
DataOutputStream messageOut = new DataOutputStream(messageBytes);

Expand Down Expand Up @@ -61,15 +62,32 @@ public void onPluginMessageReceived(String channel, Player recipient, byte[] mes
return;
}
ByteArrayDataInput input = ByteStreams.newDataInput(message);

// Plugin messages are formatted as such:
// HuskHomes:<cluster_id>:<message_type>, followed by the message arguments and data.
String messageType = input.readUTF();
int clusterID;

// Return if the message was not sent by HuskHomes
if (!messageType.contains("HuskHomes:")) {
return;
}

// Ensure the cluster ID matches
try {
clusterID = Integer.parseInt(messageType.split(":")[1]);
} catch (Exception e) {
// In case the message is malformed or the cluster ID is invalid
Bukkit.getLogger().warning("Received a HuskHomes plugin message with an invalid server cluster ID! \n" +
"Please ensure you are running the latest version of HuskHomes on all your servers and that the cluster ID is set to a valid integer on all of them.");
return;
}
if (HuskHomes.settings.getServerClusterID() != clusterID) {
return;
}

// Get the HuskHomes message type
messageType = messageType.split(":")[1];
messageType = messageType.split(":")[2];

// Get the message data packets
String messageData = "";
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ general:
bungee_options:
enable_bungee_mode: false
server_id: 'server'
cluster_id: 0
enable_warp_command: true
spawn_command:
enabled: true
Expand Down

0 comments on commit e1a22e2

Please sign in to comment.