Add ability to control online players along with max players

master
Chimney Swift 2020-07-01 01:13:05 +10:00
parent 32ae495c1f
commit 69a40e58e7
3 changed files with 69 additions and 25 deletions

View File

@ -1,27 +1,27 @@
/*
* This file is part of Neutron, licensed under the MIT License
*
* Copyright (c) 2020 Crypnotic <crypnoticofficial@gmail.com>
* Copyright (c) 2020 Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
* This file is part of Neutron, licensed under the MIT License
*
* Copyright (c) 2020 Crypnotic <crypnoticofficial@gmail.com>
* Copyright (c) 2020 Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.crypnotic.neutron.module.serverlist;
import java.util.Arrays;
@ -48,6 +48,10 @@ public class ServerListConfig {
@Setting("server-preview")
private ServerPreview serverPreview = new ServerPreview();
@Getter
@Setting("online-player-count")
private OnlinePlayerCount onlinePlayerCount = new OnlinePlayerCount();
@ConfigSerializable
public static class PlayerCount {
@ -92,4 +96,27 @@ public class ServerListConfig {
PLAYERS;
}
}
}
@ConfigSerializable
public static class OnlinePlayerCount {
@Getter
@Setting(value = "action", comment = " # The online player count has three different actions:\r\n" + " # \r\n"
+ " # STATIC - online players will always be the value of `online-player-count`\r\n"
+ " # PING - online player count shows the sum of all backend servers' online player counts. Cached every 5 minutes\r\n"
+ " # CURRENT - online players matches number of players connected to the proxy\r\n" + " #\r\n"
+ " # `online-player-count` is only used with the STATIC action")
private OnlinePlayerCountAction action = OnlinePlayerCountAction.CURRENT;
@Getter
@Setting("online-player-count")
private int onlinePlayerCount = 32;
public static enum OnlinePlayerCountAction {
STATIC,
PING,
CURRENT;
}
}
}

View File

@ -50,12 +50,23 @@ public class ServerListHandler {
Builder builder = ServerPing.builder();
builder.version(original.getVersion());
builder.onlinePlayers(playerCount);
original.getFavicon().ifPresent(builder::favicon);
builder.description(config.getMotd());
switch (config.getOnlinePlayerCount().getAction()) {
case STATIC:
builder.onlinePlayers(playerCount);
break;
case PING:
builder.onlinePlayers(module.getOnlinePlayerPing());
break;
case CURRENT:
builder.onlinePlayers(module.getNeutron().getProxy().getPlayerCount());
break;
}
switch (config.getPlayerCount().getAction()) {
case CURRENT:
builder.maximumPlayers(playerCount);

View File

@ -47,6 +47,8 @@ public class ServerListModule extends Module {
private ServerListHandler handler;
@Getter
private int maxPlayerPing;
@Getter
private int onlinePlayerPing;
@Override
public StateResult init() {
@ -90,6 +92,7 @@ public class ServerListModule extends Module {
public class PingTask implements Runnable {
int buffer = 0;
int onlineBuffer = 0;
@Override
public void run() {
@ -100,6 +103,7 @@ public class ServerListModule extends Module {
if (players.isPresent()) {
buffer += players.get().getMax();
onlineBuffer += players.get().getOnline();
}
} catch (InterruptedException | ExecutionException exception) {
/* Catch silently */
@ -108,7 +112,9 @@ public class ServerListModule extends Module {
}
maxPlayerPing = buffer;
onlinePlayerPing = onlineBuffer;
buffer = 0;
onlineBuffer = 0;
}
}
}