-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathDice.kt
78 lines (67 loc) · 2.3 KB
/
Dice.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/
@file:Suppress("NOTHING_TO_INLINE")
@file:JvmMultifileClass
@file:JvmName("MessageUtils")
package net.mamoe.mirai.message.data
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.mamoe.mirai.message.code.CodableMessage
import net.mamoe.mirai.message.data.visitor.MessageVisitor
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.annotations.Range
import net.mamoe.mirai.utils.safeCast
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
import kotlin.jvm.JvmStatic
import kotlin.random.Random
import kotlin.random.nextInt
/**
* 骰子.
*
* 构造 [Dice] 实例即可使用. 也可以通过 [Dice.random] 获得一个随机点数的实例.
*
* @since 2.5
*/
@Serializable
@SerialName(Dice.SERIAL_NAME)
public data class Dice(
/**
* 骰子的点数. 范围为 1..6
*/
public val value: @Range(from = 1, to = 6) Int
) : MarketFace, CodableMessage {
init {
require(value in 1..6) { "Dice.value must be in 1 and 6 inclusive." }
}
override val name: String
get() = "[骰子:$value]" // 官方名称应该是 "[随机骰子]"
@MiraiExperimentalApi
override val id: Int
get() = 11464
@MiraiExperimentalApi
override fun appendMiraiCodeTo(builder: StringBuilder) {
builder.append("[mirai:dice:").append(value).append(']')
}
override fun toString(): String = "[mirai:dice:$value]"
@MiraiInternalApi
override fun <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
return visitor.visitDice(this, data)
}
public companion object Key :
AbstractPolymorphicMessageKey<MarketFace, Dice>(MarketFace, { it.safeCast() }) {
public const val SERIAL_NAME: String = "Dice"
/**
* 创建随机点数的 [骰子][Dice]
*/
@JvmStatic
public fun random(): Dice = Dice(Random.nextInt(1..6))
}
}