1
mirror of https://github.com/Shiewk/SModeration.git synced 2026-04-28 05:54:16 +02:00

Allow registering skin texture provider for player heads

This commit is contained in:
Shy
2025-05-01 11:25:49 +02:00
parent 8128628b12
commit 955ab8791a
3 changed files with 101 additions and 45 deletions
@@ -29,6 +29,7 @@ public final class SModerationPaper extends JavaPlugin {
public static SModerationPaper PLUGIN = null; public static SModerationPaper PLUGIN = null;
public static SModerationConfig CONFIG = null; public static SModerationConfig CONFIG = null;
public static File SAVE_FILE = null; public static File SAVE_FILE = null;
private static SkinTextureProvider textureProvider = null;
public static final TextColor PRIMARY_COLOR = TextColor.color(212, 0, 255); public static final TextColor PRIMARY_COLOR = TextColor.color(212, 0, 255);
public static final TextColor SECONDARY_COLOR = TextColor.color(52, 143, 255); public static final TextColor SECONDARY_COLOR = TextColor.color(52, 143, 255);
@@ -75,7 +76,7 @@ public final class SModerationPaper extends JavaPlugin {
command.setExecutor(executor); command.setExecutor(executor);
command.setTabCompleter(executor); command.setTabCompleter(executor);
} else { } else {
LOGGER.warn("Command %s failed to register: This command does not exist".formatted(label)); LOGGER.warn("Command {} failed to register: This command does not exist", label);
} }
} }
@@ -89,4 +90,12 @@ public final class SModerationPaper extends JavaPlugin {
} }
} }
} }
public static SkinTextureProvider getTextureProvider() {
return textureProvider;
}
public static void setTextureProvider(SkinTextureProvider textureProvider) {
SModerationPaper.textureProvider = textureProvider;
}
} }
@@ -0,0 +1,10 @@
package de.shiewk.smoderation.paper;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public interface SkinTextureProvider {
CompletableFuture<String> textureProperty(UUID player);
}
@@ -1,5 +1,8 @@
package de.shiewk.smoderation.paper.inventory; package de.shiewk.smoderation.paper.inventory;
import com.destroystokyo.paper.profile.PlayerProfile;
import com.destroystokyo.paper.profile.ProfileProperty;
import de.shiewk.smoderation.paper.SkinTextureProvider;
import de.shiewk.smoderation.paper.input.ChatInput; import de.shiewk.smoderation.paper.input.ChatInput;
import de.shiewk.smoderation.paper.punishments.Punishment; import de.shiewk.smoderation.paper.punishments.Punishment;
import de.shiewk.smoderation.paper.punishments.PunishmentType; import de.shiewk.smoderation.paper.punishments.PunishmentType;
@@ -28,6 +31,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -269,7 +273,26 @@ public class SModMenu extends PageableCustomInventory {
return searchStack = stack; return searchStack = stack;
} }
private ItemStack createPunishmentItem(Punishment punishment){ private CompletableFuture<ItemStack> createPunishmentItem(Punishment punishment){
SkinTextureProvider provider = getTextureProvider();
if (provider != null) {
return provider.textureProperty(punishment.to)
.thenApply(texture -> {
ItemStack stack = new ItemStack(Material.PLAYER_HEAD);
stack.editMeta(meta -> {
if (meta instanceof SkullMeta skullMeta){
PlayerProfile profile = Bukkit.createProfile(punishment.to);
profile.setProperty(new ProfileProperty(
"textures",
"ewogICJ0aW1lc3RhbXAiIDogMTc0NjA5MDc5MDc1NCwKICAicHJvZmlsZUlkIiA6ICJhOGY0YzVhOWFiMmM0YWVlODg2MWRlMDhkMmJmMzYyNyIsCiAgInByb2ZpbGVOYW1lIiA6ICJlaW5lU3BlaXNlIiwKICAidGV4dHVyZXMiIDogewogICAgIlNLSU4iIDogewogICAgICAidXJsIiA6ICJodHRwOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlLzNiMDc0N2UzMTEyYjJiMmQ0MGE1M2Q5YjZlZTkxMDQ4ODQyMDc5MDllY2ZjMzdlZDdmYmZjM2FhMzBhNDE0NGQiCiAgICB9LAogICAgIkNBUEUiIDogewogICAgICAidXJsIiA6ICJodHRwOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlLzVjMjk0MTAwNTdlMzJhYmVjMDJkODcwZWNiNTJlYzI1ZmI0NWVhODFlNzg1YTc4NTRhZTg0MjlkNzIzNmNhMjYiCiAgICB9CiAgfQp9"
));
skullMeta.setPlayerProfile(profile);
}
addPunishmentInfo(punishment, meta);
});
return stack;
});
} else {
ItemStack stack = new ItemStack(Material.PLAYER_HEAD); ItemStack stack = new ItemStack(Material.PLAYER_HEAD);
stack.editMeta(meta -> { stack.editMeta(meta -> {
if (meta instanceof SkullMeta skullMeta){ if (meta instanceof SkullMeta skullMeta){
@@ -279,6 +302,13 @@ public class SModMenu extends PageableCustomInventory {
LOGGER.warn("Player {} has a punishment but was never on this server!", punishment.to); LOGGER.warn("Player {} has a punishment but was never on this server!", punishment.to);
} }
} }
addPunishmentInfo(punishment, meta);
});
return CompletableFuture.completedFuture(stack);
}
}
private void addPunishmentInfo(Punishment punishment, ItemMeta meta) {
meta.displayName(applyFormatting(text(punishment.type.name).color(NamedTextColor.RED).decorate(TextDecoration.BOLD))); meta.displayName(applyFormatting(text(punishment.type.name).color(NamedTextColor.RED).decorate(TextDecoration.BOLD)));
ArrayList<Component> lore = new ArrayList<>(); ArrayList<Component> lore = new ArrayList<>();
lore.add(applyFormatting(text("Player: ").color(SECONDARY_COLOR).append(text(PlayerUtil.offlinePlayerName(punishment.to)).color(PRIMARY_COLOR)))); lore.add(applyFormatting(text("Player: ").color(SECONDARY_COLOR).append(text(PlayerUtil.offlinePlayerName(punishment.to)).color(PRIMARY_COLOR))));
@@ -306,15 +336,16 @@ public class SModMenu extends PageableCustomInventory {
} }
} }
meta.lore(lore); meta.lore(lore);
});
return stack;
} }
private int rfId = 0;
@Override @Override
public void refresh() { public void refresh() {
int rfId = ++this.rfId;
while (getPage() > lastPage()){ while (getPage() > lastPage()){
previousPage(); previousPage();
} }
inventory.clear();
for (int i = 45; i < 54; i++) { for (int i = 45; i < 54; i++) {
inventory.setItem(i, createEmptyStack()); inventory.setItem(i, createEmptyStack());
} }
@@ -329,13 +360,19 @@ public class SModMenu extends PageableCustomInventory {
int ci = i + (getPage() * 45); int ci = i + (getPage() * 45);
if (punishments.size() > ci){ if (punishments.size() > ci){
final Punishment punishment = punishments.get(ci); final Punishment punishment = punishments.get(ci);
final ItemStack item = createPunishmentItem(punishment); int slot = i;
createPunishmentItem(punishment).thenAccept(item -> {
if (rfId != this.rfId) return;
if (punishment.isActive()){ if (punishment.isActive()){
if ((punishment.type == PunishmentType.BAN && player.hasPermission("smod.unban")) || (punishment.type == PunishmentType.MUTE && player.hasPermission("smod.unmute"))) { if ((punishment.type == PunishmentType.BAN && player.hasPermission("smod.unban")) || (punishment.type == PunishmentType.MUTE && player.hasPermission("smod.unmute"))) {
item.editMeta(meta -> meta.getPersistentDataContainer().set(PUNISHMENT_STORE_KEY, PersistentDataType.LONG, punishment.time)); item.editMeta(meta -> meta.getPersistentDataContainer().set(PUNISHMENT_STORE_KEY, PersistentDataType.LONG, punishment.time));
} }
} }
inventory.setItem(i, item); inventory.setItem(slot, item);
}).exceptionally(x -> {
LOGGER.warn("Error creating punishment item", x);
return null;
});
} else { } else {
inventory.setItem(i, new ItemStack(Material.AIR)); inventory.setItem(i, new ItemStack(Material.AIR));
} }