diff --git a/mirai-console/build.gradle.kts b/mirai-console/build.gradle.kts index dea1f41a2..8a1eb1389 100644 --- a/mirai-console/build.gradle.kts +++ b/mirai-console/build.gradle.kts @@ -33,6 +33,8 @@ dependencies { api(group = "com.moandjiezana.toml", name = "toml4j", version = "0.7.2") api("org.jsoup:jsoup:1.12.1") + api("org.jetbrains:annotations:19.0.0") + testApi("net.mamoe:mirai-core-qqandroid:${Versions.Mirai.core}") testApi(kotlin("stdlib")) } diff --git a/mirai-console/src/main/java/net/mamoe/mirai/console/utils/CommandArg.kt b/mirai-console/src/main/java/net/mamoe/mirai/console/utils/CommandArg.kt index 8a121d908..d335f662a 100644 --- a/mirai-console/src/main/java/net/mamoe/mirai/console/utils/CommandArg.kt +++ b/mirai-console/src/main/java/net/mamoe/mirai/console/utils/CommandArg.kt @@ -9,19 +9,18 @@ import net.mamoe.mirai.contact.Group * this output type of that arg * input is always String */ -interface CommandArg<T:Any>{ - operator fun invoke():T = get() +interface CommandArg<T : Any> { + operator fun invoke(): T = get() - fun get():T + fun get(): T - fun read(s:String, commandSender: CommandSender) + fun read(s: String, commandSender: CommandSender) } -abstract class CommandArgImpl<T:Any>( -):CommandArg<T>{ +abstract class CommandArgImpl<T : Any> : CommandArg<T> { - lateinit var value:T + lateinit var value: T override fun get(): T = value @@ -29,44 +28,24 @@ abstract class CommandArgImpl<T:Any>( value = parse(s, commandSender) } - abstract fun parse(s:String, commandSender: CommandSender):T + abstract fun parse(s: String, commandSender: CommandSender): T } -class IntArg:CommandArgImpl<Int>(){ - override fun parse(s: String, commandSender: CommandSender): Int { - return try{ - s.toInt() - }catch (e:Exception){ - error("无法识别整数$s") - } - } +class IntArg : CommandArgImpl<Int>() { + override fun parse(s: String, commandSender: CommandSender): Int = s.toInt() } -class LongArg:CommandArgImpl<Long>(){ - override fun parse(s: String, commandSender: CommandSender): Long { - return try{ - s.toLong() - }catch (e:Exception){ - error("无法识别长整数$s") - } - } +class LongArg : CommandArgImpl<Long>() { + override fun parse(s: String, commandSender: CommandSender): Long = s.toLong() } -class DoubleArg:CommandArgImpl<Double>(){ - override fun parse(s: String, commandSender: CommandSender): Double { - return try{ - s.toDouble() - }catch (e:Exception){ - error("无法识别小数$s") - } - } +class DoubleArg : CommandArgImpl<Double>() { + override fun parse(s: String, commandSender: CommandSender): Double = s.toDouble() } -class StringArg:CommandArgImpl<String>(){ - override fun parse(s: String, commandSender: CommandSender): String { - return s - } +class StringArg : CommandArgImpl<String>() { + override fun parse(s: String, commandSender: CommandSender): String = s } /** @@ -76,33 +55,32 @@ class StringArg:CommandArgImpl<String>(){ * errors: String->Int convert, Bot Not Exist */ -class ExistBotArg:CommandArgImpl<Bot>(){ +class ExistBotArg : CommandArgImpl<Bot>() { override fun parse(s: String, commandSender: CommandSender): Bot { - val uin = try{ + val uin = try { s.toLong() - }catch (e:Exception){ + } catch (e: Exception) { error("无法识别QQ UIN$s") } - return try{ + return try { Bot.getInstance(uin) - }catch (e:NoSuchElementException){ + } catch (e: NoSuchElementException) { error("无法找到Bot $uin") } } } - -class ExistGroupArg:CommandArgImpl<Group>(){ +class ExistGroupArg : CommandArgImpl<Group>() { override fun parse(s: String, commandSender: CommandSender): Group { - if((s === "" || s === "~") && commandSender is GroupContactCommandSender){ + if ((s === "" || s === "~") && commandSender is GroupContactCommandSender) { return commandSender.contact as Group } - val code = try{ + val code = try { s.toLong() - }catch (e:Exception){ + } catch (e: Exception) { error("无法识别Group Code$s") } diff --git a/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Utils.java b/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Utils.java index 72f9d3f2a..6f2c654db 100644 --- a/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Utils.java +++ b/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Utils.java @@ -1,22 +1,18 @@ package net.mamoe.mirai.console.utils; +import org.jetbrains.annotations.Range; + import java.util.concurrent.Callable; -public class Utils { +public final class Utils { /** * 执行N次 callable * 成功一次就会结束 * 否则就会throw */ - public static <T> T tryNTimes( - /*@Range(from=1, to=Integer.MAX_VALUE)*/ - int n, - Callable<T> callable - ) throws Exception { - if (n < 0) { - throw new IllegalArgumentException("Must be executed at least once."); - } + public static <T> T tryNTimes(@Range(from = 1, to = Integer.MAX_VALUE) int n, + Callable<T> callable) throws Exception { Exception last = null; while (n-- > 0) { @@ -26,15 +22,14 @@ public class Utils { if (last == null) { last = e; } else { - last.addSuppressed(e); + try { + last.addSuppressed(e); + } catch (Throwable ignored) { + } } } } - if (last == null) { - throw new Exception("unknown error"); - } - throw last; } } diff --git a/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Value.kt b/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Value.kt index 81e5296ba..85a3ba822 100644 --- a/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Value.kt +++ b/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Value.kt @@ -1,22 +1,25 @@ package net.mamoe.mirai.console.utils +import net.mamoe.mirai.utils.MiraiExperimentalAPI + /** * A Value * the input type of this Value is T while the output is V */ -abstract class Value<T,V>{ - operator fun invoke():V = get() +@MiraiExperimentalAPI +abstract class Value<T, V> { + operator fun invoke(): V = get() - abstract fun get():V + abstract fun get(): V - abstract fun set(t:T) + abstract fun set(t: T) } - /** * This value can be used as a Config Value */ +@MiraiExperimentalAPI interface ConfigValue @@ -25,9 +28,10 @@ interface ConfigValue * the input type is same as output value */ +@MiraiExperimentalAPI open class SimpleValue<T>( - var value:T -):Value<T,T>() { + var value: T +) : Value<T, T>() { override fun get() = this.value override fun set(t: T) { @@ -35,11 +39,12 @@ open class SimpleValue<T>( } } +@MiraiExperimentalAPI open class NullableSimpleValue<T>( - value:T? = null -):SimpleValue<T?>( + value: T? = null +) : SimpleValue<T?>( value -){ - fun isNull() = value == null +) { + fun isNull() = value == null }