From 8d508358f71d30ffa00d71526d968e65ecebafc3 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Mon, 25 Sep 2023 12:29:29 +0800 Subject: [PATCH] Implement gamerule reset --- .../core/commands/GameruleCommand.java | 62 +++++++++++++++---- .../multiverse/core/utils/MVCorei18n.java | 9 ++- .../resources/multiverse-core_en.properties | 26 +++++--- 3 files changed, 75 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/mvplugins/multiverse/core/commands/GameruleCommand.java b/src/main/java/org/mvplugins/multiverse/core/commands/GameruleCommand.java index 10566296..b3f68a1d 100644 --- a/src/main/java/org/mvplugins/multiverse/core/commands/GameruleCommand.java +++ b/src/main/java/org/mvplugins/multiverse/core/commands/GameruleCommand.java @@ -1,6 +1,10 @@ package org.mvplugins.multiverse.core.commands; -import co.aikar.commands.BukkitCommandIssuer; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; + import co.aikar.commands.CommandIssuer; import co.aikar.commands.InvalidCommandArgument; import co.aikar.commands.MessageType; @@ -34,9 +38,6 @@ 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") @@ -76,8 +77,8 @@ class GameruleCommand extends MultiverseCommand { @CommandCompletion("@gamerules true|false|@range:1-10 @mvworlds:multiple|*") @Syntax(" [World or *]") @Description("{@@mv-core.gamerule.set.description}") - void onGameruleCommand( - BukkitCommandIssuer issuer, + void onGameruleSetCommand( + MVCommandIssuer issuer, @Syntax("") @Description("{@@mv-core.gamerule.set.gamerule.description}") @@ -90,8 +91,7 @@ class GameruleCommand extends MultiverseCommand { @Flags("resolve=issuerAware") @Syntax("[World or *]") @Description("{@@mv-core.gamerule.set.world.description}") - LoadedMultiverseWorld[] worlds) - { + LoadedMultiverseWorld[] worlds) { Object value = gameRuleValue.getValue(); boolean success = true; for (LoadedMultiverseWorld world : worlds) { @@ -122,13 +122,53 @@ class GameruleCommand extends MultiverseCommand { } } + @Subcommand("reset") + @CommandPermission("multiverse.core.gamerule.set") + @CommandCompletion("@gamerules @mvworlds:multiple|*") + @Syntax(" [World or *]") + @Description("{@@mv-core.gamerule.reset.description}") + void onGameruleSetCommand( + MVCommandIssuer issuer, + + @Syntax("") + @Description("{@@mv-core.gamerule.reset.gamerule.description}") + GameRule gamerule, + + @Flags("resolve=issuerAware") + @Syntax("[World or *]") + @Description("{@@mv-core.gamerule.reset.world.description}") + LoadedMultiverseWorld[] worlds) { + AtomicBoolean success = new AtomicBoolean(true); + Arrays.stream(worlds).forEach(world -> world.getBukkitWorld().peek(bukkitWorld -> { + bukkitWorld.setGameRule(gamerule, bukkitWorld.getGameRuleDefault(gamerule)); + }).onEmpty(() -> { + success.set(false); + issuer.sendError(MVCorei18n.GAMERULE_RESET_FAILED, + "{gamerule}", gamerule.getName(), + "{world}", world.getName()); + })); + + // Tell user if it was successful + if (success.get()) { + if (worlds.length == 1) { + issuer.sendInfo(MVCorei18n.GAMERULE_RESET_SUCCESS_SINGLE, + "{gamerule}", gamerule.getName(), + "{world}", worlds[0].getName()); + } else if (worlds.length > 1) { + issuer.sendInfo(MVCorei18n.GAMERULE_RESET_SUCCESS_MULTIPLE, + "{gamerule}", gamerule.getName(), + "{count}", String.valueOf(worlds.length)); + } + } + } + @Subcommand("list") @CommandPermission("multiverse.core.gamerule.list") - @CommandCompletion("@mvworlds|@flags:groupName=mvgamerulescommand @flags:groupName=mvgamerulescommand") + @CommandCompletion("@mvworlds|@flags:groupName=mvgamerulecommand @flags:groupName=mvgamerulecommand") @Syntax("[world] [--page ] [--filter ]") @Description("{@@mv-core.gamerule.list.description}") - void onGamerulesCommand( - @NotNull MVCommandIssuer issuer, + void onGameruleListCommand( + MVCommandIssuer issuer, @Flags("resolve=issuerAware") @Syntax("") diff --git a/src/main/java/org/mvplugins/multiverse/core/utils/MVCorei18n.java b/src/main/java/org/mvplugins/multiverse/core/utils/MVCorei18n.java index 7006b9a5..6e10976c 100644 --- a/src/main/java/org/mvplugins/multiverse/core/utils/MVCorei18n.java +++ b/src/main/java/org/mvplugins/multiverse/core/utils/MVCorei18n.java @@ -49,12 +49,17 @@ public enum MVCorei18n implements MessageKeyProvider { DUMPS_DESCRIPTION, DUMPS_URL_LIST, - // gamerule command + // gamerule set command GAMERULE_SET_FAILED, GAMERULE_SET_SUCCESS_SINGLE, GAMERULE_SET_SUCCESS_MULTIPLE, - // Gamerules command + // gamerule reset command + GAMERULE_RESET_FAILED, + GAMERULE_RESET_SUCCESS_SINGLE, + GAMERULE_RESET_SUCCESS_MULTIPLE, + + // gamerule list command GAMERULE_LIST_DESCRIPTION, GAMERULE_LIST_DESCRIPTION_PAGE, GAMERULE_LIST_DESCRIPTION_WORLD, diff --git a/src/main/resources/multiverse-core_en.properties b/src/main/resources/multiverse-core_en.properties index c78be4b4..d3f6bced 100644 --- a/src/main/resources/multiverse-core_en.properties +++ b/src/main/resources/multiverse-core_en.properties @@ -60,19 +60,27 @@ mv-core.delete.success=&aWorld '{world}' deleted! 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.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 gamerule set +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 player's 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.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 gamerule reset +mv-core.gamerule.reset.description=Resets a gamerule in one or more worlds to it's default value. +mv-core.gamerule.reset.gamerule.description=Gamerule to reset. +mv-core.gamerule.reset.world.description=World to reset gamerule of, current player's world by default. +mv-core.gamerule.reset.failed=Failed to reset gamerule {gamerule} in {world}. +mv-core.gamerule.reset.success.single=&aSuccessfully reset {gamerule} in {world}. +mv-core.gamerule.reset.success.multiple=&aSuccessfully reset {gamerule} in {count} worlds. + +# /mv gamerule list +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