From 655923a650336df550fc214dcea093ea7d87a369 Mon Sep 17 00:00:00 2001 From: Sollace Date: Sat, 4 Apr 2020 00:24:08 +0200 Subject: [PATCH] Split the background pony code into enother class and clean up PonyManager --- .../minelittlepony/api/pony/IPonyManager.java | 14 +- .../client/hdskins/MineLPHDSkins.java | 2 +- .../client/pony/BackgroundPonyList.java | 31 ++-- .../client/pony/PonyManager.java | 159 +----------------- 4 files changed, 34 insertions(+), 172 deletions(-) diff --git a/src/main/java/com/minelittlepony/api/pony/IPonyManager.java b/src/main/java/com/minelittlepony/api/pony/IPonyManager.java index 6484c26b..382a9901 100644 --- a/src/main/java/com/minelittlepony/api/pony/IPonyManager.java +++ b/src/main/java/com/minelittlepony/api/pony/IPonyManager.java @@ -11,10 +11,8 @@ import java.util.UUID; */ public interface IPonyManager { - public static final Identifier STEVE = new Identifier("minelittlepony", "textures/entity/steve_pony.png"); - public static final Identifier ALEX = new Identifier("minelittlepony", "textures/entity/alex_pony.png"); - - public static final String BGPONIES_JSON = "textures/entity/pony/bgponies.json"; + Identifier STEVE = new Identifier("minelittlepony", "textures/entity/steve_pony.png"); + Identifier ALEX = new Identifier("minelittlepony", "textures/entity/alex_pony.png"); /** * Gets or creates a pony for the given player. @@ -22,14 +20,14 @@ public interface IPonyManager { * * @param player the player */ - public IPony getPony(PlayerEntity player); + IPony getPony(PlayerEntity player); /** * Gets or creates a pony for the given skin resource and vanilla model type. * * @param resource A texture resource */ - public IPony getPony(Identifier resource); + IPony getPony(Identifier resource); /** * Gets or creates a pony for the given skin resource and entity id. @@ -64,14 +62,14 @@ public interface IPonyManager { */ void removePony(Identifier resource); - public static Identifier getDefaultSkin(UUID uuid) { + static Identifier getDefaultSkin(UUID uuid) { return isSlimSkin(uuid) ? ALEX : STEVE; } /** * Returns true if the given uuid is of a player would would use the ALEX skin type. */ - public static boolean isSlimSkin(UUID uuid) { + static boolean isSlimSkin(UUID uuid) { return (uuid.hashCode() & 1) == 1; } } diff --git a/src/main/java/com/minelittlepony/client/hdskins/MineLPHDSkins.java b/src/main/java/com/minelittlepony/client/hdskins/MineLPHDSkins.java index 9c0e6faf..5862356e 100644 --- a/src/main/java/com/minelittlepony/client/hdskins/MineLPHDSkins.java +++ b/src/main/java/com/minelittlepony/client/hdskins/MineLPHDSkins.java @@ -27,7 +27,7 @@ public class MineLPHDSkins extends SkinsProxy implements ClientModInitializer { ClientReadyCallback.EVENT.register(client -> { // Clear ponies when skins are cleared PonyManager ponyManager = (PonyManager) MineLittlePony.getInstance().getManager(); - SkinCacheClearCallback.EVENT.register(ponyManager::onSkinCacheCleared); + SkinCacheClearCallback.EVENT.register(ponyManager::clearCache); // Ponify the skins GUI. GuiSkins.setSkinsGui(GuiSkinsMineLP::new); diff --git a/src/main/java/com/minelittlepony/client/pony/BackgroundPonyList.java b/src/main/java/com/minelittlepony/client/pony/BackgroundPonyList.java index ab03bad5..50dddab7 100644 --- a/src/main/java/com/minelittlepony/client/pony/BackgroundPonyList.java +++ b/src/main/java/com/minelittlepony/client/pony/BackgroundPonyList.java @@ -1,15 +1,13 @@ package com.minelittlepony.client.pony; +import net.minecraft.client.MinecraftClient; import net.minecraft.resource.Resource; import net.minecraft.resource.ResourceManager; -import net.minecraft.resource.ResourceReloadListener.Synchronizer; import net.minecraft.util.Identifier; -import net.minecraft.util.profiler.Profiler; import com.google.common.collect.Lists; import com.google.gson.Gson; import com.google.gson.JsonParseException; -import com.minelittlepony.api.pony.IPony; import com.minelittlepony.api.pony.IPonyManager; import com.minelittlepony.client.MineLittlePony; import com.minelittlepony.common.util.MoreStreams; @@ -23,26 +21,29 @@ import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.UUID; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.Executor; -public class BackgroundPonyList { +/** + * 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(); - public IPony getBackgroundPony(UUID uuid) { - if (getNumberOfPonies() == 0 || isUser(uuid)) { - return getPony(IPonyManager.getDefaultSkin(uuid)); + public Identifier getId(UUID uuid) { + if (size() == 0 || isUser(uuid)) { + return IPonyManager.getDefaultSkin(uuid); } - int bgi = MathUtil.mod(uuid.hashCode(), getNumberOfPonies()); + int bgi = MathUtil.mod(uuid.hashCode(), size()); - return getPony(backgroundPonyList.get(bgi)); + return backgroundPonyList.get(bgi); } public void reloadAll(ResourceManager resourceManager) { @@ -79,7 +80,7 @@ public class BackgroundPonyList { backgroundPonyList = MoreStreams.distinct(backgroundPonyList); - MineLittlePony.logger.info("Detected {} background ponies installed.", getNumberOfPonies()); + MineLittlePony.logger.info("Detected {} background ponies installed.", size()); } @@ -108,10 +109,14 @@ public class BackgroundPonyList { return collectedPonies; } - private int getNumberOfPonies() { + 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; diff --git a/src/main/java/com/minelittlepony/client/pony/PonyManager.java b/src/main/java/com/minelittlepony/client/pony/PonyManager.java index dad38574..5110d2da 100644 --- a/src/main/java/com/minelittlepony/client/pony/PonyManager.java +++ b/src/main/java/com/minelittlepony/client/pony/PonyManager.java @@ -3,37 +3,21 @@ package com.minelittlepony.client.pony; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; -import com.google.common.collect.Lists; -import com.google.gson.Gson; -import com.google.gson.JsonParseException; import com.minelittlepony.api.pony.IPony; import com.minelittlepony.api.pony.IPonyManager; import com.minelittlepony.client.MineLittlePony; -import com.minelittlepony.common.util.MoreStreams; import com.minelittlepony.settings.PonyConfig; import com.minelittlepony.settings.PonyLevel; -import com.minelittlepony.util.MathUtil; - import javax.annotation.Nullable; import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener; -import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.AbstractClientPlayerEntity; -import net.minecraft.client.network.PlayerListEntry; import net.minecraft.client.util.DefaultSkinHelper; -import net.minecraft.resource.Resource; import net.minecraft.resource.ResourceManager; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.util.Identifier; import net.minecraft.util.profiler.Profiler; -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; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; @@ -48,12 +32,7 @@ public class PonyManager implements IPonyManager, IdentifiableResourceReloadList private static final Identifier ID = new Identifier("minelittlepony", "background_ponies"); - private static final Gson GSON = new Gson(); - - /** - * All currently loaded background ponies. - */ - private List backgroundPonyList = Lists.newArrayList(); + private final BackgroundPonyList backgroundPonyList = new BackgroundPonyList(); private final PonyConfig config; @@ -99,17 +78,6 @@ public class PonyManager implements IPonyManager, IdentifiableResourceReloadList return null; } - public IPony getPony(PlayerListEntry playerInfo) { - Identifier skin = playerInfo.getSkinTexture(); - UUID uuid = playerInfo.getProfile().getId(); - - if (skin == null) { - return getDefaultPony(uuid); - } - - return getPony(skin, uuid); - } - @Override public IPony getPony(Identifier resource, UUID uuid) { IPony pony = getPony(resource); @@ -132,17 +100,7 @@ public class PonyManager implements IPonyManager, IdentifiableResourceReloadList @Override public IPony getBackgroundPony(UUID uuid) { - if (getNumberOfPonies() == 0 || isUser(uuid)) { - return getPony(IPonyManager.getDefaultSkin(uuid)); - } - - int bgi = MathUtil.mod(uuid.hashCode(), getNumberOfPonies()); - - return getPony(backgroundPonyList.get(bgi)); - } - - private boolean isUser(UUID uuid) { - return MinecraftClient.getInstance().player != null && MinecraftClient.getInstance().player.getUuid().equals(uuid); + return getPony(backgroundPonyList.getId(uuid)); } @Override @@ -150,6 +108,11 @@ public class PonyManager implements IPonyManager, IdentifiableResourceReloadList poniesCache.invalidate(resource); } + public void clearCache() { + MineLittlePony.logger.info("Flushed {} cached ponies.", poniesCache.size()); + poniesCache.invalidateAll(); + } + @Override public CompletableFuture reload(Synchronizer sync, ResourceManager sender, Profiler serverProfiler, Profiler clientProfiler, @@ -159,7 +122,8 @@ public class PonyManager implements IPonyManager, IdentifiableResourceReloadList return sync.whenPrepared(null).thenRunAsync(() -> { clientProfiler.startTick(); clientProfiler.push("Reloading all background ponies"); - reloadAll(sender); + poniesCache.invalidateAll(); + backgroundPonyList.reloadAll(sender); clientProfiler.pop(); clientProfiler.endTick(); }, clientExecutor); @@ -169,109 +133,4 @@ public class PonyManager implements IPonyManager, IdentifiableResourceReloadList public Identifier getFabricId() { return ID; } - - public void clearCache() { - MineLittlePony.logger.info("Flushed {} cached ponies.", poniesCache.size()); - poniesCache.invalidateAll(); - } - - public void reloadAll(ResourceManager resourceManager) { - poniesCache.invalidateAll(); - 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.", getNumberOfPonies()); - } - - 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 getNumberOfPonies() { - return backgroundPonyList.size(); - } - - 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); - } - } - - public void onSkinCacheCleared() { - MineLittlePony.logger.info("Flushed {} cached ponies.", poniesCache.size()); - poniesCache.invalidateAll(); - } }