Skip to content

Commit

Permalink
feat: add samolego/FabricTailor support for player avatars (closes #68
Browse files Browse the repository at this point in the history
) (#75)
  • Loading branch information
axieum authored Jun 6, 2023
1 parent 207070c commit 6a14aba
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ allprojects {
mavenCentral()
maven { name 'dv8tion'; url 'https://m2.dv8tion.net/releases' }
maven { name 'Fabric'; url 'https://maven.fabricmc.net/' }
maven { name 'Modrinth'; url 'https://api.modrinth.com/maven' }
maven { name 'Nucleoid'; url 'https://maven.nucleoid.xyz/' }
maven { name 'Shedaniel'; url 'https://maven.shedaniel.me/' }
}
Expand Down
1 change: 1 addition & 0 deletions minecord-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies {
exclude module: 'modmenu'
}
modApi include("eu.pb4:placeholder-api:${project.placeholder_api_version}")
modImplementation "maven.modrinth:fabrictailor:${project.fabric_tailor_version}"
implementation shade("com.vdurmont:emoji-java:${project.emoji_java_version}")
implementation shade("net.dv8tion:JDA:${project.jda_version}") {
exclude module: 'opus-java' // exclude audio
Expand Down
1 change: 1 addition & 0 deletions minecord-api/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod_version = 1.1.0-beta.3
# Dependencies
cloth_config_version = 10.0.96
emoji_java_version = 5.1.1
fabric_tailor_version = 2.1.1
jda_version = 5.0.0-beta.9
log4j_version = 2.20.0
placeholder_api_version = 2.1.0+1.19.4
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import eu.pb4.placeholders.api.PlaceholderContext;
import me.shedaniel.autoconfig.ConfigHolder;
Expand All @@ -14,11 +15,14 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
import org.samo_lego.fabrictailor.casts.TailoredPlayer;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer;

import net.fabricmc.api.DedicatedServerModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint;
import net.fabricmc.loader.impl.entrypoint.EntrypointUtils;

Expand Down Expand Up @@ -120,11 +124,24 @@ public Optional<String> getAvatarUrl(@Nullable String uuid, int height)
{
// Only return an avatar URL if they are enabled and the provided UUID is valid
if (getConfig().misc.enableAvatars && uuid != null && !uuid.isBlank()) {
return getMinecraft().map(server -> PlaceholdersExt.parseString(
getConfig().misc.avatarUrlNode,
PlaceholderContext.of(server),
Map.of("uuid", string(uuid), "size", string(String.valueOf(height)))
));
return getMinecraft().map(server -> {
// Handle Fabric Tailor (https://github.com/samolego/FabricTailor) skins
String skinId = null;
if (FabricLoader.getInstance().isModLoaded("fabrictailor")) {
PlayerEntity player = server.getPlayerManager().getPlayer(UUID.fromString(uuid));
if (player != null) skinId = ((TailoredPlayer) player).getSkinId();
}
// Format the avatar URL template and return
return PlaceholdersExt.parseString(
getConfig().misc.avatarUrlNode,
PlaceholderContext.of(server),
Map.of(
"uuid", string(uuid),
"skin_id", string(skinId != null ? skinId : uuid),
"size", string(String.valueOf(height))
)
);
});
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ public class MiscConfig implements ConfigData
*
* <ul>
* <li>{@code ${uuid}} &mdash; the UUID of the Minecraft player</li>
* <li>{@code ${skin_id}} &mdash; the skin id (from Fabric Tailor mod) or UUID of the Minecraft player</li>
* <li>{@code ${size}} &mdash; the desired avatar height in pixels</li>
* </ul>
*/
@Comment("""
The URL used for retrieving Minecraft player avatars
Usages: ${uuid} and ${size} (height in pixels)""")
public String avatarUrl = "https://api.tydiumcraft.net/v1/players/skin?uuid=${uuid}&type=avatar&size=${size}";
Usages: ${uuid}, ${skin_id} and ${size} (height in pixels)""")
public String avatarUrl = "https://api.tydiumcraft.net/v1/players/skin?uuid=${skin_id}&type=avatar&size=${size}";

/** Pre-parsed 'avatarUrl' text node. */
public transient TextNode avatarUrlNode;
Expand Down
3 changes: 3 additions & 0 deletions minecord-api/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@
"fabricloader": ">=0.14.17",
"fabric-lifecycle-events-v1": "*",
"placeholder-api": "*"
},
"suggests": {
"fabrictailor": "*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ public static void embed(BiConsumer<EmbedBuilder, ChatEntrySchema> builder, Pred
*
* @param builder consumer to modify the Discord embed builder for a chat entry before queuing
* @param predicate predicate that filters configured chat entries
* @param username Minecraft player username for the avatar embed thumbnail
* @param uuid the UUID of the Minecraft player in the avatar embed thumbnail
* @see #embedWithAvatar(BiConsumer, Predicate, String)
*/
public static void embedWithAvatar(
BiConsumer<EmbedBuilder, ChatEntrySchema> builder,
Predicate<ChatEntrySchema> predicate,
@Nullable String username
@Nullable String uuid
)
{
embedWithAvatar(builder, (action, entry) -> action.queue(), predicate, username);
embedWithAvatar(builder, (action, entry) -> action.queue(), predicate, uuid);
}

/**
Expand Down

0 comments on commit 6a14aba

Please sign in to comment.