From 7d2767d9f88788fc338dda1e121eb82dd4d3e66b Mon Sep 17 00:00:00 2001 From: Sollace Date: Fri, 19 Jun 2020 10:01:29 +0200 Subject: [PATCH] Simplify bg pony skin loading (removes the need for a bgponies.json) --- .../client/pony/BackgroundPonyList.java | 119 ++---------------- .../textures/entity/pony/bgponies.json | 12 -- 2 files changed, 8 insertions(+), 123 deletions(-) delete mode 100644 src/main/resources/assets/minelittlepony/textures/entity/pony/bgponies.json diff --git a/src/main/java/com/minelittlepony/client/pony/BackgroundPonyList.java b/src/main/java/com/minelittlepony/client/pony/BackgroundPonyList.java index 50dddab7..1f28f0e3 100644 --- a/src/main/java/com/minelittlepony/client/pony/BackgroundPonyList.java +++ b/src/main/java/com/minelittlepony/client/pony/BackgroundPonyList.java @@ -1,147 +1,44 @@ package com.minelittlepony.client.pony; import net.minecraft.client.MinecraftClient; -import net.minecraft.resource.Resource; import net.minecraft.resource.ResourceManager; import net.minecraft.util.Identifier; -import com.google.common.collect.Lists; -import com.google.gson.Gson; -import com.google.gson.JsonParseException; import com.minelittlepony.api.pony.IPonyManager; import com.minelittlepony.client.MineLittlePony; -import com.minelittlepony.common.util.MoreStreams; import com.minelittlepony.util.MathUtil; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.Reader; import java.util.ArrayList; -import java.util.LinkedList; import java.util.List; -import java.util.Queue; import java.util.UUID; /** * All currently loaded background ponies. */ class BackgroundPonyList { - - private static final Gson GSON = new Gson(); - - private static final String BGPONIES_JSON = "textures/entity/pony/bgponies.json"; - /** * All currently loaded background ponies. */ - private List backgroundPonyList = Lists.newArrayList(); + private final List backgroundPonyList = new ArrayList<>(); public Identifier getId(UUID uuid) { - if (size() == 0 || isUser(uuid)) { + if (backgroundPonyList.isEmpty() || isUser(uuid)) { return IPonyManager.getDefaultSkin(uuid); } - int bgi = MathUtil.mod(uuid.hashCode(), size()); + int bgi = MathUtil.mod(uuid.hashCode(), backgroundPonyList.size()); return backgroundPonyList.get(bgi); } public void reloadAll(ResourceManager resourceManager) { backgroundPonyList.clear(); - - List collectedPaths = new LinkedList<>(); - List collectedPonies = new LinkedList<>(); - - Queue processingQueue = new LinkedList<>(); - - for (String domain : resourceManager.getAllNamespaces()) { - processingQueue.addAll(loadBgPonies(resourceManager, new Identifier(domain, BGPONIES_JSON))); - } - - BackgroundPonies item; - while ((item = processingQueue.poll()) != null) { - for (Identifier imp : item.getImports()) { - if (!collectedPaths.contains(imp)) { - collectedPaths.add(imp); - processingQueue.addAll(loadBgPonies(resourceManager, imp)); - } - } - - collectedPonies.add(item); - } - - for (BackgroundPonies i : collectedPonies) { - if (i.override) { - backgroundPonyList.clear(); - } - - backgroundPonyList.addAll(i.getPonies()); - } - - backgroundPonyList = MoreStreams.distinct(backgroundPonyList); - - MineLittlePony.logger.info("Detected {} background ponies installed.", size()); + backgroundPonyList.addAll(resourceManager.findResources("textures/entity/pony", path -> path.endsWith(".png"))); + MineLittlePony.logger.info("Detected {} background ponies installed.", backgroundPonyList.size()); } - - private Queue loadBgPonies(ResourceManager resourceManager, Identifier location) { - Queue collectedPonies = new LinkedList<>(); - - try { - String path = location.getPath().replace("bgponies.json", ""); - - for (Resource res : resourceManager.getAllResources(location)) { - try (Reader reader = new InputStreamReader((res.getInputStream()))) { - BackgroundPonies ponies = GSON.fromJson(reader, BackgroundPonies.class); - - ponies.domain = location.getNamespace(); - ponies.path = path; - - collectedPonies.add(ponies); - } catch (JsonParseException e) { - MineLittlePony.logger.error("Invalid bgponies.json in " + res.getResourcePackName(), e); - } - } - } catch (IOException ignored) { - // this isn't the exception you're looking for. - } - - return collectedPonies; - } - - private int size() { - return backgroundPonyList.size(); - } - - private static boolean isUser(UUID uuid) { - return MinecraftClient.getInstance().player != null && MinecraftClient.getInstance().player.getUuid().equals(uuid); - } - - private static class BackgroundPonies { - - private boolean override; - - private List ponies; - - private List imports = new ArrayList<>(); - - private String domain; - private String path; - - private Identifier apply(String input) { - return new Identifier(domain, String.format("%s%s.png", path, input)); - } - - private Identifier makeImport(String input) { - return new Identifier(domain, String.format("%s%s/bgponies.json", path, input)); - } - - public List getPonies() { - return MoreStreams.map(ponies, this::apply); - } - - public List getImports() { - return MoreStreams.map(imports, this::makeImport); - } + private boolean isUser(UUID uuid) { + return MinecraftClient.getInstance().player != null + && MinecraftClient.getInstance().player.getUuid().equals(uuid); } } diff --git a/src/main/resources/assets/minelittlepony/textures/entity/pony/bgponies.json b/src/main/resources/assets/minelittlepony/textures/entity/pony/bgponies.json deleted file mode 100644 index 30534e67..00000000 --- a/src/main/resources/assets/minelittlepony/textures/entity/pony/bgponies.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - // Removes all of the previous resourcepack's ponies - "override": false, - // A list of sub-directories to include - // Pony textures are loaded according to subpath/bgponies.json - "imports": [], - // A list of all the pony names without the extension - // e.g. textures/entity/pony/bgpony_12.png - "ponies": [ - "the_dude" - ] -}