remove from cache on death

This commit is contained in:
xGinko 2024-07-24 16:51:26 +02:00
parent 159c03f3cd
commit b0d1c42955
2 changed files with 20 additions and 1 deletions

View File

@ -192,6 +192,7 @@ public final class VillagerOptimizer extends JavaPlugin {
config = new Config();
if (wrapperCache != null) wrapperCache.disable();
wrapperCache = new WrapperCache(config.cache_keep_time);
wrapperCache.enable();
VillagerOptimizerCommand.reloadCommands();
VillagerOptimizerModule.reloadModules();
config.saveConfig();

View File

@ -3,14 +3,20 @@ package me.xginko.villageroptimizer;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import me.xginko.villageroptimizer.utils.Disableable;
import me.xginko.villageroptimizer.utils.Enableable;
import me.xginko.villageroptimizer.wrapper.WrappedVillager;
import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
import org.jetbrains.annotations.NotNull;
import java.time.Duration;
import java.util.UUID;
public final class WrapperCache implements Disableable {
public final class WrapperCache implements Enableable, Disableable, Listener {
private final @NotNull Cache<UUID, WrappedVillager> wrapperCache;
@ -18,12 +24,24 @@ public final class WrapperCache implements Disableable {
this.wrapperCache = Caffeine.newBuilder().expireAfterWrite(cacheDuration).build();
}
@Override
public void enable() {
VillagerOptimizer plugin = VillagerOptimizer.getInstance();
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
@Override
public void disable() {
HandlerList.unregisterAll(this);
this.wrapperCache.invalidateAll();
this.wrapperCache.cleanUp();
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onEntityDeath(EntityDeathEvent event) {
this.wrapperCache.invalidate(event.getEntity().getUniqueId());
}
@SuppressWarnings("DataFlowIssue")
public @NotNull WrappedVillager get(@NotNull Villager villager) {
return this.wrapperCache.get(villager.getUniqueId(), k -> new WrappedVillager(villager));