2018-12-09 22:18:17 +01:00
|
|
|
package com.minelittlepony.pony.data;
|
2015-08-02 00:36:33 +02:00
|
|
|
|
2016-08-22 23:32:03 +02:00
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.JsonParseException;
|
2018-12-09 22:18:17 +01:00
|
|
|
import com.minelittlepony.MineLittlePony;
|
|
|
|
import com.minelittlepony.PonyConfig;
|
2019-01-06 17:07:42 +01:00
|
|
|
import com.minelittlepony.util.chron.ChronicCache;
|
2018-10-23 10:05:29 +02:00
|
|
|
import com.minelittlepony.util.math.MathUtil;
|
2018-06-03 11:57:22 +02:00
|
|
|
import com.voxelmodpack.hdskins.ISkinCacheClearListener;
|
2018-11-02 13:38:50 +01:00
|
|
|
import com.voxelmodpack.hdskins.util.MoreStreams;
|
|
|
|
|
2018-11-02 13:40:14 +01:00
|
|
|
import net.minecraft.client.Minecraft;
|
2015-08-02 00:36:33 +02:00
|
|
|
import net.minecraft.client.entity.AbstractClientPlayer;
|
2018-04-26 15:56:36 +02:00
|
|
|
import net.minecraft.client.network.NetworkPlayerInfo;
|
2018-04-24 17:12:23 +02:00
|
|
|
import net.minecraft.client.resources.DefaultPlayerSkin;
|
2016-08-22 23:32:03 +02:00
|
|
|
import net.minecraft.client.resources.IResource;
|
|
|
|
import net.minecraft.client.resources.IResourceManager;
|
|
|
|
import net.minecraft.client.resources.IResourceManagerReloadListener;
|
2015-08-02 00:36:33 +02:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
2016-11-25 05:40:19 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
2017-06-13 05:55:50 +02:00
|
|
|
import java.io.Reader;
|
2018-11-02 13:38:50 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.LinkedList;
|
2016-11-25 05:40:19 +01:00
|
|
|
import java.util.List;
|
2018-11-02 13:38:50 +01:00
|
|
|
import java.util.Queue;
|
2016-11-25 05:40:19 +01:00
|
|
|
import java.util.UUID;
|
2015-08-02 00:36:33 +02:00
|
|
|
|
2018-04-25 21:29:49 +02:00
|
|
|
/**
|
|
|
|
* The PonyManager is responsible for reading and recoding all the pony data associated with an entity of skin.
|
|
|
|
*
|
|
|
|
*/
|
2018-06-03 11:57:22 +02:00
|
|
|
public class PonyManager implements IResourceManagerReloadListener, ISkinCacheClearListener {
|
2015-12-09 04:14:42 +01:00
|
|
|
|
2018-04-26 23:53:03 +02:00
|
|
|
public static final ResourceLocation STEVE = new ResourceLocation("minelittlepony", "textures/entity/steve_pony.png");
|
|
|
|
public static final ResourceLocation ALEX = new ResourceLocation("minelittlepony", "textures/entity/alex_pony.png");
|
2018-11-02 13:38:50 +01:00
|
|
|
|
|
|
|
public static final String BGPONIES_JSON = "textures/entity/pony/bgponies.json";
|
2017-06-13 05:55:50 +02:00
|
|
|
|
|
|
|
private static final Gson GSON = new Gson();
|
|
|
|
|
2018-04-25 21:29:49 +02:00
|
|
|
/**
|
|
|
|
* All currently loaded background ponies.
|
|
|
|
*/
|
2016-08-22 23:32:03 +02:00
|
|
|
private List<ResourceLocation> backgroundPonyList = Lists.newArrayList();
|
2015-11-17 06:17:35 +01:00
|
|
|
|
2019-03-23 18:48:20 +01:00
|
|
|
private final PonyConfig config;
|
2015-11-17 06:17:35 +01:00
|
|
|
|
2019-01-06 17:07:42 +01:00
|
|
|
private final ChronicCache<ResourceLocation, Pony> poniesCache = new ChronicCache<>();
|
2015-11-17 06:17:35 +01:00
|
|
|
|
|
|
|
public PonyManager(PonyConfig config) {
|
|
|
|
this.config = config;
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
|
2018-04-25 21:29:49 +02:00
|
|
|
/**
|
|
|
|
* Gets or creates a pony for the given skin resource and vanilla model type.
|
2018-04-27 13:49:33 +02:00
|
|
|
*
|
2018-04-25 21:29:49 +02:00
|
|
|
* @param resource A texture resource
|
|
|
|
*/
|
2018-08-30 16:12:21 +02:00
|
|
|
public IPony getPony(ResourceLocation resource) {
|
2019-01-06 17:07:42 +01:00
|
|
|
return poniesCache.retrieve(resource, Pony::new);
|
2018-04-24 14:55:32 +02:00
|
|
|
}
|
|
|
|
|
2018-04-25 21:29:49 +02:00
|
|
|
/**
|
|
|
|
* Gets or creates a pony for the given player.
|
|
|
|
* Delegates to the background-ponies registry if no pony skins were available and client settings allows it.
|
2018-04-27 13:49:33 +02:00
|
|
|
*
|
2018-04-25 21:29:49 +02:00
|
|
|
* @param player the player
|
|
|
|
*/
|
2018-08-30 16:12:21 +02:00
|
|
|
public IPony getPony(AbstractClientPlayer player) {
|
2018-04-29 20:19:30 +02:00
|
|
|
ResourceLocation skin = player.getLocationSkin();
|
|
|
|
UUID uuid = player.getGameProfile().getId();
|
|
|
|
|
2019-01-06 17:09:47 +01:00
|
|
|
if (Pony.getBufferedImage(skin) == null) {
|
2018-08-27 00:30:04 +02:00
|
|
|
return getDefaultPony(uuid);
|
|
|
|
}
|
2018-04-29 20:19:30 +02:00
|
|
|
|
|
|
|
return getPony(skin, uuid);
|
2018-04-26 15:56:36 +02:00
|
|
|
}
|
2018-04-27 13:49:33 +02:00
|
|
|
|
2018-08-30 16:12:21 +02:00
|
|
|
public IPony getPony(NetworkPlayerInfo playerInfo) {
|
2018-04-26 15:56:36 +02:00
|
|
|
ResourceLocation skin = playerInfo.getLocationSkin();
|
|
|
|
UUID uuid = playerInfo.getGameProfile().getId();
|
2018-04-27 13:49:33 +02:00
|
|
|
|
2019-01-06 17:09:47 +01:00
|
|
|
if (Pony.getBufferedImage(skin) == null) {
|
2018-08-27 00:30:04 +02:00
|
|
|
return getDefaultPony(uuid);
|
|
|
|
}
|
2018-04-27 13:49:33 +02:00
|
|
|
|
2018-04-26 15:56:36 +02:00
|
|
|
return getPony(skin, uuid);
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
2018-04-27 13:49:33 +02:00
|
|
|
|
2018-04-25 21:29:49 +02:00
|
|
|
/**
|
|
|
|
* Gets or creates a pony for the given skin resource and entity id.
|
2018-04-27 13:49:33 +02:00
|
|
|
*
|
2018-04-25 21:29:49 +02:00
|
|
|
* Whether is has slim arms is determined by the id.
|
2018-04-27 13:49:33 +02:00
|
|
|
*
|
2018-04-25 21:29:49 +02:00
|
|
|
* Delegates to the background-ponies registry if no pony skins were available and client settings allows it.
|
2018-04-27 13:49:33 +02:00
|
|
|
*
|
2018-04-25 21:29:49 +02:00
|
|
|
* @param resource A texture resource
|
|
|
|
* @param uuid id of a player or entity
|
|
|
|
*/
|
2018-08-30 16:12:21 +02:00
|
|
|
public IPony getPony(ResourceLocation resource, UUID uuid) {
|
|
|
|
IPony pony = getPony(resource);
|
2018-04-27 13:49:33 +02:00
|
|
|
|
2018-04-24 17:12:23 +02:00
|
|
|
if (config.getPonyLevel() == PonyLevel.PONIES && pony.getMetadata().getRace().isHuman()) {
|
|
|
|
return getBackgroundPony(uuid);
|
|
|
|
}
|
2018-04-27 13:49:33 +02:00
|
|
|
|
2018-04-24 17:12:23 +02:00
|
|
|
return pony;
|
2017-06-16 07:41:36 +02:00
|
|
|
}
|
|
|
|
|
2018-04-25 21:29:49 +02:00
|
|
|
/**
|
|
|
|
* Gets the default pony. Either STEVE/ALEX, or a background pony based on client settings.
|
2018-04-27 13:49:33 +02:00
|
|
|
*
|
2018-04-25 21:29:49 +02:00
|
|
|
* @param uuid id of a player or entity
|
|
|
|
*/
|
2018-08-30 16:12:21 +02:00
|
|
|
public IPony getDefaultPony(UUID uuid) {
|
2018-04-24 17:12:23 +02:00
|
|
|
if (config.getPonyLevel() != PonyLevel.PONIES) {
|
2018-08-26 21:32:17 +02:00
|
|
|
return getPony(DefaultPlayerSkin.getDefaultSkin(uuid));
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
2018-04-24 17:12:23 +02:00
|
|
|
|
|
|
|
return getBackgroundPony(uuid);
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 10:05:29 +02:00
|
|
|
/**
|
|
|
|
* Gets a random background pony determined by the given uuid.
|
|
|
|
*
|
|
|
|
* Useful for mods that offer customisation, especially ones that have a whole lot of NPCs.
|
|
|
|
*
|
|
|
|
* @param uuid A UUID. Either a user or an entity.
|
|
|
|
*/
|
|
|
|
public IPony getBackgroundPony(UUID uuid) {
|
2019-01-06 17:09:47 +01:00
|
|
|
if (getNumberOfPonies() == 0 || isUser(uuid)) {
|
2018-08-26 21:32:17 +02:00
|
|
|
return getPony(getDefaultSkin(uuid));
|
2018-04-26 15:56:36 +02:00
|
|
|
}
|
2018-04-24 17:12:23 +02:00
|
|
|
|
2018-10-23 10:05:29 +02:00
|
|
|
int bgi = MathUtil.mod(uuid.hashCode(), getNumberOfPonies());
|
2018-04-24 17:12:23 +02:00
|
|
|
|
2018-08-26 21:32:17 +02:00
|
|
|
return getPony(backgroundPonyList.get(bgi));
|
2018-04-24 14:55:32 +02:00
|
|
|
}
|
2018-04-27 13:49:33 +02:00
|
|
|
|
2018-04-26 15:56:36 +02:00
|
|
|
private boolean isUser(UUID uuid) {
|
2018-11-02 13:40:14 +01:00
|
|
|
return Minecraft.getMinecraft().player != null && Minecraft.getMinecraft().player.getUniqueID().equals(uuid);
|
2018-04-26 15:56:36 +02:00
|
|
|
}
|
2015-08-02 00:36:33 +02:00
|
|
|
|
2018-04-25 21:29:49 +02:00
|
|
|
/**
|
|
|
|
* De-registers a pony from the cache.
|
|
|
|
*/
|
2018-08-30 16:12:21 +02:00
|
|
|
public IPony removePony(ResourceLocation resource) {
|
2018-05-01 12:38:13 +02:00
|
|
|
return poniesCache.remove(resource);
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
2016-08-22 23:32:03 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResourceManagerReload(IResourceManager resourceManager) {
|
2018-04-27 13:49:33 +02:00
|
|
|
poniesCache.clear();
|
|
|
|
backgroundPonyList.clear();
|
2018-11-02 13:38:50 +01:00
|
|
|
|
|
|
|
List<ResourceLocation> collectedPaths = new LinkedList<>();
|
|
|
|
List<BackgroundPonies> collectedPonies = new LinkedList<>();
|
|
|
|
|
|
|
|
Queue<BackgroundPonies> processingQueue = new LinkedList<>();
|
|
|
|
|
|
|
|
for (String domain : resourceManager.getResourceDomains()) {
|
|
|
|
processingQueue.addAll(loadBgPonies(resourceManager, new ResourceLocation(domain, BGPONIES_JSON)));
|
|
|
|
}
|
|
|
|
|
|
|
|
BackgroundPonies item;
|
|
|
|
while ((item = processingQueue.poll()) != null) {
|
|
|
|
for (ResourceLocation 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(IResourceManager resourceManager, ResourceLocation location) {
|
|
|
|
Queue<BackgroundPonies> collectedPonies = new LinkedList<>();
|
|
|
|
|
2016-08-22 23:32:03 +02:00
|
|
|
try {
|
2018-11-02 13:38:50 +01:00
|
|
|
String path = location.getPath().replace("bgponies.json", "");
|
|
|
|
|
|
|
|
for (IResource res : resourceManager.getAllResources(location)) {
|
2017-06-13 05:55:50 +02:00
|
|
|
try (Reader reader = new InputStreamReader((res.getInputStream()))) {
|
|
|
|
BackgroundPonies ponies = GSON.fromJson(reader, BackgroundPonies.class);
|
2018-11-02 13:38:50 +01:00
|
|
|
|
|
|
|
ponies.domain = location.getNamespace();
|
|
|
|
ponies.path = path;
|
|
|
|
|
|
|
|
collectedPonies.add(ponies);
|
2016-08-22 23:32:03 +02:00
|
|
|
} catch (JsonParseException e) {
|
2016-11-25 05:40:19 +01:00
|
|
|
MineLittlePony.logger.error("Invalid bgponies.json in " + res.getResourcePackName(), e);
|
2016-08-22 23:32:03 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-12 16:21:19 +02:00
|
|
|
} catch (IOException ignored) {
|
2016-08-22 23:32:03 +02:00
|
|
|
// this isn't the exception you're looking for.
|
|
|
|
}
|
2018-11-02 13:38:50 +01:00
|
|
|
|
|
|
|
return collectedPonies;
|
2016-08-22 23:32:03 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 02:03:42 +02:00
|
|
|
public static ResourceLocation getDefaultSkin(UUID uuid) {
|
2018-04-24 17:12:23 +02:00
|
|
|
return isSlimSkin(uuid) ? ALEX : STEVE;
|
|
|
|
}
|
2018-04-27 13:49:33 +02:00
|
|
|
|
2018-04-25 21:29:49 +02:00
|
|
|
/**
|
|
|
|
* Returns true if the given uuid is of a player would would use the ALEX skin type.
|
|
|
|
*/
|
2018-04-24 17:12:23 +02:00
|
|
|
public static boolean isSlimSkin(UUID uuid) {
|
2019-01-06 17:09:47 +01:00
|
|
|
return (uuid.hashCode() & 1) == 1;
|
2016-01-26 09:16:11 +01:00
|
|
|
}
|
2015-08-02 00:36:33 +02:00
|
|
|
|
2016-11-17 05:45:04 +01:00
|
|
|
private int getNumberOfPonies() {
|
2015-12-09 04:14:42 +01:00
|
|
|
return backgroundPonyList.size();
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
2016-08-22 23:32:03 +02:00
|
|
|
|
2016-11-25 05:40:19 +01:00
|
|
|
private static class BackgroundPonies {
|
2016-08-22 23:32:03 +02:00
|
|
|
|
2016-11-25 05:40:19 +01:00
|
|
|
private boolean override;
|
2018-11-02 13:38:50 +01:00
|
|
|
|
2016-08-22 23:32:03 +02:00
|
|
|
private List<String> ponies;
|
|
|
|
|
2018-11-02 13:38:50 +01:00
|
|
|
private List<String> imports = new ArrayList<>();
|
|
|
|
|
|
|
|
private String domain;
|
|
|
|
private String path;
|
2016-11-25 05:40:19 +01:00
|
|
|
|
|
|
|
private ResourceLocation apply(String input) {
|
2018-11-03 21:47:35 +01:00
|
|
|
return new ResourceLocation(domain, String.format("%s%s.png", path, input));
|
2018-11-02 13:38:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private ResourceLocation makeImport(String input) {
|
|
|
|
return new ResourceLocation(domain, String.format("%s%s/bgponies.json", path, input));
|
2016-08-22 23:32:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<ResourceLocation> getPonies() {
|
2018-11-02 13:38:50 +01:00
|
|
|
return MoreStreams.map(ponies, this::apply);
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<ResourceLocation> getImports() {
|
|
|
|
return MoreStreams.map(imports, this::makeImport);
|
2016-08-22 23:32:03 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-03 11:57:22 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onSkinCacheCleared() {
|
|
|
|
MineLittlePony.logger.info("Flushed {} cached ponies.", poniesCache.size());
|
|
|
|
poniesCache.clear();
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|