Skip to content

Commit

Permalink
Merge pull request #3055 from Multiverse/zax71/MV5/whoCommand
Browse files Browse the repository at this point in the history
Add `/mv who` and `/mv whoall`
  • Loading branch information
benwoo1110 authored Aug 28, 2024
2 parents 833c33e + 7b20cd6 commit 533e843
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 0 deletions.
162 changes: 162 additions & 0 deletions src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package org.mvplugins.multiverse.core.commands;

import co.aikar.commands.InvalidCommandArgument;
import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandCompletion;
import co.aikar.commands.annotation.CommandPermission;
import co.aikar.commands.annotation.Description;
import co.aikar.commands.annotation.Optional;
import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Syntax;
import com.dumptruckman.minecraft.util.Logging;
import jakarta.inject.Inject;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.commandtools.MultiverseCommand;
import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer;
import org.mvplugins.multiverse.core.commandtools.MVCommandManager;
import org.mvplugins.multiverse.core.commandtools.flags.CommandValueFlag;
import org.mvplugins.multiverse.core.commandtools.flags.ParsedCommandFlags;
import org.mvplugins.multiverse.core.display.ContentDisplay;
import org.mvplugins.multiverse.core.display.filters.ContentFilter;
import org.mvplugins.multiverse.core.display.filters.DefaultContentFilter;
import org.mvplugins.multiverse.core.display.filters.RegexContentFilter;
import org.mvplugins.multiverse.core.display.handlers.PagedSendHandler;
import org.mvplugins.multiverse.core.display.parsers.MapContentProvider;
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
import org.mvplugins.multiverse.core.world.WorldManager;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
@CommandAlias("mv")
public class WhoCommand extends MultiverseCommand {

private final WorldManager worldManager;

private final CommandValueFlag<Integer> PAGE_FLAG = flag(CommandValueFlag
.builder("--page", Integer.class)
.addAlias("-p")
.context(value -> {
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
throw new InvalidCommandArgument("Invalid page number: " + value);
}
})
.build());

private final CommandValueFlag<ContentFilter> FILTER_FLAG = flag(CommandValueFlag
.builder("--filter", ContentFilter.class)
.addAlias("-f")
.context(value -> {
try {
return RegexContentFilter.fromString(value);
} catch (IllegalArgumentException e) {
throw new InvalidCommandArgument("Invalid filter: " + value);
}
})
.build());

@Inject
WhoCommand(@NotNull MVCommandManager commandManager, @NotNull WorldManager worldManager) {
super(commandManager);
this.worldManager = worldManager;
}

@Subcommand("whoall")
@CommandPermission("multiverse.core.list.who.all")
@CommandCompletion("@flags:groupName=mvwhocommand")
@Syntax("[--page <page>] [--filter <filter>]")
@Description("{@@mv-core.who.all.description}")
void onWhoAllCommand(
MVCommandIssuer issuer,

@Optional
@Syntax("[--page <page>] [--filter <filter>]")
@Description("{@@mv-core.who.flags.description}")
String[] flags) {
ParsedCommandFlags parsedFlags = parseFlags(flags);

// Send the display
getListDisplay(
worldManager.getLoadedWorlds(),
parsedFlags.flagValue(PAGE_FLAG, 1),
parsedFlags.flagValue(FILTER_FLAG, DefaultContentFilter.get()),
true
).send(issuer);

}

@Subcommand("who")
@CommandPermission("multiverse.core.list.who")
@CommandCompletion("@mvworlds:scope=both @flags:groupName=mvwhocommand")
@Syntax("<world> [--page <page>] [--filter <filter>]")
@Description("{@@mv-core.who.description}")
void onWhoCommand(
MVCommandIssuer issuer,

@Syntax("<world>")
@Description("{@@mv-core.who.world.description}")
LoadedMultiverseWorld inputtedWorld,

@Optional
@Syntax("[--page <page>] [--filter <filter>]")
@Description("{@@mv-core.who.flags.description}")
String[] flags) {
ParsedCommandFlags parsedFlags = parseFlags(flags);

// Send the display
getListDisplay(
inputtedWorld,
parsedFlags.flagValue(PAGE_FLAG, 1),
parsedFlags.flagValue(FILTER_FLAG, DefaultContentFilter.get()),
false
).send(issuer);
}

private String phrasePlayerList(List<Player> players) {
return players.stream().map(Player::getName).collect(Collectors.joining(", "));
}

private ContentDisplay getListDisplay(LoadedMultiverseWorld world, int page, ContentFilter filter, boolean ignoreEmptyWorlds) {
Collection<LoadedMultiverseWorld> listingWorlds = new ArrayList<>();
listingWorlds.add(world);
return getListDisplay(listingWorlds, page, filter, ignoreEmptyWorlds);
}

private ContentDisplay getListDisplay(Collection<LoadedMultiverseWorld> worlds, int page, ContentFilter filter, boolean ignoreEmptyWorlds) {
HashMap<String, String> outMap = new HashMap<>();

// Add all the worlds to our hashmap
for (LoadedMultiverseWorld world : worlds) {
@Nullable List<Player> players = world.getPlayers().getOrNull();

// If the world has 0 players in it, say that it is empty
if ((players == null || players.isEmpty()) && !ignoreEmptyWorlds) {
outMap.put(world.getAlias(), ChatColor.RED + "Empty");
continue;
}
if (players == null || players.isEmpty()) {
continue;
}

outMap.put(world.getAlias(), phrasePlayerList(players));
}

return ContentDisplay.create()
.addContent(MapContentProvider.forContent(outMap))
.withSendHandler(PagedSendHandler.create()
.withHeader("%s====[ Multiverse World Players List ]====", ChatColor.AQUA)
.doPagination(true)
.withTargetPage(page)
.withFilter(filter));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ public enum MVCorei18n implements MessageKeyProvider {
UNLOAD_UNLOADING,
UNLOAD_SUCCESS,

// who command
WHO_DESCRIPTION,
WHO_ALL_DESCRIPTION,
WHO_WORLD_DESCRIPTION,
WHO_FLAGS_DESCRIPTION,
WHO_EMPTY,

// version command
VERSION_MV,
VERSION_AUTHORS,
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/multiverse-core_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ mv-core.unload.success=&aWorld '{world}' unloaded!
# /mv usage
mv-core.usage.description=Show Multiverse-Core command usage.

# /mv who
# /mv whoall
mv-core.who.description=Lists the players in the world specified
mv-core.who.all.description=Lists the players in all worlds
mv-core.who.world.description=Name of the world you want to list players in
mv-core.who.flags.description=Filter - only shows entries matching this. Page - the page to show
mv-core.who.empty=&rEmpty

# /mv version
mv-core.version.description=Displays version and authors
mv-core.version.mv=Multiverse Core Version &fv{version}
Expand Down

0 comments on commit 533e843

Please sign in to comment.