Skip to content

Commit d50f3d6

Browse files
committed
Scheduled server update
1 parent cd1dd95 commit d50f3d6

File tree

8 files changed

+139
-70
lines changed

8 files changed

+139
-70
lines changed

lobbybalancer.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.2" level="project" />
117117
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
118118
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.2" level="project" />
119-
<orderEntry type="library" name="Maven: org.bstats:bstats-bungeecord:1.0" level="project" />
119+
<orderEntry type="library" name="Maven: org.bstats:bstats-bungeecord:1.1-SNAPSHOT" level="project" />
120120
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
121121
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
122122
</component>

src/main/java/me/jaimemartz/lobbybalancer/LobbyBalancer.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package me.jaimemartz.lobbybalancer;
22

33
import com.fasterxml.jackson.databind.ext.Java7Support;
4-
import com.google.gson.Gson;
54
import me.jaimemartz.faucet.ConfigFactory;
65
import me.jaimemartz.lobbybalancer.commands.FallbackCommand;
76
import me.jaimemartz.lobbybalancer.commands.MainCommand;
@@ -31,7 +30,6 @@ public class LobbyBalancer extends Plugin {
3130
public static final String NONCE_ID = "%%__NONCE__%%";
3231
private static final int LAST_VER_CONFIG_UPDATE = 20600;
3332

34-
private final Gson gson = new Gson();
3533
private boolean failed = false;
3634

3735
private ConfigFactory factory;
@@ -212,10 +210,6 @@ public void reloadPlugin() {
212210
getLogger().info(String.format("The plugin has been reloaded, took %sms", ending));
213211
}
214212

215-
public Gson getGson() {
216-
return gson;
217-
}
218-
219213
public GeolocationManager getGeolocationManager() {
220214
return geolocationManager;
221215
}

src/main/java/me/jaimemartz/lobbybalancer/commands/ManageCommand.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.Iterator;
2121
import java.util.Set;
22+
import java.util.function.Consumer;
2223

2324
public class ManageCommand extends Command {
2425
private final LobbyBalancer plugin;
@@ -57,7 +58,7 @@ public void execute(CommandSender sender, String[] args) {
5758
msgr.send(ConfigEntries.UNKNOWN_SECTION_MESSAGE.get());
5859
}
5960
} else {
60-
sendHelper(msgr);
61+
help.accept(msgr);
6162
}
6263
break;
6364
}
@@ -133,7 +134,7 @@ public void execute(CommandSender sender, String[] args) {
133134
msgr.send(ConfigEntries.UNKNOWN_SECTION_MESSAGE.get());
134135
}
135136
} else {
136-
sendHelper(msgr);
137+
help.accept(msgr);
137138
}
138139
break;
139140
}
@@ -174,25 +175,23 @@ public void execute(CommandSender sender, String[] args) {
174175

175176
default: {
176177
msgr.send("&cThis is not a valid argument for this command!");
177-
sendHelper(msgr);
178+
help.accept(msgr);
178179
}
179180
}
180181
} else {
181-
sendHelper(msgr);
182+
help.accept(msgr);
182183
}
183184
} else {
184185
msgr.send(ChatColor.RED + "You do not have permission to execute this command!");
185186
}
186187
}
187188

188-
private void sendHelper(Messager msgr) {
189-
msgr.send(
190-
"&7&m-----------------------------------------------------",
191-
"&7Available commands:",
192-
"&3/section list &7- &cTells you which sections are configured in the plugin",
193-
"&3/section info <section> &7- &cTells you info about the section",
194-
"&3/section connect <section> [player] &7- &cConnects you or the specified player to that section",
195-
"&7&m-----------------------------------------------------"
196-
);
197-
}
189+
private static final Consumer<Messager> help = (msgr) -> msgr.send(
190+
"&7&m-----------------------------------------------------",
191+
"&7Available commands:",
192+
"&3/section list &7- &cTells you which sections are configured in the plugin",
193+
"&3/section info <section> &7- &cTells you info about the section",
194+
"&3/section connect <section> [player] &7- &cConnects you or the specified player to that section",
195+
"&7&m-----------------------------------------------------"
196+
);
198197
}

