Skip to content

Commit 8d47f52

Browse files
committed
Restructured the providers, now using SecureRandom
1 parent 2c0b36c commit 8d47f52

File tree

9 files changed

+97
-34
lines changed

9 files changed

+97
-34
lines changed

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
import com.jaimemartz.playerbalancer.PlayerBalancer;
44
import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider;
5-
import com.jaimemartz.playerbalancer.connection.provider.types.*;
5+
import com.jaimemartz.playerbalancer.connection.provider.types.NullProvider;
6+
import com.jaimemartz.playerbalancer.connection.provider.types.progressive.ProgressiveFillerProvider;
7+
import com.jaimemartz.playerbalancer.connection.provider.types.progressive.ProgressiveLowestProvider;
8+
import com.jaimemartz.playerbalancer.connection.provider.types.progressive.ProgressiveProvider;
9+
import com.jaimemartz.playerbalancer.connection.provider.types.random.RandomFillerProvider;
10+
import com.jaimemartz.playerbalancer.connection.provider.types.random.RandomLowestProvider;
11+
import com.jaimemartz.playerbalancer.connection.provider.types.random.RandomProvider;
612
import com.jaimemartz.playerbalancer.section.ServerSection;
713
import net.md_5.bungee.api.config.ServerInfo;
814
import net.md_5.bungee.api.connection.ProxiedPlayer;
@@ -28,17 +34,17 @@ public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, Li
2834
}
2935
},
3036

31-
LOWEST {
32-
LowestProvider provider = new LowestProvider();
37+
RANDOM_LOWEST {
38+
RandomLowestProvider provider = new RandomLowestProvider();
3339

3440
@Override
3541
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
3642
return provider.requestTarget(plugin, section, servers, player);
3743
}
3844
},
3945

40-
BALANCED {
41-
BalancedProvider provider = new BalancedProvider();
46+
RANDOM_FILLER {
47+
RandomFillerProvider provider = new RandomFillerProvider();
4248

4349
@Override
4450
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
@@ -55,8 +61,17 @@ public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, Li
5561
}
5662
},
5763

58-
FILLER {
59-
FillerProvider provider = new FillerProvider();
64+
PROGRESSIVE_LOWEST {
65+
ProgressiveLowestProvider provider = new ProgressiveLowestProvider();
66+
67+
@Override
68+
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
69+
return provider.requestTarget(plugin, section, servers, player);
70+
}
71+
},
72+
73+
PROGRESSIVE_FILLER {
74+
ProgressiveFillerProvider provider = new ProgressiveFillerProvider();
6075

6176
@Override
6277
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/connection/provider/types/FillerProvider.java renamed to Main Plugin/src/main/java/com/jaimemartz/playerbalancer/connection/provider/types/progressive/ProgressiveFillerProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.jaimemartz.playerbalancer.connection.provider.types;
1+
package com.jaimemartz.playerbalancer.connection.provider.types.progressive;
22

33
import com.jaimemartz.playerbalancer.PlayerBalancer;
44
import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider;
@@ -9,7 +9,7 @@
99

1010
import java.util.List;
1111

12-
public class FillerProvider extends AbstractProvider {
12+
public class ProgressiveFillerProvider extends AbstractProvider {
1313
@Override
1414
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
1515
int max = Integer.MIN_VALUE;

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/connection/provider/types/LowestProvider.java renamed to Main Plugin/src/main/java/com/jaimemartz/playerbalancer/connection/provider/types/progressive/ProgressiveLowestProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.jaimemartz.playerbalancer.connection.provider.types;
1+
package com.jaimemartz.playerbalancer.connection.provider.types.progressive;
22

33
import com.jaimemartz.playerbalancer.PlayerBalancer;
44
import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider;
@@ -8,7 +8,7 @@
88

99
import java.util.List;
1010

11-
public class LowestProvider extends AbstractProvider {
11+
public class ProgressiveLowestProvider extends AbstractProvider {
1212
@Override
1313
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
1414
int min = Integer.MAX_VALUE;

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/connection/provider/types/ProgressiveProvider.java renamed to Main Plugin/src/main/java/com/jaimemartz/playerbalancer/connection/provider/types/progressive/ProgressiveProvider.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.jaimemartz.playerbalancer.connection.provider.types;
1+
package com.jaimemartz.playerbalancer.connection.provider.types.progressive;
22

33
import com.jaimemartz.playerbalancer.PlayerBalancer;
44
import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider;
@@ -8,7 +8,6 @@
88
import net.md_5.bungee.api.connection.ProxiedPlayer;
99

1010
import java.util.List;
11-
import java.util.concurrent.ThreadLocalRandom;
1211

1312
public class ProgressiveProvider extends AbstractProvider {
1413
@Override
@@ -20,6 +19,6 @@ public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, Li
2019
}
2120
}
2221

23-
return servers.get(ThreadLocalRandom.current().nextInt(servers.size()));
22+
return null;
2423
}
2524
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.jaimemartz.playerbalancer.connection.provider.types.random;
2+
3+
import com.jaimemartz.playerbalancer.PlayerBalancer;
4+
import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider;
5+
import com.jaimemartz.playerbalancer.section.ServerSection;
6+
import net.md_5.bungee.api.config.ServerInfo;
7+
import net.md_5.bungee.api.connection.ProxiedPlayer;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
import static com.jaimemartz.playerbalancer.utils.RandomUtils.random;
13+
14+
public class RandomFillerProvider extends AbstractProvider {
15+
@Override
16+
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
17+
List<ServerInfo> results = new ArrayList<>();
18+
int max = Integer.MIN_VALUE;
19+
20+
for (ServerInfo server : servers) {
21+
int count = plugin.getNetworkManager().getPlayers(server);
22+
23+
if (count >= max) {
24+
if (count > max) {
25+
max = count;
26+
results.clear();
27+
}
28+
29+
results.add(server);
30+
}
31+
}
32+
33+
return random(results);
34+
}
35+
}

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/connection/provider/types/BalancedProvider.java renamed to Main Plugin/src/main/java/com/jaimemartz/playerbalancer/connection/provider/types/random/RandomLowestProvider.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.jaimemartz.playerbalancer.connection.provider.types;
1+
package com.jaimemartz.playerbalancer.connection.provider.types.random;
22

33
import com.jaimemartz.playerbalancer.PlayerBalancer;
44
import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider;
@@ -8,9 +8,10 @@
88

99
import java.util.ArrayList;
1010
import java.util.List;
11-
import java.util.concurrent.ThreadLocalRandom;
1211

13-
public class BalancedProvider extends AbstractProvider {
12+
import static com.jaimemartz.playerbalancer.utils.RandomUtils.random;
13+
14+
public class RandomLowestProvider extends AbstractProvider {
1415
@Override
1516
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
1617
List<ServerInfo> results = new ArrayList<>();
@@ -28,6 +29,6 @@ public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, Li
2829
}
2930
}
3031

31-
return results.get(ThreadLocalRandom.current().nextInt(results.size()));
32+
return random(results);
3233
}
3334
}

