Proper check for successful connections

This commit is contained in:
Jaime Martinez Rincon 2017-08-09 11:34:12 +02:00
parent d0ac9dc89b
commit 7b77f33c72
13 changed files with 25 additions and 24 deletions

View File

@ -85,15 +85,7 @@ public class FallbackCommand extends Command {
MessageUtils.send(player, ConfigEntries.INVALID_INPUT_MESSAGE.get()); MessageUtils.send(player, ConfigEntries.INVALID_INPUT_MESSAGE.get());
} }
} else { } else {
//TODO instead of checking if the sections are the same, we have to check if the section the player is ConnectionIntent.simple(plugin, player, target);
//TODO currently going to contains the server the player is connected to
//TODO this should be done in ConnectionIntent instead of here
ServerSection current = plugin.getSectionManager().getByPlayer(player);
if (current != target) {
ConnectionIntent.simple(plugin, player, target);
} else {
MessageUtils.send(player, ConfigEntries.SAME_SECTION.get());
}
} }
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -9,6 +9,7 @@ import com.jaimemartz.playerbalancer.utils.MessageUtils;
import net.md_5.bungee.api.Callback; import net.md_5.bungee.api.Callback;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.connection.Server;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -30,7 +31,15 @@ public abstract class ConnectionIntent {
); );
if (servers == section.getServers()) { if (servers == section.getServers()) {
throw new IllegalStateException("The servers list parameter is the same object as the section servers list, this cannot happen"); throw new IllegalStateException("The servers list parameter is the same reference, this cannot happen");
}
Server current = player.getServer();
if (current != null) {
if (section.getServers().contains(current.getInfo())) {
MessageUtils.send(player, ConfigEntries.SAME_SECTION.get());
return;
}
} }
if (section.getProvider() != ProviderType.NONE) { if (section.getProvider() != ProviderType.NONE) {

View File

@ -1,5 +0,0 @@
package com.jaimemartz.playerbalancer.connection.types;
public class DirectionConnection {
// TODO: 09/08/2017
}

View File

@ -7,6 +7,7 @@ import com.jaimemartz.playerbalancer.connection.ServerAssignRegistry;
import com.jaimemartz.playerbalancer.manager.PlayerLocker; import com.jaimemartz.playerbalancer.manager.PlayerLocker;
import com.jaimemartz.playerbalancer.section.ServerSection; import com.jaimemartz.playerbalancer.section.ServerSection;
import com.jaimemartz.playerbalancer.utils.MessageUtils; import com.jaimemartz.playerbalancer.utils.MessageUtils;
import net.md_5.bungee.api.Callback;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.ServerConnectEvent; import net.md_5.bungee.api.event.ServerConnectEvent;
@ -64,12 +65,13 @@ public class ServerConnectListener implements Listener {
if (section != null) { if (section != null) {
new ConnectionIntent(plugin, player, section) { new ConnectionIntent(plugin, player, section) {
@Override @Override
public void connect(ServerInfo server) { public void connect(ServerInfo server, Callback<Boolean> callback) {
if (ConfigEntries.ASSIGN_TARGETS_ENABLED.get()) { if (ConfigEntries.ASSIGN_TARGETS_ENABLED.get()) {
ServerAssignRegistry.assignTarget(player, section, server); ServerAssignRegistry.assignTarget(player, section, server);
} }
event.setTarget(server); event.setTarget(server);
callback.done(true, null);
} }
}; };
} }

View File

@ -6,6 +6,7 @@ import com.jaimemartz.playerbalancer.connection.ConnectionIntent;
import com.jaimemartz.playerbalancer.manager.PlayerLocker; import com.jaimemartz.playerbalancer.manager.PlayerLocker;
import com.jaimemartz.playerbalancer.section.ServerSection; import com.jaimemartz.playerbalancer.section.ServerSection;
import com.jaimemartz.playerbalancer.utils.MessageUtils; import com.jaimemartz.playerbalancer.utils.MessageUtils;
import net.md_5.bungee.api.Callback;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.config.ServerInfo; import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
@ -110,7 +111,7 @@ public class ServerKickListener implements Listener {
new ConnectionIntent(plugin, player, section, servers) { new ConnectionIntent(plugin, player, section, servers) {
@Override @Override
public void connect(ServerInfo server) { public void connect(ServerInfo server, Callback<Boolean> callback) {
PlayerLocker.lock(player); PlayerLocker.lock(player);
MessageUtils.send(player, ConfigEntries.KICK_MESSAGE.get(), MessageUtils.send(player, ConfigEntries.KICK_MESSAGE.get(),
(str) -> str.replace("{from}", from.getName()) (str) -> str.replace("{from}", from.getName())
@ -121,6 +122,7 @@ public class ServerKickListener implements Listener {
plugin.getProxy().getScheduler().schedule(plugin, () -> { plugin.getProxy().getScheduler().schedule(plugin, () -> {
PlayerLocker.unlock(player); PlayerLocker.unlock(player);
}, 5, TimeUnit.SECONDS); }, 5, TimeUnit.SECONDS);
callback.done(true, null);
} }
}; };
} }

View File

@ -56,6 +56,7 @@ public class SectionManager {
section.postInit(); section.postInit();
}); });
//todo unify loading code with SectionManager
if (ConfigEntries.SERVERS_UPDATE.get()) { if (ConfigEntries.SERVERS_UPDATE.get()) {
updateTask = plugin.getProxy().getScheduler().schedule(plugin, () -> { updateTask = plugin.getProxy().getScheduler().schedule(plugin, () -> {
this.sections.forEach((name, section) -> { this.sections.forEach((name, section) -> {

View File

@ -39,7 +39,7 @@ import java.util.Comparator;
* Use the static "sort" method from the java.util.Collections class: * Use the static "sort" method from the java.util.Collections class:
* Collections.sort(your list, new AlphanumComparator()); * Collections.sort(your list, new AlphanumComparator());
*/ */
public class AlphanumComparator implements Comparator public final class AlphanumComparator implements Comparator
{ {
private final boolean isDigit(char ch) private final boolean isDigit(char ch)
{ {

View File

@ -1,6 +1,6 @@
package com.jaimemartz.playerbalancer.utils; package com.jaimemartz.playerbalancer.utils;
public class DigitUtils { public final class DigitUtils {
public static int getDigits(String string, int digits) { public static int getDigits(String string, int digits) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();

View File

@ -7,7 +7,7 @@ import net.md_5.bungee.api.config.ServerInfo;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class FixedAdapter extends AdapterWrapper { public final class FixedAdapter extends AdapterWrapper {
private static final Map<String, ServerInfo> fakeServers = new HashMap<>(); private static final Map<String, ServerInfo> fakeServers = new HashMap<>();
static { static {

View File

@ -15,7 +15,7 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
@Data @Data
public class GuestPaste { public final class GuestPaste {
private final String key; private final String key;
private final String code; private final String code;

View File

@ -1,6 +1,6 @@
package com.jaimemartz.playerbalancer.utils; package com.jaimemartz.playerbalancer.utils;
public class LevenshteinDistance { public final class LevenshteinDistance {
public static <T> T closest(Iterable<T> collection, T target) { public static <T> T closest(Iterable<T> collection, T target) {
int distance = Integer.MAX_VALUE; int distance = Integer.MAX_VALUE;
T closest = null; T closest = null;

View File

@ -7,7 +7,7 @@ import net.md_5.bungee.api.chat.TextComponent;
import java.util.function.Function; import java.util.function.Function;
public class MessageUtils { public final class MessageUtils {
public static void send(CommandSender sender, String text) { public static void send(CommandSender sender, String text) {
if (text != null) { if (text != null) {
text = ChatColor.translateAlternateColorCodes('&', text); text = ChatColor.translateAlternateColorCodes('&', text);

View File

@ -16,7 +16,7 @@ import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.util.List; import java.util.List;
public class ServerListPing { public final class ServerListPing {
private int timeout = 7000; private int timeout = 7000;
private static Gson gson = new GsonBuilder() private static Gson gson = new GsonBuilder()
.registerTypeAdapter(BaseComponent.class, new ComponentSerializer()) .registerTypeAdapter(BaseComponent.class, new ComponentSerializer())