mirror of
https://github.com/MineLittlePony/MineLittlePony.git
synced 2025-03-15 16:10:05 +01:00
Remove Chronic in favor for guava caches
This commit is contained in:
parent
48d4a24fbd
commit
41580d058b
7 changed files with 23 additions and 76 deletions
|
@ -11,7 +11,6 @@ import com.minelittlepony.pony.IPony;
|
|||
import com.minelittlepony.pony.IPonyData;
|
||||
import com.minelittlepony.pony.meta.Race;
|
||||
import com.minelittlepony.pony.meta.Size;
|
||||
import com.minelittlepony.util.chron.Touchable;
|
||||
|
||||
import net.minecraft.block.Material;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
|
@ -40,7 +39,7 @@ import javax.annotation.Nullable;
|
|||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
@Immutable
|
||||
public class Pony extends Touchable<Pony> implements IPony {
|
||||
public class Pony implements IPony {
|
||||
|
||||
private static final AtomicInteger ponyCount = new AtomicInteger();
|
||||
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
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;
|
||||
|
@ -10,7 +13,6 @@ import com.minelittlepony.pony.IPony;
|
|||
import com.minelittlepony.pony.IPonyManager;
|
||||
import com.minelittlepony.settings.PonyConfig;
|
||||
import com.minelittlepony.settings.PonyLevel;
|
||||
import com.minelittlepony.util.chron.ChronicCache;
|
||||
import com.minelittlepony.util.math.MathUtil;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
@ -36,6 +38,7 @@ import java.util.Queue;
|
|||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* The PonyManager is responsible for reading and recoding all the pony data associated with an entity of skin.
|
||||
|
@ -52,7 +55,9 @@ public class PonyManager implements IPonyManager, ResourceReloadListener, ISkinC
|
|||
|
||||
private final PonyConfig config;
|
||||
|
||||
private final ChronicCache<Identifier, Pony> poniesCache = new ChronicCache<>();
|
||||
private final LoadingCache<Identifier, Pony> poniesCache = CacheBuilder.newBuilder()
|
||||
.expireAfterAccess(30, TimeUnit.SECONDS)
|
||||
.build(CacheLoader.from(Pony::new));
|
||||
|
||||
public PonyManager(PonyConfig config) {
|
||||
this.config = config;
|
||||
|
@ -60,7 +65,7 @@ public class PonyManager implements IPonyManager, ResourceReloadListener, ISkinC
|
|||
|
||||
@Override
|
||||
public IPony getPony(Identifier resource) {
|
||||
return poniesCache.retrieve(resource, Pony::new);
|
||||
return poniesCache.getUnchecked(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -135,8 +140,8 @@ public class PonyManager implements IPonyManager, ResourceReloadListener, ISkinC
|
|||
}
|
||||
|
||||
@Override
|
||||
public IPony removePony(Identifier resource) {
|
||||
return poniesCache.remove(resource);
|
||||
public void removePony(Identifier resource) {
|
||||
poniesCache.invalidate(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -155,7 +160,7 @@ public class PonyManager implements IPonyManager, ResourceReloadListener, ISkinC
|
|||
}
|
||||
|
||||
public void reloadAll(ResourceManager resourceManager) {
|
||||
poniesCache.clear();
|
||||
poniesCache.invalidateAll();
|
||||
backgroundPonyList.clear();
|
||||
|
||||
List<Identifier> collectedPaths = new LinkedList<>();
|
||||
|
@ -252,7 +257,7 @@ public class PonyManager implements IPonyManager, ResourceReloadListener, ISkinC
|
|||
@Override
|
||||
public boolean onSkinCacheCleared() {
|
||||
MineLittlePony.logger.info("Flushed {} cached ponies.", poniesCache.size());
|
||||
poniesCache.clear();
|
||||
poniesCache.invalidateAll();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,7 +62,7 @@ public interface IPonyManager {
|
|||
/**
|
||||
* De-registers a pony from the cache.
|
||||
*/
|
||||
IPony removePony(Identifier resource);
|
||||
void removePony(Identifier resource);
|
||||
|
||||
public static Identifier getDefaultSkin(UUID uuid) {
|
||||
return isSlimSkin(uuid) ? ALEX : STEVE;
|
||||
|
|
|
@ -1,29 +1,25 @@
|
|||
package com.minelittlepony.util.animation;
|
||||
|
||||
import com.minelittlepony.util.chron.ChronicCache;
|
||||
import com.minelittlepony.util.chron.Touchable;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class BasicEasingInterpolator extends Touchable<BasicEasingInterpolator> implements IInterpolator {
|
||||
public class BasicEasingInterpolator implements IInterpolator {
|
||||
|
||||
private static ChronicCache<UUID, BasicEasingInterpolator> instanceCache = new ChronicCache<>();
|
||||
private static LoadingCache<UUID, BasicEasingInterpolator> instanceCache = CacheBuilder.newBuilder()
|
||||
.expireAfterAccess(30, TimeUnit.SECONDS)
|
||||
.build(CacheLoader.from(BasicEasingInterpolator::new));
|
||||
|
||||
/**
|
||||
* Gets or creates a new basic, linear interpolator for the provided id.
|
||||
*/
|
||||
public static IInterpolator getInstance(UUID id) {
|
||||
return instanceCache.retrieve(id, BasicEasingInterpolator::new);
|
||||
}
|
||||
|
||||
public BasicEasingInterpolator() {
|
||||
|
||||
}
|
||||
|
||||
private BasicEasingInterpolator(UUID id) {
|
||||
|
||||
return instanceCache.getUnchecked(id);
|
||||
}
|
||||
|
||||
private final Map<String, Float> properties = new HashMap<String, Float>();
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
package com.minelittlepony.util.chron;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Special version of a map that culls its own values.
|
||||
*/
|
||||
public class ChronicCache<K, V extends Touchable<V>> extends HashMap<K, V> {
|
||||
private static final long serialVersionUID = 6454924015818181978L;
|
||||
|
||||
public V retrieve(K key, Function<? super K, ? extends V> mappingFunction) {
|
||||
V result = computeIfAbsent(key, mappingFunction).touch();
|
||||
|
||||
entrySet().removeIf(entry -> entry.getValue().hasExpired());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
package com.minelittlepony.util.chron;
|
||||
|
||||
/**
|
||||
* DON'T TOUCH ME I'M SCAREED
|
||||
*
|
||||
* Basic touchable object that expires if not gently caressed for all of 30 seconds.
|
||||
*/
|
||||
public abstract class Touchable<T extends Touchable<T>> {
|
||||
|
||||
private long expirationPeriod;
|
||||
|
||||
/**
|
||||
* Returns whether this object is dead (expired).
|
||||
* Expired Touchables are flushed from the ChronicCache on next access.
|
||||
*/
|
||||
public boolean hasExpired() {
|
||||
return expirationPeriod <= System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
* Touches this object.
|
||||
* Internally just updates the expiration date.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public T touch() {
|
||||
expirationPeriod = System.currentTimeMillis() + 30000;
|
||||
return (T)this;
|
||||
}
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
@ParametersAreNonnullByDefault
|
||||
package com.minelittlepony.util.chron;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
Loading…
Add table
Reference in a new issue