more structure improvements
This commit is contained in:
parent
4306f8ab4e
commit
a1b1a3991a
@ -3,6 +3,7 @@ package me.xginko.villageroptimizer;
|
||||
import me.xginko.villageroptimizer.config.Config;
|
||||
import me.xginko.villageroptimizer.config.LanguageCache;
|
||||
import me.xginko.villageroptimizer.enums.OptimizationType;
|
||||
import me.xginko.villageroptimizer.models.VillagerCache;
|
||||
import me.xginko.villageroptimizer.models.WrappedVillager;
|
||||
import me.xginko.villageroptimizer.modules.VillagerOptimizerModule;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@ -15,6 +16,7 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Villager;
|
||||
import org.bukkit.entity.memory.MemoryKey;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@ -38,23 +40,15 @@ public final class VillagerOptimizer extends JavaPlugin {
|
||||
public void onEnable() {
|
||||
instance = this;
|
||||
logger = getLogger();
|
||||
villagerCache = new VillagerCache();
|
||||
|
||||
villagerCache = new VillagerCache(30);
|
||||
logger.info("Loading Translations");
|
||||
reloadLang();
|
||||
|
||||
logger.info("Loading Config");
|
||||
reloadConfiguration();
|
||||
|
||||
logger.info("Done.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
// Plugin shutdown logic
|
||||
}
|
||||
|
||||
public static OptimizationType computeOptimization(WrappedVillager wrapped) {
|
||||
public static OptimizationType computeOptimization(@NotNull WrappedVillager wrapped) {
|
||||
if (config.enable_nametag_optimization) {
|
||||
Component name = wrapped.villager().customName();
|
||||
if (name != null && config.nametags.contains(PlainTextComponentSerializer.plainText().serialize(name).toLowerCase())) {
|
||||
@ -79,7 +73,7 @@ public final class VillagerOptimizer extends JavaPlugin {
|
||||
return wrapped.getOptimizationType();
|
||||
}
|
||||
|
||||
public static OptimizationType computeOptimization(Villager villager) {
|
||||
public static OptimizationType computeOptimization(@NotNull Villager villager) {
|
||||
if (config.enable_nametag_optimization) {
|
||||
Component name = villager.customName();
|
||||
if (name != null && config.nametags.contains(PlainTextComponentSerializer.plainText().serialize(name).toLowerCase())) {
|
||||
@ -105,7 +99,7 @@ public final class VillagerOptimizer extends JavaPlugin {
|
||||
}
|
||||
|
||||
public void reloadPlugin() {
|
||||
villagerCache = new VillagerCache();
|
||||
villagerCache = new VillagerCache(30);
|
||||
reloadLang();
|
||||
reloadConfiguration();
|
||||
}
|
||||
|
@ -3,16 +3,16 @@ package me.xginko.villageroptimizer.enums;
|
||||
import me.xginko.villageroptimizer.VillagerOptimizer;
|
||||
import org.bukkit.NamespacedKey;
|
||||
|
||||
public enum NamespacedKeys {
|
||||
public enum Keys {
|
||||
|
||||
OPTIMIZED(VillagerOptimizer.getKey("optimized")),
|
||||
COOLDOWN_RESTOCK(VillagerOptimizer.getKey("restock-cooldown")),
|
||||
COOLDOWN_EXPERIENCE(VillagerOptimizer.getKey("experience-cooldown")),
|
||||
GAME_TIME(VillagerOptimizer.getKey("game-time"));
|
||||
WORLDTIME(VillagerOptimizer.getKey("world-time"));
|
||||
|
||||
private final NamespacedKey key;
|
||||
|
||||
NamespacedKeys(NamespacedKey key) {
|
||||
Keys(NamespacedKey key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
@ -1,8 +1,7 @@
|
||||
package me.xginko.villageroptimizer;
|
||||
package me.xginko.villageroptimizer.models;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import me.xginko.villageroptimizer.models.WrappedVillager;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Villager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -16,8 +15,8 @@ public class VillagerCache {
|
||||
|
||||
private final Cache<UUID, WrappedVillager> villagerCache;
|
||||
|
||||
protected VillagerCache() {
|
||||
this.villagerCache = Caffeine.newBuilder().expireAfterWrite(Duration.ofSeconds(30)).build();
|
||||
public VillagerCache(long expireAfterWriteSeconds) {
|
||||
this.villagerCache = Caffeine.newBuilder().expireAfterWrite(Duration.ofSeconds(expireAfterWriteSeconds)).build();
|
||||
}
|
||||
|
||||
public @NotNull Collection<WrappedVillager> getAll() {
|
||||
@ -43,6 +42,10 @@ public class VillagerCache {
|
||||
return add(new WrappedVillager(villager));
|
||||
}
|
||||
|
||||
public boolean contains(@NotNull UUID uuid) {
|
||||
return villagerCache.getIfPresent(uuid) != null;
|
||||
}
|
||||
|
||||
public boolean contains(@NotNull WrappedVillager villager) {
|
||||
return villagerCache.getIfPresent(villager.villager().getUniqueId()) != null;
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
package me.xginko.villageroptimizer.models;
|
||||
|
||||
import me.xginko.villageroptimizer.VillagerOptimizer;
|
||||
import me.xginko.villageroptimizer.enums.NamespacedKeys;
|
||||
import me.xginko.villageroptimizer.enums.Keys;
|
||||
import me.xginko.villageroptimizer.enums.OptimizationType;
|
||||
import org.bukkit.entity.Villager;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
@ -14,28 +14,28 @@ public record WrappedVillager(Villager villager) {
|
||||
}
|
||||
|
||||
public boolean isOptimized() {
|
||||
return villager.getPersistentDataContainer().has(NamespacedKeys.OPTIMIZED.key());
|
||||
return villager.getPersistentDataContainer().has(Keys.OPTIMIZED.key());
|
||||
}
|
||||
|
||||
public void setOptimization(OptimizationType type) {
|
||||
if (type.equals(OptimizationType.OFF) && isOptimized()) {
|
||||
villager.getPersistentDataContainer().remove(NamespacedKeys.OPTIMIZED.key());
|
||||
villager.getPersistentDataContainer().remove(Keys.OPTIMIZED.key());
|
||||
} else {
|
||||
villager.getPersistentDataContainer().set(NamespacedKeys.OPTIMIZED.key(), PersistentDataType.STRING, type.name());
|
||||
villager.getPersistentDataContainer().set(Keys.OPTIMIZED.key(), PersistentDataType.STRING, type.name());
|
||||
}
|
||||
}
|
||||
|
||||
public OptimizationType getOptimizationType() {
|
||||
return isOptimized() ? OptimizationType.valueOf(villager().getPersistentDataContainer().get(NamespacedKeys.OPTIMIZED.key(), PersistentDataType.STRING)) : OptimizationType.OFF;
|
||||
return isOptimized() ? OptimizationType.valueOf(villager().getPersistentDataContainer().get(Keys.OPTIMIZED.key(), PersistentDataType.STRING)) : OptimizationType.OFF;
|
||||
}
|
||||
|
||||
public void setRestockCooldown(long milliseconds) {
|
||||
villager.getPersistentDataContainer().set(NamespacedKeys.COOLDOWN_RESTOCK.key(), PersistentDataType.LONG, System.currentTimeMillis() + milliseconds);
|
||||
villager.getPersistentDataContainer().set(Keys.COOLDOWN_RESTOCK.key(), PersistentDataType.LONG, System.currentTimeMillis() + milliseconds);
|
||||
}
|
||||
|
||||
public boolean shouldRestock() {
|
||||
PersistentDataContainer villagerData = villager.getPersistentDataContainer();
|
||||
return villagerData.has(NamespacedKeys.COOLDOWN_RESTOCK.key(), PersistentDataType.LONG) && villagerData.get(NamespacedKeys.COOLDOWN_RESTOCK.key(), PersistentDataType.LONG) <= System.currentTimeMillis();
|
||||
return villagerData.has(Keys.COOLDOWN_RESTOCK.key(), PersistentDataType.LONG) && villagerData.get(Keys.COOLDOWN_RESTOCK.key(), PersistentDataType.LONG) <= System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public void restock() {
|
||||
@ -43,20 +43,20 @@ public record WrappedVillager(Villager villager) {
|
||||
}
|
||||
|
||||
public void setExpCooldown(long milliseconds) {
|
||||
villager.getPersistentDataContainer().set(NamespacedKeys.COOLDOWN_EXPERIENCE.key(), PersistentDataType.LONG, System.currentTimeMillis() + milliseconds);
|
||||
villager.getPersistentDataContainer().set(Keys.COOLDOWN_EXPERIENCE.key(), PersistentDataType.LONG, System.currentTimeMillis() + milliseconds);
|
||||
}
|
||||
|
||||
public boolean isOnExpCooldown() {
|
||||
PersistentDataContainer villagerData = villager.getPersistentDataContainer();
|
||||
return villagerData.has(NamespacedKeys.COOLDOWN_EXPERIENCE.key(), PersistentDataType.LONG) && villagerData.get(NamespacedKeys.COOLDOWN_EXPERIENCE.key(), PersistentDataType.LONG) <= System.currentTimeMillis();
|
||||
return villagerData.has(Keys.COOLDOWN_EXPERIENCE.key(), PersistentDataType.LONG) && villagerData.get(Keys.COOLDOWN_EXPERIENCE.key(), PersistentDataType.LONG) <= System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public void saveWorldTime() {
|
||||
villager.getPersistentDataContainer().set(NamespacedKeys.GAME_TIME.key(), PersistentDataType.LONG, villager.getWorld().getFullTime());
|
||||
villager.getPersistentDataContainer().set(Keys.WORLDTIME.key(), PersistentDataType.LONG, villager.getWorld().getFullTime());
|
||||
}
|
||||
|
||||
public long getSavedWorldTime() {
|
||||
PersistentDataContainer villagerData = villager.getPersistentDataContainer();
|
||||
return villagerData.has(NamespacedKeys.GAME_TIME.key(), PersistentDataType.LONG) ? villagerData.get(NamespacedKeys.GAME_TIME.key(), PersistentDataType.LONG) : villager.getWorld().getFullTime();
|
||||
return villagerData.has(Keys.WORLDTIME.key(), PersistentDataType.LONG) ? villagerData.get(Keys.WORLDTIME.key(), PersistentDataType.LONG) : villager.getWorld().getFullTime();
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package me.xginko.villageroptimizer.modules;
|
||||
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
|
||||
import me.xginko.villageroptimizer.VillagerOptimizer;
|
||||
import me.xginko.villageroptimizer.config.Config;
|
||||
import me.xginko.villageroptimizer.models.WrappedVillager;
|
||||
import me.xginko.villageroptimizer.utils.LogUtils;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.World;
|
||||
@ -114,6 +115,6 @@ public class ChunkLimit implements VillagerOptimizerModule, Listener {
|
||||
|
||||
private int getProfessionPriority(Villager villager) {
|
||||
Villager.Profession profession = villager.getProfession();
|
||||
return removalPriority.contains(profession) ? removalPriority.indexOf(profession) : Integer.MAX_VALUE;
|
||||
return removalPriority.contains(profession) && !WrappedVillager.fromVillager(villager).isOptimized() ? removalPriority.indexOf(profession) : Integer.MAX_VALUE;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user