Added config option for showing player count. Moved join & leave Events to a new class.

This commit is contained in:
JoeShimell 2020-04-20 13:45:11 +01:00
parent dda4a8f33e
commit be4bc8fff5
3 changed files with 53 additions and 17 deletions

View File

@ -102,15 +102,20 @@ public class DiscordClient extends ListenerAdapter {
removeCommandInfo = embedBuilderInfo.build();
}
public static String getOnlineStatus() {
try {
public static String getOnlineStatus()
{
try
{
return javaDiscordAPI.getStatus().name();
} catch(NullPointerException ex) {
}
catch(NullPointerException ex)
{
return "OFFLINE";
}
}
public static void onServerPlayerCountChange(int playerCount) {
public static void SetPlayerCountStatus(int playerCount)
{
javaDiscordAPI.getPresence().setActivity(Activity.watching(playerCount + "/" + DiscordWhitelister.getMaximumAllowedPlayers() + " players."));
}

View File

@ -11,12 +11,13 @@ 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;
import uk.co.angrybee.joe.Events.JoinLeaveEvents;
import java.io.File;
import java.io.IOException;
import java.util.*;
public class DiscordWhitelister extends JavaPlugin implements Listener
public class DiscordWhitelister extends JavaPlugin
{
private static File whitelisterBotConfigFile;
private static File userListFile;
@ -118,9 +119,14 @@ public class DiscordWhitelister extends JavaPlugin implements Listener
}
}
this.getServer().getPluginManager().registerEvents(this, this);
DiscordClient.onServerPlayerCountChange(getOnlineUsers());
if(getWhitelisterBotConfig().getBoolean("show-player-count"))
{
// Register events if enabled
getServer().getPluginManager().registerEvents(new JoinLeaveEvents(), this);
// Set initial player count
DiscordClient.SetPlayerCountStatus(getOnlineUsers());
}
}
public static JavaPlugin getPlugin()
@ -179,16 +185,6 @@ public class DiscordWhitelister extends JavaPlugin implements Listener
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(); }
@ -400,6 +396,16 @@ public class DiscordWhitelister extends JavaPlugin implements Listener
}
}
if(getWhitelisterBotConfig().get("show-player-count") == null)
{
getWhitelisterBotConfig().set("show-player-count", true);
if(!configCreated)
{
getPlugin().getLogger().warning("Entry 'show-player-count' was not found, adding it to the config...");
}
}
try
{
getWhitelisterBotConfig().save((whitelisterBotConfigFile.getPath()));

View File

@ -0,0 +1,25 @@
package uk.co.angrybee.joe.Events;
import uk.co.angrybee.joe.DiscordWhitelister;
import uk.co.angrybee.joe.DiscordClient;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
// Used for showing player count in the discord bots status
public class JoinLeaveEvents implements Listener
{
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event)
{
DiscordClient.SetPlayerCountStatus(DiscordWhitelister.getOnlineUsers());
}
@EventHandler
public void onPlayerLeave(PlayerQuitEvent event)
{
DiscordClient.SetPlayerCountStatus(DiscordWhitelister.getOnlineUsers() - 1);
}
}