Skip to content

Commit f97e9a4

Browse files
committed
Message for when you try to connect to the same section
1 parent 6b3293c commit f97e9a4

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,29 @@ public void execute(CommandSender sender, String[] args) {
2929
ProxiedPlayer player = (ProxiedPlayer) sender;
3030

3131
Callable<ServerSection> callable = () -> {
32-
ServerSection section = plugin.getSectionManager().getByServer(player.getServer().getInfo());
32+
ServerSection current = plugin.getSectionManager().getByPlayer(player);
3333

34-
if (section != null) {
35-
if ((ConfigEntries.FALLBACK_COMMAND_IGNORED_SECTIONS.get()).contains(section.getName())) {
34+
if (current != null) {
35+
if ((ConfigEntries.FALLBACK_COMMAND_IGNORED_SECTIONS.get()).contains(current.getName())) {
3636
MessageUtils.send(player, ConfigEntries.UNAVAILABLE_MESSAGE.get());
3737
return null;
3838
}
3939

4040
Configuration rules = plugin.getConfigHandle().getSection("settings.fallback-command.rules");
41-
String bind = rules.getString(section.getName());
41+
String bind = rules.getString(current.getName());
4242
ServerSection target = plugin.getSectionManager().getByName(bind);
4343

4444
if (target == null) {
45-
if (section.getParent() != null) {
46-
target = section.getParent();
45+
if (current.getParent() != null) {
46+
target = current.getParent();
4747
} else {
4848
MessageUtils.send(player, ConfigEntries.UNAVAILABLE_MESSAGE.get());
4949
return null;
5050
}
5151
}
5252

5353
if (ConfigEntries.FALLBACK_COMMAND_RESTRICTED.get()) {
54-
if (section.getPosition() >= 0 && target.getPosition() < 0) {
54+
if (current.getPosition() >= 0 && target.getPosition() < 0) {
5555
MessageUtils.send(player, ConfigEntries.UNAVAILABLE_MESSAGE.get());
5656
return null;
5757
}
@@ -68,24 +68,29 @@ public void execute(CommandSender sender, String[] args) {
6868
};
6969

7070
try {
71-
ServerSection section = callable.call();
72-
if (section != null) {
71+
ServerSection target = callable.call();
72+
if (target != null) {
7373
if (args.length == 1) {
7474
try {
7575
int number = Integer.parseInt(args[0]);
7676
if (number <= 0) {
7777
MessageUtils.send(player, ConfigEntries.INVALID_INPUT_MESSAGE.get());
78-
} else if (number > section.getServers().size()) {
78+
} else if (number > target.getServers().size()) {
7979
MessageUtils.send(player, ConfigEntries.FAILURE_MESSAGE.get());
8080
} else {
81-
ServerInfo server = section.getSortedServers().get(number - 1);
81+
ServerInfo server = target.getSortedServers().get(number - 1);
8282
ConnectionIntent.direct(plugin, player, server);
8383
}
8484
} catch (NumberFormatException e) {
8585
MessageUtils.send(player, ConfigEntries.INVALID_INPUT_MESSAGE.get());
8686
}
8787
} else {
88-
ConnectionIntent.simple(plugin, player, section);
88+
ServerSection current = plugin.getSectionManager().getByPlayer(player);
89+
if (current != target) {
90+
ConnectionIntent.simple(plugin, player, target);
91+
} else {
92+
MessageUtils.send(player, ConfigEntries.SAME_SECTION.get());
93+
}
8994
}
9095
}
9196
} catch (Exception e) {

src/main/java/com/jaimemartz/playerbalancer/configuration/ConfigEntries.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class ConfigEntries implements ConfigEntryHolder {
4949
public static final ConfigEntry<String> UNAVAILABLE_MESSAGE = new ConfigEntry<>(0, "settings.messages.unavailable-server", null);
5050
public static final ConfigEntry<String> KICK_MESSAGE = new ConfigEntry<>(0, "settings.messages.player-kicked", null);
5151
public static final ConfigEntry<String> BYPASS_MESSAGE = new ConfigEntry<>(0, "settings.messages.player-bypass", null);
52+
public static final ConfigEntry<String> SAME_SECTION = new ConfigEntry<>(0, "settings.messages.same-section", null);
5253

5354
public static final ConfigEntry<String> CONFIG_VERSION = new ConfigEntry<>(0, "version", null);
5455
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public void connect(ServerInfo server) {
101101
};
102102
}
103103

104+
//todo make this return true or false depending if the player really got moved or not
104105
public static void direct(PlayerBalancer plugin, ProxiedPlayer player, ServerInfo server) {
105106
PlayerLocker.lock(player);
106107
player.connect(server);

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public SectionCommand(PlayerBalancer plugin, String name, String permission, Lis
2828
public void execute(CommandSender sender, String[] args) {
2929
if (sender instanceof ProxiedPlayer) {
3030
ProxiedPlayer player = (ProxiedPlayer) sender;
31-
3231
//todo share this code with the fallback command instead of having it duplicated
3332
if (args.length == 1) {
3433
try {
@@ -45,7 +44,12 @@ public void execute(CommandSender sender, String[] args) {
4544
MessageUtils.send(sender, ConfigEntries.INVALID_INPUT_MESSAGE.get());
4645
}
4746
} else {
48-
ConnectionIntent.simple(plugin, player, section);
47+
ServerSection current = plugin.getSectionManager().getByPlayer(player);
48+
if (current != section) {
49+
ConnectionIntent.simple(plugin, player, section);
50+
} else {
51+
MessageUtils.send(player, ConfigEntries.SAME_SECTION.get());
52+
}
4953
}
5054
} else {
5155
sender.sendMessage(new ComponentBuilder("This command can only be executed by a player").color(ChatColor.RED).create());

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
import com.jaimemartz.playerbalancer.PlayerBalancer;
66
import com.jaimemartz.playerbalancer.configuration.ConfigEntries;
77
import net.md_5.bungee.api.config.ServerInfo;
8+
import net.md_5.bungee.api.connection.ProxiedPlayer;
9+
import net.md_5.bungee.api.connection.Server;
810
import net.md_5.bungee.api.scheduler.ScheduledTask;
911
import net.md_5.bungee.config.Configuration;
12+
import org.apache.commons.lang3.Validate;
1013

1114
import java.util.HashMap;
1215
import java.util.Map;
@@ -133,4 +136,18 @@ public ServerSection getByServer(ServerInfo server) {
133136

134137
return servers.get(server);
135138
}
139+
140+
public ServerSection getByPlayer(ProxiedPlayer player) {
141+
if (player == null) {
142+
return null;
143+
}
144+
145+
Server server = player.getServer();
146+
147+
if (server == null) {
148+
return null;
149+
}
150+
151+
return getByServer(server.getInfo());
152+
}
136153
}

src/main/resources/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ settings:
118118
unavailable-server: '&cThis command cannot be executed on this server'
119119
player-kicked: '&cYou have been kicked from &a{from} &cand you are being moved to &a{to}&c, reason: &a{reason}'
120120
player-bypass: '&cYou have not been moved because you have the playerbalancer.bypass permission'
121+
same-section: '&cYou are already connected to a server of this section!'
121122

122123
# Here you have an example of what you can do with the sections
123124
# The plugin will print out info telling you if your config is right or not

0 commit comments

Comments
 (0)