2019-03-23 21:49:34 +02:00
|
|
|
package com.minelittlepony.client.pony;
|
2015-08-01 18:36:33 -04:00
|
|
|
|
2019-06-24 20:08:51 -04:00
|
|
|
import com.google.common.cache.CacheBuilder;
|
|
|
|
import com.google.common.cache.CacheLoader;
|
|
|
|
import com.google.common.cache.LoadingCache;
|
2020-04-03 23:54:12 +02:00
|
|
|
import com.minelittlepony.api.pony.IPony;
|
|
|
|
import com.minelittlepony.api.pony.IPonyManager;
|
2019-07-07 22:47:13 -04:00
|
|
|
import com.minelittlepony.client.MineLittlePony;
|
2019-03-23 21:58:50 +02:00
|
|
|
import com.minelittlepony.settings.PonyConfig;
|
|
|
|
import com.minelittlepony.settings.PonyLevel;
|
2021-06-10 19:32:21 +02:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2019-03-23 20:17:46 +02:00
|
|
|
|
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.network.AbstractClientPlayerEntity;
|
|
|
|
import net.minecraft.client.util.DefaultSkinHelper;
|
|
|
|
import net.minecraft.resource.ResourceManager;
|
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.util.Identifier;
|
|
|
|
import net.minecraft.util.profiler.Profiler;
|
2016-11-24 23:40:19 -05:00
|
|
|
|
|
|
|
import java.util.UUID;
|
2019-05-27 17:59:15 +02:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
2019-07-13 20:11:14 -04:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2019-05-27 17:59:15 +02:00
|
|
|
import java.util.concurrent.Executor;
|
2019-06-24 20:08:51 -04:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2015-08-01 18:36:33 -04: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-12 23:55:50 -04:00
|
|
|
|
2020-04-04 00:24:08 +02:00
|
|
|
private final BackgroundPonyList backgroundPonyList = new BackgroundPonyList();
|
2015-11-17 00:17:35 -05:00
|
|
|
|
2019-03-23 19:48:20 +02:00
|
|
|
private final PonyConfig config;
|
2015-11-17 00:17:35 -05:00
|
|
|
|
2019-07-03 15:58:28 +02:00
|
|
|
private final LoadingCache<Identifier, IPony> poniesCache = CacheBuilder.newBuilder()
|
2019-06-24 20:08:51 -04:00
|
|
|
.expireAfterAccess(30, TimeUnit.SECONDS)
|
|
|
|
.build(CacheLoader.from(Pony::new));
|
2015-11-17 00:17:35 -05:00
|
|
|
|
|
|
|
public PonyManager(PonyConfig config) {
|
|
|
|
this.config = config;
|
2015-08-01 18:36:33 -04:00
|
|
|
}
|
|
|
|
|
2019-03-23 20:17:46 +02:00
|
|
|
@Override
|
2019-05-27 17:59:15 +02:00
|
|
|
public IPony getPony(Identifier resource) {
|
2019-07-13 20:11:14 -04:00
|
|
|
try {
|
|
|
|
return poniesCache.get(resource);
|
|
|
|
} catch (ExecutionException e) {
|
2021-11-25 00:41:12 +02:00
|
|
|
return new Pony(resource, Memoize.of(PonyData.NULL));
|
2019-07-13 20:11:14 -04:00
|
|
|
}
|
2018-04-24 14:55:32 +02:00
|
|
|
}
|
|
|
|
|
2019-03-23 20:17:46 +02:00
|
|
|
@Override
|
2019-05-27 17:59:15 +02:00
|
|
|
public IPony getPony(PlayerEntity player) {
|
2019-07-03 22:06:00 -04: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-03 22:06:00 -04:00
|
|
|
if (skin == null) {
|
2018-08-26 18:30:04 -04:00
|
|
|
return getDefaultPony(uuid);
|
|
|
|
}
|
2018-04-29 20:19:30 +02:00
|
|
|
|
2021-05-16 16:20:02 +02:00
|
|
|
if (player instanceof IPonyManager.ForcedPony) {
|
|
|
|
return getPony(skin);
|
|
|
|
}
|
|
|
|
|
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 20:17:46 +02:00
|
|
|
@Nullable
|
2019-07-03 22:06:00 -04: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 20:17:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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 01:41:36 -04:00
|
|
|
}
|
|
|
|
|
2019-03-23 20:17:46 +02: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) {
|
2021-02-03 13:45:52 +02:00
|
|
|
return ((Pony)getPony(DefaultSkinHelper.getTexture(uuid))).defaulted();
|
2015-08-01 18:36:33 -04:00
|
|
|
}
|
2018-04-24 17:12:23 +02:00
|
|
|
|
|
|
|
return getBackgroundPony(uuid);
|
2015-08-01 18:36:33 -04:00
|
|
|
}
|
|
|
|
|
2019-03-23 20:17:46 +02:00
|
|
|
@Override
|
2018-10-23 10:05:29 +02:00
|
|
|
public IPony getBackgroundPony(UUID uuid) {
|
2021-02-03 13:45:52 +02:00
|
|
|
return ((Pony)getPony(backgroundPonyList.getId(uuid))).defaulted();
|
2018-04-26 15:56:36 +02:00
|
|
|
}
|
2015-08-01 18:36:33 -04:00
|
|
|
|
2019-03-23 20:17:46 +02:00
|
|
|
@Override
|
2019-06-24 20:08:51 -04:00
|
|
|
public void removePony(Identifier resource) {
|
|
|
|
poniesCache.invalidate(resource);
|
2015-08-01 18:36:33 -04:00
|
|
|
}
|
2016-08-22 17:32:03 -04:00
|
|
|
|
2020-04-04 00:24:08 +02:00
|
|
|
public void clearCache() {
|
|
|
|
MineLittlePony.logger.info("Flushed {} cached ponies.", poniesCache.size());
|
|
|
|
poniesCache.invalidateAll();
|
|
|
|
}
|
|
|
|
|
2016-08-22 17:32:03 -04: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");
|
2020-04-04 00:24:08 +02:00
|
|
|
poniesCache.invalidateAll();
|
|
|
|
backgroundPonyList.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;
|
|
|
|
}
|
2015-08-01 18:36:33 -04:00
|
|
|
}
|