src/main/java/me/jaimemartz/lobbybalancer/configuration/ConfigEntries.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class ConfigEntries implements ConfigEntryHolder {
4444
public static final ConfigEntry<Boolean> REDIS_BUNGEE_ENABLED = new ConfigEntry<>(0, "settings.redis-bungee", false);
4545
public static final ConfigEntry<Boolean> ASSIGN_TARGETS_ENABLED = new ConfigEntry<>(0, "settings.assign-targets", false);
4646
public static final ConfigEntry<Boolean> FALLBACK_PRINCIPAL_ENABLED = new ConfigEntry<>(0, "settings.fallback-principal", true);
47+
public static final ConfigEntry<Boolean> SERVERS_UPDATE = new ConfigEntry<>(0, "settings.servers-update", true);
4748

4849
public static final ConfigEntry<String> CONNECTING_MESSAGE = new ConfigEntry<>(0, "settings.messages.connecting", null);
4950
public static final ConfigEntry<String> FAILURE_MESSAGE = new ConfigEntry<>(0, "settings.messages.failure", null);

src/main/java/me/jaimemartz/lobbybalancer/listener/PluginMessageListener.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.google.common.io.ByteArrayDataInput;
44
import com.google.common.io.ByteStreams;
5+
import com.google.gson.Gson;
6+
import com.google.gson.GsonBuilder;
57
import me.jaimemartz.lobbybalancer.LobbyBalancer;
68
import me.jaimemartz.lobbybalancer.connection.ConnectionIntent;
79
import me.jaimemartz.lobbybalancer.section.ServerSection;
@@ -18,9 +20,14 @@
1820

1921
public class PluginMessageListener implements Listener {
2022
private final LobbyBalancer plugin;
23+
private final Gson gson;
2124

2225
public PluginMessageListener(LobbyBalancer plugin) {
2326
this.plugin = plugin;
27+
GsonBuilder builder = new GsonBuilder();
28+
builder.serializeNulls();
29+
builder.excludeFieldsWithoutExposeAnnotation();
30+
gson = builder.create();
2431
}
2532

2633
@EventHandler
@@ -69,7 +76,7 @@ public void onPluginMessage(PluginMessageEvent event) {
6976
}
7077

7178
try {
72-
String output = plugin.getGson().toJson(section);
79+
String output = gson.toJson(section);
7380
out.writeUTF(output);
7481
} catch (IOException e) {
7582
e.printStackTrace();
@@ -94,7 +101,7 @@ public void onPluginMessage(PluginMessageEvent event) {
94101
}
95102

96103
try {
97-
String output = plugin.getGson().toJson(section);
104+
String output = gson.toJson(section);
98105
out.writeUTF(output);
99106
} catch (IOException e) {
100107
e.printStackTrace();

src/main/java/me/jaimemartz/lobbybalancer/section/SectionManager.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package me.jaimemartz.lobbybalancer.section;
22

33
import me.jaimemartz.lobbybalancer.LobbyBalancer;
4+
import me.jaimemartz.lobbybalancer.configuration.ConfigEntries;
45
import net.md_5.bungee.api.config.ServerInfo;
6+
import net.md_5.bungee.api.scheduler.ScheduledTask;
57
import net.md_5.bungee.config.Configuration;
68

7-
import java.util.Collections;
89
import java.util.Map;
910
import java.util.concurrent.ConcurrentHashMap;
11+
import java.util.concurrent.TimeUnit;
12+
import java.util.regex.Matcher;
13+
import java.util.regex.Pattern;
1014

1115
public class SectionManager {
1216
private ServerSection principal;
17+
private ScheduledTask updateTask;
1318
private final LobbyBalancer plugin;
1419
private final Map<String, ServerSection> sectionStorage = new ConcurrentHashMap<>();
1520
private final Map<ServerInfo, ServerSection> sectionServers = new ConcurrentHashMap<>();
@@ -26,25 +31,44 @@ public void load() throws RuntimeException {
2631
sections.getKeys().forEach(name -> {
2732
plugin.getLogger().info(String.format("Construction of section with name \"%s\"", name));
2833
Configuration section = sections.getSection(name);
29-
ServerSection object = new ServerSection(name, section);
34+
ServerSection object = new ServerSection(plugin, name, section);
3035
sectionStorage.put(name, object);
3136
});
3237

3338
sectionStorage.forEach((name, section) -> {
3439
plugin.getLogger().info(String.format("Pre-Initialization of section with name \"%s\"", name));
35-
section.preInit(plugin);
40+
section.preInit();
3641
});
3742

3843
sectionStorage.forEach((name, section) -> {
3944
plugin.getLogger().info(String.format("Initialization of section with name \"%s\"", name));
40-
section.load(plugin);
45+
section.load();
4146
});
4247

4348
sectionStorage.forEach((name, section) -> {
4449
plugin.getLogger().info(String.format("Post-Initialization of section with name \"%s\"", name));
45-
section.postInit(plugin);
50+
section.postInit();
4651
});
4752

53+
if (ConfigEntries.SERVERS_UPDATE.get()) {
54+
updateTask = plugin.getProxy().getScheduler().schedule(plugin, () -> {
55+
sectionStorage.forEach((name, section) -> {
56+
section.getConfiguration().getStringList("servers").forEach(entry -> {
57+
Pattern pattern = Pattern.compile(entry);
58+
plugin.getProxy().getServers().forEach((key, value) -> {
59+
Matcher matcher = pattern.matcher(key);
60+
if (matcher.matches()) {
61+
if (!section.getServers().contains(value)) {
62+
plugin.getLogger().info(String.format("Found a new match with \"%s\" for entry \"%s\"", key, entry));
63+
this.register(value, section);
64+
section.getServers().add(value);
65+
}
66+
}
67+
});
68+
});
69+
});
70+
}, 1, 1, TimeUnit.MINUTES);
71+
}
4872

4973
long ending = System.currentTimeMillis() - starting;
5074
plugin.getLogger().info(String.format("A total of %s section(s) have been loaded in %sms", sectionStorage.size(), ending));
@@ -67,6 +91,10 @@ public void flush() {
6791
});
6892

6993
principal = null;
94+
if (updateTask != null) {
95+
updateTask.cancel();
96+
updateTask = null;
97+
}
7098
sectionStorage.clear();
7199
sectionServers.clear();
72100
}

0 commit comments

Comments
 (0)