Skip to content

Commit b3b0707

Browse files
Main -> target server
1 parent 7b83fa8 commit b3b0707

9 files changed

Lines changed: 153 additions & 219 deletions

File tree

PistonQueuePlaceholder/src/main/java/net/pistonmaster/pistonqueue/placeholder/PAPIExpansion.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public String onRequest(OfflinePlayer player, String identifier) {
3939
}
4040
}
4141

42-
for (Map.Entry<String, Integer> entry : plugin.getOnlineMain().entrySet()) {
43-
if (identifier.equalsIgnoreCase("online_main_" + entry.getKey())) {
42+
for (Map.Entry<String, Integer> entry : plugin.getOnlineTarget().entrySet()) {
43+
if (identifier.equalsIgnoreCase("online_target_" + entry.getKey())) {
4444
return String.valueOf(entry.getValue());
4545
}
4646
}

PistonQueuePlaceholder/src/main/java/net/pistonmaster/pistonqueue/placeholder/PistonQueuePlaceholder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
@Getter
1818
public final class PistonQueuePlaceholder extends JavaPlugin implements PluginMessageListener {
1919
private final Map<String, Integer> onlineQueue = new ConcurrentHashMap<>();
20-
private final Map<String, Integer> onlineMain = new ConcurrentHashMap<>();
20+
private final Map<String, Integer> onlineTarget = new ConcurrentHashMap<>();
2121

2222
@Override
2323
public void onEnable() {
@@ -53,14 +53,14 @@ public void onPluginMessageReceived(String channel, @NotNull Player player, byte
5353

5454
onlineQueue.put(queue, online);
5555
}
56-
} else if (subChannel.equalsIgnoreCase("onlineMain")) {
56+
} else if (subChannel.equalsIgnoreCase("onlineTarget")) {
5757
int count = in.readInt();
5858

5959
for (int i = 0; i < count; i++) {
6060
String queue = in.readUTF();
6161
int online = in.readInt();
6262

63-
onlineMain.put(queue, online);
63+
onlineTarget.put(queue, online);
6464
}
6565
}
6666
}

shared/src/main/java/net/pistonmaster/pistonqueue/shared/Config.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,21 @@
2424
import java.util.List;
2525

2626
public final class Config {
27-
public static String SERVER_NAME, SERVER_IS_FULL_MESSAGE, QUEUE_POSITION, JOINING_MAIN_SERVER,
27+
public static String SERVER_NAME, SERVER_IS_FULL_MESSAGE, QUEUE_POSITION, JOINING_TARGET_SERVER,
2828
QUEUE_BYPASS_PERMISSION, QUEUE_SERVER, USERNAME_REGEX,
29-
KICK_MESSAGE, ADMIN_PERMISSION, MAIN_SERVER, AUTH_SERVER, SERVER_DOWN_KICK_MESSAGE,
30-
USERNAME_REGEX_MESSAGE, PAUSE_QUEUE_IF_MAIN_DOWN_MESSAGE,
31-
SHADOW_BAN_MESSAGE, IF_MAIN_DOWN_SEND_TO_QUEUE_MESSAGE, RECOVERY_MESSAGE;
29+
KICK_MESSAGE, ADMIN_PERMISSION, TARGET_SERVER, SOURCE_SERVER, SERVER_DOWN_KICK_MESSAGE,
30+
USERNAME_REGEX_MESSAGE, PAUSE_QUEUE_IF_TARGET_DOWN_MESSAGE,
31+
SHADOW_BAN_MESSAGE, IF_TARGET_DOWN_SEND_TO_QUEUE_MESSAGE, RECOVERY_MESSAGE;
3232

3333
public static boolean POSITION_MESSAGE_HOT_BAR, ENABLE_KICK_MESSAGE,
34-
ENABLE_AUTH_SERVER, ALWAYS_QUEUE, REGISTER_TAB, AUTH_FIRST, POSITION_MESSAGE_CHAT,
35-
PAUSE_QUEUE_IF_MAIN_DOWN, KICK_WHEN_DOWN, FORCE_MAIN_SERVER,
36-
IF_MAIN_DOWN_SEND_TO_QUEUE, RECOVERY, ENABLE_USERNAME_REGEX, SEND_XP_SOUND;
34+
ENABLE_SOURCE_SERVER, ALWAYS_QUEUE, REGISTER_TAB, POSITION_MESSAGE_CHAT,
35+
PAUSE_QUEUE_IF_TARGET_DOWN, KICK_WHEN_DOWN, FORCE_TARGET_SERVER,
36+
IF_TARGET_DOWN_SEND_TO_QUEUE, RECOVERY, ENABLE_USERNAME_REGEX, SEND_XP_SOUND;
3737

3838
public static int QUEUE_MOVE_DELAY, SERVER_ONLINE_CHECK_DELAY, POSITION_MESSAGE_DELAY,
39-
START_TIME, CUSTOM_PERCENT_PERCENTAGE,
40-
MAX_PLAYERS_PER_MOVE;
39+
PERCENT_PERCENTAGE, MAX_PLAYERS_PER_MOVE;
4140

42-
public static List<String> DOWN_WORD_LIST;
41+
public static List<String> DOWN_WORD_LIST, KICK_WHEN_DOWN_SERVERS;
4342
public static QueueType[] QUEUE_TYPES; // Not allowed to be resized due to data corruption
4443
public static BanType SHADOW_BAN_TYPE;
4544

shared/src/main/java/net/pistonmaster/pistonqueue/shared/MainCommandShared.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ default void onCommand(CommandSourceWrapper sender, String[] args, PistonQueuePl
4949
case "slotstats":
5050
if (sender.hasPermission(Config.ADMIN_PERMISSION)) {
5151
sendLine(sender);
52-
sender.sendMessage(getWrapperFactory().text("Main slot stats").color(TextColorWrapper.GOLD));
52+
sender.sendMessage(getWrapperFactory().text("Target slot stats").color(TextColorWrapper.GOLD));
5353
for (QueueType type : Config.QUEUE_TYPES) {
54-
sender.sendMessage(getWrapperFactory().text(type.getName() + ": ").color(TextColorWrapper.GOLD).append(getWrapperFactory().text(type.getPlayersWithTypeInMain().get() + " / " + type.getReservedSlots()).color(TextColorWrapper.GOLD).decorate(TextDecorationWrapper.BOLD)));
54+
sender.sendMessage(getWrapperFactory().text(type.getName() + ": ").color(TextColorWrapper.GOLD).append(getWrapperFactory().text(type.getPlayersWithTypeInTarget().get() + " / " + type.getReservedSlots()).color(TextColorWrapper.GOLD).decorate(TextDecorationWrapper.BOLD)));
5555
}
5656
sendLine(sender);
5757
} else {

shared/src/main/java/net/pistonmaster/pistonqueue/shared/PistonQueuePlugin.java

Lines changed: 68 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@
2929
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
3030

3131
import java.io.IOException;
32+
import java.nio.charset.StandardCharsets;
3233
import java.nio.file.Files;
3334
import java.nio.file.Path;
3435
import java.time.Duration;
35-
import java.time.Instant;
3636
import java.time.temporal.ChronoUnit;
3737
import java.util.*;
38+
import java.util.concurrent.CompletableFuture;
39+
import java.util.concurrent.CountDownLatch;
3840
import java.util.concurrent.TimeUnit;
39-
import java.util.concurrent.atomic.AtomicBoolean;
4041
import java.util.concurrent.atomic.AtomicInteger;
4142
import java.util.stream.Collectors;
4243

@@ -66,21 +67,20 @@ public interface PistonQueuePlugin {
6667
default void scheduleTasks(QueueListenerShared queueListener) {
6768
// Sends the position message and updates tab on an interval in chat
6869
schedule(() -> {
69-
if (!queueListener.isMainOnline())
70-
return;
71-
72-
for (QueueType type : Config.QUEUE_TYPES) {
73-
sendMessage(type, Config.POSITION_MESSAGE_CHAT, MessageType.CHAT);
74-
}
75-
}, Config.POSITION_MESSAGE_DELAY, Config.POSITION_MESSAGE_DELAY, TimeUnit.MILLISECONDS);
76-
77-
// Sends the position message and updates tab on an interval on hot bar
78-
schedule(() -> {
79-
if (!queueListener.isMainOnline())
80-
return;
81-
82-
for (QueueType type : Config.QUEUE_TYPES) {
83-
sendMessage(type, Config.POSITION_MESSAGE_HOT_BAR, MessageType.ACTION_BAR);
70+
if (queueListener.getOnlineServers().contains(Config.TARGET_SERVER)) {
71+
for (QueueType type : Config.QUEUE_TYPES) {
72+
if (Config.POSITION_MESSAGE_CHAT) {
73+
sendMessage(type, MessageType.CHAT);
74+
}
75+
if (Config.POSITION_MESSAGE_HOT_BAR) {
76+
sendMessage(type, MessageType.ACTION_BAR);
77+
}
78+
}
79+
} else if (Config.PAUSE_QUEUE_IF_TARGET_DOWN) {
80+
for (QueueType type : Config.QUEUE_TYPES) {
81+
type.getQueueMap().forEach((UUID id, String str) ->
82+
getPlayer(id).ifPresent(value -> value.sendMessage(Config.PAUSE_QUEUE_IF_TARGET_DOWN_MESSAGE)));
83+
}
8484
}
8585
}, Config.POSITION_MESSAGE_DELAY, Config.POSITION_MESSAGE_DELAY, TimeUnit.MILLISECONDS);
8686

@@ -91,91 +91,52 @@ default void scheduleTasks(QueueListenerShared queueListener) {
9191
}
9292
}, Config.QUEUE_MOVE_DELAY, Config.QUEUE_MOVE_DELAY, TimeUnit.MILLISECONDS);
9393

94-
schedule(() -> {
95-
if (!Config.PAUSE_QUEUE_IF_MAIN_DOWN || queueListener.isMainOnline()) {
96-
return;
97-
}
98-
99-
for (QueueType type : Config.QUEUE_TYPES) {
100-
type.getQueueMap().forEach((UUID id, String str) ->
101-
getPlayer(id).ifPresent(value -> value.sendMessage(Config.PAUSE_QUEUE_IF_MAIN_DOWN_MESSAGE)));
102-
}
103-
}, Config.POSITION_MESSAGE_DELAY, Config.POSITION_MESSAGE_DELAY, TimeUnit.MILLISECONDS);
104-
10594
// Send plugin message
10695
schedule(this::sendCustomData, Config.QUEUE_MOVE_DELAY, Config.QUEUE_MOVE_DELAY, TimeUnit.MILLISECONDS);
10796

108-
// Moves the queue when someone logs off the main server on an interval set in the config.yml
97+
// Moves the queue when someone logs off the target server on an interval set in the config.yml
10998
schedule(queueListener::moveQueue, Config.QUEUE_MOVE_DELAY, Config.QUEUE_MOVE_DELAY, TimeUnit.MILLISECONDS);
11099

111-
String message = "%s \"%s\" not set up!!! Check out: https://github.com/AlexProgrammerDE/PistonQueue/wiki/FAQ#server-not-set-up";
112-
AtomicBoolean isFirstRun = new AtomicBoolean(true);
113100
// Checks the status of all the servers
114101
schedule(() -> {
115-
Optional<ServerInfoWrapper> serverInfoWrapper = getServer(Config.MAIN_SERVER);
116-
117-
if (serverInfoWrapper.isPresent()) {
118-
if (serverInfoWrapper.get().isOnline()) {
119-
if (!isFirstRun.get() && !queueListener.isMainOnline()) {
120-
queueListener.setOnlineSince(Instant.now());
102+
List<String> servers = new ArrayList<>(Config.KICK_WHEN_DOWN_SERVERS);
103+
CountDownLatch latch = new CountDownLatch(servers.size());
104+
for (String server : servers) {
105+
CompletableFuture.runAsync(() -> {
106+
try {
107+
Optional<ServerInfoWrapper> serverInfoWrapper = getServer(server);
108+
109+
if (serverInfoWrapper.isPresent()) {
110+
if (serverInfoWrapper.get().isOnline()) {
111+
queueListener.getOnlineServers().add(server);
112+
} else {
113+
warning(String.format("Server %s is down!!!", server));
114+
queueListener.getOnlineServers().remove(server);
115+
}
116+
} else {
117+
warning(String.format("Server \"%s\" not set up!!! Check out: https://github.com/AlexProgrammerDE/PistonQueue/wiki/FAQ#server-not-set-up", server));
118+
}
119+
} finally {
120+
latch.countDown();
121121
}
122-
123-
queueListener.setMainOnline(true);
124-
} else {
125-
warning("Main Server is down!!!");
126-
queueListener.setMainOnline(false);
127-
}
128-
isFirstRun.set(false);
129-
} else {
130-
warning(String.format(message, "Main Server", Config.MAIN_SERVER));
131-
}
132-
}, 500, Config.SERVER_ONLINE_CHECK_DELAY, TimeUnit.MILLISECONDS);
133-
134-
schedule(() -> {
135-
Optional<ServerInfoWrapper> serverInfoWrapper = getServer(Config.QUEUE_SERVER);
136-
137-
if (serverInfoWrapper.isPresent()) {
138-
if (serverInfoWrapper.get().isOnline()) {
139-
queueListener.setQueueOnline(true);
140-
} else {
141-
warning("Queue Server is down!!!");
142-
queueListener.setQueueOnline(false);
143-
}
144-
} else {
145-
warning(String.format(message, "Queue Server", Config.QUEUE_SERVER));
122+
});
146123
}
147-
}, 500, Config.SERVER_ONLINE_CHECK_DELAY, TimeUnit.MILLISECONDS);
148-
149-
schedule(() -> {
150-
if (Config.ENABLE_AUTH_SERVER) {
151-
Optional<ServerInfoWrapper> serverInfoWrapper = getServer(Config.AUTH_SERVER);
152-
153-
if (serverInfoWrapper.isPresent()) {
154-
if (serverInfoWrapper.get().isOnline()) {
155-
queueListener.setAuthOnline(true);
156-
} else {
157-
warning("Auth Server is down!!!");
158-
queueListener.setAuthOnline(false);
159-
}
160-
} else {
161-
warning(String.format(message, "Auth Server", Config.AUTH_SERVER));
162-
}
163-
} else {
164-
queueListener.setAuthOnline(true);
124+
try {
125+
latch.await();
126+
} catch (InterruptedException e) {
127+
e.printStackTrace();
165128
}
166129
}, 500, Config.SERVER_ONLINE_CHECK_DELAY, TimeUnit.MILLISECONDS);
167130
}
168131

169-
default void sendMessage(QueueType queue, boolean bool, MessageType type) {
170-
if (bool) {
171-
AtomicInteger position = new AtomicInteger();
132+
default void sendMessage(QueueType queue, MessageType type) {
133+
AtomicInteger position = new AtomicInteger();
172134

173-
for (Map.Entry<UUID, String> entry : new LinkedHashMap<>(queue.getQueueMap()).entrySet()) {
174-
getPlayer(entry.getKey()).ifPresent(player ->
175-
player.sendMessage(type, Config.QUEUE_POSITION
176-
.replace("%position%", String.valueOf(position.incrementAndGet()))
177-
.replace("%total%", String.valueOf(queue.getQueueMap().size()))));
178-
}
135+
for (Map.Entry<UUID, String> entry : new LinkedHashMap<>(queue.getQueueMap()).entrySet()) {
136+
getPlayer(entry.getKey()).ifPresent(player ->
137+
player.sendMessage(type, Config.QUEUE_POSITION
138+
.replace("%position%", String.valueOf(position.incrementAndGet()))
139+
.replace("%total%", String.valueOf(queue.getQueueMap().size()))));
179140
}
180141
}
181142

@@ -194,39 +155,36 @@ default void updateTab(QueueType queue) {
194155
}
195156

196157
default String replacePosition(String text, int position, QueueType type) {
197-
if (type.getDurationToPosition().containsKey(position)) {
198-
Duration duration = type.getDurationToPosition().get(position);
158+
if (type.getDurationFromPosition().containsKey(position)) {
159+
Duration duration = type.getDurationFromPosition().get(position);
199160

200161
return SharedChatUtils.formatDuration(text, duration, position);
201162
} else {
202-
int biggestPosition = 0;
203-
Duration biggestDuration = Duration.ZERO;
204-
205-
for (Map.Entry<Integer, Duration> entry : type.getDurationToPosition().entrySet()) {
206-
int positionOfDuration = entry.getKey();
207-
if (positionOfDuration > biggestPosition) {
208-
biggestPosition = positionOfDuration;
209-
biggestDuration = entry.getValue();
163+
Map.Entry<Integer, Duration> biggestEntry = null;
164+
165+
for (Map.Entry<Integer, Duration> entry : type.getDurationFromPosition().entrySet()) {
166+
if (biggestEntry == null || entry.getKey() > biggestEntry.getKey()) {
167+
biggestEntry = entry;
210168
}
211169
}
212170

213-
int difference = position - biggestPosition;
214-
215-
Duration imaginaryDuration = biggestDuration.plus(difference, ChronoUnit.MINUTES);
171+
Duration predictedDuration = biggestEntry == null ?
172+
Duration.of(position, ChronoUnit.MINUTES) :
173+
biggestEntry.getValue().plus(position - biggestEntry.getKey(), ChronoUnit.MINUTES);
216174

217-
return SharedChatUtils.formatDuration(text, imaginaryDuration, position);
175+
return SharedChatUtils.formatDuration(text, predictedDuration, position);
218176
}
219177
}
220178

221179
default void initializeReservationSlots() {
222180
schedule(() -> {
223-
Optional<ServerInfoWrapper> mainServer = getServer(Config.MAIN_SERVER);
224-
if (!mainServer.isPresent())
181+
Optional<ServerInfoWrapper> targetServer = getServer(Config.TARGET_SERVER);
182+
if (!targetServer.isPresent())
225183
return;
226184

227185
Map<QueueType, AtomicInteger> map = new HashMap<>();
228186

229-
for (PlayerWrapper player : mainServer.get().getConnectedPlayers()) {
187+
for (PlayerWrapper player : targetServer.get().getConnectedPlayers()) {
230188
QueueType playerType = QueueType.getQueueType(player::hasPermission);
231189

232190
if (map.containsKey(playerType)) {
@@ -236,7 +194,7 @@ default void initializeReservationSlots() {
236194
}
237195
}
238196

239-
map.forEach((type, count) -> type.getPlayersWithTypeInMain().set(count.get()));
197+
map.forEach((type, count) -> type.getPlayersWithTypeInTarget().set(count.get()));
240198
}, 0, 1, TimeUnit.SECONDS);
241199
}
242200

@@ -257,12 +215,12 @@ default void sendCustomData() {
257215
outOnlineQueue.writeInt(queueType.getQueueMap().size());
258216
}
259217

260-
ByteArrayDataOutput outOnlineMain = ByteStreams.newDataOutput();
218+
ByteArrayDataOutput outOnlineTarget = ByteStreams.newDataOutput();
261219

262-
outOnlineMain.writeUTF("onlineMain");
220+
outOnlineTarget.writeUTF("onlineTarget");
263221
outOnlineQueue.writeInt(Config.QUEUE_TYPES.length);
264222
for (QueueType queueType : Config.QUEUE_TYPES) {
265-
outOnlineQueue.writeInt(queueType.getPlayersWithTypeInMain().get());
223+
outOnlineQueue.writeInt(queueType.getPlayersWithTypeInTarget().get());
266224
}
267225

268226
Set<String> servers = new HashSet<>();
@@ -272,7 +230,7 @@ default void sendCustomData() {
272230
getServer(server).ifPresent(serverInfoWrapper ->
273231
serverInfoWrapper.sendPluginMessage("piston:queue", outOnlineQueue.toByteArray()));
274232
getServer(server).ifPresent(serverInfoWrapper ->
275-
serverInfoWrapper.sendPluginMessage("piston:queue", outOnlineMain.toByteArray()));
233+
serverInfoWrapper.sendPluginMessage("piston:queue", outOnlineTarget.toByteArray()));
276234
}
277235
}
278236

0 commit comments

Comments
 (0)