Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /mv who and /mv whoall #3055

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

Check warning on line 11 in src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Unused import - com.dumptruckman.minecraft.util.Logging. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java:11:8: warning: Unused import - com.dumptruckman.minecraft.util.Logging. (com.puppycrawl.tools.checkstyle.checks.imports.UnusedImportsCheck)
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;

Check warning on line 18 in src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 'org.mvplugins.multiverse.core.commandtools.MultiverseCommand' should be separated from previous imports. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java:18:1: warning: 'org.mvplugins.multiverse.core.commandtools.MultiverseCommand' should be separated from previous imports. (com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck)
import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer;

Check warning on line 19 in src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Wrong order for 'org.mvplugins.multiverse.core.commandtools.MVCommandIssuer' import. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java:19:1: warning: Wrong order for 'org.mvplugins.multiverse.core.commandtools.MVCommandIssuer' import. (com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck)
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;

Check warning on line 32 in src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Wrong order for 'java.util.ArrayList' import. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java:32:1: warning: Wrong order for 'java.util.ArrayList' import. (com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck)
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service

Check warning on line 38 in src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java:38:1: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck)

Check warning on line 38 in src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Class Fan-Out Complexity is 21 (max allowed is 20). Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java:38:1: info: Class Fan-Out Complexity is 21 (max allowed is 20). (FanOutComplexityGettingTooHigh)
@CommandAlias("mv")
public class WhoCommand extends MultiverseCommand {

private final WorldManager worldManager;

private final CommandValueFlag<Integer> PAGE_FLAG = flag(CommandValueFlag

Check warning on line 44 in src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Abbreviation in name 'PAGE_FLAG' must contain no more than '1' consecutive capital letters. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java:44:45: warning: Abbreviation in name 'PAGE_FLAG' must contain no more than '1' consecutive capital letters. (com.puppycrawl.tools.checkstyle.checks.naming.AbbreviationAsWordInNameCheck)
.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

Check warning on line 56 in src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Abbreviation in name 'FILTER_FLAG' must contain no more than '1' consecutive capital letters. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java:56:51: warning: Abbreviation in name 'FILTER_FLAG' must contain no more than '1' consecutive capital letters. (com.puppycrawl.tools.checkstyle.checks.naming.AbbreviationAsWordInNameCheck)
.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")

Check warning on line 74 in src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Class 'WhoCommand' looks like designed for extension (can be subclassed), but the method 'onWhoAllCommand' does not have javadoc that explains how to do that safely. If class is not designed for extension consider making the class 'WhoCommand' final or making the method 'onWhoAllCommand' static/final/abstract/empty, or adding allowed annotation for the method. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java:74:5: info: Class 'WhoCommand' looks like designed for extension (can be subclassed), but the method 'onWhoAllCommand' does not have javadoc that explains how to do that safely. If class is not designed for extension consider making the class 'WhoCommand' final or making the method 'onWhoAllCommand' static/final/abstract/empty, or adding allowed annotation for the method. (com.puppycrawl.tools.checkstyle.checks.design.DesignForExtensionCheck)
@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);

Check warning on line 94 in src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 ')' should be on the previous line. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/commands/WhoCommand.java:94:9: warning: ')' should be on the previous line. (SeparatorWrapEol)

}

@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
Loading