Commit 4bec7d75 authored by Him188's avatar Him188

Make java user happier

parent 29091e81
...@@ -118,9 +118,9 @@ kotlin { ...@@ -118,9 +118,9 @@ kotlin {
} }
} }
} }
//
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> { //tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.jvmTarget = "1.8" // kotlinOptions.jvmTarget = "1.8"
} //}
apply(from = rootProject.file("gradle/publish.gradle")) apply(from = rootProject.file("gradle/publish.gradle"))
...@@ -216,7 +216,8 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler ...@@ -216,7 +216,8 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
troopListData.groups.forEach { troopNum -> troopListData.groups.forEach { troopNum ->
launch { launch {
try {
try {
bot.groups.delegate.addLast( bot.groups.delegate.addLast(
@Suppress("DuplicatedCode") @Suppress("DuplicatedCode")
GroupImpl( GroupImpl(
......
...@@ -150,9 +150,9 @@ kotlin { ...@@ -150,9 +150,9 @@ kotlin {
} }
} }
} }
//
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> { //tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.jvmTarget = "1.8" // kotlinOptions.jvmTarget = "1.8"
} //}
apply(from = rootProject.file("gradle/publish.gradle")) apply(from = rootProject.file("gradle/publish.gradle"))
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:JvmMultifileClass
@file:JvmName("MessageUtils")
@file:Suppress("EXPERIMENTAL_API_USAGE") @file:Suppress("EXPERIMENTAL_API_USAGE")
package net.mamoe.mirai.message.data package net.mamoe.mirai.message.data
...@@ -14,6 +17,8 @@ package net.mamoe.mirai.message.data ...@@ -14,6 +17,8 @@ package net.mamoe.mirai.message.data
import net.mamoe.mirai.contact.Member import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.contact.groupCardOrNick import net.mamoe.mirai.contact.groupCardOrNick
import net.mamoe.mirai.utils.MiraiInternalAPI import net.mamoe.mirai.utils.MiraiInternalAPI
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
/** /**
......
...@@ -7,8 +7,14 @@ ...@@ -7,8 +7,14 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:JvmMultifileClass
@file:JvmName("MessageUtils")
package net.mamoe.mirai.message.data package net.mamoe.mirai.message.data
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
/** /**
* "@全体成员" * "@全体成员"
* *
......
...@@ -7,8 +7,13 @@ ...@@ -7,8 +7,13 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:JvmMultifileClass
@file:JvmName("MessageUtils")
package net.mamoe.mirai.message.data package net.mamoe.mirai.message.data
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
import kotlin.jvm.JvmStatic import kotlin.jvm.JvmStatic
/** /**
......
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:JvmMultifileClass
@file:JvmName("MessageUtils")
@file:Suppress("EXPERIMENTAL_API_USAGE") @file:Suppress("EXPERIMENTAL_API_USAGE")
package net.mamoe.mirai.message.data package net.mamoe.mirai.message.data
...@@ -14,13 +17,18 @@ package net.mamoe.mirai.message.data ...@@ -14,13 +17,18 @@ package net.mamoe.mirai.message.data
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient import kotlinx.serialization.Transient
import net.mamoe.mirai.utils.io.chunkedHexToBytes import net.mamoe.mirai.utils.io.chunkedHexToBytes
import kotlin.js.JsName
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName import kotlin.jvm.JvmName
import kotlin.jvm.JvmStatic
/** /**
* 自定义表情 (收藏的表情), 图片 * 自定义表情 (收藏的表情), 图片
*/ */
sealed class Image : Message { sealed class Image : Message {
companion object Key : Message.Key<Image> { companion object Key : Message.Key<Image> {
@JvmStatic
@JsName("fromId")
@JvmName("fromId") @JvmName("fromId")
operator fun invoke(imageId: String): Image = when (imageId.length) { operator fun invoke(imageId: String): Image = when (imageId.length) {
37 -> NotOnlineImageFromFile(imageId) // /f8f1ab55-bf8e-4236-b55e-955848d7069f 37 -> NotOnlineImageFromFile(imageId) // /f8f1ab55-bf8e-4236-b55e-955848d7069f
......
...@@ -13,6 +13,7 @@ package net.mamoe.mirai.message.data ...@@ -13,6 +13,7 @@ package net.mamoe.mirai.message.data
import net.mamoe.mirai.contact.Contact import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.contact.sendMessage import net.mamoe.mirai.contact.sendMessage
import kotlin.jvm.JvmSynthetic
/** /**
* 可发送的或从服务器接收的消息. * 可发送的或从服务器接收的消息.
...@@ -82,6 +83,7 @@ interface Message { ...@@ -82,6 +83,7 @@ interface Message {
* println(c)// "Hello world!" * println(c)// "Hello world!"
* ``` * ```
*/ */
@JvmSynthetic // in java they should use `plus` instead
fun followedBy(tail: Message): MessageChain { fun followedBy(tail: Message): MessageChain {
require(tail !is SingleOnly) { "SingleOnly Message cannot follow another message" } require(tail !is SingleOnly) { "SingleOnly Message cannot follow another message" }
require(this !is SingleOnly) { "SingleOnly Message cannot be followed" } require(this !is SingleOnly) { "SingleOnly Message cannot be followed" }
...@@ -92,6 +94,7 @@ interface Message { ...@@ -92,6 +94,7 @@ interface Message {
override fun toString(): String override fun toString(): String
operator fun plus(another: Message): MessageChain = this.followedBy(another) operator fun plus(another: Message): MessageChain = this.followedBy(another)
operator fun plus(another: String): MessageChain = this.followedBy(another.toMessage()) operator fun plus(another: String): MessageChain = this.followedBy(another.toMessage())
// `+ ""` will be resolved to `plus(String)` instead of `plus(CharSeq)` // `+ ""` will be resolved to `plus(String)` instead of `plus(CharSeq)`
operator fun plus(another: CharSequence): MessageChain = this.followedBy(another.toString().toMessage()) operator fun plus(another: CharSequence): MessageChain = this.followedBy(another.toString().toMessage())
......
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:JvmMultifileClass
@file:JvmName("MessageUtils")
package net.mamoe.mirai.message.data package net.mamoe.mirai.message.data
import net.mamoe.mirai.message.data.NullMessageChain.toString import net.mamoe.mirai.message.data.NullMessageChain.toString
...@@ -14,6 +17,9 @@ import net.mamoe.mirai.utils.MiraiExperimentalAPI ...@@ -14,6 +17,9 @@ import net.mamoe.mirai.utils.MiraiExperimentalAPI
import kotlin.contracts.ExperimentalContracts import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract import kotlin.contracts.contract
import kotlin.js.JsName import kotlin.js.JsName
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
import kotlin.jvm.JvmSynthetic
import kotlin.jvm.Volatile import kotlin.jvm.Volatile
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
...@@ -35,10 +41,12 @@ interface MessageChain : Message, MutableList<Message> { ...@@ -35,10 +41,12 @@ interface MessageChain : Message, MutableList<Message> {
override fun followedBy(tail: Message): MessageChain override fun followedBy(tail: Message): MessageChain
// endregion // endregion
@JvmSynthetic
operator fun plusAssign(message: Message) { operator fun plusAssign(message: Message) {
this.followedBy(message) this.followedBy(message)
} }
@JvmSynthetic // make java user happier
operator fun plusAssign(plain: String) { operator fun plusAssign(plain: String) {
this.plusAssign(plain.toMessage()) this.plusAssign(plain.toMessage())
} }
...@@ -53,7 +61,7 @@ interface MessageChain : Message, MutableList<Message> { ...@@ -53,7 +61,7 @@ interface MessageChain : Message, MutableList<Message> {
operator fun <M : Message> get(key: Message.Key<M>): M = first(key) operator fun <M : Message> get(key: Message.Key<M>): M = first(key)
override fun eq(other: Message): Boolean { override fun eq(other: Message): Boolean {
if(other is MessageChain && other.size != this.size) if (other is MessageChain && other.size != this.size)
return false return false
return this.toString() == other.toString() return this.toString() == other.toString()
} }
...@@ -67,13 +75,16 @@ inline operator fun <reified T : Message> MessageChain.getValue(thisRef: Any?, p ...@@ -67,13 +75,16 @@ inline operator fun <reified T : Message> MessageChain.getValue(thisRef: Any?, p
/** /**
* 构造无初始元素的可修改的 [MessageChain]. 初始大小将会被设定为 8 * 构造无初始元素的可修改的 [MessageChain]. 初始大小将会被设定为 8
*/ */
@JsName("emptyMessageChain") @JvmName("newChain")
@JsName("newChain")
@Suppress("FunctionName") @Suppress("FunctionName")
fun MessageChain(): MessageChain = EmptyMessageChain() fun MessageChain(): MessageChain = EmptyMessageChain()
/** /**
* 构造无初始元素的可修改的 [MessageChain]. 初始大小将会被设定为 [initialCapacity] * 构造无初始元素的可修改的 [MessageChain]. 初始大小将会被设定为 [initialCapacity]
*/ */
@JvmName("newChain")
@JsName("newChain")
@Suppress("FunctionName") @Suppress("FunctionName")
fun MessageChain(initialCapacity: Int): MessageChain = fun MessageChain(initialCapacity: Int): MessageChain =
if (initialCapacity == 0) EmptyMessageChain() if (initialCapacity == 0) EmptyMessageChain()
...@@ -83,6 +94,8 @@ fun MessageChain(initialCapacity: Int): MessageChain = ...@@ -83,6 +94,8 @@ fun MessageChain(initialCapacity: Int): MessageChain =
* 构造 [MessageChain] * 构造 [MessageChain]
* 若仅提供一个参数, 请考虑使用 [Message.toChain] 以优化性能 * 若仅提供一个参数, 请考虑使用 [Message.toChain] 以优化性能
*/ */
@JvmName("newChain")
@JsName("newChain")
@Suppress("FunctionName") @Suppress("FunctionName")
fun MessageChain(vararg messages: Message): MessageChain = fun MessageChain(vararg messages: Message): MessageChain =
if (messages.isEmpty()) EmptyMessageChain() if (messages.isEmpty()) EmptyMessageChain()
...@@ -91,6 +104,8 @@ fun MessageChain(vararg messages: Message): MessageChain = ...@@ -91,6 +104,8 @@ fun MessageChain(vararg messages: Message): MessageChain =
/** /**
* 构造 [MessageChain] * 构造 [MessageChain]
*/ */
@JvmName("newChain")
@JsName("newChain")
@Suppress("FunctionName") @Suppress("FunctionName")
fun MessageChain(messages: Iterable<Message>): MessageChain = fun MessageChain(messages: Iterable<Message>): MessageChain =
MessageChainImpl(messages.toMutableList()) MessageChainImpl(messages.toMutableList())
...@@ -106,6 +121,8 @@ fun MessageChain(messages: Iterable<Message>): MessageChain = ...@@ -106,6 +121,8 @@ fun MessageChain(messages: Iterable<Message>): MessageChain =
* *
* @see Message.toChain receiver 模式 * @see Message.toChain receiver 模式
*/ */
@JvmName("newSingleMessageChain")
@JsName("newChain")
@MiraiExperimentalAPI @MiraiExperimentalAPI
@UseExperimental(ExperimentalContracts::class) @UseExperimental(ExperimentalContracts::class)
@Suppress("FunctionName") @Suppress("FunctionName")
...@@ -301,7 +318,7 @@ internal inline class MessageChainImpl constructor( ...@@ -301,7 +318,7 @@ internal inline class MessageChainImpl constructor(
constructor(vararg messages: Message) : this(messages.toMutableList()) constructor(vararg messages: Message) : this(messages.toMutableList())
// region Message override // region Message override
override fun toString(): String = this.delegate.joinToString("") { it.toString() } override fun toString(): String = this.delegate.joinToString("") { it.toString() }
override operator fun contains(sub: String): Boolean = delegate.any { it.contains(sub) } override operator fun contains(sub: String): Boolean = delegate.any { it.contains(sub) }
override fun followedBy(tail: Message): MessageChain { override fun followedBy(tail: Message): MessageChain {
...@@ -352,6 +369,7 @@ internal inline class SingleMessageChainImpl( ...@@ -352,6 +369,7 @@ internal inline class SingleMessageChainImpl(
// region Message override // region Message override
override operator fun contains(sub: String): Boolean = delegate.contains(sub) override operator fun contains(sub: String): Boolean = delegate.contains(sub)
override fun followedBy(tail: Message): MessageChain { override fun followedBy(tail: Message): MessageChain {
require(tail !is SingleOnly) { "SingleOnly Message cannot follow another message" } require(tail !is SingleOnly) { "SingleOnly Message cannot follow another message" }
return if (tail is MessageChain) tail.apply { followedBy(delegate) } return if (tail is MessageChain) tail.apply { followedBy(delegate) }
......
...@@ -7,8 +7,14 @@ ...@@ -7,8 +7,14 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:JvmMultifileClass
@file:JvmName("MessageUtils")
package net.mamoe.mirai.message.data package net.mamoe.mirai.message.data
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
/** /**
* 消息源, 用于被引用. 它将由协议模块实现. * 消息源, 用于被引用. 它将由协议模块实现.
* 消息源只用于 [QuoteReply] * 消息源只用于 [QuoteReply]
...@@ -18,7 +24,7 @@ package net.mamoe.mirai.message.data ...@@ -18,7 +24,7 @@ package net.mamoe.mirai.message.data
* @see MessageSource.quote 引用这条消息, 创建 [MessageChain] * @see MessageSource.quote 引用这条消息, 创建 [MessageChain]
*/ */
interface MessageSource : Message { interface MessageSource : Message {
companion object : Message.Key<MessageSource> companion object Key : Message.Key<MessageSource>
/** /**
* 实际上是个随机数, 但服务器确实是用它当做 uid * 实际上是个随机数, 但服务器确实是用它当做 uid
......
...@@ -7,9 +7,19 @@ ...@@ -7,9 +7,19 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:JvmMultifileClass
@file:JvmName("MessageUtils")
package net.mamoe.mirai.message.data package net.mamoe.mirai.message.data
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
/**
* 纯文本. 可含 emoji 表情.
*
* 一般不需要主动构造 [PlainText], [Message] 可直接与 [String] 相加. Java 用户请使用 [MessageChain.plus]
*/
inline class PlainText(val stringValue: String) : Message { inline class PlainText(val stringValue: String) : Message {
override operator fun contains(sub: String): Boolean = sub in stringValue override operator fun contains(sub: String): Boolean = sub in stringValue
override fun toString(): String = stringValue override fun toString(): String = stringValue
...@@ -17,7 +27,7 @@ inline class PlainText(val stringValue: String) : Message { ...@@ -17,7 +27,7 @@ inline class PlainText(val stringValue: String) : Message {
companion object Key : Message.Key<PlainText> companion object Key : Message.Key<PlainText>
override fun eq(other: Message): Boolean { override fun eq(other: Message): Boolean {
if(other is MessageChain){ if (other is MessageChain) {
return other eq this.toString() return other eq this.toString()
} }
return other is PlainText && other.stringValue == this.stringValue return other is PlainText && other.stringValue == this.stringValue
......
...@@ -7,10 +7,15 @@ ...@@ -7,10 +7,15 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:JvmMultifileClass
@file:JvmName("MessageUtils")
package net.mamoe.mirai.message.data package net.mamoe.mirai.message.data
import net.mamoe.mirai.contact.Member import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.utils.MiraiInternalAPI import net.mamoe.mirai.utils.MiraiInternalAPI
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
/** /**
......
...@@ -7,10 +7,17 @@ ...@@ -7,10 +7,17 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:JvmMultifileClass
@file:JvmName("MessageUtils")
@file:Suppress("MemberVisibilityCanBePrivate") @file:Suppress("MemberVisibilityCanBePrivate")
package net.mamoe.mirai.message.data package net.mamoe.mirai.message.data
import net.mamoe.mirai.utils.MiraiExperimentalAPI
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
/** /**
* XML 消息, 如分享, 卡片等. * XML 消息, 如分享, 卡片等.
* *
...@@ -30,6 +37,7 @@ inline class XMLMessage(val stringValue: String) : Message, ...@@ -30,6 +37,7 @@ inline class XMLMessage(val stringValue: String) : Message,
* 构造一条 XML 消息 * 构造一条 XML 消息
*/ */
@XMLDsl @XMLDsl
@MiraiExperimentalAPI("还未支持")
inline fun buildXMLMessage(block: @XMLDsl XMLMessageBuilder.() -> Unit): XMLMessage = inline fun buildXMLMessage(block: @XMLDsl XMLMessageBuilder.() -> Unit): XMLMessage =
XMLMessage(XMLMessageBuilder().apply(block).text) XMLMessage(XMLMessageBuilder().apply(block).text)
......
...@@ -5,6 +5,9 @@ import net.mamoe.mirai.japt.BlockingContacts; ...@@ -5,6 +5,9 @@ import net.mamoe.mirai.japt.BlockingContacts;
import net.mamoe.mirai.japt.BlockingQQ; import net.mamoe.mirai.japt.BlockingQQ;
import net.mamoe.mirai.japt.Events; import net.mamoe.mirai.japt.Events;
import net.mamoe.mirai.message.GroupMessage; import net.mamoe.mirai.message.GroupMessage;
import net.mamoe.mirai.message.data.At;
import net.mamoe.mirai.message.data.Image;
import net.mamoe.mirai.message.data.MessageUtils;
class BlockingTest { class BlockingTest {
...@@ -19,8 +22,14 @@ class BlockingTest { ...@@ -19,8 +22,14 @@ class BlockingTest {
Events.subscribeAlways(GroupMessage.class, (GroupMessage message) -> { Events.subscribeAlways(GroupMessage.class, (GroupMessage message) -> {
final BlockingQQ sender = BlockingContacts.createBlocking(message.getSender()); final BlockingQQ sender = BlockingContacts.createBlocking(message.getSender());
sender.sendMessage("Hello World!");
sender.sendMessage("Hello"); System.out.println("发送完了");
sender.sendMessage(MessageUtils.newChain()
.plus(new At(message.getSender()))
.plus(Image.fromId("{xxxx}.jpg"))
.plus("123465")
);
}); });
Thread.sleep(999999999); Thread.sleep(999999999);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment