Skip to content

Commit

Permalink
兼容所有使用GameProfile中的textures进行实时皮肤更换的模组 / 插件
Browse files Browse the repository at this point in the history
Fixes #222
Fixes #228
  • Loading branch information
Xujiayao committed Jul 25, 2024
1 parent 144f854 commit bf495ed
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
4 changes: 1 addition & 3 deletions src/main/java/com/xujiayao/discord_mc_chat/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ public static class Generic {
public String consoleLogChannelId = "";
public String updateNotificationChannelId = "";

public boolean useUuidInsteadOfName = true;

public String avatarApi = "https://mc-heads.net/avatar/%player%.png";
public String avatarApi = "https://mc-heads.net/avatar/{player_uuid}.png";

public boolean broadcastPlayerCommandExecution = true;
public boolean broadcastSlashCommandExecution = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//$$ import net.minecraft.network.chat.TextComponent;
//#endif
import net.minecraft.network.chat.contents.TranslatableContents;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.GameRules;
import okhttp3.MediaType;
import okhttp3.Request;
Expand All @@ -27,7 +28,10 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -152,7 +156,7 @@ public static void init() {
}

if (CONFIG.generic.broadcastChatMessages) {
sendDiscordMessage(contentToDiscord, Objects.requireNonNull(player.getDisplayName()).getString(), CONFIG.generic.avatarApi.replace("%player%", (CONFIG.generic.useUuidInsteadOfName ? player.getUUID().toString() : player.getDisplayName().getString())));
sendDiscordMessage(contentToDiscord, Objects.requireNonNull(player.getDisplayName()).getString(), getAvatarUrl(player));
if (CONFIG.multiServer.enable) {
MULTI_SERVER.sendMessage(false, true, false, Objects.requireNonNull(player.getDisplayName()).getString(), CONFIG.generic.formatChatMessages ? contentToMinecraft : message);
}
Expand Down Expand Up @@ -196,7 +200,7 @@ public static void init() {
//$$ SERVER.sendMessage(message);
//#endif

sendDiscordMessage(MarkdownSanitizer.escape(command), Objects.requireNonNull(player.getDisplayName()).getString(), CONFIG.generic.avatarApi.replace("%player%", (CONFIG.generic.useUuidInsteadOfName ? player.getUUID().toString() : player.getDisplayName().getString())));
sendDiscordMessage(MarkdownSanitizer.escape(command), Objects.requireNonNull(player.getDisplayName()).getString(), getAvatarUrl(player));
if (CONFIG.multiServer.enable) {
MULTI_SERVER.sendMessage(false, true, false, player.getDisplayName().getString(), MarkdownSanitizer.escape(command));
}
Expand Down Expand Up @@ -330,4 +334,30 @@ private static void sendDiscordMessage(String content, String username, String a
executor.shutdown();
}
}

// {player_name} conflicts with nickname-changing mods
// TODO Move to Placeholder class
private static String getAvatarUrl(Player player) {
String hash = "null";
if (CONFIG.generic.avatarApi.contains("{player_textures}")) {
try {
//#if MC > 12001
String textures = player.getGameProfile().getProperties().get("textures").iterator().next().value();
//#else
//$$ String textures = player.getGameProfile().getProperties().get("textures").iterator().next().getValue();
//#endif

JsonObject json = new Gson().fromJson(new String(Base64.getDecoder().decode(textures), StandardCharsets.UTF_8), JsonObject.class);
String url = json.getAsJsonObject("textures").getAsJsonObject("SKIN").get("url").getAsString();

hash = url.replace("http://textures.minecraft.net/texture/", "");
} catch (NoSuchElementException ignored) {
}
}

return CONFIG.generic.avatarApi
.replace("{player_uuid}", player.getUUID().toString())
.replace("{player_name}", Objects.requireNonNull(player.getDisplayName()).getString())
.replace("{player_textures}", hash);
}
}

0 comments on commit bf495ed

Please sign in to comment.