add first features and fix some stuff

This commit is contained in:
xGinko 2023-09-05 20:44:39 +02:00
parent a1b1a3991a
commit 80ef3e7af7
8 changed files with 136 additions and 22 deletions

View File

@ -40,7 +40,6 @@ public final class VillagerOptimizer extends JavaPlugin {
public void onEnable() { public void onEnable() {
instance = this; instance = this;
logger = getLogger(); logger = getLogger();
villagerCache = new VillagerCache(30);
logger.info("Loading Translations"); logger.info("Loading Translations");
reloadLang(); reloadLang();
logger.info("Loading Config"); logger.info("Loading Config");
@ -99,7 +98,6 @@ public final class VillagerOptimizer extends JavaPlugin {
} }
public void reloadPlugin() { public void reloadPlugin() {
villagerCache = new VillagerCache(30);
reloadLang(); reloadLang();
reloadConfiguration(); reloadConfiguration();
} }
@ -107,10 +105,11 @@ public final class VillagerOptimizer extends JavaPlugin {
private void reloadConfiguration() { private void reloadConfiguration() {
try { try {
config = new Config(); config = new Config();
villagerCache = new VillagerCache(config.cache_keep_time_seconds);
VillagerOptimizerModule.reloadModules(); VillagerOptimizerModule.reloadModules();
config.saveConfig(); config.saveConfig();
} catch (Exception e) { } catch (Exception e) {
logger.severe("Failed to load config file! - " + e.getLocalizedMessage()); logger.severe("Failed to load config! - " + e.getLocalizedMessage());
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -147,13 +146,12 @@ public final class VillagerOptimizer extends JavaPlugin {
private Set<String> getDefaultLanguageFiles() { private Set<String> getDefaultLanguageFiles() {
Set<String> languageFiles = new HashSet<>(); Set<String> languageFiles = new HashSet<>();
try (JarFile jarFile = new JarFile(this.getFile())) { try (JarFile jarFile = new JarFile(this.getFile())) {
Enumeration<JarEntry> entries = jarFile.entries(); Iterator<JarEntry> defFileIterator = jarFile.entries().asIterator();
while (entries.hasMoreElements()) { while (defFileIterator.hasNext()) {
String path = entries.nextElement().getName(); final String path = defFileIterator.next().getName();
if (path.startsWith("lang/") && path.endsWith(".yml")) { if (path.startsWith("lang/") && path.endsWith(".yml"))
languageFiles.add(path); languageFiles.add(path);
} }
}
} catch (IOException e) { } catch (IOException e) {
logger.severe("Error while getting default language file names! - " + e.getLocalizedMessage()); logger.severe("Error while getting default language file names! - " + e.getLocalizedMessage());
e.printStackTrace(); e.printStackTrace();
@ -197,6 +195,7 @@ public final class VillagerOptimizer extends JavaPlugin {
public static Logger getLog() { public static Logger getLog() {
return logger; return logger;
} }
public static VillagerCache getVillagerCache() { public static VillagerCache getVillagerCache() {
return villagerCache; return villagerCache;
} }

View File

@ -17,11 +17,13 @@ public class Config {
private final ConfigFile config; private final ConfigFile config;
public final Locale default_lang; public final Locale default_lang;
public final boolean auto_lang, enable_nametag_optimization, enable_workstation_optimization, enable_block_optimization; public final boolean auto_lang, enable_nametag_optimization, enable_workstation_optimization, enable_block_optimization,
cache_enabled;
public final double workstation_max_distance; public final double workstation_max_distance;
public final long cache_keep_time_seconds;
public final HashSet<String> nametags = new HashSet<>(2); public final HashSet<String> nametags = new HashSet<>(2);
public final HashSet<Material> blocks_that_disable = new HashSet<>(2); public final HashSet<Material> blocks_that_disable = new HashSet<>(3);
public final HashSet<Material> workstations_that_disable = new HashSet<>(13); public final HashSet<Material> workstations_that_disable = new HashSet<>(13);
public Config() throws Exception { public Config() throws Exception {
@ -35,23 +37,28 @@ public class Config {
"The default language that will be used if auto-language is false or no matching language file was found.") "The default language that will be used if auto-language is false or no matching language file was found.")
.replace("_", "-")); .replace("_", "-"));
this.auto_lang = getBoolean("language.auto-language", true, "If set to true, will display messages based on client language"); this.auto_lang = getBoolean("language.auto-language", true, "If set to true, will display messages based on client language");
/**
* General
*/
this.cache_enabled = getBoolean("general.plugin-cache.enable", true, "Leave enabled if you don't know what you're doing and or don't experience any issues.");
this.cache_keep_time_seconds = getInt("general.plugin-cache.expire-after-x-seconds", 30, "The amount of time in seconds a villager will be kept in cache.");
/** /**
* Optimization * Optimization
*/ */
// Nametags // Nametags
this.enable_nametag_optimization = getBoolean("optimization.by-nametag.enable", true); this.enable_nametag_optimization = getBoolean("optimization.methods.by-nametag.enable", true);
this.nametags.addAll(getList("optimization.by-nametag.names", List.of("Optimize", "DisableAI"), "Names are case insensitive") this.nametags.addAll(getList("optimization.methods.by-nametag.names", List.of("Optimize", "DisableAI"), "Names are case insensitive")
.stream().map(String::toLowerCase).toList()); .stream().map(String::toLowerCase).toList());
// Workstations // Workstations
this.enable_workstation_optimization = getBoolean("optimization.by-workstation.enable", true, this.enable_workstation_optimization = getBoolean("optimization.methods.by-workstation.enable", true,
""" """
Optimize villagers that are standing near their acquired workstations /s Optimize villagers that are standing near their acquired workstations /s
Values here need to be valid bukkit Material enums for your server version. Values here need to be valid bukkit Material enums for your server version.
""" """
); );
this.workstation_max_distance = getDouble("optimization.by-workstation.", 4.0, this.workstation_max_distance = getDouble("optimization.methods.by-workstation.", 4.0,
"How close in blocks a villager needs to be to get optimized by its workstation"); "How close in blocks a villager needs to be to get optimized by its workstation");
this.getList("optimization.by-workstation.workstation-materials", List.of( this.getList("optimization.methods.by-workstation.workstation-materials", List.of(
"COMPOSTER", "SMOKER", "BARREL", "LOOM", "BLAST_FURNACE", "BREWING_STAND", "CAULDRON", "COMPOSTER", "SMOKER", "BARREL", "LOOM", "BLAST_FURNACE", "BREWING_STAND", "CAULDRON",
"FLETCHING_TABLE", "CARTOGRAPHY_TABLE", "LECTERN", "SMITHING_TABLE", "STONECUTTER", "GRINDSTONE" "FLETCHING_TABLE", "CARTOGRAPHY_TABLE", "LECTERN", "SMITHING_TABLE", "STONECUTTER", "GRINDSTONE"
)).forEach(configuredMaterial -> { )).forEach(configuredMaterial -> {
@ -59,24 +66,24 @@ public class Config {
Material disableBlock = Material.valueOf(configuredMaterial); Material disableBlock = Material.valueOf(configuredMaterial);
this.blocks_that_disable.add(disableBlock); this.blocks_that_disable.add(disableBlock);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
LogUtils.materialNotRecognized("optimization.by-workstation", configuredMaterial); LogUtils.materialNotRecognized("optimization.methods.by-workstation", configuredMaterial);
} }
}); });
// Blocks // Blocks
this.enable_block_optimization = getBoolean("optimization.by-specific-block.enable", true, this.enable_block_optimization = getBoolean("optimization.methods.by-specific-block.enable", true,
""" """
Optimize villagers that are standing on these specific block materials /s Optimize villagers that are standing on these specific block materials /s
Values here need to be valid bukkit Material enums for your server version. Values here need to be valid bukkit Material enums for your server version.
""" """
); );
this.getList("optimization.by-specific-block.materials", List.of( this.getList("optimization.methods.by-specific-block.materials", List.of(
"LAPIS_BLOCK", "GLOWSTONE", "IRON_BLOCK" "LAPIS_BLOCK", "GLOWSTONE", "IRON_BLOCK"
)).forEach(configuredMaterial -> { )).forEach(configuredMaterial -> {
try { try {
Material disableBlock = Material.valueOf(configuredMaterial); Material disableBlock = Material.valueOf(configuredMaterial);
this.blocks_that_disable.add(disableBlock); this.blocks_that_disable.add(disableBlock);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
LogUtils.materialNotRecognized("optimization.by-specific-block", configuredMaterial); LogUtils.materialNotRecognized("optimization.methods.by-specific-block", configuredMaterial);
} }
}); });
} }

View File

@ -6,6 +6,7 @@ import org.bukkit.NamespacedKey;
public enum Keys { public enum Keys {
OPTIMIZED(VillagerOptimizer.getKey("optimized")), OPTIMIZED(VillagerOptimizer.getKey("optimized")),
COOLDOWN_OPTIMIZE(VillagerOptimizer.getKey("optimization-toggle-cooldown")),
COOLDOWN_RESTOCK(VillagerOptimizer.getKey("restock-cooldown")), COOLDOWN_RESTOCK(VillagerOptimizer.getKey("restock-cooldown")),
COOLDOWN_EXPERIENCE(VillagerOptimizer.getKey("experience-cooldown")), COOLDOWN_EXPERIENCE(VillagerOptimizer.getKey("experience-cooldown")),
WORLDTIME(VillagerOptimizer.getKey("world-time")); WORLDTIME(VillagerOptimizer.getKey("world-time"));

View File

@ -20,8 +20,11 @@ public record WrappedVillager(Villager villager) {
public void setOptimization(OptimizationType type) { public void setOptimization(OptimizationType type) {
if (type.equals(OptimizationType.OFF) && isOptimized()) { if (type.equals(OptimizationType.OFF) && isOptimized()) {
villager.getPersistentDataContainer().remove(Keys.OPTIMIZED.key()); villager.getPersistentDataContainer().remove(Keys.OPTIMIZED.key());
villager.setAware(true);
villager.setAI(true);
} else { } else {
villager.getPersistentDataContainer().set(Keys.OPTIMIZED.key(), PersistentDataType.STRING, type.name()); villager.getPersistentDataContainer().set(Keys.OPTIMIZED.key(), PersistentDataType.STRING, type.name());
villager.setAware(false);
} }
} }

View File

@ -0,0 +1,53 @@
package me.xginko.villageroptimizer.modules;
import io.papermc.paper.event.entity.EntityPushedByEntityAttackEvent;
import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.models.VillagerCache;
import org.bukkit.entity.EntityType;
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.EntityDamageEvent;
public class AntiVillagerDamage implements VillagerOptimizerModule, Listener {
private final VillagerCache cache;
protected AntiVillagerDamage() {
this.cache = VillagerOptimizer.getVillagerCache();
}
@Override
public void enable() {
VillagerOptimizer plugin = VillagerOptimizer.getInstance();
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
@Override
public void disable() {
HandlerList.unregisterAll(this);
}
@Override
public boolean shouldEnable() {
return VillagerOptimizer.getConfiguration().getBoolean("optimization.behavior.optimized-villagers-dont-take-damage", true);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onDamage(EntityDamageEvent event) {
if (!event.getEntity().getType().equals(EntityType.VILLAGER)) return;
if (cache.get((Villager) event.getEntity()).isOptimized()) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPathfind(EntityPushedByEntityAttackEvent event) {
if (!event.getEntity().getType().equals(EntityType.VILLAGER)) return;
if (cache.get((Villager) event.getEntity()).isOptimized()) {
event.setCancelled(true);
}
}
}

View File

@ -0,0 +1,50 @@
package me.xginko.villageroptimizer.modules;
import com.destroystokyo.paper.event.entity.EntityPathfindEvent;
import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.models.VillagerCache;
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.EntityTargetLivingEntityEvent;
public class AntiVillagerTargetting implements VillagerOptimizerModule, Listener {
private final VillagerCache cache;
protected AntiVillagerTargetting() {
this.cache = VillagerOptimizer.getVillagerCache();
}
@Override
public void enable() {
VillagerOptimizer plugin = VillagerOptimizer.getInstance();
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
@Override
public void disable() {
HandlerList.unregisterAll(this);
}
@Override
public boolean shouldEnable() {
return VillagerOptimizer.getConfiguration().getBoolean("optimization.behavior.optimized-villagers-dont-get-targeted", true);
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onTarget(EntityTargetLivingEntityEvent event) {
if (event.getTarget() instanceof Villager villager && cache.get(villager).isOptimized()) {
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onTarget(EntityPathfindEvent event) {
if (event.getTargetEntity() instanceof Villager villager && cache.get(villager).isOptimized()) {
event.setCancelled(true);
}
}
}

View File

@ -30,7 +30,7 @@ public class ChunkLimit implements VillagerOptimizerModule, Listener {
private final boolean logIsEnabled; private final boolean logIsEnabled;
private final long checkPeriod; private final long checkPeriod;
public ChunkLimit() { protected ChunkLimit() {
shouldEnable(); shouldEnable();
this.plugin = VillagerOptimizer.getInstance(); this.plugin = VillagerOptimizer.getInstance();
Config config = VillagerOptimizer.getConfiguration(); Config config = VillagerOptimizer.getConfiguration();

View File

@ -14,8 +14,9 @@ public interface VillagerOptimizerModule {
modules.forEach(VillagerOptimizerModule::disable); modules.forEach(VillagerOptimizerModule::disable);
modules.clear(); modules.clear();
// Modules here modules.add(new AntiVillagerDamage());
modules.add(new AntiVillagerTargetting());
modules.add(new ChunkLimit());
for (VillagerOptimizerModule module : modules) { for (VillagerOptimizerModule module : modules) {
if (module.shouldEnable()) module.enable(); if (module.shouldEnable()) module.enable();