Skip to content

Commit

Permalink
死亡消息优化:更好的多语言支持
Browse files Browse the repository at this point in the history
  • Loading branch information
Xujiayao committed Feb 10, 2024
1 parent a138b4f commit 1c8961d
Showing 1 changed file with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
import net.minecraft.ChatFormatting;
import net.minecraft.advancements.DisplayInfo;
import net.minecraft.network.chat.Component;
//#if MC >= 11900
import net.minecraft.network.chat.ComponentContents;
//#endif
import net.minecraft.network.chat.MutableComponent;
//#if MC < 11900
//$$ import net.minecraft.network.chat.TextComponent;
//#endif
import net.minecraft.network.chat.contents.TranslatableContents;
import net.minecraft.world.level.GameRules;
import okhttp3.MediaType;
import okhttp3.Request;
Expand Down Expand Up @@ -227,7 +231,40 @@ public static void init() {

MinecraftEvents.PLAYER_DIE.register((player, source) -> {
if (CONFIG.generic.announceDeathMessages) {
System.out.println(source.getLocalizedDeathMessage(player));
//#if MC >= 11900
TranslatableContents deathMessage = (TranslatableContents) source.getLocalizedDeathMessage(player).getContents();
//#else
//$$ TranslatableComponent deathMessage = (TranslatableComponent) source.getLocalizedDeathMessage(player);
//#endif
String key = deathMessage.getKey();
Object[] args = new String[deathMessage.getArgs().length];
for (int i = 0; i < deathMessage.getArgs().length; i++) {
Object object = deathMessage.getArgs()[i];
if (object instanceof Component component) {
//#if MC >= 11900
ComponentContents componentContents = component.getContents();
if (componentContents instanceof TranslatableContents) {
args[i] = Translations.translate(((TranslatableContents) componentContents).getKey());
//#else
//$$ if (component instanceof TranslatableComponent) {
//$$ args[i] = Translations.translate(((TranslatableComponent) component).getKey());
//#endif
} else {
args[i] = component.getString();
}
} else {
args[i] = object == null ? "null" : object.toString();
}
}

CHANNEL.sendMessage(Translations.translateMessage("message.deathMessage")
.replace("%deathMessage%", MarkdownSanitizer.escape(Translations.translate(key, args)))
.replace("%playerName%", MarkdownSanitizer.escape(Objects.requireNonNull(player.getDisplayName()).getString()))).queue();
if (CONFIG.multiServer.enable) {
MULTI_SERVER.sendMessage(false, false, false, null, Translations.translateMessage("message.deathMessage")
.replace("%deathMessage%", MarkdownSanitizer.escape(Translations.translate(key, args)))
.replace("%playerName%", MarkdownSanitizer.escape(player.getDisplayName().getString())));
}
}
});

Expand Down

0 comments on commit 1c8961d

Please sign in to comment.