Skip to content

Commit cefe7f9

Browse files
committed
Added option to disable prevention of connections in the same section
1 parent bd5cf50 commit cefe7f9

File tree

7 files changed

+92
-76
lines changed

7 files changed

+92
-76
lines changed

Main Plugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<name>PlayerBalancer Plugin</name>
1313
<artifactId>playerbalancer-plugin</artifactId>
14-
<version>2.1.4.2</version>
14+
<version>2.1.4.3</version>
1515

1616
<build>
1717
<finalName>PlayerBalancer</finalName>

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

Lines changed: 0 additions & 58 deletions
This file was deleted.

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

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,83 @@
11
package com.jaimemartz.playerbalancer.commands;
22

3+
import com.google.common.collect.Iterables;
34
import com.jaimemartz.playerbalancer.PlayerBalancer;
5+
import com.jaimemartz.playerbalancer.connection.ConnectionIntent;
46
import com.jaimemartz.playerbalancer.section.ServerSection;
57
import com.jaimemartz.playerbalancer.settings.props.MessagesProps;
68
import com.jaimemartz.playerbalancer.settings.props.features.FallbackCommandProps;
9+
import com.jaimemartz.playerbalancer.settings.props.shared.CommandProps;
710
import com.jaimemartz.playerbalancer.utils.MessageUtils;
11+
import net.md_5.bungee.api.ChatColor;
12+
import net.md_5.bungee.api.CommandSender;
13+
import net.md_5.bungee.api.chat.ComponentBuilder;
14+
import net.md_5.bungee.api.config.ServerInfo;
815
import net.md_5.bungee.api.connection.ProxiedPlayer;
16+
import net.md_5.bungee.api.connection.Server;
17+
import net.md_5.bungee.api.plugin.Command;
918

