Skip to content

Commit

Permalink
skin cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSchlueter committed Nov 23, 2024
1 parent 4ff5489 commit f9c869c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void setTextureSignature(String textureSignature) {
}

public enum SkinVariant {
AUTO,
SLIM,
DEFAULT
}
Expand Down
14 changes: 8 additions & 6 deletions api/src/main/java/de/oliver/fancynpcs/api/skins/SkinManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
import java.util.UUID;

public interface SkinManager {

SkinData getByUUID(UUID uuid);

SkinData getByUsername(String username);
SkinData getByIdentifier(String identifier, SkinData.SkinVariant variant);

SkinData getByURL(String url);
SkinData getByUUID(UUID uuid, SkinData.SkinVariant variant);

SkinData getByFile(String filePath);
SkinData getByUsername(String username, SkinData.SkinVariant variant);

SkinData get(String name, String value, String signature);
SkinData getByURL(String url, SkinData.SkinVariant variant);

SkinData getByFile(String filePath, SkinData.SkinVariant variant);

SkinData get(String name, String value, String signature, SkinData.SkinVariant variant);

}
8 changes: 1 addition & 7 deletions src/main/java/de/oliver/fancynpcs/commands/npc/SkinCMD.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,7 @@ public void onSkin(
translator.translate("command_npc_modification_cancelled").send(sender);
}
} else {
SkinData skinData;
try {
skinData = new SkinData("", SkinData.SkinVariant.DEFAULT, "", ""); // TODO
} catch (Exception e) {
translator.translate("npc_skin_failure_invalid").replaceStripped("input", skin).send(sender);
return;
}
SkinData skinData = FancyNpcs.getInstance().getSkinManager().getByIdentifier(skin, SkinData.SkinVariant.AUTO); //TODO add variant option

if (new NpcModifyEvent(npc, NpcModifyEvent.NpcModification.SKIN, false, sender).callEvent() && new NpcModifyEvent(npc, NpcModifyEvent.NpcModification.SKIN, skinData, sender).callEvent()) {
npc.getData().setMirrorSkin(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void onPlayerJoin(PlayerJoinEvent event) {

SkinData skinData = new SkinData(
event.getPlayer().getUniqueId().toString(),
SkinData.SkinVariant.DEFAULT,
SkinData.SkinVariant.AUTO,
property.getValue(),
property.getSignature()
);
Expand Down
52 changes: 46 additions & 6 deletions src/main/java/de/oliver/fancynpcs/skins/SkinManagerImpl.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package de.oliver.fancynpcs.skins;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import de.oliver.fancylib.UUIDFetcher;
import de.oliver.fancynpcs.FancyNpcs;
import de.oliver.fancynpcs.api.skins.SkinData;
import de.oliver.fancynpcs.api.skins.SkinManager;
import de.oliver.fancynpcs.skins.cache.SkinCache;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.lushplugins.chatcolorhandler.ChatColorHandler;
import org.mineskin.JsoupRequestHandler;
import org.mineskin.MineSkinClient;
import org.mineskin.data.CodeAndMessage;
import org.mineskin.data.SkinInfo;
import org.mineskin.data.Variant;
import org.mineskin.exception.MineSkinRequestException;
import org.mineskin.request.GenerateRequest;
import org.mineskin.response.JobResponse;
Expand Down Expand Up @@ -54,8 +57,42 @@ public SkinManagerImpl(SkinCache fileCache, SkinCache memCache) {
}

@Override
public SkinData getByUUID(UUID uuid) {
public SkinData getByIdentifier(String identifier, SkinData.SkinVariant variant) {
if (SkinUtils.isUUID(identifier)) {
return getByUUID(UUID.fromString(identifier), variant);
}

if (SkinUtils.isURL(identifier)) {
return getByURL(identifier, variant);
}

if (SkinUtils.isFile(identifier)) {
return getByFile(identifier, variant);
}

if (SkinUtils.isPlaceholder(identifier)) {
String parsed = ChatColorHandler.translate(identifier);

if (SkinUtils.isPlaceholder(parsed)) {
return null;
}

return getByIdentifier(parsed, variant);
}

// is username
UUID uuid = UUIDFetcher.getUUID(identifier);
if (uuid == null) {
return null;
}

return getByUUID(uuid, variant);
}

@Override
public SkinData getByUUID(UUID uuid, SkinData.SkinVariant variant) {
GenerateRequest genReq = GenerateRequest.user(uuid);
genReq.variant(Variant.valueOf(variant.name()));
SkinInfo skinInfo = executeRequest(genReq);

if (skinInfo == null) {
Expand All @@ -71,10 +108,11 @@ public SkinData getByUUID(UUID uuid) {
}

@Override
public SkinData getByUsername(String username) {
public SkinData getByUsername(String username, SkinData.SkinVariant variant) {
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(username); // TODO: implement a better way to get the UUID

GenerateRequest genReq = GenerateRequest.user(offlinePlayer.getUniqueId());
genReq.variant(Variant.valueOf(variant.name()));
SkinInfo skinInfo = executeRequest(genReq);

if (skinInfo == null) {
Expand All @@ -90,14 +128,15 @@ public SkinData getByUsername(String username) {
}

@Override
public SkinData getByURL(String url) {
public SkinData getByURL(String url, SkinData.SkinVariant variant) {
GenerateRequest genReq;
try {
genReq = GenerateRequest.url(url);
} catch (MalformedURLException e) {
FancyNpcs.getInstance().getFancyLogger().error("Invalid URL: " + url);
return null;
}
genReq.variant(Variant.valueOf(variant.name()));

SkinInfo skinInfo = executeRequest(genReq);

Expand All @@ -114,14 +153,15 @@ public SkinData getByURL(String url) {
}

@Override
public SkinData getByFile(String filePath) {
public SkinData getByFile(String filePath, SkinData.SkinVariant variant) {
File file = new File(filePath);
if (!file.exists()) {
FancyNpcs.getInstance().getFancyLogger().error("File does not exist: " + filePath);
return null;
}

GenerateRequest genReq = GenerateRequest.upload(file);
genReq.variant(Variant.valueOf(variant.name()));
SkinInfo skinInfo = executeRequest(genReq);

if (skinInfo == null) {
Expand All @@ -137,10 +177,10 @@ public SkinData getByFile(String filePath) {
}

@Override
public SkinData get(String name, String value, String signature) {
public SkinData get(String name, String value, String signature, SkinData.SkinVariant variant) {
return new SkinData(
name,
SkinData.SkinVariant.DEFAULT,
variant,
value,
signature
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void setUp(Player player) {

@FNTest(name = "Test fetch skin by UUID")
public void testSkinByUUID(Player player) {
SkinData skin = skinFetcher.getByUUID(player.getUniqueId());
SkinData skin = skinFetcher.getByUUID(player.getUniqueId(), SkinData.SkinVariant.AUTO);

expect(skin).toBeDefined();
expect(skin.getIdentifier()).toEqual(player.getUniqueId().toString());
Expand All @@ -37,7 +37,7 @@ public void testSkinByUUID(Player player) {

@FNTest(name = "Test fetch skin by username")
public void testSkinByUsername(Player player) {
SkinData skin = skinFetcher.getByUsername(player.getName());
SkinData skin = skinFetcher.getByUsername(player.getName(), SkinData.SkinVariant.AUTO);

expect(skin).toBeDefined();
expect(skin.getIdentifier()).toEqual(player.getName());
Expand All @@ -49,7 +49,7 @@ public void testSkinByUsername(Player player) {

@FNTest(name = "Test fetch skin by URL")
public void testSkinByURL(Player player) {
SkinData skin = skinFetcher.getByURL("https://s.namemc.com/i/de7d8a3ffd1f584c.png");
SkinData skin = skinFetcher.getByURL("https://s.namemc.com/i/de7d8a3ffd1f584c.png", SkinData.SkinVariant.AUTO);

expect(skin).toBeDefined();
expect(skin.getIdentifier()).toEqual("https://s.namemc.com/i/de7d8a3ffd1f584c.png");
Expand All @@ -62,7 +62,7 @@ public void testSkinByURL(Player player) {

@FNTest(name = "Test fetch skin by file")
public void testSkinByFile(Player player) {
SkinData skin = skinFetcher.getByFile("plugins/FancyNpcs/testskin.png");
SkinData skin = skinFetcher.getByFile("plugins/FancyNpcs/testskin.png", SkinData.SkinVariant.AUTO);
FancyNpcs.getInstance().getPlugin().saveResource("testskin.png", false);

expect(skin).toBeDefined();
Expand All @@ -78,7 +78,7 @@ public void testSkinByFile(Player player) {

@FNTest(name = "Test get skin")
public void testGetSkin(Player player) {
SkinData skin = skinFetcher.get("skinname", "value", "signature");
SkinData skin = skinFetcher.get("skinname", "value", "signature", SkinData.SkinVariant.AUTO);

expect(skin).toBeDefined();
expect(skin.getIdentifier()).toEqual("skinname");
Expand Down

0 comments on commit f9c869c

Please sign in to comment.