Merge /mv gamerule and /mv gamerules

This commit is contained in:
Zax71 2023-09-24 10:34:11 +01:00
parent 6611ec4457
commit 7c6d7d5883
4 changed files with 129 additions and 159 deletions

View File

@ -1,61 +1,104 @@
package org.mvplugins.multiverse.core.commands;
import co.aikar.commands.BukkitCommandIssuer;
import co.aikar.commands.CommandIssuer;
import co.aikar.commands.InvalidCommandArgument;
import co.aikar.commands.MessageType;
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.Flags;
import co.aikar.commands.annotation.Optional;
import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Syntax;
import jakarta.inject.Inject;
import org.bukkit.ChatColor;
import org.bukkit.GameRule;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;
import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer;
import org.mvplugins.multiverse.core.commandtools.MVCommandManager;
import org.mvplugins.multiverse.core.commandtools.MultiverseCommand;
import org.mvplugins.multiverse.core.commandtools.context.GameRuleValue;
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.utils.MVCorei18n;
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
import java.util.HashMap;
import java.util.Map;
@Service
@CommandAlias("mv")
@Subcommand("gamerule|rule|gamerules|rules")
class GameruleCommand extends MultiverseCommand {
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
GameruleCommand(@NotNull MVCommandManager commandManager) {
super(commandManager);
}
@Subcommand("gamerule")
@Subcommand("set")
@CommandPermission("multiverse.core.gamerule.set")
@CommandCompletion("@gamerules true|false|@range:1-10 @mvworlds:multiple|*")
@Syntax("<Gamerule> <Gamerule value> [World or *]")
@Description("{@@mv-core.gamerule.description}")
@Description("{@@mv-core.gamerule.set.description}")
void onGameruleCommand(
BukkitCommandIssuer issuer,
@Syntax("<Gamerule>")
@Description("{@@mv-core.gamerule.gamerule.description}")
@Description("{@@mv-core.gamerule.set.gamerule.description}")
GameRule gamerule,
@Syntax("<Value>")
@Description("{@@mv-core.gamerule.value.description}")
@Description("{@@mv-core.gamerule.set.value.description}")
GameRuleValue gameRuleValue,
@Flags("resolve=issuerAware")
@Syntax("[World or *]")
@Description("{@@mv-core.gamerule.world.description}")
LoadedMultiverseWorld[] worlds) {
@Description("{@@mv-core.gamerule.set.world.description}")
LoadedMultiverseWorld[] worlds)
{
Object value = gameRuleValue.getValue();
boolean success = true;
for (LoadedMultiverseWorld world : worlds) {
// Set gamerules and add false to list if it fails
World bukkitWorld = world.getBukkitWorld().getOrNull();
if (bukkitWorld == null || !bukkitWorld.setGameRule(gamerule, value)) {
issuer.sendError(MVCorei18n.GAMERULE_FAILED,
issuer.sendError(MVCorei18n.GAMERULE_SET_FAILED,
"{gamerule}", gamerule.getName(),
"{value}", value.toString(),
"{world}", world.getName(),
@ -66,16 +109,75 @@ class GameruleCommand extends MultiverseCommand {
// Tell user if it was successful
if (success) {
if (worlds.length == 1) {
issuer.sendInfo(MVCorei18n.GAMERULE_SUCCESS_SINGLE,
issuer.sendInfo(MVCorei18n.GAMERULE_SET_SUCCESS_SINGLE,
"{gamerule}", gamerule.getName(),
"{value}", value.toString(),
"{world}", worlds[0].getName());
} else if (worlds.length > 1) {
issuer.sendInfo(MVCorei18n.GAMERULE_SUCCESS_MULTIPLE,
issuer.sendInfo(MVCorei18n.GAMERULE_SET_SUCCESS_MULTIPLE,
"{gamerule}", gamerule.getName(),
"{value}", value.toString(),
"{count}", String.valueOf(worlds.length));
}
}
}
@Subcommand("list")
@CommandPermission("multiverse.core.gamerule.list")
@CommandCompletion("@mvworlds|@flags:groupName=mvgamerulescommand @flags:groupName=mvgamerulescommand")
@Syntax("[world] [--page <page>] [--filter <filter>]")
@Description("{@@mv-core.gamerule.list.description}")
void onGamerulesCommand(
@NotNull MVCommandIssuer issuer,
@Flags("resolve=issuerAware")
@Syntax("<world>")
@Description("{@@mv-core.gamerule.list.description.world}")
LoadedMultiverseWorld world,
@Optional
@Syntax("[--page <page>] [--filter <filter>]")
@Description("{@@mv-core.gamerule.list.description.page}")
String[] flags) {
ParsedCommandFlags parsedFlags = parseFlags(flags);
ContentDisplay.create()
.addContent(MapContentProvider.forContent(getGameRuleMap(world.getBukkitWorld().getOrNull())) // TODO: Handle null
.withKeyColor(ChatColor.AQUA)
.withValueColor(ChatColor.WHITE))
.withSendHandler(PagedSendHandler.create()
.withHeader(this.getListTitle(issuer, world.getBukkitWorld().getOrNull()))
.doPagination(true)
.withTargetPage(parsedFlags.flagValue(PAGE_FLAG, 1))
.withFilter(parsedFlags.flagValue(FILTER_FLAG, DefaultContentFilter.get())))
.send(issuer);
}
/**
* Gets all the gamerules and their values for a given world.
*
* @param world The world to find gamerules for.
* @return A map of the gamerules and their values
*/
private Map<String, String> getGameRuleMap(World world) {
Map<String, String> gameRuleMap = new HashMap<>();
for (GameRule<?> gamerule : GameRule.values()) {
Object gameruleValue = world.getGameRuleValue(gamerule);
if (gameruleValue == null) {
gameRuleMap.put(gamerule.getName(), "null");
continue;
}
gameRuleMap.put(gamerule.getName(), gameruleValue.toString());
}
return gameRuleMap;
}
private String getListTitle(CommandIssuer issuer, World world) {
return this.commandManager.formatMessage(
issuer,
MessageType.INFO,
MVCorei18n.GAMERULE_LIST_TITLE,
"{world}", world.getName());
}
}

View File

@ -1,132 +0,0 @@
package org.mvplugins.multiverse.core.commands;
import java.util.HashMap;
import java.util.Map;
import co.aikar.commands.CommandIssuer;
import co.aikar.commands.InvalidCommandArgument;
import co.aikar.commands.MessageType;
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.Flags;
import co.aikar.commands.annotation.Optional;
import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Syntax;
import jakarta.inject.Inject;
import org.bukkit.ChatColor;
import org.bukkit.GameRule;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;
import org.jvnet.hk2.annotations.Service;
import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer;
import org.mvplugins.multiverse.core.commandtools.MVCommandManager;
import org.mvplugins.multiverse.core.commandtools.MultiverseCommand;
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.utils.MVCorei18n;
import org.mvplugins.multiverse.core.world.LoadedMultiverseWorld;
/**
* List all gamerules in your current or specified world.
*/
@Service
@CommandAlias("mv")
class GamerulesCommand extends MultiverseCommand {
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
GamerulesCommand(@NotNull MVCommandManager commandManager) {
super(commandManager);
}
@Subcommand("gamerules|rules")
@CommandPermission("multiverse.core.gamerule.list")
@CommandCompletion("@mvworlds|@flags:groupName=mvgamerulescommand @flags:groupName=mvgamerulescommand")
@Syntax("[world] [--page <page>] [--filter <filter>]")
@Description("{@@mv-core.gamerules.description}")
void onGamerulesCommand(
@NotNull MVCommandIssuer issuer,
@Flags("resolve=issuerAware")
@Syntax("<world>")
@Description("{@@mv-core.gamerules.description.world}")
LoadedMultiverseWorld world,
@Optional
@Syntax("[--page <page>] [--filter <filter>]")
@Description("{@@mv-core.gamerules.description.page}")
String[] flags) {
ParsedCommandFlags parsedFlags = parseFlags(flags);
ContentDisplay.create()
.addContent(MapContentProvider.forContent(getGameRuleMap(world.getBukkitWorld().getOrNull())) // TODO: Handle null
.withKeyColor(ChatColor.AQUA)
.withValueColor(ChatColor.WHITE))
.withSendHandler(PagedSendHandler.create()
.withHeader(this.getTitle(issuer, world.getBukkitWorld().getOrNull()))
.doPagination(true)
.withTargetPage(parsedFlags.flagValue(PAGE_FLAG, 1))
.withFilter(parsedFlags.flagValue(FILTER_FLAG, DefaultContentFilter.get())))
.send(issuer);
}
/**
* Gets all the gamerules and their values for a given world.
*
* @param world The world to find gamerules for.
* @return A map of the gamerules and their values
*/
private Map<String, String> getGameRuleMap(World world) {
Map<String, String> gameRuleMap = new HashMap<>();
for (GameRule<?> gamerule : GameRule.values()) {
Object gameruleValue = world.getGameRuleValue(gamerule);
if (gameruleValue == null) {
gameRuleMap.put(gamerule.getName(), "null");
continue;
}
gameRuleMap.put(gamerule.getName(), gameruleValue.toString());
}
return gameRuleMap;
}
private String getTitle(CommandIssuer issuer, World world) {
return this.commandManager.formatMessage(
issuer,
MessageType.INFO,
MVCorei18n.GAMERULES_TITLE,
"{world}", world.getName());
}
}

View File

@ -50,15 +50,15 @@ public enum MVCorei18n implements MessageKeyProvider {
DUMPS_URL_LIST,
// gamerule command
GAMERULE_FAILED,
GAMERULE_SUCCESS_SINGLE,
GAMERULE_SUCCESS_MULTIPLE,
GAMERULE_SET_FAILED,
GAMERULE_SET_SUCCESS_SINGLE,
GAMERULE_SET_SUCCESS_MULTIPLE,
// Gamerules command
GAMERULES_DESCRIPTION,
GAMERULES_DESCRIPTION_PAGE,
GAMERULES_DESCRIPTION_WORLD,
GAMERULES_TITLE,
GAMERULE_LIST_DESCRIPTION,
GAMERULE_LIST_DESCRIPTION_PAGE,
GAMERULE_LIST_DESCRIPTION_WORLD,
GAMERULE_LIST_TITLE,
// import command
IMPORT_IMPORTING,

View File

@ -61,19 +61,19 @@ mv-core.dumps.description=Dumps version info to the console or paste services
mv-core.dumps.url.list={service} : {link}
# /mv gamerule
mv-core.gamerule.description=Changes a gamerule in one or more worlds
mv-core.gamerule.gamerule.description=Gamerule to set
mv-core.gamerule.value.description=Value of gamerule
mv-core.gamerule.world.description=World to apply gamerule to, current world by default
mv-core.gamerule.failed=Failed to set gamerule {gamerule} to {value} in {world}. &fIt should be a {type}.
mv-core.gamerule.success.single=&aSuccessfully set {gamerule} to {value} in {world}.
mv-core.gamerule.success.multiple=&aSuccessfully set {gamerule} to {value} in {count} worlds.
mv-core.gamerule.set.description=Changes a gamerule in one or more worlds
mv-core.gamerule.set.gamerule.description=Gamerule to set
mv-core.gamerule.set.value.description=Value of gamerule
mv-core.gamerule.set.world.description=World to apply gamerule to, current world by default
mv-core.gamerule.set.failed=Failed to set gamerule {gamerule} to {value} in {world}. &fIt should be a {type}.
mv-core.gamerule.set.success.single=&aSuccessfully set {gamerule} to {value} in {world}.
mv-core.gamerule.set.success.multiple=&aSuccessfully set {gamerule} to {value} in {count} worlds.
# /mv gamerules
mv-core.gamerules.description=Lists gamerules for the specified world
mv-core.gamerules.description.page=The page to view
mv-core.gamerules.description.world=The world to list gamerules in
mv-core.gamerules.title= --- Gamerules for {world} ---
mv-core.gamerule.list.description=Lists gamerules for the specified world
mv-core.gamerule.list.description.page=The page to view
mv-core.gamerule.list.description.world=The world to list gamerules in
mv-core.gamerule.list.title= --- Gamerules for {world} ---
# /mv import
mv-core.import.description=Imports an existing world folder.