Refactored player count methods to a new class MinecraftInformationHandler;
This class should handle all complex code that needs to be used at more than one place (like player count). Signed-off-by: Mijago <afruehwald@mijago.de>master
parent
97f2b06cb8
commit
64db9374bc
|
@ -16,11 +16,7 @@ package chikachi.discord;
|
|||
|
||||
import chikachi.discord.core.DiscordClient;
|
||||
import chikachi.discord.core.DiscordIntegrationLogger;
|
||||
import chikachi.discord.core.TextFormatter;
|
||||
import chikachi.discord.core.config.Configuration;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraftforge.fml.common.FMLCommonHandler;
|
||||
|
||||
import static java.lang.Thread.sleep;
|
||||
|
||||
|
@ -47,12 +43,6 @@ public class DiscordThread implements Runnable {
|
|||
if (!Configuration.getConfig().discord.presence.enabled)
|
||||
return;
|
||||
|
||||
String[] players = (String[]) FMLCommonHandler.instance().getMinecraftServerInstance()
|
||||
.getPlayerList().getPlayers().stream()
|
||||
.filter(player -> !player.getDisplayNameString().startsWith("@"))
|
||||
.filter(EntityPlayerMP.class::isInstance)
|
||||
.map(EntityPlayer::getDisplayNameString).toArray();
|
||||
|
||||
DiscordClient.getInstance().setDiscordPresencePlayerCount(players);
|
||||
DiscordClient.getInstance().setDiscordPresencePlayerCount(MinecraftInformationHandler.getOnlineRealPlayerNames());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
package chikachi.discord;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraftforge.fml.common.FMLCommonHandler;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public abstract class MinecraftInformationHandler {
|
||||
public static Stream<EntityPlayerMP> getOnlineRealPlayerStream() {
|
||||
return FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers()
|
||||
.stream()
|
||||
.filter(player -> !player.getDisplayNameString().startsWith("@"))
|
||||
.filter(EntityPlayerMP.class::isInstance);
|
||||
}
|
||||
|
||||
public static String[] getOnlineRealPlayerNames() {
|
||||
return (String[]) MinecraftInformationHandler
|
||||
.getOnlineRealPlayerStream()
|
||||
.map(EntityPlayer::getDisplayNameString).toArray();
|
||||
}
|
||||
|
||||
public static long getOnlineRealPlayerCount() {
|
||||
return getOnlineRealPlayerStream().count();
|
||||
}
|
||||
}
|
|
@ -15,19 +15,16 @@
|
|||
package chikachi.discord.command;
|
||||
|
||||
import chikachi.discord.DiscordCommandSender;
|
||||
import chikachi.discord.MinecraftInformationHandler;
|
||||
import com.google.common.base.Joiner;
|
||||
import mcp.MethodsReturnNonnullByDefault;
|
||||
import net.minecraft.command.CommandBase;
|
||||
import net.minecraft.command.CommandException;
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.util.text.TextComponentString;
|
||||
import net.minecraftforge.fml.common.FMLCommonHandler;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ParametersAreNonnullByDefault
|
||||
@MethodsReturnNonnullByDefault
|
||||
|
@ -46,46 +43,24 @@ class SubCommandOnline extends CommandBase {
|
|||
public void execute(MinecraftServer server, ICommandSender sender, String[] strings) throws CommandException {
|
||||
boolean isDiscord = sender instanceof DiscordCommandSender;
|
||||
|
||||
List<String> playerNames = new ArrayList<>();
|
||||
String[] playerNames = MinecraftInformationHandler.getOnlineRealPlayerNames();
|
||||
int playersOnline = playerNames.length;
|
||||
|
||||
List<EntityPlayerMP> players = FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers();
|
||||
|
||||
for (EntityPlayerMP player : players) {
|
||||
String playerName = player.getDisplayNameString();
|
||||
if (playerName.startsWith("@")) {
|
||||
continue;
|
||||
}
|
||||
playerNames.add(playerName);
|
||||
}
|
||||
|
||||
int playersOnline = playerNames.size();
|
||||
if (playersOnline == 0) {
|
||||
sender.sendMessage(
|
||||
new TextComponentString("No players online")
|
||||
);
|
||||
return;
|
||||
}
|
||||
String message = "No players online";
|
||||
|
||||
if (playersOnline == 1) {
|
||||
sender.sendMessage(
|
||||
new TextComponentString(
|
||||
String.format(
|
||||
isDiscord ? "Currently 1 player online: `%s`" : "Currently 1 player online: %s",
|
||||
playerNames.get(0)
|
||||
)
|
||||
)
|
||||
message = String.format(
|
||||
isDiscord ? "Currently 1 player online: `%s`" : "Currently 1 player online: %s",
|
||||
playerNames[0]
|
||||
);
|
||||
} else if (playersOnline > 1) {
|
||||
message = String.format(
|
||||
isDiscord ? "Currently %d players online:\n`%s`" : "Currently %d players online:\n%s",
|
||||
playersOnline,
|
||||
Joiner.on(isDiscord ? "`, `" : ", ").join(playerNames)
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
sender.sendMessage(
|
||||
new TextComponentString(
|
||||
String.format(
|
||||
isDiscord ? "Currently %d players online:\n`%s`" : "Currently %d players online:\n%s",
|
||||
playersOnline,
|
||||
Joiner.on(isDiscord ? "`, `" : ", ").join(playerNames)
|
||||
)
|
||||
)
|
||||
);
|
||||
sender.sendMessage(new TextComponentString(message));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue