Skip to content

Commit e94b39f

Browse files
committed
Done some thingies
1 parent ffa6284 commit e94b39f

File tree

16 files changed

+239
-50
lines changed

16 files changed

+239
-50
lines changed

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/PlayerBalancer.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.jaimemartz.playerbalancer;
22

33
import com.google.common.reflect.TypeToken;
4-
import com.jaimemartz.playerbalancer.commands.FallbackCommand;
4+
import com.jaimemartz.playerbalancer.services.FallbackService;
55
import com.jaimemartz.playerbalancer.commands.MainCommand;
66
import com.jaimemartz.playerbalancer.commands.ManageCommand;
77
import com.jaimemartz.playerbalancer.connection.ServerAssignRegistry;
8-
import com.jaimemartz.playerbalancer.listener.*;
8+
import com.jaimemartz.playerbalancer.listeners.*;
99
import com.jaimemartz.playerbalancer.manager.NetworkManager;
1010
import com.jaimemartz.playerbalancer.manager.PasteHelper;
1111
import com.jaimemartz.playerbalancer.manager.PlayerLocker;
@@ -35,7 +35,8 @@ public class PlayerBalancer extends Plugin {
3535
private SectionManager sectionManager;
3636
private NetworkManager networkManager;
3737
private ConfigurationLoader<CommentedConfigurationNode> loader;
38-
private Command fallbackCommand, mainCommand, manageCommand;
38+
private FallbackService fallbackService;
39+
private Command mainCommand, manageCommand;
3940
private Listener connectListener, kickListener, messageListener, reloadListener;
4041

4142
@Override
@@ -104,11 +105,14 @@ private void enable() {
104105
statusManager.start();
105106
}
106107

108+
fallbackService = new FallbackService(this, settings.getFallbackCommandProps().getCommand());
109+
107110
if (settings.getFallbackCommandProps().isEnabled()) {
108-
fallbackCommand = new FallbackCommand(this, settings.getFallbackCommandProps().getCommand());
109-
getProxy().getPluginManager().registerCommand(this, fallbackCommand);
111+
getProxy().getPluginManager().registerCommand(this, fallbackService);
110112
}
111113

114+
getProxy().getPluginManager().registerListener(this, fallbackService);
115+
112116
connectListener = new ServerConnectListener(this);
113117
getProxy().getPluginManager().registerListener(this, connectListener);
114118

@@ -161,10 +165,13 @@ private void disable() {
161165
}
162166

163167
if (settings.getFallbackCommandProps().isEnabled()) {
164-
getProxy().getPluginManager().unregisterCommand(fallbackCommand);
165-
fallbackCommand = null;
168+
getProxy().getPluginManager().unregisterCommand(fallbackService);
166169
}
167170

171+
getProxy().getPluginManager().unregisterListener(fallbackService);
172+
173+
fallbackService = null;
174+
168175
if (settings.getKickHandlerProps().isEnabled()) {
169176
getProxy().getPluginManager().unregisterListener(kickListener);
170177
kickListener = null;

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/commands/ManageCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void execute(CommandSender sender, String[] args) {
4646
if (sender instanceof ProxiedPlayer) {
4747
ConnectionIntent.simple(plugin, (ProxiedPlayer) sender, section);
4848
} else {
49-
sender.sendMessage(new ComponentBuilder("This command can only be executed by a player").color(ChatColor.RED).create());
49+
sender.sendMessage(new ComponentBuilder("This command variant can only be executed by a player").color(ChatColor.RED).create());
5050
}
5151
}
5252
} else {

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/connection/ProviderType.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,18 @@ public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, Li
7272
@Override
7373
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
7474
AbstractProvider provider = section.getExternalProvider();
75+
if (provider == null) {
76+
plugin.getLogger().warning("Target requested to the EXTERNAL provider with the section not having a provider instance, falling back to RANDOM...");
77+
return RANDOM.requestTarget(plugin, section, servers, player);
78+
}
7579
return provider.requestTarget(plugin, section, servers, player);
7680
}
7781
};
7882

79-
public abstract ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player);
83+
public abstract ServerInfo requestTarget(
84+
PlayerBalancer plugin,
85+
ServerSection section,
86+
List<ServerInfo> servers,
87+
ProxiedPlayer player
88+
);
8089
}

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/listener/PlayerDisconnectListener.java renamed to Main Plugin/src/main/java/com/jaimemartz/playerbalancer/listeners/PlayerDisconnectListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.jaimemartz.playerbalancer.listener;
1+
package com.jaimemartz.playerbalancer.listeners;
22

