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("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"))
}

View File

@ -18,8 +18,7 @@ interface CommandArg<T:Any>{
}
abstract class CommandArgImpl<T:Any>(
):CommandArg<T>{
abstract class CommandArgImpl<T : Any> : CommandArg<T> {
lateinit var value: T
@ -33,40 +32,20 @@ abstract class CommandArgImpl<T:Any>(
}
class IntArg : CommandArgImpl<Int>() {
override fun parse(s: String, commandSender: CommandSender): Int {
return try{
s.toInt()
}catch (e:Exception){
error("无法识别整数$s")
}
}
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")
}
}
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")
}
}
override fun parse(s: String, commandSender: CommandSender): Double = s.toDouble()
}
class StringArg : CommandArgImpl<String>() {
override fun parse(s: String, commandSender: CommandSender): String {
return s
}
override fun parse(s: String, commandSender: CommandSender): String = s
}
/**
@ -92,7 +71,6 @@ class ExistBotArg:CommandArgImpl<Bot>(){
}
class ExistGroupArg : CommandArgImpl<Group>() {
override fun parse(s: String, commandSender: CommandSender): Group {

View File

@ -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,13 +22,12 @@ public class Utils {
if (last == null) {
last = e;
} else {
try {
last.addSuppressed(e);
} catch (Throwable ignored) {
}
}
}
if (last == null) {
throw new Exception("unknown error");
}
throw last;

View File

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