Message for when you try to connect to the same section

This commit is contained in:
Jaime Martinez Rincon 2017-08-08 21:31:25 +02:00
parent 6b3293c0c2
commit f97e9a46eb
6 changed files with 43 additions and 14 deletions

View File

@ -29,21 +29,21 @@ public class FallbackCommand extends Command {
ProxiedPlayer player = (ProxiedPlayer) sender;
Callable<ServerSection> callable = () -> {
ServerSection section = plugin.getSectionManager().getByServer(player.getServer().getInfo());
ServerSection current = plugin.getSectionManager().getByPlayer(player);
if (section != null) {
if ((ConfigEntries.FALLBACK_COMMAND_IGNORED_SECTIONS.get()).contains(section.getName())) {
if (current != null) {
if ((ConfigEntries.FALLBACK_COMMAND_IGNORED_SECTIONS.get()).contains(current.getName())) {
MessageUtils.send(player, ConfigEntries.UNAVAILABLE_MESSAGE.get());
return null;
}
Configuration rules = plugin.getConfigHandle().getSection("settings.fallback-command.rules");
String bind = rules.getString(section.getName());
String bind = rules.getString(current.getName());
ServerSection target = plugin.getSectionManager().getByName(bind);
if (target == null) {
if (section.getParent() != null) {
target = section.getParent();
if (current.getParent() != null) {
target = current.getParent();
} else {
MessageUtils.send(player, ConfigEntries.UNAVAILABLE_MESSAGE.get());
return null;
@ -51,7 +51,7 @@ public class FallbackCommand extends Command {
}
if (ConfigEntries.FALLBACK_COMMAND_RESTRICTED.get()) {
if (section.getPosition() >= 0 && target.getPosition() < 0) {
if (current.getPosition() >= 0 && target.getPosition() < 0) {
MessageUtils.send(player, ConfigEntries.UNAVAILABLE_MESSAGE.get());
return null;
}
@ -68,24 +68,29 @@ public class FallbackCommand extends Command {
};
try {
ServerSection section = callable.call();
if (section != null) {
ServerSection target = callable.call();
if (target != null) {
if (args.length == 1) {
try {
int number = Integer.parseInt(args[0]);
if (number <= 0) {
MessageUtils.send(player, ConfigEntries.INVALID_INPUT_MESSAGE.get());
} else if (number > section.getServers().size()) {
} else if (number > target.getServers().size()) {
MessageUtils.send(player, ConfigEntries.FAILURE_MESSAGE.get());
} else {
ServerInfo server = section.getSortedServers().get(number - 1);
ServerInfo server = target.getSortedServers().get(number - 1);
ConnectionIntent.direct(plugin, player, server);
}
} catch (NumberFormatException e) {
MessageUtils.send(player, ConfigEntries.INVALID_INPUT_MESSAGE.get());
}
} else {
ConnectionIntent.simple(plugin, player, section);
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) {

View File

@ -49,6 +49,7 @@ public class ConfigEntries implements ConfigEntryHolder {
public static final ConfigEntry<String> UNAVAILABLE_MESSAGE = new ConfigEntry<>(0, "settings.messages.unavailable-server", null);
public static final ConfigEntry<String> KICK_MESSAGE = new ConfigEntry<>(0, "settings.messages.player-kicked", null);
public static final ConfigEntry<String> BYPASS_MESSAGE = new ConfigEntry<>(0, "settings.messages.player-bypass", null);
public static final ConfigEntry<String> SAME_SECTION = new ConfigEntry<>(0, "settings.messages.same-section", null);
public static final ConfigEntry<String> CONFIG_VERSION = new ConfigEntry<>(0, "version", null);
}

View File

@ -101,6 +101,7 @@ public abstract class ConnectionIntent {
};
}
//todo make this return true or false depending if the player really got moved or not
public static void direct(PlayerBalancer plugin, ProxiedPlayer player, ServerInfo server) {
PlayerLocker.lock(player);
player.connect(server);

View File

@ -28,7 +28,6 @@ public class SectionCommand extends Command {
public void execute(CommandSender sender, String[] args) {
if (sender instanceof ProxiedPlayer) {
ProxiedPlayer player = (ProxiedPlayer) sender;
//todo share this code with the fallback command instead of having it duplicated
if (args.length == 1) {
try {
@ -45,7 +44,12 @@ public class SectionCommand extends Command {
MessageUtils.send(sender, ConfigEntries.INVALID_INPUT_MESSAGE.get());
}
} else {
ConnectionIntent.simple(plugin, player, section);
ServerSection current = plugin.getSectionManager().getByPlayer(player);
if (current != section) {
ConnectionIntent.simple(plugin, player, section);
} else {
MessageUtils.send(player, ConfigEntries.SAME_SECTION.get());
}
}
} else {
sender.sendMessage(new ComponentBuilder("This command can only be executed by a player").color(ChatColor.RED).create());

View File

@ -5,8 +5,11 @@ import lombok.Setter;
import com.jaimemartz.playerbalancer.PlayerBalancer;
import com.jaimemartz.playerbalancer.configuration.ConfigEntries;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.connection.Server;
import net.md_5.bungee.api.scheduler.ScheduledTask;
import net.md_5.bungee.config.Configuration;
import org.apache.commons.lang3.Validate;
import java.util.HashMap;
import java.util.Map;
@ -133,4 +136,18 @@ public class SectionManager {
return servers.get(server);
}
public ServerSection getByPlayer(ProxiedPlayer player) {
if (player == null) {
return null;
}
Server server = player.getServer();
if (server == null) {
return null;
}
return getByServer(server.getInfo());
}
}

View File

@ -118,6 +118,7 @@ settings:
unavailable-server: '&cThis command cannot be executed on this server'
player-kicked: '&cYou have been kicked from &a{from} &cand you are being moved to &a{to}&c, reason: &a{reason}'
player-bypass: '&cYou have not been moved because you have the playerbalancer.bypass permission'
same-section: '&cYou are already connected to a server of this section!'
# Here you have an example of what you can do with the sections
# The plugin will print out info telling you if your config is right or not