From 8cb168260a2f75b8a703dfaaae002ff0f47e7b27 Mon Sep 17 00:00:00 2001 From: "Timothy Roe, Jr" Date: Sun, 19 Apr 2020 00:44:10 -0700 Subject: [PATCH] Added Activity to Discord Bot to show number of Users Online. --- .../uk/co/angrybee/joe/DiscordClient.java | 4 ++++ .../co/angrybee/joe/DiscordWhitelister.java | 23 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/uk/co/angrybee/joe/DiscordClient.java b/src/main/java/uk/co/angrybee/joe/DiscordClient.java index 434016c..a709537 100644 --- a/src/main/java/uk/co/angrybee/joe/DiscordClient.java +++ b/src/main/java/uk/co/angrybee/joe/DiscordClient.java @@ -110,6 +110,10 @@ public class DiscordClient extends ListenerAdapter { } } + public static void onServerPlayerCountChange(int playerCount) { + javaDiscordAPI.getPresence().setActivity(Activity.watching(playerCount + "/" + DiscordWhitelister.getMaximumAllowedPlayers() + " players.")); + } + @Override public void onMessageReceived(MessageReceivedEvent messageReceivedEvent) { if (messageReceivedEvent.isFromType(ChannelType.TEXT)) { diff --git a/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java b/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java index e464a2e..e810aa6 100755 --- a/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java +++ b/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java @@ -5,6 +5,10 @@ import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.event.Listener; +import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; import uk.co.angrybee.joe.Commands.CommandAbout; import uk.co.angrybee.joe.Commands.CommandStatus; @@ -12,7 +16,7 @@ import java.io.File; import java.io.IOException; import java.util.*; -public class DiscordWhitelister extends JavaPlugin +public class DiscordWhitelister extends JavaPlugin implements Listener { private static File whitelisterBotConfigFile; private static File userListFile; @@ -113,6 +117,9 @@ public class DiscordWhitelister extends JavaPlugin getLogger().severe("Discord Client failed to initialize, please check if your config file is valid."); } } + + this.getServer().getPluginManager().registerEvents(this, this); + } public static JavaPlugin getPlugin() @@ -171,6 +178,20 @@ public class DiscordWhitelister extends JavaPlugin getUserList().save(getUserListFile().getPath()); } + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + DiscordClient.onServerPlayerCountChange(getOnlineUsers()); + } + + @EventHandler + public void onPlayerLeave(PlayerQuitEvent event) { + DiscordClient.onServerPlayerCountChange(getOnlineUsers() - 1); + } + + public static int getOnlineUsers() { return thisPlugin.getServer().getOnlinePlayers().size(); } + + public static int getMaximumAllowedPlayers() { return thisPlugin.getServer().getMaxPlayers(); } + public void ConfigSetup() { whitelisterBotConfigFile = new File(getDataFolder(), "discord-whitelister.yml");