10-
public class FallbackCommand extends AbstractMoveCommand {
19+
public class FallbackCommand extends Command {
1120
private final PlayerBalancer plugin;
1221
private final MessagesProps messages;
13-
private final FallbackCommandProps props;
22+
protected final FallbackCommandProps props;
1423

24+
/**
25+
* Constructor for `fallback-command`
26+
*/
1527
public FallbackCommand(PlayerBalancer plugin) {
16-
super(plugin, plugin.getSettings().getFallbackCommandProps().getCommand());
17-
this.props = plugin.getSettings().getFallbackCommandProps();
28+
this(plugin, plugin.getSettings().getFallbackCommandProps().getCommand());
29+
}
30+
31+
/**
32+
* Constructor for `section-command` or others
33+
*/
34+
public FallbackCommand(PlayerBalancer plugin, CommandProps commandProps) {
35+
super(commandProps.getName(), commandProps.getPermission(), commandProps.getAliasesArray());
1836
this.messages = plugin.getSettings().getMessagesProps();
37+
this.props = plugin.getSettings().getFallbackCommandProps();
1938
this.plugin = plugin;
2039
}
2140

2241
@Override
42+
public void execute(CommandSender sender, String[] args) {
43+
if (sender instanceof ProxiedPlayer) {
44+
ProxiedPlayer player = (ProxiedPlayer) sender;
45+
ServerSection target = getSection(player);
46+
47+
if (target != null) {
48+
if (args.length == 1) {
49+
try {
50+
int number = Integer.parseInt(args[0]);
51+
if (number <= 0) {
52+
MessageUtils.send(player, messages.getInvalidInputMessage());
53+
} else if (number > target.getServers().size()) {
54+
MessageUtils.send(player, messages.getInvalidInputMessage());
55+
} else {
56+
ServerInfo server = Iterables.get(target.getServers(), number - 1);
57+
ConnectionIntent.direct(plugin, player, server, null);
58+
}
59+
} catch (NumberFormatException e) {
60+
MessageUtils.send(player, messages.getInvalidInputMessage());
61+
}
62+
} else {
63+
if (props.isPreventSameSection()) {
64+
Server current = player.getServer();
65+
if (current != null) {
66+
if (target.getServers().contains(current.getInfo())) {
67+
MessageUtils.send(player, plugin.getSettings().getMessagesProps().getSameSectionMessage());
68+
return;
69+
}
70+
}
71+
}
72+
73+
ConnectionIntent.simple(plugin, player, target);
74+
}
75+
}
76+
} else {
77+
sender.sendMessage(new ComponentBuilder("This command can only be executed by a player").color(ChatColor.RED).create());
78+
}
79+
}
80+
2381
public ServerSection getSection(ProxiedPlayer player) {
2482
ServerSection current = plugin.getSectionManager().getByPlayer(player);
2583

@@ -54,4 +112,6 @@ public ServerSection getSection(ProxiedPlayer player) {
54112

55113
return null;
56114
}
115+
116+
57117
}

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,17 @@ public ConnectionIntent(PlayerBalancer plugin, ProxiedPlayer player, ProviderTyp
2828
(str) -> str.replace("{section}", section.getName())
2929
);
3030

31+
//Prevents removing servers from the section
3132
if (servers == section.getServers()) {
3233
throw new IllegalStateException("The servers list parameter is the same reference, this cannot happen");
3334
}
3435

36+
//Prevents connections to the same server
37+
Server current = player.getServer();
38+
if (current != null) {
39+
servers.remove(current.getInfo());
40+
}
41+
3542
if (section.getImplicitProvider() != ProviderType.NONE) {
3643
ServerInfo target = this.fetchServer(player, section, provider, servers);
3744
if (target != null) {
@@ -93,16 +100,6 @@ private ServerInfo fetchServer(ProxiedPlayer player, ServerSection section, Prov
93100

94101
//TODO Create this as a type
95102
public static void simple(PlayerBalancer plugin, ProxiedPlayer player, ServerSection section) {
96-
//TODO Make this apply to all situations except kicks
97-
//TODO (It already works like that, but I want a better way)
98-
Server current = player.getServer();
99-
if (current != null) {
100-
if (section.getServers().contains(current.getInfo())) {
101-
MessageUtils.send(player, plugin.getSettings().getMessagesProps().getSameSectionMessage());
102-
return;
103-
}
104-
}
105-
106103
new ConnectionIntent(plugin, player, section) {
107104
@Override
108105
public void connect(ServerInfo server, Callback<Boolean> callback) {

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.AbstractMoveCommand;
4+
import com.jaimemartz.playerbalancer.commands.FallbackCommand;
55
import net.md_5.bungee.api.connection.ProxiedPlayer;
66

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

1010
public SectionCommand(PlayerBalancer plugin, ServerSection section) {

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/settings/props/features/FallbackCommandProps.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public class FallbackCommandProps {
2121
@Setting
2222
private boolean restrictive;
2323

24+
@Setting(value = "prevent-same-section")
25+
private boolean preventSameSection;
26+
2427
@Setting
2528
private Map<String, String> rules;
2629

@@ -56,6 +59,14 @@ public void setRestrictive(boolean restrictive) {
5659
this.restrictive = restrictive;
5760
}
5861

62+
public boolean isPreventSameSection() {
63+
return preventSameSection;
64+
}
65+
66+
public void setPreventSameSection(boolean preventSameSection) {
67+
this.preventSameSection = preventSameSection;
68+
}
69+
5970
public Map<String, String> getRules() {
6071
return rules;
6172
}
@@ -71,6 +82,7 @@ public String toString() {
7182
", command=" + command +
7283
", excludedSections=" + excludedSections +
7384
", restrictive=" + restrictive +
85+
", preventSameSection=" + preventSameSection +
7486
", rules=" + rules +
7587
'}';
7688
}

Main Plugin/src/main/resources/default.conf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ features {
167167
# When true, players will not be able to get connected to sections that are parents of the principal section
168168
restrictive=true
169169

170+
# When true, players will not be able get connected within servers of the same section
171+
# This does not affect the parametized variant of the command (/command [number])
172+
# This also affects section commands
173+
prevent-same-section=true
174+
170175
# You can override the behavior with rules, overriding the parent section
171176
# This will set the section to go when you come from the section specified
172177
rules {
@@ -199,7 +204,7 @@ features {
199204
debug-info=false
200205

201206
# You can override the behavior with rules, overriding the parent section
202-
# This will set the section to go when you come from the section specified
207+
# When you get kicked from the section on the left, you go to the one on the right
203208
rules {
204209
section-from=section-to
205210
}

0 commit comments

Comments
 (0)