Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

弃用 MessageContent.Key #1363

Closed
khjxiaogu opened this issue Jun 24, 2021 · 13 comments · Fixed by #1759
Closed

弃用 MessageContent.Key #1363

khjxiaogu opened this issue Jun 24, 2021 · 13 comments · Fixed by #1759
Labels
N 优先级: 一般 s:idea 子系统: mirai-console-intellij t:enhancement 类型: 现有功能上的优化
Milestone

Comments

@khjxiaogu
Copy link

使用如下Java代码获取第一个纯文本信息的文本内容:

public static String getFirstPlainText(MessageChain msg) {
PlainText pt = (PlainText) msg.get(PlainText.Key);
if (pt == null)
	return "";
return pt.getContent().trim();
}

发生如下错误:

java.lang.ClassCastException: class net.mamoe.mirai.message.data.At cannot be cast to class net.mamoe.mirai.message.data.PlainText (net.mamoe.mirai.message.data.At and net.mamoe.mirai.message.data.PlainText are in unnamed module of loader java.net.URLClassLoader @6db9f5a4)

复现

  1. 注册消息监听器使用如上java代码处理信息。
  2. 向bot发送 @xxx 文本
  3. 发生错误

版本

  • mirai-core-api: 2.7-M1
  • mirai-console: 2.7-M1
@khjxiaogu khjxiaogu added the x:question 标签: 需要更多信息 label Jun 24, 2021
@zhaodice
Copy link
Contributor

At类不能强制转换成PlainText类(大概

@Karlatemp
Copy link
Member

#903

@Karlatemp Karlatemp changed the title MessageChain.get(MessageKey<>)方法的返回值错误 令 Java 环境下 MessageChain.get(PlainText.Key) 得到一个编译时错误 Jun 24, 2021
@Karlatemp Karlatemp added N 优先级: 一般 t:problem 类型: 不容易归类为特性或 bug 的综合问题 labels Jun 24, 2021
@khjxiaogu khjxiaogu changed the title 令 Java 环境下 MessageChain.get(PlainText.Key) 得到一个编译时错误 令 Java 环境下 MessageChain.get(PlainText.Key) 得到一个运行时错误 Jun 24, 2021
@AdoptOSS
Copy link
Contributor

java.lang.ClassCastException: class net.mamoe.mirai.message.data.At cannot be cast to class net.mamoe.mirai.message.data.PlainText (net.mamoe.mirai.message.data.At and net.mamoe.mirai.message.data.PlainText are in unnamed module of loader java.net.URLClassLoader @6db9f5a4)

考虑优化报错信息

@Him188
Copy link
Member

Him188 commented Jun 24, 2021

PlainText.Key 引用的是 MessageContent.Key 因此 CCE 是来自强转

@Him188
Copy link
Member

Him188 commented Jun 24, 2021

可以考虑弃用 MessageContent.Key

MessageChain.get 的注释:

此方法目前仅适用于 [ConstrainSingle] 的消息类型, 如 [MessageSource].

因此我觉得可以弃用 MessageContent.Key

@Him188 Him188 changed the title 令 Java 环境下 MessageChain.get(PlainText.Key) 得到一个运行时错误 弃用 MessageContent.Key Jun 24, 2021
@Him188 Him188 added this to the Backlog milestone Jun 24, 2021
@AdoptOSS
Copy link
Contributor

此方法目前仅适用于 [ConstrainSingle] 的消息类型, 如 [MessageSource].

那么泛型约束应该做出修改

另外,想问一下是否有数据说明,单独设计Key会比使用KClass之类的效率更好?

@Him188
Copy link
Member

Him188 commented Jun 25, 2021

@AdoptOSS

另外,想问一下是否有数据说明,单独设计Key会比使用KClass之类的效率更好?

KClass 具体是指哪里用到的?

@AdoptOSS
Copy link
Contributor

KClass 具体是指哪里用到的?

指直接用KClass作为类型标志

@Him188
Copy link
Member

Him188 commented Jun 25, 2021

指直接用KClass作为类型标志

似乎并没有使用? Key 是一个 companion object, 类型判断通过 reified 类型的 as?

@AdoptOSS
Copy link
Contributor

似乎并没有使用? Key 是一个 companion object, 类型判断通过 reified 类型的 as?

就是想问一问这种方式比直接用KClass会有什么优势

@Him188
Copy link
Member

Him188 commented Nov 25, 2021

@AdoptOSS 通常不考虑这样微小的性能优化。这是为了允许如下写法:

val source = chain[MessageSource]

@Him188 Him188 modified the milestones: Backlog, 2.10 Nov 25, 2021
@Him188
Copy link
Member

Him188 commented Nov 25, 2021

For mamoe:

  • 2.10: review

@Him188 Him188 modified the milestones: 2.10, 2.10.0-RC Dec 13, 2021
@Him188
Copy link
Member

Him188 commented Dec 23, 2021

考虑到 MessageContent.Key 正用做其他 Key 的 parent, 将不会在源码中弃用. 添加了一个 IDE inspection:
image

Him188 added a commit that referenced this issue Dec 23, 2021
Him188 added a commit that referenced this issue Dec 23, 2021
Him188 added a commit that referenced this issue Dec 27, 2021
@Him188 Him188 added the s:idea 子系统: mirai-console-intellij label Jan 20, 2022
@Him188 Him188 added t:enhancement 类型: 现有功能上的优化 and removed x:question 标签: 需要更多信息 t:problem 类型: 不容易归类为特性或 bug 的综合问题 labels Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
N 优先级: 一般 s:idea 子系统: mirai-console-intellij t:enhancement 类型: 现有功能上的优化
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants