Skip to content

Commit d0ac9dc

Browse files
committed
Callback for connections
1 parent 4e4289c commit d0ac9dc

File tree

6 files changed

+30
-19
lines changed

6 files changed

+30
-19
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [x] Get dummy sections able to have already registered servers on other sections
88
- [x] Add a new message for when a player gets connected to a server and repurpose the connecting one
99
- [ ] Add support for wildcards, contains, equalsIgnoreCase and regex at the same time
10+
- [ ] Add option to force joining a specific section
1011
- [x] Add tooltip when you hover over a server in /section info
1112
- [ ] Stop using inventivetalent's deprecated bungee-update
1213
- [ ] Create a spigot addon that adds connector signs and placeholders

src/main/java/com/jaimemartz/playerbalancer/commands/FallbackCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void execute(CommandSender sender, String[] args) {
7979
MessageUtils.send(player, ConfigEntries.FAILURE_MESSAGE.get());
8080
} else {
8181
ServerInfo server = target.getSortedServers().get(number - 1);
82-
ConnectionIntent.direct(plugin, player, server);
82+
ConnectionIntent.direct(plugin, player, server, (response, throwable) -> {});
8383
}
8484
} catch (NumberFormatException e) {
8585
MessageUtils.send(player, ConfigEntries.INVALID_INPUT_MESSAGE.get());

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void execute(CommandSender sender, String[] args) {
167167
.append(server.getName())
168168
.color(ChatColor.AQUA)
169169
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
170-
new ComponentBuilder("This is a test\nThis is a test").create()))
170+
new ComponentBuilder("This is a test\nThis is a test").create())) //todo implement this
171171
.append(String.format(" (%d/%d) ",
172172
status.getOnline(),
173173
status.getMaximum()))

src/main/java/com/jaimemartz/playerbalancer/connection/ConnectionIntent.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import com.jaimemartz.playerbalancer.ping.ServerStatus;
77
import com.jaimemartz.playerbalancer.section.ServerSection;
88
import com.jaimemartz.playerbalancer.utils.MessageUtils;
9+
import net.md_5.bungee.api.Callback;
910
import net.md_5.bungee.api.config.ServerInfo;
1011
import net.md_5.bungee.api.connection.ProxiedPlayer;
1112

1213
import java.util.ArrayList;
1314
import java.util.List;
1415
import java.util.concurrent.TimeUnit;
16+
import java.util.concurrent.atomic.AtomicBoolean;
1517

1618
public abstract class ConnectionIntent {
1719
protected final PlayerBalancer plugin;
@@ -34,12 +36,13 @@ public ConnectionIntent(PlayerBalancer plugin, ProxiedPlayer player, ProviderTyp
3436
if (section.getProvider() != ProviderType.NONE) {
3537
ServerInfo target = this.fetchServer(plugin, player, section, provider, servers);
3638
if (target != null) {
37-
this.connect(target);
38-
39-
//todo only send this if the above method returns true
40-
MessageUtils.send(player, ConfigEntries.CONNECTED_MESSAGE.get(),
41-
(str) -> str.replace("{server}", target.getName())
42-
);
39+
this.connect(target, (response, throwable) -> {
40+
if (response) { //only if the connect has been executed correctly
41+
MessageUtils.send(player, ConfigEntries.CONNECTED_MESSAGE.get(),
42+
(str) -> str.replace("{server}", target.getName())
43+
);
44+
}
45+
});
4346
} else {
4447
MessageUtils.send(player, ConfigEntries.FAILURE_MESSAGE.get());
4548
}
@@ -74,7 +77,6 @@ private ServerInfo fetchServer(PlayerBalancer plugin, ProxiedPlayer player, Serv
7477
int intents = ConfigEntries.SERVER_CHECK_ATTEMPTS.get();
7578
for (int intent = 1; intent <= intents; intent++) {
7679
if (servers.size() == 0) return null;
77-
if (servers.size() == 1) return servers.get(0);
7880

7981
ServerInfo target = provider.requestTarget(plugin, section, servers, player);
8082
if (target == null) continue;
@@ -90,23 +92,26 @@ private ServerInfo fetchServer(PlayerBalancer plugin, ProxiedPlayer player, Serv
9092
return null;
9193
}
9294

93-
public abstract void connect(ServerInfo server);
95+
public abstract void connect(ServerInfo server, Callback<Boolean> callback);
9496

97+
//todo create this as a type
9598
public static void simple(PlayerBalancer plugin, ProxiedPlayer player, ServerSection section) {
9699
new ConnectionIntent(plugin, player, section) {
97100
@Override
98-
public void connect(ServerInfo server) {
99-
direct(plugin, player, server);
101+
public void connect(ServerInfo server, Callback<Boolean> callback) {
102+
ConnectionIntent.direct(plugin, player, server, callback);
100103
}
101104
};
102105
}
103106

104-
//todo make this return true or false depending if the player really got moved or not
105-
public static void direct(PlayerBalancer plugin, ProxiedPlayer player, ServerInfo server) {
107+
//todo create this as a type
108+
public static void direct(PlayerBalancer plugin, ProxiedPlayer player, ServerInfo server, Callback<Boolean> callback) {
106109
PlayerLocker.lock(player);
107-
player.connect(server);
108-
plugin.getProxy().getScheduler().schedule(plugin, () -> {
109-
PlayerLocker.unlock(player);
110-
}, 5, TimeUnit.SECONDS);
110+
player.connect(server, (result, throwable) -> {
111+
plugin.getProxy().getScheduler().schedule(plugin, () -> {
112+
PlayerLocker.unlock(player);
113+
}, 5, TimeUnit.SECONDS);
114+
callback.done(result, throwable);
115+
});
111116
}
112117
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.jaimemartz.playerbalancer.connection.types;
2+
3+
public class DirectionConnection {
4+
// TODO: 09/08/2017
5+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void execute(CommandSender sender, String[] args) {
3838
MessageUtils.send(sender, ConfigEntries.FAILURE_MESSAGE.get());
3939
} else {
4040
ServerInfo server = section.getSortedServers().get(number - 1);
41-
ConnectionIntent.direct(plugin, player, server);
41+
ConnectionIntent.direct(plugin, player, server, (response, throwable) -> {});
4242
}
4343
} catch (NumberFormatException e) {
4444
MessageUtils.send(sender, ConfigEntries.INVALID_INPUT_MESSAGE.get());

0 commit comments

Comments
 (0)