Skip to content

Commit

Permalink
refactor: 完成迁移 Util
Browse files Browse the repository at this point in the history
  • Loading branch information
nostalfinals committed Dec 27, 2024
1 parent 89856e4 commit 9c5684e
Show file tree
Hide file tree
Showing 95 changed files with 427 additions and 583 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package plutoproject.framework.common.util

import com.google.protobuf.Empty as EmptyClass

val Empty: EmptyClass = EmptyClass.getDefaultInstance()
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package plutoproject.framework.common.util.logger
package plutoproject.framework.common.util

import java.util.logging.Logger

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import ink.pmc.advkt.component.newline
import ink.pmc.advkt.component.text
import ink.pmc.advkt.send
import net.kyori.adventure.audience.Audience
import plutoproject.framework.common.util.chat.palette.MOCHA_MAROON
import plutoproject.framework.common.util.chat.palette.MOCHA_SUBTEXT_0
import plutoproject.framework.common.util.logger.logger
import plutoproject.framework.common.util.chat.palettes.MOCHA_MAROON
import plutoproject.framework.common.util.chat.palettes.MOCHA_SUBTEXT_0
import java.util.logging.Level

inline fun <T> T.catchException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package plutoproject.framework.common.util

import kotlin.math.roundToInt

fun Double.toTrimmedString(): String {
var result = toBigDecimal().stripTrailingZeros().toPlainString()
if (result.endsWith(".")) {
result = result.dropLast(1)
}
return result
}

