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

@ -18,8 +18,7 @@ interface CommandArg<T:Any>{
} }
abstract class CommandArgImpl<T:Any>( abstract class CommandArgImpl<T : Any> : CommandArg<T> {
):CommandArg<T>{
lateinit var value: T lateinit var value: T
@ -33,40 +32,20 @@ abstract class CommandArgImpl<T:Any>(
} }
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
}
} }
/** /**
@ -92,7 +71,6 @@ class ExistBotArg:CommandArgImpl<Bot>(){
} }
class ExistGroupArg : CommandArgImpl<Group>() { class ExistGroupArg : CommandArgImpl<Group>() {
override fun parse(s: String, commandSender: CommandSender): Group { override fun parse(s: String, commandSender: CommandSender): Group {

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,9 +1,12 @@
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
*/ */
@MiraiExperimentalAPI
abstract class Value<T, V> { abstract class Value<T, V> {
operator fun invoke(): V = get() operator fun invoke(): V = get()
@ -13,10 +16,10 @@ abstract class Value<T,V>{
} }
/** /**
* This value can be used as a Config Value * This value can be used as a Config Value
*/ */
@MiraiExperimentalAPI
interface ConfigValue interface ConfigValue
@ -25,6 +28,7 @@ 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>() {
@ -35,6 +39,7 @@ open class SimpleValue<T>(
} }
} }
@MiraiExperimentalAPI
open class NullableSimpleValue<T>( open class NullableSimpleValue<T>(
value: T? = null value: T? = null
) : SimpleValue<T?>( ) : SimpleValue<T?>(