more refactor
This commit is contained in:
parent
2a9322d0a2
commit
e29bb8fcb1
@ -3,7 +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.cache.VillagerManager;
|
||||
import me.xginko.villageroptimizer.models.WrappedVillager;
|
||||
import me.xginko.villageroptimizer.modules.VillagerOptimizerModule;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@ -33,10 +33,10 @@ import java.util.regex.Pattern;
|
||||
public final class VillagerOptimizer extends JavaPlugin {
|
||||
|
||||
private static VillagerOptimizer instance;
|
||||
private static Logger logger;
|
||||
private static Config config;
|
||||
private static HashMap<String, LanguageCache> languageCacheMap;
|
||||
private static VillagerCache villagerCache;
|
||||
private static VillagerManager villagerManager;
|
||||
private static Config config;
|
||||
private static Logger logger;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
@ -49,54 +49,20 @@ public final class VillagerOptimizer extends JavaPlugin {
|
||||
logger.info("Done.");
|
||||
}
|
||||
|
||||
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())) {
|
||||
return OptimizationType.NAMETAG;
|
||||
}
|
||||
}
|
||||
if (config.enable_block_optimization) {
|
||||
if (config.blocks_that_disable.contains(wrapped.villager().getLocation().getBlock().getRelative(BlockFace.DOWN).getType())) {
|
||||
return OptimizationType.BLOCK;
|
||||
}
|
||||
}
|
||||
if (config.enable_workstation_optimization) {
|
||||
final Location jobSite = wrapped.villager().getMemory(MemoryKey.JOB_SITE);
|
||||
if (
|
||||
jobSite != null
|
||||
&& config.workstations_that_disable.contains(jobSite.getBlock().getType())
|
||||
&& wrapped.villager().getLocation().distance(jobSite) <= config.workstation_max_distance
|
||||
) {
|
||||
return OptimizationType.WORKSTATION;
|
||||
}
|
||||
}
|
||||
return wrapped.getOptimizationType();
|
||||
public static VillagerOptimizer getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
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())) {
|
||||
return OptimizationType.NAMETAG;
|
||||
}
|
||||
}
|
||||
if (config.enable_block_optimization) {
|
||||
if (config.blocks_that_disable.contains(villager.getLocation().getBlock().getRelative(BlockFace.DOWN).getType())) {
|
||||
return OptimizationType.BLOCK;
|
||||
}
|
||||
}
|
||||
if (config.enable_workstation_optimization) {
|
||||
final Location jobSite = villager.getMemory(MemoryKey.JOB_SITE);
|
||||
if (
|
||||
jobSite != null
|
||||
&& config.workstations_that_disable.contains(jobSite.getBlock().getType())
|
||||
&& villager.getLocation().distance(jobSite) <= config.workstation_max_distance
|
||||
) {
|
||||
return OptimizationType.WORKSTATION;
|
||||
}
|
||||
}
|
||||
return villagerCache.getOrAdd(villager).getOptimizationType();
|
||||
public static VillagerManager getVillagerManager() {
|
||||
return villagerManager;
|
||||
}
|
||||
public static Config getConfiguration() {
|
||||
return config;
|
||||
}
|
||||
public static NamespacedKey getKey(String key) {
|
||||
return new NamespacedKey(instance, key);
|
||||
}
|
||||
public static Logger getLog() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
public void reloadPlugin() {
|
||||
@ -107,11 +73,11 @@ public final class VillagerOptimizer extends JavaPlugin {
|
||||
private void reloadConfiguration() {
|
||||
try {
|
||||
config = new Config();
|
||||
villagerCache = new VillagerCache(config.cache_keep_time_seconds);
|
||||
villagerManager = new VillagerManager(config.cache_keep_time_seconds);
|
||||
VillagerOptimizerModule.reloadModules();
|
||||
config.saveConfig();
|
||||
} catch (Exception e) {
|
||||
logger.severe("Failed to load config! - " + e.getLocalizedMessage());
|
||||
logger.severe("Error while loading config! - " + e.getLocalizedMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@ -181,23 +147,53 @@ public final class VillagerOptimizer extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
public static VillagerOptimizer getInstance() {
|
||||
return instance;
|
||||
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())) {
|
||||
return OptimizationType.NAMETAG;
|
||||
}
|
||||
}
|
||||
if (config.enable_block_optimization) {
|
||||
if (config.blocks_that_disable.contains(wrapped.villager().getLocation().getBlock().getRelative(BlockFace.DOWN).getType())) {
|
||||
return OptimizationType.BLOCK;
|
||||
}
|
||||
}
|
||||
if (config.enable_workstation_optimization) {
|
||||
final Location jobSite = wrapped.villager().getMemory(MemoryKey.JOB_SITE);
|
||||
if (
|
||||
jobSite != null
|
||||
&& config.workstations_that_disable.contains(jobSite.getBlock().getType())
|
||||
&& wrapped.villager().getLocation().distance(jobSite) <= config.workstation_max_distance
|
||||
) {
|
||||
return OptimizationType.WORKSTATION;
|
||||
}
|
||||
}
|
||||
return wrapped.getOptimizationType();
|
||||
}
|
||||
|
||||
public static NamespacedKey getKey(String key) {
|
||||
return new NamespacedKey(instance, key);
|
||||
}
|
||||
|
||||
public static Config getConfiguration() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public static Logger getLog() {
|
||||
return logger;
|
||||
}
|
||||
|
||||
public static VillagerCache getVillagerCache() {
|
||||
return villagerCache;
|
||||
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())) {
|
||||
return OptimizationType.NAMETAG;
|
||||
}
|
||||
}
|
||||
if (config.enable_block_optimization) {
|
||||
if (config.blocks_that_disable.contains(villager.getLocation().getBlock().getRelative(BlockFace.DOWN).getType())) {
|
||||
return OptimizationType.BLOCK;
|
||||
}
|
||||
}
|
||||
if (config.enable_workstation_optimization) {
|
||||
final Location jobSite = villager.getMemory(MemoryKey.JOB_SITE);
|
||||
if (
|
||||
jobSite != null
|
||||
&& config.workstations_that_disable.contains(jobSite.getBlock().getType())
|
||||
&& villager.getLocation().distance(jobSite) <= config.workstation_max_distance
|
||||
) {
|
||||
return OptimizationType.WORKSTATION;
|
||||
}
|
||||
}
|
||||
return villagerManager.getOrAdd(villager).getOptimizationType();
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,8 @@
|
||||
package me.xginko.villageroptimizer.models;
|
||||
package me.xginko.villageroptimizer.cache;
|
||||
|
||||
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;
|
||||
@ -11,11 +12,11 @@ import java.time.Duration;
|
||||
import java.util.Collection;
|
||||
import java.util.UUID;
|
||||
|
||||
public class VillagerCache {
|
||||
public class VillagerManager {
|
||||
|
||||
private final Cache<UUID, WrappedVillager> villagerCache;
|
||||
|
||||
public VillagerCache(long expireAfterWriteSeconds) {
|
||||
public VillagerManager(long expireAfterWriteSeconds) {
|
||||
this.villagerCache = Caffeine.newBuilder().expireAfterWrite(Duration.ofSeconds(expireAfterWriteSeconds)).build();
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ public final class WrappedVillager {
|
||||
}
|
||||
|
||||
public static @NotNull WrappedVillager fromCache(Villager villager) {
|
||||
return VillagerOptimizer.getVillagerCache().getOrAdd(villager);
|
||||
return VillagerOptimizer.getVillagerManager().getOrAdd(villager);
|
||||
}
|
||||
|
||||
public boolean isOptimized() {
|
||||
|
@ -3,7 +3,7 @@ package me.xginko.villageroptimizer.modules;
|
||||
import me.xginko.villageroptimizer.VillagerOptimizer;
|
||||
import me.xginko.villageroptimizer.config.Config;
|
||||
import me.xginko.villageroptimizer.enums.OptimizationType;
|
||||
import me.xginko.villageroptimizer.models.VillagerCache;
|
||||
import me.xginko.villageroptimizer.cache.VillagerManager;
|
||||
import me.xginko.villageroptimizer.models.WrappedVillager;
|
||||
import me.xginko.villageroptimizer.utils.CommonUtils;
|
||||
import net.kyori.adventure.text.TextReplacementConfig;
|
||||
@ -24,12 +24,12 @@ import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||
|
||||
public class BlockOptimization implements VillagerOptimizerModule, Listener {
|
||||
|
||||
private final VillagerCache cache;
|
||||
private final VillagerManager villagerManager;
|
||||
private final Config config;
|
||||
private final boolean shouldLog, shouldNotifyPlayer;
|
||||
|
||||
protected BlockOptimization() {
|
||||
this.cache = VillagerOptimizer.getVillagerCache();
|
||||
this.villagerManager = VillagerOptimizer.getVillagerManager();
|
||||
this.config = VillagerOptimizer.getConfiguration();
|
||||
this.config.addComment("optimization.methods.by-specific-block.enable", """
|
||||
When enabled, villagers standing on the configured specific blocks will become optimized once a\s
|
||||
@ -63,7 +63,7 @@ public class BlockOptimization implements VillagerOptimizerModule, Listener {
|
||||
|
||||
placed.getRelative(BlockFace.UP).getLocation().getNearbyEntities(0.5,0.5,0.5).forEach(entity -> {
|
||||
if (entity.getType().equals(EntityType.VILLAGER)) {
|
||||
WrappedVillager wVillager = cache.getOrAdd((Villager) entity);
|
||||
WrappedVillager wVillager = villagerManager.getOrAdd((Villager) entity);
|
||||
if (!wVillager.isOptimized()) {
|
||||
if (wVillager.setOptimization(OptimizationType.BLOCK)) {
|
||||
if (shouldNotifyPlayer) {
|
||||
@ -91,7 +91,7 @@ public class BlockOptimization implements VillagerOptimizerModule, Listener {
|
||||
|
||||
broken.getRelative(BlockFace.UP).getLocation().getNearbyEntities(0.5,0.5,0.5).forEach(entity -> {
|
||||
if (entity.getType().equals(EntityType.VILLAGER)) {
|
||||
WrappedVillager wVillager = cache.getOrAdd((Villager) entity);
|
||||
WrappedVillager wVillager = villagerManager.getOrAdd((Villager) entity);
|
||||
if (wVillager.getOptimizationType().equals(OptimizationType.BLOCK)) {
|
||||
wVillager.setOptimization(OptimizationType.OFF);
|
||||
if (shouldNotifyPlayer) {
|
||||
@ -110,7 +110,7 @@ public class BlockOptimization implements VillagerOptimizerModule, Listener {
|
||||
Entity interacted = event.getRightClicked();
|
||||
if (!interacted.getType().equals(EntityType.VILLAGER)) return;
|
||||
|
||||
WrappedVillager wVillager = cache.getOrAdd((Villager) interacted);
|
||||
WrappedVillager wVillager = villagerManager.getOrAdd((Villager) interacted);
|
||||
final Location entityLegs = interacted.getLocation();
|
||||
|
||||
if (
|
||||
|
@ -4,7 +4,7 @@ import io.papermc.paper.event.player.PlayerNameEntityEvent;
|
||||
import me.xginko.villageroptimizer.VillagerOptimizer;
|
||||
import me.xginko.villageroptimizer.config.Config;
|
||||
import me.xginko.villageroptimizer.enums.OptimizationType;
|
||||
import me.xginko.villageroptimizer.models.VillagerCache;
|
||||
import me.xginko.villageroptimizer.cache.VillagerManager;
|
||||
import me.xginko.villageroptimizer.models.WrappedVillager;
|
||||
import me.xginko.villageroptimizer.utils.CommonUtils;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@ -20,12 +20,12 @@ import org.bukkit.event.Listener;
|
||||
|
||||
public class NametagOptimization implements VillagerOptimizerModule, Listener {
|
||||
|
||||
private final VillagerCache cache;
|
||||
private final VillagerManager villagerManager;
|
||||
private final Config config;
|
||||
private final boolean shouldLog, shouldNotifyPlayer;
|
||||
|
||||
protected NametagOptimization() {
|
||||
this.cache = VillagerOptimizer.getVillagerCache();
|
||||
this.villagerManager = VillagerOptimizer.getVillagerManager();
|
||||
this.config = VillagerOptimizer.getConfiguration();
|
||||
this.config.addComment("optimization.methods.by-nametag.enable",
|
||||
"""
|
||||
@ -60,7 +60,7 @@ public class NametagOptimization implements VillagerOptimizerModule, Listener {
|
||||
if (name == null) return;
|
||||
|
||||
final String nameTag = PlainTextComponentSerializer.plainText().serialize(name);
|
||||
WrappedVillager wVillager = cache.getOrAdd((Villager) event.getEntity());
|
||||
WrappedVillager wVillager = villagerManager.getOrAdd((Villager) event.getEntity());
|
||||
|
||||
if (config.nametags.contains(nameTag.toLowerCase())) {
|
||||
if (!wVillager.isOptimized()) {
|
||||
|
@ -2,7 +2,7 @@ package me.xginko.villageroptimizer.modules;
|
||||
|
||||
import io.papermc.paper.event.entity.EntityPushedByEntityAttackEvent;
|
||||
import me.xginko.villageroptimizer.VillagerOptimizer;
|
||||
import me.xginko.villageroptimizer.models.VillagerCache;
|
||||
import me.xginko.villageroptimizer.cache.VillagerManager;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Villager;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@ -13,10 +13,10 @@ import org.bukkit.event.entity.EntityDamageEvent;
|
||||
|
||||
public class PreventVillagerDamage implements VillagerOptimizerModule, Listener {
|
||||
|
||||
private final VillagerCache cache;
|
||||
private final VillagerManager villagerManager;
|
||||
|
||||
protected PreventVillagerDamage() {
|
||||
this.cache = VillagerOptimizer.getVillagerCache();
|
||||
this.villagerManager = VillagerOptimizer.getVillagerManager();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -38,7 +38,7 @@ public class PreventVillagerDamage implements VillagerOptimizerModule, Listener
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
private void onDamageReceive(EntityDamageEvent event) {
|
||||
if (!event.getEntityType().equals(EntityType.VILLAGER)) return;
|
||||
if (cache.getOrAdd((Villager) event.getEntity()).isOptimized()) {
|
||||
if (villagerManager.getOrAdd((Villager) event.getEntity()).isOptimized()) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
@ -46,7 +46,7 @@ public class PreventVillagerDamage implements VillagerOptimizerModule, Listener
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
private void onPushByEntityAttack(EntityPushedByEntityAttackEvent event) {
|
||||
if (!event.getEntityType().equals(EntityType.VILLAGER)) return;
|
||||
if (cache.getOrAdd((Villager) event.getEntity()).isOptimized()) {
|
||||
if (villagerManager.getOrAdd((Villager) event.getEntity()).isOptimized()) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package me.xginko.villageroptimizer.modules;
|
||||
|
||||
import com.destroystokyo.paper.event.entity.EntityPathfindEvent;
|
||||
import me.xginko.villageroptimizer.VillagerOptimizer;
|
||||
import me.xginko.villageroptimizer.models.VillagerCache;
|
||||
import me.xginko.villageroptimizer.cache.VillagerManager;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Mob;
|
||||
import org.bukkit.entity.Villager;
|
||||
@ -15,10 +15,10 @@ import org.bukkit.event.entity.EntityTargetLivingEntityEvent;
|
||||
|
||||
public class PreventVillagerTargetting implements VillagerOptimizerModule, Listener {
|
||||
|
||||
private final VillagerCache cache;
|
||||
private final VillagerManager villagerManager;
|
||||
|
||||
protected PreventVillagerTargetting() {
|
||||
this.cache = VillagerOptimizer.getVillagerCache();
|
||||
this.villagerManager = VillagerOptimizer.getVillagerManager();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -39,14 +39,14 @@ public class PreventVillagerTargetting implements VillagerOptimizerModule, Liste
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
private void onTarget(EntityTargetLivingEntityEvent event) {
|
||||
if (event.getTarget() instanceof Villager villager && cache.getOrAdd(villager).isOptimized()) {
|
||||
if (event.getTarget() instanceof Villager villager && villagerManager.getOrAdd(villager).isOptimized()) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
private void onEntityTargetVillager(EntityPathfindEvent event) {
|
||||
if (event.getTargetEntity() instanceof Villager villager && cache.getOrAdd(villager).isOptimized()) {
|
||||
if (event.getTargetEntity() instanceof Villager villager && villagerManager.getOrAdd(villager).isOptimized()) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
@ -56,7 +56,7 @@ public class PreventVillagerTargetting implements VillagerOptimizerModule, Liste
|
||||
if (
|
||||
event.getEntityType().equals(EntityType.VILLAGER)
|
||||
&& event.getDamager() instanceof Mob attacker
|
||||
&& cache.getOrAdd((Villager) event.getEntity()).isOptimized()
|
||||
&& villagerManager.getOrAdd((Villager) event.getEntity()).isOptimized()
|
||||
) {
|
||||
attacker.setTarget(null);
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package me.xginko.villageroptimizer.modules;
|
||||
|
||||
import me.xginko.villageroptimizer.VillagerOptimizer;
|
||||
import me.xginko.villageroptimizer.config.Config;
|
||||
import me.xginko.villageroptimizer.models.VillagerCache;
|
||||
import me.xginko.villageroptimizer.cache.VillagerManager;
|
||||
import me.xginko.villageroptimizer.models.WrappedVillager;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Villager;
|
||||
@ -14,12 +14,12 @@ import org.bukkit.event.player.PlayerInteractEntityEvent;
|
||||
|
||||
public class RestockTrades implements VillagerOptimizerModule, Listener {
|
||||
|
||||
private final VillagerCache cache;
|
||||
private final VillagerManager villagerManager;
|
||||
private final long restock_delay;
|
||||
private final boolean shouldLog;
|
||||
|
||||
protected RestockTrades() {
|
||||
this.cache = VillagerOptimizer.getVillagerCache();
|
||||
this.villagerManager = VillagerOptimizer.getVillagerManager();
|
||||
Config config = VillagerOptimizer.getConfiguration();
|
||||
config.addComment("optimization.trade-restocking.enable", """
|
||||
This is for automatic restocking of trades for optimized villagers. Optimized Villagers\s
|
||||
@ -48,7 +48,7 @@ public class RestockTrades implements VillagerOptimizerModule, Listener {
|
||||
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
|
||||
private void onInteract(PlayerInteractEntityEvent event) {
|
||||
if (!event.getRightClicked().getType().equals(EntityType.VILLAGER)) return;
|
||||
WrappedVillager wVillager = cache.getOrAdd((Villager) event.getRightClicked());
|
||||
WrappedVillager wVillager = villagerManager.getOrAdd((Villager) event.getRightClicked());
|
||||
if (!wVillager.isOptimized()) return;
|
||||
|
||||
|
||||
|
@ -3,7 +3,7 @@ package me.xginko.villageroptimizer.modules;
|
||||
import io.papermc.paper.event.entity.EntityMoveEvent;
|
||||
import me.xginko.villageroptimizer.VillagerOptimizer;
|
||||
import me.xginko.villageroptimizer.config.Config;
|
||||
import me.xginko.villageroptimizer.models.VillagerCache;
|
||||
import me.xginko.villageroptimizer.cache.VillagerManager;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
@ -12,12 +12,12 @@ import org.bukkit.event.Listener;
|
||||
|
||||
public class WorkstationOptimization implements VillagerOptimizerModule, Listener {
|
||||
|
||||
private final VillagerCache cache;
|
||||
private final VillagerManager villagerManager;
|
||||
private final Config config;
|
||||
private final boolean shouldLog, shouldNotifyPlayer;
|
||||
|
||||
protected WorkstationOptimization() {
|
||||
this.cache = VillagerOptimizer.getVillagerCache();
|
||||
this.villagerManager = VillagerOptimizer.getVillagerManager();
|
||||
this.config = VillagerOptimizer.getConfiguration();
|
||||
this.config.addComment("optimization.methods.by-workstation.enable", """
|
||||
When enabled, villagers near a configured radius to a workstation specific to their profession\s
|
||||
|
Loading…
x
Reference in New Issue
Block a user