英文:
Java - emoji4j static method call ends/vanishes/dies without error
问题
我正在编写一个插件,它会从 Discord 获取消息并将其发送到 Minecraft 服务器。
Minecraft 客户端在渲染表情符号时遇到困难。因此,我选择使用 https://github.com/kcthota/emoji4j 将所有表情符号转换为它们的短代码(例如:😃 -> :smile: 或类似的内容)。
**问题:**
当调用静态方法 `shortCodify` 时,它永远不会返回。就好像它在调用的位置终止了代码,永远不会继续执行。控制台没有错误信息。
几乎似乎调用该方法就在那里终止了。步骤 1 从未被打印出来。
它能够多次运行此过程(每次发送 Discord 消息时)。它并没有完全终止进程。
**我已尝试:**
在各个地方添加调试打印,以尝试追踪问题。
PS:不要因为我混合使用了 logger.info 和 system println 而讨厌我,我以后会全部删除的 xD
**控制台输出**
> 13:35:48 [INFO] [Core] 表情管理器已存在。
>
> 13:35:48 [INFO] [Core] 尝试进行短代码转换(包含 1738 个表情符号)
>
> 13:35:48 [INFO] 调试:EventChat.java 步骤 0
*是的.... 它就在那里停下了!*
**代码片段:**
我的代码 / EventChat.java
*注意:`msg` 是一个 `String`*
*`if` 语句(你看到 `else` 的地方)只是检查表情数据是否已加载,因为我在单独的线程中运行了配置加载。知道它能够到达这里并打印出数据存在,所以这不是问题所在。*
...
} else {
logger.info("表情管理器已存在。");
try {
logger.info("尝试进行短代码转换(包含 " + EmojiManager.data().size() + " 个表情符号)");
System.out.println("调试:EventChat.java 步骤 0");
msg = EmojiUtils.shortCodify(msg);
logger.info("新消息:" + msg);
} catch (Exception e) {
logger.info("捕获异常");
e.printStackTrace();
}
}
logger.info("表情已处理。");
Emoji4j / EmojiUtils.java
public static String shortCodify(String text) {
System.out.println("调试:EmojiUtils.java 步骤 1");
String emojifiedText = emojify(text);
System.out.println("调试:EmojiUtils.java 步骤 2");
for (Emoji emoji : EmojiManager.data()) {
StringBuilder shortCodeBuilder = new StringBuilder();
shortCodeBuilder.append(":").append(emoji.getAliases().get(0)).append(":");
emojifiedText = emojifiedText.replace(emoji.getEmoji(), shortCodeBuilder.toString());
System.out.println("调试:EmojiUtils.java 步骤 2.loop");
}
System.out.println("调试:EmojiUtils.java 步骤 3");
return emojifiedText;
}
英文:
I am writing a plugin which takes a message from discord and sends it to a minecraft server.
Minecraft clients have a hard time rendering emojis. Therefore I opted to use https://github.com/kcthota/emoji4j to convert all emojis into their shortcodes (example: 😃 -> ..or similar)
The problem:
When calling the static method shortCodify
it never returns. Almost as if it kills the code where it is and never continues. No errors in console.
It almost seems as though calling the method kills it right there. Step 1 is never printed.
It is able to run through this multiple times (every time I send a discord message). It has not killed the process completely.
I have tried:
Adding the debug prints all over the place to try to track down the issue.
PS: don't hate me for mixing logger.info and system println, I am removing all of this later xD
Console output
> 13:35:48 [INFO] [Core] Emoji manager exists.
>
> 13:35:48 [INFO] [Core] Attempting shortcodify (contains 1738 emojis)
>
>13:35:48 [INFO] DEBUG: EventChat.java step 0
Yes.... it stops there!
Code snippets:
My code / EventChat.java
Note: msg
is a String
The if
statement (of which you see the else
) just checks that the emoji data was loaded, because I ran the config loading in a separate thread. Knowing it is able to get to here and prints that the data exists, this is not the problem.
...
} else {
logger.info("Emoji manager exists.");
try {
logger.info("Attempting shortcodify (contains " + EmojiManager.data().size() + " emojis)");
System.out.println("DEBUG: EventChat.java step 0");
msg = EmojiUtils.shortCodify(msg);
logger.info("new message: " + msg);
} catch (Exception e) {
logger.info("Catching exception");
e.printStackTrace();
}
}
logger.info("Emoji processed.");
Emoji4j / EmojiUtils.java
public static String shortCodify(String text) {
System.out.println("DEBUG: EmojiUtils.java step 1");
String emojifiedText = emojify(text);
System.out.println("DEBUG: EmojiUtils.java step 2");
for (Emoji emoji : EmojiManager.data()) {
StringBuilder shortCodeBuilder = new StringBuilder();
shortCodeBuilder.append(":").append(emoji.getAliases().get(0)).append(":");
emojifiedText = emojifiedText.replace(emoji.getEmoji(), shortCodeBuilder.toString());
System.out.println("DEBUG: EmojiUtils.java step 2.loop");
}
System.out.println("DEBUG: EmojiUtils.java step 3");
return emojifiedText;
}
答案1
得分: 0
我在漫长的时间之后找到了答案。 (是的,2个月哈哈)
注意:这仅适用于使用emoji4j的任何人使用JDA
JDA默认捕获所有的Throwables,并尝试将其记录到控制台,但由于BungeeCord未使用相同的日志记录器(或类似的东西,我不太清楚原因),记录失败了。
我并不太蠢,因为我尝试捕捉所有的异常并将它们记录下来。但是它抛出了一个Throwable而不是异常...原因是什么我也不知道...
所以,长话短说,我在捕获异常时,JDA捕获了指示缺少依赖项的Throwable,使错误消失而不是打印到控制台。
修复方法
try {
} catch (Throwable t) {
// 现在捕获了错误,并可以使用Bungee的记录器进行记录
}
英文:
I found the answer after what seems to be wayyy too long. (yes, 2 months lol)
NOTE: this only applies to anyone using JDA with emoji4j
JDA catches all Throwables by default and attempts to log it to the console but fails due to bungeecord not using the same logger (or something similar, I don't really know why).
I wasn't too stupid, as I tried catching all exceptions and logging them. BUT it was throwing a throwable instead of an exception.... for whatever reason...
So, long story short, I was catching excpetions and JDA was catching the Throwable that indicated the missing dependency and making the error vanish instead of printing to console.
Fix
try {
} catch (Throwable t) {
// error is now caught and can be logged using bungee's logger
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论