Allow /mv list to show world names instead of aliases

This commit is contained in:
Zax71 2024-08-27 18:51:30 +01:00
parent 278c51d056
commit fef9ba44cd

View File

@ -19,6 +19,7 @@ import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer; import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer;
import org.mvplugins.multiverse.core.commandtools.MVCommandManager; import org.mvplugins.multiverse.core.commandtools.MVCommandManager;
import org.mvplugins.multiverse.core.commandtools.MultiverseCommand; import org.mvplugins.multiverse.core.commandtools.MultiverseCommand;
import org.mvplugins.multiverse.core.commandtools.flags.CommandFlag;
import org.mvplugins.multiverse.core.commandtools.flags.CommandValueFlag; import org.mvplugins.multiverse.core.commandtools.flags.CommandValueFlag;
import org.mvplugins.multiverse.core.commandtools.flags.ParsedCommandFlags; import org.mvplugins.multiverse.core.commandtools.flags.ParsedCommandFlags;
import org.mvplugins.multiverse.core.display.ContentDisplay; import org.mvplugins.multiverse.core.display.ContentDisplay;
@ -28,6 +29,7 @@ import org.mvplugins.multiverse.core.display.filters.RegexContentFilter;
import org.mvplugins.multiverse.core.display.handlers.PagedSendHandler; import org.mvplugins.multiverse.core.display.handlers.PagedSendHandler;
import org.mvplugins.multiverse.core.display.parsers.ListContentProvider; import org.mvplugins.multiverse.core.display.parsers.ListContentProvider;
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld; import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
import org.mvplugins.multiverse.core.world.MultiverseWorld;
import org.mvplugins.multiverse.core.world.WorldManager; import org.mvplugins.multiverse.core.world.WorldManager;
import org.mvplugins.multiverse.core.world.entrycheck.WorldEntryChecker; import org.mvplugins.multiverse.core.world.entrycheck.WorldEntryChecker;
import org.mvplugins.multiverse.core.world.entrycheck.WorldEntryCheckerProvider; import org.mvplugins.multiverse.core.world.entrycheck.WorldEntryCheckerProvider;
@ -63,6 +65,10 @@ class ListCommand extends MultiverseCommand {
}) })
.build()); .build());
private final CommandFlag RAW_FLAG = flag(CommandFlag.builder("--raw")
.addAlias("-r")
.build());
@Inject @Inject
ListCommand( ListCommand(
@NotNull MVCommandManager commandManager, @NotNull MVCommandManager commandManager,
@ -76,7 +82,7 @@ class ListCommand extends MultiverseCommand {
@Subcommand("list") @Subcommand("list")
@CommandPermission("multiverse.core.list.worlds") @CommandPermission("multiverse.core.list.worlds")
@CommandCompletion("@flags:groupName=mvlistcommand") @CommandCompletion("@flags:groupName=mvlistcommand")
@Syntax("--filter [filter] --page [page]") @Syntax("--filter [filter] --page [page] --raw")
@Description("Displays a listing of all worlds that you can enter.") @Description("Displays a listing of all worlds that you can enter.")
public void onListCommand( public void onListCommand(
MVCommandIssuer issuer, MVCommandIssuer issuer,
@ -86,7 +92,7 @@ class ListCommand extends MultiverseCommand {
String[] flags) { String[] flags) {
ParsedCommandFlags parsedFlags = parseFlags(flags); ParsedCommandFlags parsedFlags = parseFlags(flags);
ContentDisplay.create() ContentDisplay.create()
.addContent(ListContentProvider.forContent(getListContents(issuer))) .addContent(ListContentProvider.forContent(getListContents(issuer, parsedFlags.hasFlag(RAW_FLAG))))
.withSendHandler(PagedSendHandler.create() .withSendHandler(PagedSendHandler.create()
.withHeader("%s====[ Multiverse World List ]====", ChatColor.GOLD) .withHeader("%s====[ Multiverse World List ]====", ChatColor.GOLD)
.withTargetPage(parsedFlags.flagValue(PAGE_FLAG, 1)) .withTargetPage(parsedFlags.flagValue(PAGE_FLAG, 1))
@ -94,26 +100,40 @@ class ListCommand extends MultiverseCommand {
.send(issuer); .send(issuer);
} }
private List<String> getListContents(MVCommandIssuer issuer) { private List<String> getListContents(MVCommandIssuer issuer, boolean useRawNames) {
List<String> worldList = new ArrayList<>(); List<String> worldList = new ArrayList<>();
WorldEntryChecker worldEntryChecker = worldEntryCheckerProvider.forSender(issuer.getIssuer()); WorldEntryChecker worldEntryChecker = worldEntryCheckerProvider.forSender(issuer.getIssuer());
worldManager.getLoadedWorlds().stream() worldManager.getLoadedWorlds().stream()
.filter(world -> worldEntryChecker.canAccessWorld(world).isSuccess()) .filter(world -> worldEntryChecker.canAccessWorld(world).isSuccess())
.filter(world -> canSeeWorld(issuer, world)) .filter(world -> canSeeWorld(issuer, world))
.map(world -> hiddenText(world) + world.getAlias() + " - " + parseColouredEnvironment(world.getEnvironment())) .map(world -> hiddenText(world) + getWorldName(world, useRawNames) + " - " + parseColouredEnvironment(world.getEnvironment()))
.sorted() .sorted()
.forEach(worldList::add); .forEach(worldList::add);
worldManager.getUnloadedWorlds().stream() worldManager.getUnloadedWorlds().stream()
.filter(world -> worldEntryChecker.canAccessWorld(world).isSuccess()) .filter(world -> worldEntryChecker.canAccessWorld(world).isSuccess())
.map(world -> ChatColor.GRAY + world.getAlias() + " - UNLOADED") .map(world -> ChatColor.GRAY + getWorldName(world, useRawNames) + " - UNLOADED")
.sorted() .sorted()
.forEach(worldList::add); .forEach(worldList::add);
return worldList; return worldList;
} }
/**
* Gets a world's name or alias
* @param world The world to retrieve the name of
* @param useRawNames True to return the name, false to return the alias
* @return The name
*/
private String getWorldName(MultiverseWorld world, boolean useRawNames) {
if (useRawNames) {
return world.getName();
}
return world.getAlias();
}
private boolean canSeeWorld(MVCommandIssuer issuer, LoadedMultiverseWorld world) { private boolean canSeeWorld(MVCommandIssuer issuer, LoadedMultiverseWorld world) {
return !world.isHidden() return !world.isHidden()
|| issuer.hasPermission("multiverse.core.modify"); // TODO: Refactor stray permission check || issuer.hasPermission("multiverse.core.modify"); // TODO: Refactor stray permission check