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

feat: add samolego/FabricTailor support for player avatars (closes #68) #75

Merged
merged 1 commit into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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