Skip to content

Commit

Permalink
saving loading
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSchlueter committed Nov 23, 2024
1 parent eef3316 commit 25fe045
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public enum SkinVariant {
}

public enum SkinType {
PLACEHOLDER,
USERNAME,
UUID,
URL,
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/de/oliver/fancynpcs/FancyNpcs.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class FancyNpcs extends JavaPlugin implements FancyNpcsPlugin {
private Function<NpcData, Npc> npcAdapter;
private NpcManagerImpl npcManager;
private AttributeManagerImpl attributeManager;
private SkinManager skinManager;
private SkinManagerImpl skinManager;
private ActionManagerImpl actionManager;
private VisibilityTracker visibilityTracker;
private boolean usingPlotSquared;
Expand Down Expand Up @@ -501,6 +501,10 @@ public SkinManager getSkinManager() {
return skinManager;
}

public SkinManagerImpl getSkinManagerImpl() {
return skinManager;
}

@Override
public ActionManagerImpl getActionManager() {
return actionManager;
Expand Down
52 changes: 32 additions & 20 deletions src/main/java/de/oliver/fancynpcs/NpcManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import de.oliver.fancynpcs.api.actions.NpcAction;
import de.oliver.fancynpcs.api.skins.SkinData;
import de.oliver.fancynpcs.api.utils.NpcEquipmentSlot;
import de.oliver.fancynpcs.skins.SkinUtils;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand Down Expand Up @@ -178,6 +179,8 @@ public void saveNpcs(boolean force) {

if (data.getSkin() != null) {
npcConfig.set("npcs." + data.getId() + ".skin.identifier", data.getSkin().identifier());
npcConfig.set("npcs." + data.getId() + ".skin.skinType", data.getSkin().type());
npcConfig.set("npcs." + data.getId() + ".skin.skinVariant", data.getSkin().variant());
} else {
npcConfig.set("npcs." + data.getId() + ".skin.identifier", null);
}
Expand Down Expand Up @@ -267,26 +270,35 @@ public void loadNpcs() {
}

String skinIdentifier = npcConfig.getString("npcs." + id + ".skin.identifier", npcConfig.getString("npcs." + id + ".skin.uuid", ""));
SkinData skin = null;
//TODO
// if (!skinIdentifier.isEmpty()) {
// skin = new SkinData(skinIdentifier, "", "");
// }
//
// if (npcConfig.isSet("npcs." + id + ".skin.value") && npcConfig.isSet("npcs." + id + ".skin.signature")) {
// // using old skin system --> take backup
// takeBackup(npcConfig);
//
// String value = npcConfig.getString("npcs." + id + ".skin.value");
// String signature = npcConfig.getString("npcs." + id + ".skin.signature");
//
// if (value != null && !value.isEmpty() && signature != null && !signature.isEmpty()) {
// skin = new SkinFetcher.SkinData(skinIdentifier, value, signature);
// SkinFetcher.SkinData oldSkinData = new SkinFetcher.SkinData(skinIdentifier, value, signature);
// SkinFetcher.skinCache.put(skinIdentifier, oldSkinData);
// FancyNpcsPlugin.get().getSkinCache().upsert(new SkinFetcher.SkinCacheData(oldSkinData, System.currentTimeMillis(), 1000 * 60 * 60 * 24));
// }
// }

String skinVariantStr = npcConfig.getString("npcs." + id + ".skin.variant", "DEFAULT");
SkinData.SkinVariant skinVariant = SkinData.SkinVariant.valueOf(skinVariantStr);

String skinTypeStr = npcConfig.getString("npcs." + id + ".skin.type", "N/A");
SkinData.SkinType skinType = null;

if (skinTypeStr.equals("N/A")) {
skinType = SkinUtils.getSkinType(skinIdentifier);
} else {
skinType = SkinData.SkinType.valueOf(skinTypeStr);
}

SkinData skin = new SkinData(skinIdentifier, skinType, skinVariant, "", "");


if (npcConfig.isSet("npcs." + id + ".skin.value") && npcConfig.isSet("npcs." + id + ".skin.signature")) {
// using old skin system --> take backup
takeBackup(npcConfig);

String value = npcConfig.getString("npcs." + id + ".skin.value");
String signature = npcConfig.getString("npcs." + id + ".skin.signature");

if (value != null && !value.isEmpty() && signature != null && !signature.isEmpty()) {
SkinData oldSkin = new SkinData(skinIdentifier, SkinData.SkinType.UUID, SkinData.SkinVariant.DEFAULT, value, signature);
FancyNpcs.getInstance().getSkinManagerImpl().getFileCache().addSkin(oldSkin);
FancyNpcs.getInstance().getSkinManagerImpl().getMemCache().addSkin(oldSkin);
}
}

boolean oldMirrorSkin = npcConfig.getBoolean("npcs." + id + ".mirrorSkin"); //TODO: remove in next version
boolean mirrorSkin = oldMirrorSkin || npcConfig.getBoolean("npcs." + id + ".skin.mirrorSkin");
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/de/oliver/fancynpcs/skins/SkinUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.oliver.fancynpcs.skins;

import de.oliver.fancynpcs.api.skins.SkinData;

public class SkinUtils {

public static boolean isPlaceholder(String identifier) {
Expand All @@ -14,4 +16,28 @@ public static boolean isURL(String identifier) {
return identifier.startsWith("http");
}

public static boolean isFile(String identifier) {
return identifier.endsWith(".png") || identifier.endsWith(".jpg") || identifier.endsWith(".jpeg");
}

public static SkinData.SkinType getSkinType(String identifier) {
if (isPlaceholder(identifier)) {
return SkinData.SkinType.PLACEHOLDER;
}

if (isUUID(identifier)) {
return SkinData.SkinType.UUID;
}

if (isURL(identifier)) {
return SkinData.SkinType.URL;
}

if (isFile(identifier)) {
return SkinData.SkinType.FILE;
}

return SkinData.SkinType.USERNAME;
}

}

0 comments on commit 25fe045

Please sign in to comment.