Skip to content

Commit

Permalink
2.0.0 - add setorder sub-command to game (it breaks compatibility bec…
Browse files Browse the repository at this point in the history
…ause the game with old version will not work without leaving any errors)
  • Loading branch information
acrylic-style committed Aug 5, 2021
1 parent 5f39ee3 commit 4b34732
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Any> = mapOf(
"id" to id,
"name" to name,
"order" to order.name,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package xyz.acrylicstyle.dailyranking.api.game

import java.util.UUID

enum class SortOrder(val sorter: Comparator<Map.Entry<UUID, Int>>) {
ASC({ a, b -> a.value - b.value }),
DESC({ a, b -> b.value - a.value }),
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -29,4 +30,10 @@ public Map<String, Object> getAsMap() {
public String getValueToStringFunction(int value) {
return Game.super.getValueToStringFunction(value);
}

@NotNull
@Override
public SortOrder getOrder() {
return Game.super.getOrder();
}
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = "xyz.acrylicstyle.dailyrankingboard"
version = "1.1.12"
version = "2.0.0"

plugins {
java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ object GameArgument {
}

fun fillSuggestions(builder: SuggestionsBuilder): CompletableFuture<Suggestions> =
ICompletionProvider.b(GameManager.getGames().filter { it.game is SerializableGame }.map { it.id }, builder)
ICompletionProvider.b(GameManager.getGames().map { it.id }, builder)
}
Original file line number Diff line number Diff line change
@@ -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<Suggestions> =
ICompletionProvider.b(SortOrder.values().map { it.name }, builder)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@ 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)
}
}

override fun getAsMap(): Map<String, Any> = mapOf(
"id" to id,
"name" to name,
"format" to format,
"order" to order.name,
)

override fun getValueToStringFunction(value: Int): String = String.format(format, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions plugin/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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"
Expand Down

0 comments on commit 4b34732

Please sign in to comment.