mirror of
https://github.com/MineLittlePony/MineLittlePony.git
synced 2025-02-13 08:14:23 +01:00
Split the background pony code into enother class and clean up PonyManager
This commit is contained in:
parent
01188a9ea6
commit
655923a650
4 changed files with 34 additions and 172 deletions
|
@ -11,10 +11,8 @@ import java.util.UUID;
|
||||||
*/
|
*/
|
||||||
public interface IPonyManager {
|
public interface IPonyManager {
|
||||||
|
|
||||||
public static final Identifier STEVE = new Identifier("minelittlepony", "textures/entity/steve_pony.png");
|
Identifier STEVE = new Identifier("minelittlepony", "textures/entity/steve_pony.png");
|
||||||
public static final Identifier ALEX = new Identifier("minelittlepony", "textures/entity/alex_pony.png");
|
Identifier ALEX = new Identifier("minelittlepony", "textures/entity/alex_pony.png");
|
||||||
|
|
||||||
public static final String BGPONIES_JSON = "textures/entity/pony/bgponies.json";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets or creates a pony for the given player.
|
* Gets or creates a pony for the given player.
|
||||||
|
@ -22,14 +20,14 @@ public interface IPonyManager {
|
||||||
*
|
*
|
||||||
* @param player the player
|
* @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.
|
* Gets or creates a pony for the given skin resource and vanilla model type.
|
||||||
*
|
*
|
||||||
* @param resource A texture resource
|
* @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.
|
* Gets or creates a pony for the given skin resource and entity id.
|
||||||
|
@ -64,14 +62,14 @@ public interface IPonyManager {
|
||||||
*/
|
*/
|
||||||
void removePony(Identifier resource);
|
void removePony(Identifier resource);
|
||||||
|
|
||||||
public static Identifier getDefaultSkin(UUID uuid) {
|
static Identifier getDefaultSkin(UUID uuid) {
|
||||||
return isSlimSkin(uuid) ? ALEX : STEVE;
|
return isSlimSkin(uuid) ? ALEX : STEVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the given uuid is of a player would would use the ALEX skin type.
|
* 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;
|
return (uuid.hashCode() & 1) == 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ public class MineLPHDSkins extends SkinsProxy implements ClientModInitializer {
|
||||||
ClientReadyCallback.EVENT.register(client -> {
|
ClientReadyCallback.EVENT.register(client -> {
|
||||||
// Clear ponies when skins are cleared
|
// Clear ponies when skins are cleared
|
||||||
PonyManager ponyManager = (PonyManager) MineLittlePony.getInstance().getManager();
|
PonyManager ponyManager = (PonyManager) MineLittlePony.getInstance().getManager();
|
||||||
SkinCacheClearCallback.EVENT.register(ponyManager::onSkinCacheCleared);
|
SkinCacheClearCallback.EVENT.register(ponyManager::clearCache);
|
||||||
|
|
||||||
// Ponify the skins GUI.
|
// Ponify the skins GUI.
|
||||||
GuiSkins.setSkinsGui(GuiSkinsMineLP::new);
|
GuiSkins.setSkinsGui(GuiSkinsMineLP::new);
|
||||||
|
|
|
@ -1,15 +1,13 @@
|
||||||
package com.minelittlepony.client.pony;
|
package com.minelittlepony.client.pony;
|
||||||
|
|
||||||
|
import net.minecraft.client.MinecraftClient;
|
||||||
import net.minecraft.resource.Resource;
|
import net.minecraft.resource.Resource;
|
||||||
import net.minecraft.resource.ResourceManager;
|
import net.minecraft.resource.ResourceManager;
|
||||||
import net.minecraft.resource.ResourceReloadListener.Synchronizer;
|
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
import net.minecraft.util.profiler.Profiler;
|
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonParseException;
|
import com.google.gson.JsonParseException;
|
||||||
import com.minelittlepony.api.pony.IPony;
|
|
||||||
import com.minelittlepony.api.pony.IPonyManager;
|
import com.minelittlepony.api.pony.IPonyManager;
|
||||||
import com.minelittlepony.client.MineLittlePony;
|
import com.minelittlepony.client.MineLittlePony;
|
||||||
import com.minelittlepony.common.util.MoreStreams;
|
import com.minelittlepony.common.util.MoreStreams;
|
||||||
|
@ -23,26 +21,29 @@ import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
import java.util.UUID;
|
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 Gson GSON = new Gson();
|
||||||
|
|
||||||
|
private static final String BGPONIES_JSON = "textures/entity/pony/bgponies.json";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All currently loaded background ponies.
|
* All currently loaded background ponies.
|
||||||
*/
|
*/
|
||||||
private List<Identifier> backgroundPonyList = Lists.newArrayList();
|
private List<Identifier> backgroundPonyList = Lists.newArrayList();
|
||||||
|
|
||||||
public IPony getBackgroundPony(UUID uuid) {
|
public Identifier getId(UUID uuid) {
|
||||||
if (getNumberOfPonies() == 0 || isUser(uuid)) {
|
if (size() == 0 || isUser(uuid)) {
|
||||||
return getPony(IPonyManager.getDefaultSkin(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) {
|
public void reloadAll(ResourceManager resourceManager) {
|
||||||
|
@ -79,7 +80,7 @@ public class BackgroundPonyList {
|
||||||
|
|
||||||
backgroundPonyList = MoreStreams.distinct(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;
|
return collectedPonies;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getNumberOfPonies() {
|
private int size() {
|
||||||
return backgroundPonyList.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 static class BackgroundPonies {
|
||||||
|
|
||||||
private boolean override;
|
private boolean override;
|
||||||
|
|
|
@ -3,37 +3,21 @@ package com.minelittlepony.client.pony;
|
||||||
import com.google.common.cache.CacheBuilder;
|
import com.google.common.cache.CacheBuilder;
|
||||||
import com.google.common.cache.CacheLoader;
|
import com.google.common.cache.CacheLoader;
|
||||||
import com.google.common.cache.LoadingCache;
|
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.IPony;
|
||||||
import com.minelittlepony.api.pony.IPonyManager;
|
import com.minelittlepony.api.pony.IPonyManager;
|
||||||
import com.minelittlepony.client.MineLittlePony;
|
import com.minelittlepony.client.MineLittlePony;
|
||||||
import com.minelittlepony.common.util.MoreStreams;
|
|
||||||
import com.minelittlepony.settings.PonyConfig;
|
import com.minelittlepony.settings.PonyConfig;
|
||||||
import com.minelittlepony.settings.PonyLevel;
|
import com.minelittlepony.settings.PonyLevel;
|
||||||
import com.minelittlepony.util.MathUtil;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
|
import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
|
||||||
import net.minecraft.client.MinecraftClient;
|
|
||||||
import net.minecraft.client.network.AbstractClientPlayerEntity;
|
import net.minecraft.client.network.AbstractClientPlayerEntity;
|
||||||
import net.minecraft.client.network.PlayerListEntry;
|
|
||||||
import net.minecraft.client.util.DefaultSkinHelper;
|
import net.minecraft.client.util.DefaultSkinHelper;
|
||||||
import net.minecraft.resource.Resource;
|
|
||||||
import net.minecraft.resource.ResourceManager;
|
import net.minecraft.resource.ResourceManager;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
import net.minecraft.util.profiler.Profiler;
|
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.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ExecutionException;
|
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 Identifier ID = new Identifier("minelittlepony", "background_ponies");
|
||||||
|
|
||||||
private static final Gson GSON = new Gson();
|
private final BackgroundPonyList backgroundPonyList = new BackgroundPonyList();
|
||||||
|
|
||||||
/**
|
|
||||||
* All currently loaded background ponies.
|
|
||||||
*/
|
|
||||||
private List<Identifier> backgroundPonyList = Lists.newArrayList();
|
|
||||||
|
|
||||||
private final PonyConfig config;
|
private final PonyConfig config;
|
||||||
|
|
||||||
|
@ -99,17 +78,6 @@ public class PonyManager implements IPonyManager, IdentifiableResourceReloadList
|
||||||
return null;
|
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
|
@Override
|
||||||
public IPony getPony(Identifier resource, UUID uuid) {
|
public IPony getPony(Identifier resource, UUID uuid) {
|
||||||
IPony pony = getPony(resource);
|
IPony pony = getPony(resource);
|
||||||
|
@ -132,17 +100,7 @@ public class PonyManager implements IPonyManager, IdentifiableResourceReloadList
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IPony getBackgroundPony(UUID uuid) {
|
public IPony getBackgroundPony(UUID uuid) {
|
||||||
if (getNumberOfPonies() == 0 || isUser(uuid)) {
|
return getPony(backgroundPonyList.getId(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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -150,6 +108,11 @@ public class PonyManager implements IPonyManager, IdentifiableResourceReloadList
|
||||||
poniesCache.invalidate(resource);
|
poniesCache.invalidate(resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void clearCache() {
|
||||||
|
MineLittlePony.logger.info("Flushed {} cached ponies.", poniesCache.size());
|
||||||
|
poniesCache.invalidateAll();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<Void> reload(Synchronizer sync, ResourceManager sender,
|
public CompletableFuture<Void> reload(Synchronizer sync, ResourceManager sender,
|
||||||
Profiler serverProfiler, Profiler clientProfiler,
|
Profiler serverProfiler, Profiler clientProfiler,
|
||||||
|
@ -159,7 +122,8 @@ public class PonyManager implements IPonyManager, IdentifiableResourceReloadList
|
||||||
return sync.whenPrepared(null).thenRunAsync(() -> {
|
return sync.whenPrepared(null).thenRunAsync(() -> {
|
||||||
clientProfiler.startTick();
|
clientProfiler.startTick();
|
||||||
clientProfiler.push("Reloading all background ponies");
|
clientProfiler.push("Reloading all background ponies");
|
||||||
reloadAll(sender);
|
poniesCache.invalidateAll();
|
||||||
|
backgroundPonyList.reloadAll(sender);
|
||||||
clientProfiler.pop();
|
clientProfiler.pop();
|
||||||
clientProfiler.endTick();
|
clientProfiler.endTick();
|
||||||
}, clientExecutor);
|
}, clientExecutor);
|
||||||
|
@ -169,109 +133,4 @@ public class PonyManager implements IPonyManager, IdentifiableResourceReloadList
|
||||||
public Identifier getFabricId() {
|
public Identifier getFabricId() {
|
||||||
return ID;
|
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<Identifier> collectedPaths = new LinkedList<>();
|
|
||||||
List<BackgroundPonies> collectedPonies = new LinkedList<>();
|
|
||||||
|
|
||||||
Queue<BackgroundPonies> 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<BackgroundPonies> loadBgPonies(ResourceManager resourceManager, Identifier location) {
|
|
||||||
Queue<BackgroundPonies> 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<String> ponies;
|
|
||||||
|
|
||||||
private List<String> 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<Identifier> getPonies() {
|
|
||||||
return MoreStreams.map(ponies, this::apply);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Identifier> getImports() {
|
|
||||||
return MoreStreams.map(imports, this::makeImport);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onSkinCacheCleared() {
|
|
||||||
MineLittlePony.logger.info("Flushed {} cached ponies.", poniesCache.size());
|
|
||||||
poniesCache.invalidateAll();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue