From 4b347326417bbd60531cfa22335713236740c52d Mon Sep 17 00:00:00 2001 From: acrylic-style Date: Fri, 6 Aug 2021 01:50:07 +0900 Subject: [PATCH] 2.0.0 - add setorder sub-command to game (it breaks compatibility because the game with old version will not work without leaving any errors) --- .../dailyranking/api/game/Game.kt | 4 +++ .../dailyranking/api/game/SortOrder.kt | 8 ++++++ .../test/java/api/game/GameImpl.java | 7 ++++++ build.gradle.kts | 2 +- .../plugin/DailyRankingBoardPlugin.kt | 19 ++++++++++++++ .../plugin/argument/GameArgument.kt | 2 +- .../plugin/argument/OrderArgument.kt | 25 +++++++++++++++++++ .../plugin/configuration/UserCacheFile.kt | 1 + .../plugin/game/SerializableGame.kt | 12 +++++++-- .../plugin/util/PlayerArmorStandData.kt | 2 +- plugin/src/main/resources/plugin.yml | 8 ++++-- 11 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 api/src/main/kotlin/xyz/acrylicstyle/dailyranking/api/game/SortOrder.kt create mode 100644 plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/argument/OrderArgument.kt diff --git a/api/src/main/kotlin/xyz/acrylicstyle/dailyranking/api/game/Game.kt b/api/src/main/kotlin/xyz/acrylicstyle/dailyranking/api/game/Game.kt index 263cef0..9e0c90e 100644 --- a/api/src/main/kotlin/xyz/acrylicstyle/dailyranking/api/game/Game.kt +++ b/api/src/main/kotlin/xyz/acrylicstyle/dailyranking/api/game/Game.kt @@ -13,10 +13,14 @@ interface Game: MapSerializable { */ val name: String + val order: SortOrder + get() = SortOrder.ASC + fun getValueToStringFunction(value: Int): String = value.toString() override fun getAsMap(): Map = mapOf( "id" to id, "name" to name, + "order" to order.name, ) } diff --git a/api/src/main/kotlin/xyz/acrylicstyle/dailyranking/api/game/SortOrder.kt b/api/src/main/kotlin/xyz/acrylicstyle/dailyranking/api/game/SortOrder.kt new file mode 100644 index 0000000..6046039 --- /dev/null +++ b/api/src/main/kotlin/xyz/acrylicstyle/dailyranking/api/game/SortOrder.kt @@ -0,0 +1,8 @@ +package xyz.acrylicstyle.dailyranking.api.game + +import java.util.UUID + +enum class SortOrder(val sorter: Comparator>) { + ASC({ a, b -> a.value - b.value }), + DESC({ a, b -> b.value - a.value }), +} diff --git a/api/src/test/java/xyz/acrylicstyle/dailyranking/test/java/api/game/GameImpl.java b/api/src/test/java/xyz/acrylicstyle/dailyranking/test/java/api/game/GameImpl.java index f5d2b92..0b7a138 100644 --- a/api/src/test/java/xyz/acrylicstyle/dailyranking/test/java/api/game/GameImpl.java +++ b/api/src/test/java/xyz/acrylicstyle/dailyranking/test/java/api/game/GameImpl.java @@ -2,6 +2,7 @@ import org.jetbrains.annotations.NotNull; import xyz.acrylicstyle.dailyranking.api.game.Game; +import xyz.acrylicstyle.dailyranking.api.game.SortOrder; import java.util.Map; @@ -29,4 +30,10 @@ public Map getAsMap() { public String getValueToStringFunction(int value) { return Game.super.getValueToStringFunction(value); } + + @NotNull + @Override + public SortOrder getOrder() { + return Game.super.getOrder(); + } } diff --git a/build.gradle.kts b/build.gradle.kts index b69b421..e336b36 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,5 @@ group = "xyz.acrylicstyle.dailyrankingboard" -version = "1.1.12" +version = "2.0.0" plugins { java diff --git a/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/DailyRankingBoardPlugin.kt b/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/DailyRankingBoardPlugin.kt index f7dd66c..50f9e00 100644 --- a/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/DailyRankingBoardPlugin.kt +++ b/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/DailyRankingBoardPlugin.kt @@ -21,6 +21,7 @@ import xyz.acrylicstyle.dailyranking.api.DailyRankingBoardAPI import xyz.acrylicstyle.dailyranking.api.util.Util.stringify import xyz.acrylicstyle.dailyranking.plugin.argument.GameArgument import xyz.acrylicstyle.dailyranking.plugin.argument.MapArgument +import xyz.acrylicstyle.dailyranking.plugin.argument.OrderArgument import xyz.acrylicstyle.dailyranking.plugin.configuration.GameConfigurationFile import xyz.acrylicstyle.dailyranking.plugin.configuration.UserCacheFile import xyz.acrylicstyle.dailyranking.plugin.game.GameManager @@ -268,6 +269,24 @@ class DailyRankingBoardPlugin: JavaPlugin(), DailyRankingBoardAPIImpl { } ) ) + .then(literal("setorder") + .requires { s -> s.bukkitSender.hasPermission("dailyrankingboard.games.setorder") } + .then(argument("order", OrderArgument.order()) + .suggests { _, builder -> OrderArgument.fillSuggestions(builder) } + .executes { context -> + val game = GameArgument.get(context, "game") + if (game.game !is SerializableGame) { + context.source.sendFailureMessage(ChatComponentText("変更できないゲームです。")) + } else { + val serializableGame = game.game as SerializableGame + val order = OrderArgument.get(context, "order") + serializableGame.order = order + context.source.sendMessage(ChatComponentText("${ChatColor.GREEN}ランキングボードの値の表示順序を変更しました。"), true) + } + return@executes 0 + } + ) + ) .then(literal("maps") .then(literal("add") .requires { s -> s.bukkitSender.hasPermission("dailyrankingboard.maps.add") } diff --git a/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/argument/GameArgument.kt b/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/argument/GameArgument.kt index eaa7768..71e7523 100644 --- a/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/argument/GameArgument.kt +++ b/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/argument/GameArgument.kt @@ -23,5 +23,5 @@ object GameArgument { } fun fillSuggestions(builder: SuggestionsBuilder): CompletableFuture = - ICompletionProvider.b(GameManager.getGames().filter { it.game is SerializableGame }.map { it.id }, builder) + ICompletionProvider.b(GameManager.getGames().map { it.id }, builder) } diff --git a/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/argument/OrderArgument.kt b/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/argument/OrderArgument.kt new file mode 100644 index 0000000..53af5ca --- /dev/null +++ b/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/argument/OrderArgument.kt @@ -0,0 +1,25 @@ +package xyz.acrylicstyle.dailyranking.plugin.argument + +import com.mojang.brigadier.arguments.StringArgumentType +import com.mojang.brigadier.context.CommandContext +import com.mojang.brigadier.exceptions.DynamicCommandExceptionType +import com.mojang.brigadier.suggestion.Suggestions +import com.mojang.brigadier.suggestion.SuggestionsBuilder +import net.minecraft.server.v1_16_R3.ChatComponentText +import net.minecraft.server.v1_16_R3.ICompletionProvider +import xyz.acrylicstyle.dailyranking.api.game.SortOrder +import java.util.concurrent.CompletableFuture + +object OrderArgument { + private val INVALID_ORDER = DynamicCommandExceptionType { ChatComponentText("Unknown sort order: '$it'") } + + fun order(): StringArgumentType = StringArgumentType.word() + + fun get(context: CommandContext<*>, name: String): SortOrder = + StringArgumentType.getString(context, name).let { s -> + SortOrder.values().find { it.name == s } ?: throw INVALID_ORDER.create(s) + } + + fun fillSuggestions(builder: SuggestionsBuilder): CompletableFuture = + ICompletionProvider.b(SortOrder.values().map { it.name }, builder) +} diff --git a/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/configuration/UserCacheFile.kt b/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/configuration/UserCacheFile.kt index 4a129bb..8cde4b9 100644 --- a/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/configuration/UserCacheFile.kt +++ b/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/configuration/UserCacheFile.kt @@ -30,6 +30,7 @@ object UserCacheFile { DailyRankingBoardPlugin.instance.logger.warning("Failed to load user_cache.yml") e.printStackTrace() } + DailyRankingBoardPlugin.instance.logger.info("Loaded ${cache.rawData.size} entries from user_cache.yml") } fun write() { diff --git a/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/game/SerializableGame.kt b/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/game/SerializableGame.kt index 1069bc3..7912df2 100644 --- a/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/game/SerializableGame.kt +++ b/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/game/SerializableGame.kt @@ -2,14 +2,21 @@ package xyz.acrylicstyle.dailyranking.plugin.game import util.yaml.YamlObject import xyz.acrylicstyle.dailyranking.api.game.Game +import xyz.acrylicstyle.dailyranking.api.game.SortOrder -data class SerializableGame(override val id: String, override var name: String, var format: String = "%d"): Game { +data class SerializableGame( + override val id: String, + override var name: String, + var format: String = "%d", + override var order: SortOrder = SortOrder.ASC, +): Game { companion object { fun deserialize(obj: YamlObject): SerializableGame { val id = obj.getString("id") ?: error("id is not defined") val name = obj.getString("name") ?: error("name is not defined") val format = obj.getString("format", "%d") - return SerializableGame(id, name, format) + val order = SortOrder.valueOf(obj.getString("order", "ASC")) + return SerializableGame(id, name, format, order) } } @@ -17,6 +24,7 @@ data class SerializableGame(override val id: String, override var name: String, "id" to id, "name" to name, "format" to format, + "order" to order.name, ) override fun getValueToStringFunction(value: Int): String = String.format(format, value) diff --git a/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/util/PlayerArmorStandData.kt b/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/util/PlayerArmorStandData.kt index 80bbda7..ea3a055 100644 --- a/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/util/PlayerArmorStandData.kt +++ b/plugin/src/main/kotlin/xyz/acrylicstyle/dailyranking/plugin/util/PlayerArmorStandData.kt @@ -102,7 +102,7 @@ class PlayerArmorStandData(private val uuid: UUID) { leaderboardGameCurrent.setText("${ChatColor.GOLD}ゲーム: ${ChatColor.BOLD}${registeredGame.game.name}") leaderboardMapCurrent.setText("${ChatColor.GOLD}マップ: ${ChatColor.BOLD}${map.name}") leaderboardEntriesCurrentMap.setText("${ChatColor.GRAY}${registeredGame.game.name} ${ChatColor.WHITE}- ${ChatColor.GRAY}${map.name}") - val entries = map.getLeaderboardEntries().entries.sortedBy { it.value } + val entries = map.getLeaderboardEntries().entries.sortedWith(registeredGame.game.order.sorter) for (i in 0..9) { entries.getOrNull(i).let { entry -> if (entry == null) { diff --git a/plugin/src/main/resources/plugin.yml b/plugin/src/main/resources/plugin.yml index 2d7bd70..8fb355f 100644 --- a/plugin/src/main/resources/plugin.yml +++ b/plugin/src/main/resources/plugin.yml @@ -1,7 +1,7 @@ name: DailyRankingBoard main: xyz.acrylicstyle.dailyranking.plugin.DailyRankingBoardPlugin api-version: 1.13 -version: 1.1.12 +version: 2.0.0 load: POSTWORLD softdepend: - Multiverse-Core @@ -19,6 +19,7 @@ permissions: - dailyrankingboard.games.remove - dailyrankingboard.games.rename - dailyrankingboard.games.setformat + - dailyrankingboard.games.setorder - dailyrankingboard.maps.add - dailyrankingboard.maps.remove - dailyrankingboard.maps.rename @@ -42,7 +43,10 @@ permissions: description: "Permission to rename a game" dailyrankingboard.games.setformat: default: op - description: "Permission to rename a game" + description: "Permission to set the format of leaderboard" + dailyrankingboard.games.setorder: + default: op + description: "Permission to change the order of leaderboard" dailyrankingboard.maps.add: default: op description: "Permission to add a map"