This commit is contained in:
Him188 2020-05-11 13:22:09 +08:00
parent a824ce62c1
commit 0429837011
4 changed files with 51 additions and 71 deletions

View File

@ -33,6 +33,8 @@ dependencies {
api(group = "com.moandjiezana.toml", name = "toml4j", version = "0.7.2") api(group = "com.moandjiezana.toml", name = "toml4j", version = "0.7.2")
api("org.jsoup:jsoup:1.12.1") api("org.jsoup:jsoup:1.12.1")
api("org.jetbrains:annotations:19.0.0")
testApi("net.mamoe:mirai-core-qqandroid:${Versions.Mirai.core}") testApi("net.mamoe:mirai-core-qqandroid:${Versions.Mirai.core}")
testApi(kotlin("stdlib")) testApi(kotlin("stdlib"))
} }

View File

@ -9,19 +9,18 @@ import net.mamoe.mirai.contact.Group
* this output type of that arg * this output type of that arg
* input is always String * input is always String
*/ */
interface CommandArg<T:Any>{ interface CommandArg<T : Any> {
operator fun invoke():T = get() 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>( abstract class CommandArgImpl<T : Any> : CommandArg<T> {
):CommandArg<T>{
lateinit var value:T lateinit var value: T
override fun get(): T = value override fun get(): T = value
@ -29,44 +28,24 @@ abstract class CommandArgImpl<T:Any>(
value = parse(s, commandSender) value = parse(s, commandSender)
} }
abstract fun parse(s:String, commandSender: CommandSender):T abstract fun parse(s: String, commandSender: CommandSender): T
} }
class IntArg:CommandArgImpl<Int>(){ class IntArg : CommandArgImpl<Int>() {
override fun parse(s: String, commandSender: CommandSender): Int { override fun parse(s: String, commandSender: CommandSender): Int = s.toInt()
return try{
s.toInt()
}catch (e:Exception){
error("无法识别整数$s")
}
}
} }
class LongArg:CommandArgImpl<Long>(){ class LongArg : CommandArgImpl<Long>() {
override fun parse(s: String, commandSender: CommandSender): Long { override fun parse(s: String, commandSender: CommandSender): Long = s.toLong()
return try{
s.toLong()
}catch (e:Exception){
error("无法识别长整数$s")
}
}
} }
class DoubleArg:CommandArgImpl<Double>(){ class DoubleArg : CommandArgImpl<Double>() {
override fun parse(s: String, commandSender: CommandSender): Double { override fun parse(s: String, commandSender: CommandSender): Double = s.toDouble()
return try{
s.toDouble()
}catch (e:Exception){
error("无法识别小数$s")
}
}
} }
class StringArg:CommandArgImpl<String>(){ class StringArg : CommandArgImpl<String>() {
override fun parse(s: String, commandSender: CommandSender): String { override fun parse(s: String, commandSender: CommandSender): String = s
return s
}
} }
/** /**
@ -76,33 +55,32 @@ class StringArg:CommandArgImpl<String>(){
* errors: String->Int convert, Bot Not Exist * errors: String->Int convert, Bot Not Exist
*/ */
class ExistBotArg:CommandArgImpl<Bot>(){ class ExistBotArg : CommandArgImpl<Bot>() {
override fun parse(s: String, commandSender: CommandSender): Bot { override fun parse(s: String, commandSender: CommandSender): Bot {
val uin = try{ val uin = try {
s.toLong() s.toLong()
}catch (e:Exception){ } catch (e: Exception) {
error("无法识别QQ UIN$s") error("无法识别QQ UIN$s")
} }
return try{ return try {
Bot.getInstance(uin) Bot.getInstance(uin)
}catch (e:NoSuchElementException){ } catch (e: NoSuchElementException) {
error("无法找到Bot $uin") error("无法找到Bot $uin")
} }
} }
} }
class ExistGroupArg : CommandArgImpl<Group>() {
class ExistGroupArg:CommandArgImpl<Group>(){
override fun parse(s: String, commandSender: CommandSender): 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 return commandSender.contact as Group
} }
val code = try{ val code = try {
s.toLong() s.toLong()
}catch (e:Exception){ } catch (e: Exception) {
error("无法识别Group Code$s") error("无法识别Group Code$s")
} }

View File

@ -1,22 +1,18 @@
package net.mamoe.mirai.console.utils; package net.mamoe.mirai.console.utils;
import org.jetbrains.annotations.Range;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
public class Utils { public final class Utils {
/** /**
* 执行N次 callable * 执行N次 callable
* 成功一次就会结束 * 成功一次就会结束
* 否则就会throw * 否则就会throw
*/ */
public static <T> T tryNTimes( public static <T> T tryNTimes(@Range(from = 1, to = Integer.MAX_VALUE) int n,
/*@Range(from=1, to=Integer.MAX_VALUE)*/ Callable<T> callable) throws Exception {
int n,
Callable<T> callable
) throws Exception {
if (n < 0) {
throw new IllegalArgumentException("Must be executed at least once.");
}
Exception last = null; Exception last = null;
while (n-- > 0) { while (n-- > 0) {
@ -26,13 +22,12 @@ public class Utils {
if (last == null) { if (last == null) {
last = e; last = e;
} else { } else {
try {
last.addSuppressed(e); last.addSuppressed(e);
} catch (Throwable ignored) {
} }
} }
} }
if (last == null) {
throw new Exception("unknown error");
} }
throw last; throw last;

View File

@ -1,22 +1,25 @@
package net.mamoe.mirai.console.utils package net.mamoe.mirai.console.utils
import net.mamoe.mirai.utils.MiraiExperimentalAPI
/** /**
* A Value * A Value
* the input type of this Value is T while the output is V * the input type of this Value is T while the output is V
*/ */
abstract class Value<T,V>{ @MiraiExperimentalAPI
operator fun invoke():V = get() 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 * This value can be used as a Config Value
*/ */
@MiraiExperimentalAPI
interface ConfigValue interface ConfigValue
@ -25,9 +28,10 @@ interface ConfigValue
* the input type is same as output value * the input type is same as output value
*/ */
@MiraiExperimentalAPI
open class SimpleValue<T>( open class SimpleValue<T>(
var value:T var value: T
):Value<T,T>() { ) : Value<T, T>() {
override fun get() = this.value override fun get() = this.value
override fun set(t: T) { override fun set(t: T) {
@ -35,11 +39,12 @@ open class SimpleValue<T>(
} }
} }
@MiraiExperimentalAPI
open class NullableSimpleValue<T>( open class NullableSimpleValue<T>(
value:T? = null value: T? = null
):SimpleValue<T?>( ) : SimpleValue<T?>(
value value
){ ) {
fun isNull() = value == null fun isNull() = value == null
} }