2019-03-23 20:49:34 +01:00
|
|
|
package com.minelittlepony.client.pony;
|
2015-08-02 00:36:33 +02:00
|
|
|
|
2019-06-25 02:08:51 +02:00
|
|
|
import com.google.common.cache.CacheBuilder;
|
|
|
|
import com.google.common.cache.CacheLoader;
|
|
|
|
import com.google.common.cache.LoadingCache;
|
2016-08-22 23:32:03 +02:00
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.JsonParseException;
|
2019-07-08 04:47:13 +02:00
|
|
|
import com.minelittlepony.client.MineLittlePony;
|
2019-03-24 10:30:57 +01:00
|
|
|
import com.minelittlepony.common.util.MoreStreams;
|
2019-03-23 20:58:50 +01:00
|
|
|
import com.minelittlepony.pony.IPony;
|
|
|
|
import com.minelittlepony.pony.IPonyManager;
|
|
|
|
import com.minelittlepony.settings.PonyConfig;
|
|
|
|
import com.minelittlepony.settings.PonyLevel;
|
2019-11-30 13:56:07 +01:00
|
|
|
import com.minelittlepony.util.MathUtil;
|
2018-11-02 13:38:50 +01:00
|
|
|
|
2019-03-23 19:17:46 +01:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2019-07-03 14:16:03 +02:00
|
|
|
import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
|
2019-05-27 17:59:15 +02:00
|
|
|
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;
|
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;
|
2019-05-27 17:59:15 +02:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
2019-07-14 02:11:14 +02:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2019-05-27 17:59:15 +02:00
|
|
|
import java.util.concurrent.Executor;
|
2019-06-25 02:08:51 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
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.
|
|
|
|
*
|
|
|
|
*/
|
2019-07-03 14:16:03 +02:00
|
|
|
public class PonyManager implements IPonyManager, IdentifiableResourceReloadListener {
|
|
|
|
|
|
|
|
private static final Identifier ID = new Identifier("minelittlepony", "background_ponies");
|
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.
|
|
|
|
*/
|
2019-05-27 17:59:15 +02:00
|
|
|
private List<Identifier> 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-07-03 15:58:28 +02:00
|
|
|
private final LoadingCache<Identifier, IPony> poniesCache = CacheBuilder.newBuilder()
|
2019-06-25 02:08:51 +02:00
|
|
|
.expireAfterAccess(30, TimeUnit.SECONDS)
|
|
|
|
.build(CacheLoader.from(Pony::new));
|
2015-11-17 06:17:35 +01:00
|
|
|
|
|
|
|
public PonyManager(PonyConfig config) {
|
|
|
|
this.config = config;
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
|
|
|
|
2019-03-23 19:17:46 +01:00
|
|
|
@Override
|
2019-05-27 17:59:15 +02:00
|
|
|
public IPony getPony(Identifier resource) {
|
2019-07-14 02:11:14 +02:00
|
|
|
try {
|
|
|
|
return poniesCache.get(resource);
|
|
|
|
} catch (ExecutionException e) {
|
2019-11-30 17:49:04 +01:00
|
|
|
return new Pony(resource, PonyData.NULL);
|
2019-07-14 02:11:14 +02:00
|
|
|
}
|
2018-04-24 14:55:32 +02:00
|
|
|
}
|
|
|
|
|
2019-03-23 19:17:46 +01:00
|
|
|
@Override
|
2019-05-27 17:59:15 +02:00
|
|
|
public IPony getPony(PlayerEntity player) {
|
2019-07-04 04:06:00 +02:00
|
|
|
if (player.getGameProfile() == null) {
|
2019-06-05 14:54:38 +02:00
|
|
|
return getDefaultPony(player.getUuid());
|
|
|
|
}
|
|
|
|
|
2019-05-27 17:59:15 +02:00
|
|
|
Identifier skin = getSkin(player);
|
2018-04-29 20:19:30 +02:00
|
|
|
UUID uuid = player.getGameProfile().getId();
|
|
|
|
|
2019-07-04 04:06:00 +02:00
|
|
|
if (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
|
|
|
|
2019-03-23 19:17:46 +01:00
|
|
|
@Nullable
|
2019-07-04 04:06:00 +02:00
|
|
|
private Identifier getSkin(PlayerEntity player) {
|
2019-05-27 17:59:15 +02:00
|
|
|
if (player instanceof AbstractClientPlayerEntity) {
|
|
|
|
return ((AbstractClientPlayerEntity)player).getSkinTexture();
|
2019-03-23 19:17:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-05-27 17:59:15 +02:00
|
|
|
public IPony getPony(PlayerListEntry playerInfo) {
|
|
|
|
Identifier skin = playerInfo.getSkinTexture();
|
|
|
|
UUID uuid = playerInfo.getProfile().getId();
|
2018-04-27 13:49:33 +02:00
|
|
|
|
2019-07-14 01:12:19 +02:00
|
|
|
if (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
|
|
|
|
2019-03-23 19:17:46 +01:00
|
|
|
@Override
|
2019-05-27 17:59:15 +02:00
|
|
|
public IPony getPony(Identifier resource, UUID uuid) {
|
2018-08-30 16:12:21 +02:00
|
|
|
IPony pony = getPony(resource);
|
2018-04-27 13:49:33 +02:00
|
|
|
|
2019-07-11 19:20:13 +02:00
|
|
|
if (config.ponyLevel.get() == PonyLevel.PONIES && pony.getMetadata().getRace().isHuman()) {
|
2018-04-24 17:12:23 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-03-23 19:17:46 +01:00
|
|
|
@Override
|
2018-08-30 16:12:21 +02:00
|
|
|
public IPony getDefaultPony(UUID uuid) {
|
2019-07-11 19:20:13 +02:00
|
|
|
if (config.ponyLevel.get() != PonyLevel.PONIES) {
|
2019-05-27 17:59:15 +02:00
|
|
|
return getPony(DefaultSkinHelper.getTexture(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
|
|
|
}
|
|
|
|
|
2019-03-23 19:17:46 +01:00
|
|
|
@Override
|
2018-10-23 10:05:29 +02:00
|
|
|
public IPony getBackgroundPony(UUID uuid) {
|
2019-01-06 17:09:47 +01:00
|
|
|
if (getNumberOfPonies() == 0 || isUser(uuid)) {
|
2019-03-23 19:17:46 +01:00
|
|
|
return getPony(IPonyManager.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) {
|
2019-05-27 17:59:15 +02:00
|
|
|
return MinecraftClient.getInstance().player != null && MinecraftClient.getInstance().player.getUuid().equals(uuid);
|
2018-04-26 15:56:36 +02:00
|
|
|
}
|
2015-08-02 00:36:33 +02:00
|
|
|
|
2019-03-23 19:17:46 +01:00
|
|
|
@Override
|
2019-06-25 02:08:51 +02:00
|
|
|
public void removePony(Identifier resource) {
|
|
|
|
poniesCache.invalidate(resource);
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|
2016-08-22 23:32:03 +02:00
|
|
|
|
|
|
|
@Override
|
2019-06-11 15:15:44 +02:00
|
|
|
public CompletableFuture<Void> reload(Synchronizer sync, ResourceManager sender,
|
|
|
|
Profiler serverProfiler, Profiler clientProfiler,
|
|
|
|
Executor serverExecutor, Executor clientExecutor) {
|
|
|
|
|
|
|
|
sync.getClass();
|
|
|
|
return sync.whenPrepared(null).thenRunAsync(() -> {
|
|
|
|
clientProfiler.startTick();
|
|
|
|
clientProfiler.push("Reloading all background ponies");
|
2019-05-27 17:59:15 +02:00
|
|
|
reloadAll(sender);
|
2019-06-11 15:15:44 +02:00
|
|
|
clientProfiler.pop();
|
|
|
|
clientProfiler.endTick();
|
|
|
|
}, clientExecutor);
|
2019-05-27 17:59:15 +02:00
|
|
|
}
|
|
|
|
|
2019-07-03 14:16:03 +02:00
|
|
|
@Override
|
|
|
|
public Identifier getFabricId() {
|
|
|
|
return ID;
|
|
|
|
}
|
|
|
|
|
2019-06-30 12:05:38 +02:00
|
|
|
public void clearCache() {
|
|
|
|
MineLittlePony.logger.info("Flushed {} cached ponies.", poniesCache.size());
|
|
|
|
poniesCache.invalidateAll();
|
|
|
|
}
|
|
|
|
|
2019-05-27 17:59:15 +02:00
|
|
|
public void reloadAll(ResourceManager resourceManager) {
|
2019-06-25 02:08:51 +02:00
|
|
|
poniesCache.invalidateAll();
|
2018-04-27 13:49:33 +02:00
|
|
|
backgroundPonyList.clear();
|
2018-11-02 13:38:50 +01:00
|
|
|
|
2019-05-27 17:59:15 +02:00
|
|
|
List<Identifier> collectedPaths = new LinkedList<>();
|
2018-11-02 13:38:50 +01:00
|
|
|
List<BackgroundPonies> collectedPonies = new LinkedList<>();
|
|
|
|
|
|
|
|
Queue<BackgroundPonies> processingQueue = new LinkedList<>();
|
|
|
|
|
2019-05-27 17:59:15 +02:00
|
|
|
for (String domain : resourceManager.getAllNamespaces()) {
|
|
|
|
processingQueue.addAll(loadBgPonies(resourceManager, new Identifier(domain, BGPONIES_JSON)));
|
2018-11-02 13:38:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BackgroundPonies item;
|
|
|
|
while ((item = processingQueue.poll()) != null) {
|
2019-05-27 17:59:15 +02:00
|
|
|
for (Identifier imp : item.getImports()) {
|
2018-11-02 13:38:50 +01:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2019-05-27 17:59:15 +02:00
|
|
|
private Queue<BackgroundPonies> loadBgPonies(ResourceManager resourceManager, Identifier location) {
|
2018-11-02 13:38:50 +01:00
|
|
|
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", "");
|
|
|
|
|
2019-05-27 17:59:15 +02:00
|
|
|
for (Resource 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) {
|
2019-05-27 17:59:15 +02: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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-05-27 17:59:15 +02:00
|
|
|
private Identifier apply(String input) {
|
|
|
|
return new Identifier(domain, String.format("%s%s.png", path, input));
|
2018-11-02 13:38:50 +01:00
|
|
|
}
|
|
|
|
|
2019-05-27 17:59:15 +02:00
|
|
|
private Identifier makeImport(String input) {
|
|
|
|
return new Identifier(domain, String.format("%s%s/bgponies.json", path, input));
|
2016-08-22 23:32:03 +02:00
|
|
|
}
|
|
|
|
|
2019-05-27 17:59:15 +02:00
|
|
|
public List<Identifier> getPonies() {
|
2018-11-02 13:38:50 +01:00
|
|
|
return MoreStreams.map(ponies, this::apply);
|
|
|
|
}
|
|
|
|
|
2019-05-27 17:59:15 +02:00
|
|
|
public List<Identifier> getImports() {
|
2018-11-02 13:38:50 +01:00
|
|
|
return MoreStreams.map(imports, this::makeImport);
|
2016-08-22 23:32:03 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-27 02:33:05 +02:00
|
|
|
|
2019-07-11 03:32:36 +02:00
|
|
|
public void onSkinCacheCleared() {
|
2019-06-27 02:33:05 +02:00
|
|
|
MineLittlePony.logger.info("Flushed {} cached ponies.", poniesCache.size());
|
|
|
|
poniesCache.invalidateAll();
|
|
|
|
}
|
2015-08-02 00:36:33 +02:00
|
|
|
}
|