33
import com.jaimemartz.playerbalancer.PlayerBalancer;
44
import com.jaimemartz.playerbalancer.connection.ServerAssignRegistry;

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/listener/PluginMessageListener.java renamed to Main Plugin/src/main/java/com/jaimemartz/playerbalancer/listeners/PluginMessageListener.java

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
package com.jaimemartz.playerbalancer.listener;
1+
package com.jaimemartz.playerbalancer.listeners;
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;
5+
import com.google.gson.*;
76
import com.jaimemartz.playerbalancer.PlayerBalancer;
87
import com.jaimemartz.playerbalancer.connection.ConnectionIntent;
98
import com.jaimemartz.playerbalancer.section.ServerSection;
10-
import com.jaimemartz.playerbalancer.json.ServerInfoAdapter;
9+
import com.jaimemartz.playerbalancer.utils.ServerInfoAdapter;
1110
import net.md_5.bungee.api.config.ServerInfo;
1211
import net.md_5.bungee.api.connection.ProxiedPlayer;
1312
import net.md_5.bungee.api.connection.Server;
@@ -18,6 +17,9 @@
1817
import java.io.ByteArrayOutputStream;
1918
import java.io.DataOutputStream;
2019
import java.io.IOException;
20+
import java.lang.reflect.Type;
21+
import java.util.Collection;
22+
import java.util.stream.Collectors;
2123