fun Double.roundTo2(): Double {
return ((this * 100).roundToInt() / 100.0)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package plutoproject.framework.common.util.chat

import net.kyori.adventure.text.format.TextColor
import java.awt.Color

fun Color.toTextColor(): TextColor {
return TextColor.color(red, green, blue)
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
package plutoproject.framework.common.util.chat

import ink.pmc.advkt.component.*
import plutoproject.framework.common.util.chat.palette.MOCHA_MAROON
import plutoproject.framework.common.util.chat.palette.MOCHA_SUBTEXT_0
import plutoproject.framework.common.util.chat.palette.MOCHA_YELLOW

const val ECONOMY_SYMBOL = "\uD83C\uDF1F"

val NON_PLAYER = component {
text("该命令仅限玩家使用") with MOCHA_MAROON
}

val EMPTY_LINE = component { }

val PLUTO_PROJECT = component {
miniMessage("<gradient:#c6a0f6:#f5bde6:#f0c6c6:#f4dbd6>星社 ᴘʀᴏᴊᴇᴄᴛ</gradient>")
}

val PLAYER_NOT_ONLINE = component {
text("该玩家不在线") with MOCHA_MAROON
}

val NO_PERMISSION = component {
text("你似乎没有权限这么做") with MOCHA_MAROON
newline()
text("如果你认为这是一个错误的话,请向管理组报告") with MOCHA_SUBTEXT_0
}

val UNUSUAL_ISSUE = component {
text("看起来你似乎遇见了一个很罕见的问题") with MOCHA_MAROON
newline()
text("我们建议你反馈这个问题,有助于将服务器变得更好") with MOCHA_SUBTEXT_0
}

val IN_PROGRESS = component {
text("正在施工中...") with MOCHA_MAROON
newline()
text("前面的路,以后再来探索吧!") with MOCHA_SUBTEXT_0
}

val UI_CLOSE = component {
text("关闭") with MOCHA_MAROON without italic()
}

val UI_BACK = component {
text("返回") with MOCHA_YELLOW without italic()
import plutoproject.framework.common.util.chat.palettes.MOCHA_MAROON
import plutoproject.framework.common.util.chat.palettes.MOCHA_SUBTEXT_0
import plutoproject.framework.common.util.chat.palettes.MOCHA_YELLOW

object MessageConstants {
const val ECONOMY_SYMBOL = "\uD83C\uDF1F"
val emptyLine = component { }
val nonPlayer = component {
text("该命令仅限玩家使用") with MOCHA_MAROON
}
val plutoProject = component {
miniMessage("<gradient:#c6a0f6:#f5bde6:#f0c6c6:#f4dbd6>星社 ᴘʀᴏᴊᴇᴄᴛ</gradient>")
}
val playerNotOnline = component {
text("该玩家不在线") with MOCHA_MAROON
}
val noPermission = component {
text("你似乎没有权限这么做") with MOCHA_MAROON
newline()
text("如果你认为这是一个错误的话,请向管理组报告") with MOCHA_SUBTEXT_0
}
val unusualIssue = component {
text("看起来你似乎遇见了一个很罕见的问题") with MOCHA_MAROON
newline()
text("我们建议你反馈这个问题,有助于将服务器变得更好") with MOCHA_SUBTEXT_0
}
val inProgress = component {
text("正在施工中...") with MOCHA_MAROON
newline()
text("前面的路,以后再来探索吧!") with MOCHA_SUBTEXT_0
}

object UI {
val close = component {
text("关闭") with MOCHA_MAROON without italic()
}
val back = component {
text("返回") with MOCHA_YELLOW without italic()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package plutoproject.framework.common.util.chat

import java.awt.Color
import java.text.DecimalFormat

fun Int.toRgbaColor(): Color = Color(
(this shr 16) and 0xFF,
(this shr 8) and 0xFF,
this and 0xFF,
(this shr 24) and 0xFF
)

fun Double.toCurrencyFormattedString(): String {
val decimalFormat = DecimalFormat("#,##0.00")
return decimalFormat.format(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ import ink.pmc.advkt.sound.key
import ink.pmc.advkt.sound.sound
import net.kyori.adventure.key.Key

val MESSAGE_SOUND = sound {
key(Key.key("block.decorated_pot.insert"))
}

val UI_INVALID_SOUND = sound {
key(Key.key("block.note_block.didgeridoo"))
}
object SoundConstants {
val message = sound {
key(Key.key("block.decorated_pot.insert"))
}

val UI_SUCCEED_SOUND = sound {
key(Key.key("block.note_block.bell"))
object UI {
val invalid = sound {
key(Key.key("block.note_block.didgeridoo"))
}
val succeed = sound {
key(Key.key("block.note_block.bell"))
}
val paging = sound {
key(Key.key("item.book.page_turn"))
}
val selector = sound {
key(Key.key("block.note_block.hat"))
}
}
}

val UI_PAGING_SOUND = sound {
key(Key.key("item.book.page_turn"))
}

val UI_SELECTOR_SOUND = sound {
key(Key.key("block.note_block.hat"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@ fun Component.replace(string: String, any: Any?): Component {
return replace(string, any.toString())
}

fun Iterable<Component>.replace(pattern: String, str: String): Iterable<Component> {
return map { it.replace(pattern, str) }
}

fun Iterable<Component>.replace(pattern: String, component: Component): Iterable<Component> {
return map { it.replace(pattern, component) }
}

fun Iterable<Component>.replace(pattern: String, any: Any?): Iterable<Component> {
return map { it.replace(pattern, any) }
}

fun Component.replaceColor(original: TextColor, new: TextColor): Component {
val updatedComponent =
if (this.style().color() == original) {
Expand All @@ -48,4 +36,27 @@ fun Component.replaceColor(original: TextColor, new: TextColor): Component {
.fold(updatedComponent.children(emptyList())) { comp, child ->
comp.append(child.replaceColor(original, new))
}
}
}

fun Component.splitLines(): Iterable<Component> {
var curr = Component.empty()
return buildList {
val root = this@splitLines.children(emptyList())
if (root != Component.empty()) {
add(root)
}
children().forEach { child ->
if (child == Component.newline()) {
if (curr != Component.empty()) {
add(curr)
}
curr = Component.empty()
return@forEach
}
curr = curr.append(child)
}
if (curr != Component.empty()) {
add(curr)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package plutoproject.framework.common.util.chat.component

import net.kyori.adventure.text.Component

fun Iterable<Component>.replace(pattern: String, str: String): Iterable<Component> {
return map { it.replace(pattern, str) }
}

fun Iterable<Component>.replace(pattern: String, component: Component): Iterable<Component> {
return map { it.replace(pattern, component) }
}

fun Iterable<Component>.replace(pattern: String, any: Any?): Iterable<Component> {
return map { it.replace(pattern, any) }
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package plutoproject.framework.common.util.chat.palette
package plutoproject.framework.common.util.chat.palettes

import com.catppuccin.Palette
import plutoproject.framework.common.util.chat.toTextColor
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package plutoproject.framework.common.util.command

import org.incendo.cloud.CommandManager
import org.incendo.cloud.annotations.AnnotationParser
import org.incendo.cloud.annotations.parser.MethodArgumentParser
import org.incendo.cloud.kotlin.coroutines.annotations.installCoroutineSupport
import org.incendo.cloud.parser.ArgumentParser
import plutoproject.framework.common.util.jvm.clazz.findClass
import plutoproject.framework.common.util.jvm.findClass

@Suppress("UNCHECKED_CAST", "UnusedReceiverParameter")
fun <C, T> CommandManager<C>.getKotlinMethodArgumentParserClass(): Class<ArgumentParser<C, T>> =
Expand All @@ -13,3 +15,6 @@ fun <C, T> CommandManager<C>.getKotlinMethodArgumentParserClass(): Class<Argumen
@Suppress("UNCHECKED_CAST", "UnusedReceiverParameter")
fun <C, T> CommandManager<C>.getMethodArgumentParserClass(): Class<ArgumentParser<C, T>> =
MethodArgumentParser::class.java as Class<ArgumentParser<C, T>>

inline val <reified C> CommandManager<C>.annotationParser: AnnotationParser<C>
get() = AnnotationParser(this, C::class.java).installCoroutineSupport()
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package plutoproject.framework.common.util.command.suggestion
package plutoproject.framework.common.util.command

import org.incendo.cloud.context.CommandContext
import org.incendo.cloud.context.CommandInput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package plutoproject.framework.common.util.config

import com.sksamuel.hoplite.ConfigLoaderBuilder
import com.sksamuel.hoplite.ExperimentalHoplite
import plutoproject.framework.common.util.config.decoder.CharDecoder
import plutoproject.framework.common.util.config.decoder.ComponentDecoder
import plutoproject.framework.common.util.config.decoders.CharDecoder
import plutoproject.framework.common.util.config.decoders.ComponentDecoder

@OptIn(ExperimentalHoplite::class)
fun ConfigLoaderBuilder(): ConfigLoaderBuilder {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package plutoproject.framework.common.util.config.decoder
package plutoproject.framework.common.util.config.decoders

import com.sksamuel.hoplite.*
import com.sksamuel.hoplite.decoder.NullHandlingDecoder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package plutoproject.framework.common.util.config.decoder
package plutoproject.framework.common.util.config.decoders

import com.sksamuel.hoplite.*
import com.sksamuel.hoplite.decoder.NullHandlingDecoder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import kotlin.coroutines.CoroutineContext

@Suppress("UNUSED")
fun <T> runAsync(
coroutineScope: CoroutineScope = COROUTINE_SCOPE,
coroutineScope: CoroutineScope = PlutoCoroutineScope,
coroutineContext: CoroutineContext = Dispatchers.Default,
coroutineStart: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> T,
): Deferred<T> = coroutineScope.async(coroutineContext, coroutineStart) { block() }

@Suppress("UNUSED")
fun <T> runAsyncIO(
coroutineScope: CoroutineScope = COROUTINE_SCOPE,
coroutineScope: CoroutineScope = PlutoCoroutineScope,
coroutineStart: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> T,
): Deferred<T> = coroutineScope.async(Dispatchers.IO, coroutineStart) { block() }

@Suppress("UNUSED")
fun <T> runAsyncUnconfined(
coroutineScope: CoroutineScope = COROUTINE_SCOPE,
coroutineScope: CoroutineScope = PlutoCoroutineScope,
coroutineStart: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> T,
): Deferred<T> = coroutineScope.async(Dispatchers.Unconfined, coroutineStart) { block() }
Expand Down
Loading

0 comments on commit 9c5684e

Please sign in to comment.