Skip to content

Commit

Permalink
Added Player Counts to Status
Browse files Browse the repository at this point in the history
  • Loading branch information
beanbeanjuice committed Jul 17, 2024
1 parent fac4043 commit 0a9920b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,20 @@ public void onEnable() {
epochHelper = new EpochHelper(config);

this.getLogger().info("Attempting to initialize Discord bot... (if enabled)");
discordBot = new Bot(this.config, this.getLogger()::warning);
discordBot = new Bot(this.config, this.getLogger()::warning, this::getOnlinePlayers, this::getMaxPlayers);

this.getProxy().getScheduler().runAsync(this, () -> {
try { discordBot.start();
} catch (Exception e) { getLogger().warning("There was an error starting the discord bot: " + e.getMessage()); }
try { discordBot.start(); }
catch (Exception e) { getLogger().warning("There was an error starting the discord bot: " + e.getMessage()); }
});

registerListeners();
hookPlugins();
registerCommands();

// Discord Topic Updater
this.getProxy().getScheduler().schedule(this, () -> {
int numPlayers = this.getProxy().getPlayers().size();

if (config.getAsBoolean(ConfigDataKey.VANISH_ENABLED))
numPlayers = (int) this.getProxy().getPlayers().stream()
.filter((player) -> !BungeeVanishAPI.isInvisible(player))
.count();

discordBot.channelUpdaterFunction(numPlayers);
}, 10, 10, TimeUnit.MINUTES);
// Discord Topic/Status Updater
this.getProxy().getScheduler().schedule(this, discordBot::channelUpdaterFunction, 10, 10, TimeUnit.MINUTES);
this.getProxy().getScheduler().schedule(this, discordBot::updateActivity, 5, 5, TimeUnit.MINUTES);

// Update Checker
startUpdateChecker();
Expand Down Expand Up @@ -207,6 +199,19 @@ private void stopPluginMessaging() {
this.getProxy().unregisterChannel("BungeeCord");
}

private int getOnlinePlayers() {
if (config.getAsBoolean(ConfigDataKey.VANISH_ENABLED))
return (int) this.getProxy().getPlayers().stream()
.filter((player) -> !BungeeVanishAPI.isInvisible(player))
.count();

return this.getProxy().getOnlineCount();
}

private int getMaxPlayers() {
return this.getProxy().getConfig().getPlayerLimit();
}

@Override
public void onDisable() {
this.getLogger().info("The plugin is shutting down...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,21 @@ public SimpleProxyChatVelocity(ProxyServer proxyServer, Logger logger, @DataDire
public void onProxyInitialization(ProxyInitializeEvent event) {
// Initialize discord bot.
this.getLogger().info("Attempting to initialize Discord bot... (if enabled)");
discordBot = new Bot(this.config, this.getLogger()::warn);
discordBot = new Bot(this.config, this.getLogger()::warn, this::getOnlinePlayers, this::getMaxPlayers);

// Bot ready.
this.proxyServer.getScheduler().buildTask(this, () -> {
try { discordBot.start();
} catch (Exception e) { this.getLogger().warn("There was an error starting the discord bot: {}", e.getMessage()); }
try { discordBot.start(); }
catch (Exception e) { this.getLogger().warn("There was an error starting the discord bot: {}", e.getMessage()); }
}).schedule();

hookPlugins();
registerListeners();
registerCommands();

// Start Channel Topic Updater
this.proxyServer.getScheduler()
.buildTask(this, () -> {
int numPlayers = proxyServer.getPlayerCount();

if (config.getAsBoolean(ConfigDataKey.VANISH_ENABLED))
numPlayers = (int) proxyServer.getAllPlayers().stream()
.filter((player) -> !VelocityVanishAPI.isInvisible(player))
.count();

discordBot.channelUpdaterFunction(numPlayers);
})
.delay(10, TimeUnit.MINUTES)
.repeat(10, TimeUnit.MINUTES)
.schedule();
this.proxyServer.getScheduler().buildTask(this, discordBot::channelUpdaterFunction).delay(10, TimeUnit.MINUTES).repeat(10, TimeUnit.MINUTES).schedule();
this.proxyServer.getScheduler().buildTask(this, discordBot::updateActivity).delay(5, TimeUnit.MINUTES).repeat(5, TimeUnit.MINUTES).schedule();

// Start Update Checker
startUpdateChecker();
Expand Down Expand Up @@ -274,6 +262,19 @@ private void registerCommands() {
}
}

private int getOnlinePlayers() {
if (config.getAsBoolean(ConfigDataKey.VANISH_ENABLED))
return (int) proxyServer.getAllPlayers().stream()
.filter((player) -> !VelocityVanishAPI.isInvisible(player))
.count();

return this.getProxyServer().getPlayerCount();
}

private int getMaxPlayers() {
return this.getProxyServer().getConfiguration().getShowMaxPlayers();
}

@Subscribe(order = PostOrder.LAST)
public void onProxyShutdown(ProxyShutdownEvent event) {
this.getLogger().info("The plugin is shutting down...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class Bot {
Expand All @@ -28,11 +29,18 @@ public class Bot {
private final Consumer<String> errorLogger;
private JDA bot;

private final Supplier<Integer> getOnlinePlayers;
private final Supplier<Integer> getMaxPlayers;

private final Queue<Runnable> runnables;

public Bot(final Config config, final Consumer<String> errorLogger) {
public Bot(final Config config, final Consumer<String> errorLogger, final Supplier<Integer> getOnlinePlayers, final Supplier<Integer> getMaxPlayers) {
this.config = config;
this.errorLogger = errorLogger;

this.getOnlinePlayers = getOnlinePlayers;
this.getMaxPlayers = getMaxPlayers;

runnables = new ConcurrentLinkedQueue<>();

if (!config.getAsBoolean(ConfigDataKey.USE_DISCORD)) {
Expand Down Expand Up @@ -135,9 +143,9 @@ public void updateChannelTopic(final String topic) {
);
}

public void channelUpdaterFunction(final int players) {
public void channelUpdaterFunction() {
if (bot == null) return;
String topicMessage = config.getAsString(ConfigDataKey.DISCORD_TOPIC_ONLINE).replace("%online%", String.valueOf(players));
String topicMessage = config.getAsString(ConfigDataKey.DISCORD_TOPIC_ONLINE).replace("%online%", String.valueOf(getOnlinePlayers.get()));
this.updateChannelTopic(topicMessage);
}

Expand Down Expand Up @@ -172,6 +180,9 @@ public void start() throws InterruptedException {

public void updateActivity() {
this.getJDA().ifPresent((jda) -> {
int onlinePlayers = getOnlinePlayers.get();
int maxPlayers = getMaxPlayers.get();

Activity.ActivityType type;
String text;

Expand All @@ -182,6 +193,9 @@ public void updateActivity() {
type = Activity.ActivityType.WATCHING;
text = "CONFIG ERROR";
}

text = text.replace("%online%", String.valueOf(onlinePlayers))
.replace("%max-players%", String.valueOf(maxPlayers));
jda.getPresence().setActivity(Activity.of(type, text));
});
}
Expand Down Expand Up @@ -220,6 +234,7 @@ public void sendProxyStatus(final boolean isStart) {
}

public void stop() {
if (bot == null) return;
sendProxyStatus(false);

this.updateChannelTopic(config.getAsString(ConfigDataKey.DISCORD_TOPIC_OFFLINE));
Expand Down
7 changes: 4 additions & 3 deletions projects/main-app/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ CHANNEL-ID: "GLOBAL_CHANNEL_ID"
bot-activity:
# Valid Types: ONLINE, DO_NOT_DISTURB, IDLE, INVISIBLE
status: ONLINE
# Valid Types: PLAYING, STREAMING, LISTENING, WATCHING, COMPETING
type: "COMPETING"
text: "SimpleProxyChat by beanbeanjuice"
# Valid Types: PLAYING, STREAMING, LISTENING, WATCHING, COMPETING, CUSTOM_STATUS
type: "CUSTOM_STATUS"
# Valid placeholders are %online% and/or %max-players% (UPDATES EVERY 5 MINUTES)
text: "%online%/%max-players% - SimpleProxyChat by beanbeanjuice"

# The amount of seconds to check if a server is online/offline.
# Smaller numbers can cause errors. Beware.
Expand Down

0 comments on commit 0a9920b

Please sign in to comment.