2224
public class PluginMessageListener implements Listener {
2325
private final PlayerBalancer plugin;
@@ -26,7 +28,11 @@ public class PluginMessageListener implements Listener {
2628
public PluginMessageListener(PlayerBalancer plugin) {
2729
this.plugin = plugin;
2830
GsonBuilder builder = new GsonBuilder();
29-
builder.registerTypeAdapter(ServerInfo.class, new ServerInfoAdapter());
31+
32+
builder.registerTypeAdapter(ServerInfo.class, (JsonSerializer<ServerInfo>) (server, type, context) ->
33+
context.serialize(server.getName())
34+
);
35+
3036
builder.serializeNulls();
3137
gson = builder.create();
3238
}
@@ -44,9 +50,8 @@ public void onPluginMessage(PluginMessageEvent event) {
4450
ProxiedPlayer player = (ProxiedPlayer) event.getReceiver();
4551
ServerSection section = plugin.getSectionManager().getByName(in.readUTF());
4652

47-
if (section == null) {
48-
return;
49-
}
53+
if (section == null)
54+
break;
5055

5156
ConnectionIntent.simple(plugin, player, section);
5257
}
@@ -55,14 +60,14 @@ public void onPluginMessage(PluginMessageEvent event) {
5560

5661
case "ConnectOther": {
5762
ProxiedPlayer player = plugin.getProxy().getPlayer(in.readUTF());
58-
if (player == null) {
59-
return;
60-
}
63+
64+
if (player == null)
65+
break;
6166

6267
ServerSection section = plugin.getSectionManager().getByName(in.readUTF());
63-
if (section == null) {
64-
return;
65-
}
68+
69+
if (section == null)
70+
break;
6671

6772
ConnectionIntent.simple(plugin, player, section);
6873
break;
@@ -73,9 +78,9 @@ public void onPluginMessage(PluginMessageEvent event) {
7378
DataOutputStream out = new DataOutputStream(stream);
7479

7580
ServerSection section = plugin.getSectionManager().getByName(in.readUTF());
76-
if (section == null) {
77-
return;
78-
}
81+
82+
if (section == null)
83+
break;
7984

8085
try {
8186
String output = gson.toJson(section);
@@ -94,14 +99,14 @@ public void onPluginMessage(PluginMessageEvent event) {
9499
DataOutputStream out = new DataOutputStream(stream);
95100

96101
ServerInfo server = plugin.getProxy().getServerInfo(in.readUTF());
97-
if (server == null) {
98-
return;
99-
}
102+
103+
if (server == null)
104+
break;
100105

101106
ServerSection section = plugin.getSectionManager().getByServer(server);
102-
if (section == null) {
103-
return;
104-
}
107+
108+
if (section == null)
109+
break;
105110

106111
try {
107112
String output = gson.toJson(section);
@@ -122,9 +127,9 @@ public void onPluginMessage(PluginMessageEvent event) {
122127
DataOutputStream out = new DataOutputStream(stream);
123128

124129
ServerSection section = plugin.getSectionManager().getByPlayer(player);
125-
if (section == null) {
126-
return;
127-
}
130+
131+
if (section == null)
132+
break;
128133

129134
try {
130135
String output = gson.toJson(section);
@@ -138,6 +143,27 @@ public void onPluginMessage(PluginMessageEvent event) {
138143
}
139144
break;
140145
}
146+
147+
case "GetSectionPlayerCount": {
148+
ByteArrayOutputStream stream = new ByteArrayOutputStream();
149+
DataOutputStream out = new DataOutputStream(stream);
150+
151+
ServerSection section = plugin.getSectionManager().getByName(in.readUTF());
152+
153+
if (section == null)
154+
break;
155+
156+
try {
157+
out.writeUTF("GetSectionPlayerCount");
158+
out.writeInt(section.getServers().stream()
159+
.mapToInt(a -> a.getPlayers().size()).sum());
160+
} catch (IOException e) {
161+
e.printStackTrace();
162+
}
163+
164+
sender.sendData("PlayerBalancer", stream.toByteArray());
165+
break;
166+
}
141167
}
142168
}
143169
}

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/listener/ProxyReloadListener.java renamed to Main Plugin/src/main/java/com/jaimemartz/playerbalancer/listeners/ProxyReloadListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.jaimemartz.playerbalancer.listener;
1+
package com.jaimemartz.playerbalancer.listeners;
22

33
import com.jaimemartz.playerbalancer.PlayerBalancer;
44
import net.md_5.bungee.api.event.ProxyReloadEvent;

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/listener/ServerConnectListener.java renamed to Main Plugin/src/main/java/com/jaimemartz/playerbalancer/listeners/ServerConnectListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.jaimemartz.playerbalancer.listener;
1+
package com.jaimemartz.playerbalancer.listeners;
22

33
import com.jaimemartz.playerbalancer.PlayerBalancer;
44
import com.jaimemartz.playerbalancer.connection.ConnectionIntent;

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/listener/ServerKickListener.java renamed to Main Plugin/src/main/java/com/jaimemartz/playerbalancer/listeners/ServerKickListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.jaimemartz.playerbalancer.listener;
1+
package com.jaimemartz.playerbalancer.listeners;
22

33
import com.jaimemartz.playerbalancer.PlayerBalancer;
44
import com.jaimemartz.playerbalancer.connection.ConnectionIntent;

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/section/SectionCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.jaimemartz.playerbalancer.section;
22

33
import com.jaimemartz.playerbalancer.PlayerBalancer;
4-
import com.jaimemartz.playerbalancer.commands.FallbackCommand;
4+
import com.jaimemartz.playerbalancer.services.FallbackService;
55
import net.md_5.bungee.api.connection.ProxiedPlayer;
66

7-
public class SectionCommand extends FallbackCommand {
7+
public class SectionCommand extends FallbackService {
88
private final ServerSection section;
99

1010
public SectionCommand(PlayerBalancer plugin, ServerSection section) {

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/commands/FallbackCommand.java renamed to Main Plugin/src/main/java/com/jaimemartz/playerbalancer/services/FallbackService.java

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
package com.jaimemartz.playerbalancer.commands;
1+
package com.jaimemartz.playerbalancer.services;
22

33
import com.google.common.collect.Iterables;
4+
import com.google.common.io.ByteArrayDataInput;
5+
import com.google.common.io.ByteStreams;
46
import com.jaimemartz.playerbalancer.PlayerBalancer;
57
import com.jaimemartz.playerbalancer.connection.ConnectionIntent;
68
import com.jaimemartz.playerbalancer.section.ServerSection;
@@ -13,14 +15,18 @@
1315
import net.md_5.bungee.api.chat.ComponentBuilder;
1416
import net.md_5.bungee.api.config.ServerInfo;
1517
import net.md_5.bungee.api.connection.ProxiedPlayer;
18+
import net.md_5.bungee.api.connection.Server;
19+
import net.md_5.bungee.api.event.PluginMessageEvent;
1620
import net.md_5.bungee.api.plugin.Command;
21+
import net.md_5.bungee.api.plugin.Listener;
22+
import net.md_5.bungee.event.EventHandler;
1723

18-
public class FallbackCommand extends Command {
24+
public class FallbackService extends Command implements Listener {
1925
protected final PlayerBalancer plugin;
2026
protected final MessagesProps messages;
2127
private final FallbackCommandProps props;
2228

23-
public FallbackCommand(PlayerBalancer plugin, CommandProps props) {
29+
public FallbackService(PlayerBalancer plugin, CommandProps props) {
2430
super(props.getName(), props.getPermission(), props.getAliasesArray());
2531
this.props = plugin.getSettings().getFallbackCommandProps();
2632
this.messages = plugin.getSettings().getMessagesProps();
@@ -91,4 +97,53 @@ public ServerSection getSection(ProxiedPlayer player) {
9197

9298
return null;
9399
}
100+
101+
@EventHandler
102+
public void onPluginMessage(PluginMessageEvent event) {
103+
if (event.getTag().equals("PlayerBalancer") && event.getSender() instanceof Server) {
104+
ByteArrayDataInput in = ByteStreams.newDataInput(event.getData());
105+
String request = in.readUTF();
106+
ServerInfo sender = ((Server) event.getSender()).getInfo();
107+
108+
switch (request) {
109+
case "FallbackPlayer": {
110+
if (event.getReceiver() instanceof ProxiedPlayer) {
111+
ProxiedPlayer player = (ProxiedPlayer) event.getReceiver();
112+
ServerSection target = getSection(player);
113+
114+
if (target == null)
115+
break;
116+
117+
ConnectionIntent.simple(
118+
plugin,
119+
player,
120+
target
121+
);
122+
}
123+
124+
break;
125+
}
126+
127+
case "FallbackOtherPlayer": {
128+
ProxiedPlayer player = plugin.getProxy().getPlayer(in.readUTF());
129+
130+
if (player == null)
131+
break;
132+
133+
ServerSection target = getSection(player);
134+
135+
if (target == null)
136+
break;
137+
138+
ConnectionIntent.simple(
139+
plugin,
140+
player,
141+
target
142+
);
143+
144+
break;
145+
}
146+
}
147+
}
148+
}
94149
}

0 commit comments

Comments
 (0)