Main Plugin/src/main/java/com/jaimemartz/playerbalancer/connection/provider/types/RandomProvider.java renamed to Main Plugin/src/main/java/com/jaimemartz/playerbalancer/connection/provider/types/random/RandomProvider.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.jaimemartz.playerbalancer.connection.provider.types;
1+
package com.jaimemartz.playerbalancer.connection.provider.types.random;
22

33
import com.jaimemartz.playerbalancer.PlayerBalancer;
44
import com.jaimemartz.playerbalancer.connection.provider.AbstractProvider;
@@ -7,11 +7,12 @@
77
import net.md_5.bungee.api.connection.ProxiedPlayer;
88

99
import java.util.List;
10-
import java.util.concurrent.ThreadLocalRandom;
10+
11+
import static com.jaimemartz.playerbalancer.utils.RandomUtils.random;
1112

1213
public class RandomProvider extends AbstractProvider {
1314
@Override
1415
public ServerInfo requestTarget(PlayerBalancer plugin, ServerSection section, List<ServerInfo> servers, ProxiedPlayer player) {
15-
return servers.get(ThreadLocalRandom.current().nextInt(servers.size()));
16+
return random(servers);
1617
}
1718
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.jaimemartz.playerbalancer.utils;
2+
3+
import java.security.SecureRandom;
4+
import java.util.List;
5+
6+
public class RandomUtils {
7+
private static final SecureRandom instance = new SecureRandom();
8+
9+
public static <T> T random(List<T> list) {
10+
return list.get(instance.nextInt(list.size()));
11+
}
12+
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,21 @@ messages {
4242
features {
4343
balancer {
4444
# Here you have an example of what you can do with the sections
45-
# The plugin will print out info telling you if your config is right or not
46-
# If a section does not have a provider it will be inherit from the parent
4745
# The best way to understand this is to play around with it
48-
# You can use regex to match a set of servers instead of adding each server
4946
# You can have as many sections as you want, there is no limit here
50-
# When connecting to a server in a section, you get redirected
51-
52-
# Providers you can use:
53-
# NONE: Returns no server (no one will be able to connect to this section)
54-
# BALANCED: Returns a server between the ones with the least players online
55-
# LOWEST: Returns the first server found with the least players online
56-
# RANDOM: Returns a server selected by a RNG algorithm (random)
57-
# PROGRESSIVE: Returns the first server found that is not full
58-
# FILLER: Returns the server with the most players online that is not full
59-
# EXTERNAL: Returns the server determined by a provider created by other plugin
47+
# If a section does not have a provider it will try to inherit it from the parent
48+
# When connecting to a server on a section while not being on it already, you get distributed
49+
# You can use regex to match a set of servers instead of adding each server individually
50+
51+
# Providers you can use: (you can request more!)
52+
# NONE: Returns no server (no one will be able to connect to this section)
53+
# RANDOM: Returns a random server, generated by SecureRandom
54+
# RANDOM_LOWEST: Returns a random server between the ones with the least players online
55+
# RANDOM_FILLER: Returns a random server between the ones with the most players online that is not full
56+
# PROGRESSIVE: Returns the first server that is not full
57+
# PROGRESSIVE_LOWEST: Returns the first server with the least players online
58+
# PROGRESSIVE_FILLER: Returns the first server with the most players online that is not full
59+
# EXTERNAL: Returns the server calculated by a provider created by other plugin
6060

6161
sections {
6262
auth-lobbies {

0 commit comments

Comments
 (0)