more cleanup
This commit is contained in:
parent
5dca185df2
commit
545e9d0d20
@ -1,40 +0,0 @@
|
||||
package me.xginko.villageroptimizer;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import me.xginko.villageroptimizer.models.WrappedVillager;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Villager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class VillagerCache {
|
||||
private final Cache<UUID, WrappedVillager> villagerCache = Caffeine.newBuilder().expireAfterWrite(Duration.ofSeconds(30)).build();
|
||||
|
||||
public VillagerCache(JavaPlugin plugin) {
|
||||
plugin.getServer().getGlobalRegionScheduler().run(plugin, populateCache -> {
|
||||
for (World world : plugin.getServer().getWorlds()) {
|
||||
for (Entity entity : world.getEntities()) {
|
||||
if (entity instanceof Villager villager) {
|
||||
this.villagerCache.put(villager.getUniqueId(), new WrappedVillager(villager));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public WrappedVillager getVillager(Villager villager) {
|
||||
WrappedVillager wrappedVillager = villagerCache.getIfPresent(villager.getUniqueId());
|
||||
if (wrappedVillager == null) wrappedVillager = new WrappedVillager(villager);
|
||||
this.villagerCache.put(villager.getUniqueId(), wrappedVillager);
|
||||
return wrappedVillager;
|
||||
}
|
||||
|
||||
public Map<UUID, WrappedVillager> get() {
|
||||
return this.villagerCache.asMap();
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package me.xginko.villageroptimizer;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import me.xginko.villageroptimizer.config.Config;
|
||||
import me.xginko.villageroptimizer.enums.OptimizationType;
|
||||
import me.xginko.villageroptimizer.models.WrappedVillager;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.entity.Villager;
|
||||
import org.bukkit.entity.memory.MemoryKey;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Collection;
|
||||
import java.util.UUID;
|
||||
|
||||
public class VillagerManager {
|
||||
|
||||
private final Cache<UUID, WrappedVillager> villagerCache = Caffeine.newBuilder().expireAfterWrite(Duration.ofSeconds(30)).build();
|
||||
private final Config config;
|
||||
|
||||
protected VillagerManager(JavaPlugin plugin) {
|
||||
this.config = VillagerOptimizer.getConfiguration();
|
||||
plugin.getServer().getGlobalRegionScheduler().run(plugin, reCache -> {
|
||||
for (World world : plugin.getServer().getWorlds()) {
|
||||
for (Villager villager : world.getEntitiesByClass(Villager.class)) {
|
||||
this.villagerCache.put(villager.getUniqueId(), new WrappedVillager(villager));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public Collection<WrappedVillager> getCachedVillagers() {
|
||||
return this.villagerCache.asMap().values();
|
||||
}
|
||||
|
||||
public WrappedVillager wrap(Villager villager) {
|
||||
WrappedVillager wrappedVillager = villagerCache.getIfPresent(villager.getUniqueId());
|
||||
if (wrappedVillager == null) wrappedVillager = new WrappedVillager(villager);
|
||||
this.villagerCache.put(villager.getUniqueId(), wrappedVillager);
|
||||
return wrappedVillager;
|
||||
}
|
||||
|
||||
public OptimizationType computeOptimization(Villager villager) {
|
||||
Component nameTag = villager.customName();
|
||||
if (
|
||||
nameTag != null
|
||||
&& config.names_that_disable.contains(PlainTextComponentSerializer.plainText().serialize(nameTag).toLowerCase())
|
||||
) {
|
||||
// Optimized by nametag
|
||||
|
||||
}
|
||||
|
||||
if (config.blocks_that_disable.contains(villager.getLocation().getBlock().getRelative(BlockFace.DOWN).getType())) {
|
||||
// Optimized by Block
|
||||
|
||||
}
|
||||
|
||||
final Location jobSite = villager.getMemory(MemoryKey.JOB_SITE);
|
||||
if (
|
||||
jobSite != null
|
||||
&& config.workstations_that_disable.contains(jobSite.getBlock().getType())
|
||||
) {
|
||||
// Optimized by Workstation
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -24,6 +24,7 @@ public final class VillagerOptimizer extends JavaPlugin {
|
||||
private static Logger logger;
|
||||
private static Config config;
|
||||
private static HashMap<String, LanguageCache> languageCacheMap;
|
||||
private static VillagerManager villagerManager;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
@ -68,7 +69,7 @@ public final class VillagerOptimizer extends JavaPlugin {
|
||||
public void reloadPlugin() {
|
||||
reloadLang();
|
||||
reloadConfiguration();
|
||||
|
||||
villagerManager = new VillagerManager(this);
|
||||
}
|
||||
|
||||
private void reloadConfiguration() {
|
||||
@ -143,4 +144,7 @@ public final class VillagerOptimizer extends JavaPlugin {
|
||||
public static Logger getLog() {
|
||||
return logger;
|
||||
}
|
||||
public static VillagerManager getVillagerManager() {
|
||||
return villagerManager;
|
||||
}
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ public class Config {
|
||||
|
||||
|
||||
// AI-Disabling
|
||||
this.names_that_disable.addAll(getList("ai-disabling.names-that-disable", List.of("Optimize", "DisableAI")));
|
||||
this.names_that_disable.addAll(getList("ai-disabling.names-that-disable", List.of("Optimize", "DisableAI")).stream().map(String::toLowerCase).toList());
|
||||
getList("ai-disabling.blocks-that-disable", List.of("EMERALD_BLOCK", "COBBLESTONE")).forEach(configuredMaterial -> {
|
||||
try {
|
||||
Material disableBlock = Material.valueOf(configuredMaterial);
|
||||
|
@ -8,16 +8,6 @@ import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
public record WrappedVillager(Villager villager) {
|
||||
|
||||
public int level() {
|
||||
// Villager Level depending on their XP (source: https://minecraft.fandom.com/wiki/Trading#Mechanics)
|
||||
final int experience = villager.getVillagerExperience();
|
||||
if (experience >= 250) return 5;
|
||||
if (experience >= 150) return 4;
|
||||
if (experience >= 70) return 3;
|
||||
if (experience >= 10) return 2;
|
||||
return 1;
|
||||
}
|
||||
|
||||
public boolean isOptimized() {
|
||||
return villager.getPersistentDataContainer().has(NamespacedKeys.OPTIMIZED.key());
|
||||
}
|
||||
|
@ -1,29 +0,0 @@
|
||||
package me.xginko.villageroptimizer.utils;
|
||||
|
||||
import me.xginko.villageroptimizer.VillagerOptimizer;
|
||||
import me.xginko.villageroptimizer.models.WrappedVillager;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.entity.Villager;
|
||||
|
||||
public class VillagerUtils {
|
||||
|
||||
public static boolean shouldDisable(Villager villager) {
|
||||
// Check nametag
|
||||
Component nameTag = villager.customName();
|
||||
if (nameTag != null) {
|
||||
if (VillagerOptimizer.getConfiguration().names_that_disable.contains(PlainTextComponentSerializer.plainText().serialize(nameTag))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Check block below
|
||||
if (VillagerOptimizer.getConfiguration().blocks_that_disable.contains(villager.getLocation().getBlock().getRelative(BlockFace.DOWN).getType())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check Workstation
|
||||
return new WrappedVillager(villager).isOptimized();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user