From 5068e59175f1e03fc9cb13fe5e2fab6cc2054488 Mon Sep 17 00:00:00 2001 From: xGinko Date: Sat, 23 Mar 2024 00:40:29 +0100 Subject: [PATCH] improve module logging --- .../villageroptimizer/VillagerOptimizer.java | 11 ++- .../villageroptimizer/config/Config.java | 8 +- .../config/LanguageCache.java | 4 +- .../modules/VillagerChunkLimit.java | 89 ++++++++++--------- .../modules/VillagerOptimizerModule.java | 24 +++++ .../gameplay/EnableLeashingVillagers.java | 17 ++-- .../gameplay/FixOptimisationAfterCure.java | 5 ++ .../gameplay/LevelOptimizedProfession.java | 11 ++- .../gameplay/MakeVillagersSpawnAdult.java | 7 +- .../gameplay/PreventOptimizedDamage.java | 37 ++++---- .../gameplay/PreventOptimizedTargeting.java | 7 +- .../gameplay/PreventUnoptimizedTrading.java | 11 ++- .../gameplay/RestockOptimizedTrades.java | 17 ++-- .../gameplay/UnoptimizeOnJobLoose.java | 7 +- .../gameplay/VisuallyHighlightOptimized.java | 5 ++ .../modules/optimization/OptimizeByBlock.java | 53 ++++++----- .../optimization/OptimizeByNametag.java | 29 +++--- .../optimization/OptimizeByWorkstation.java | 32 +++---- 18 files changed, 235 insertions(+), 139 deletions(-) diff --git a/src/main/java/me/xginko/villageroptimizer/VillagerOptimizer.java b/src/main/java/me/xginko/villageroptimizer/VillagerOptimizer.java index 2495b61..7d69c89 100644 --- a/src/main/java/me/xginko/villageroptimizer/VillagerOptimizer.java +++ b/src/main/java/me/xginko/villageroptimizer/VillagerOptimizer.java @@ -45,6 +45,12 @@ public final class VillagerOptimizer extends JavaPlugin { audiences = BukkitAudiences.create(this); logger = ComponentLogger.logger(getLogger().getName()); bStats = new Metrics(this, 19954); + try { + getDataFolder().mkdirs(); + } catch (Exception e) { + logger.error("Failed to create plugin directory! Cannot enable!", e); + getServer().getPluginManager().disablePlugin(this); + } logger.info(Component.text("╭────────────────────────────────────────────────────────────╮").style(GenericUtil.STYLE)); logger.info(Component.text("│ │").style(GenericUtil.STYLE)); @@ -65,14 +71,17 @@ public final class VillagerOptimizer extends JavaPlugin { .color(NamedTextColor.GRAY)).append(Component.text(" │").style(GenericUtil.STYLE))); logger.info(Component.text("│ │").style(GenericUtil.STYLE)); logger.info(Component.text("│ │").style(GenericUtil.STYLE)); + logger.info(Component.text("│ ") .style(GenericUtil.STYLE).append(Component.text(" ➤ Loading Translations...").style(GenericUtil.STYLE)) .append(Component.text(" │").style(GenericUtil.STYLE))); reloadLang(true); + logger.info(Component.text("│ ") .style(GenericUtil.STYLE).append(Component.text(" ➤ Loading Config...").style(GenericUtil.STYLE)) .append(Component.text(" │").style(GenericUtil.STYLE))); reloadConfiguration(); + logger.info(Component.text("│ ") .style(GenericUtil.STYLE).append(Component.text(" ✓ Done.").color(NamedTextColor.WHITE).decorate(TextDecoration.BOLD)) .append(Component.text(" │").style(GenericUtil.STYLE))); @@ -119,7 +128,7 @@ public final class VillagerOptimizer extends JavaPlugin { public static @NotNull FoliaLib getFoliaLib() { return foliaLib; } - public static @NotNull ComponentLogger getLog() { + public static @NotNull ComponentLogger getPrefixedLogger() { return logger; } public static @NotNull BukkitAudiences getAudiences() { diff --git a/src/main/java/me/xginko/villageroptimizer/config/Config.java b/src/main/java/me/xginko/villageroptimizer/config/Config.java index 86b6bd8..8799cb0 100644 --- a/src/main/java/me/xginko/villageroptimizer/config/Config.java +++ b/src/main/java/me/xginko/villageroptimizer/config/Config.java @@ -16,12 +16,8 @@ public class Config { public final long cache_keep_time_seconds; public Config() throws Exception { - // Create plugin folder first if it does not exist yet - File pluginFolder = VillagerOptimizer.getInstance().getDataFolder(); - if (!pluginFolder.exists() && !pluginFolder.mkdir()) - VillagerOptimizer.getLog().error("Failed to create plugin directory."); // Load config.yml with ConfigMaster - this.config = ConfigFile.loadConfig(new File(pluginFolder, "config.yml")); + this.config = ConfigFile.loadConfig(new File(VillagerOptimizer.getInstance().getDataFolder(), "config.yml")); structureConfig(); @@ -42,7 +38,7 @@ public class Config { try { this.config.save(); } catch (Throwable throwable) { - VillagerOptimizer.getLog().error("Failed to save config file!", throwable); + VillagerOptimizer.getPrefixedLogger().error("Failed to save config file!", throwable); } } diff --git a/src/main/java/me/xginko/villageroptimizer/config/LanguageCache.java b/src/main/java/me/xginko/villageroptimizer/config/LanguageCache.java index b42fe15..9f145b2 100644 --- a/src/main/java/me/xginko/villageroptimizer/config/LanguageCache.java +++ b/src/main/java/me/xginko/villageroptimizer/config/LanguageCache.java @@ -30,7 +30,7 @@ public class LanguageCache { // Check if the lang folder has already been created File parent = langYML.getParentFile(); if (!parent.exists() && !parent.mkdir()) - VillagerOptimizer.getLog().error("Failed to create lang directory."); + VillagerOptimizer.getPrefixedLogger().error("Failed to create lang directory."); // Check if the file already exists and save the one from the plugin's resources folder if it does not if (!langYML.exists()) plugin.saveResource("lang/" + locale + ".yml", false); @@ -86,7 +86,7 @@ public class LanguageCache { try { this.lang.save(); } catch (Throwable throwable) { - VillagerOptimizer.getLog().error("Failed to save language file: "+ langYML.getName(), throwable); + VillagerOptimizer.getPrefixedLogger().error("Failed to save language file: " + langYML.getName(), throwable); } } diff --git a/src/main/java/me/xginko/villageroptimizer/modules/VillagerChunkLimit.java b/src/main/java/me/xginko/villageroptimizer/modules/VillagerChunkLimit.java index e8664e7..172613a 100644 --- a/src/main/java/me/xginko/villageroptimizer/modules/VillagerChunkLimit.java +++ b/src/main/java/me/xginko/villageroptimizer/modules/VillagerChunkLimit.java @@ -6,7 +6,6 @@ import me.xginko.villageroptimizer.VillagerCache; import me.xginko.villageroptimizer.VillagerOptimizer; import me.xginko.villageroptimizer.config.Config; import me.xginko.villageroptimizer.utils.GenericUtil; -import net.kyori.adventure.text.Component; import org.bukkit.Chunk; import org.bukkit.Server; import org.bukkit.World; @@ -39,48 +38,58 @@ public class VillagerChunkLimit implements VillagerOptimizerModule, Listener { this.scheduler = VillagerOptimizer.getFoliaLib().getImpl(); this.villagerCache = VillagerOptimizer.getCache(); Config config = VillagerOptimizer.getConfiguration(); - config.master().addComment("villager-chunk-limit.enable", + config.master().addComment(configPath() + ".enable", "Checks chunks for too many villagers and removes excess villagers based on priority."); - this.check_period = config.getInt("villager-chunk-limit.check-period-in-ticks", 600, + this.check_period = config.getInt(configPath() + ".check-period-in-ticks", 600, "Check all loaded chunks every X ticks. 1 second = 20 ticks\n" + "A shorter delay in between checks is more efficient but is also more resource intense.\n" + "A larger delay is less resource intense but could become inefficient."); - this.skip_unloaded_entity_chunks = config.getBoolean("villager-chunk-limit.skip-if-chunk-has-not-loaded-entities", true, + this.skip_unloaded_entity_chunks = config.getBoolean(configPath() + ".skip-if-chunk-has-not-loaded-entities", true, "Does not check chunks that don't have their entities loaded."); - this.log_enabled = config.getBoolean("villager-chunk-limit.log-removals", true); - this.non_optimized_max_per_chunk = config.getInt("villager-chunk-limit.unoptimized.max-per-chunk", 20, + this.log_enabled = config.getBoolean(configPath() + ".log-removals", true); + this.non_optimized_max_per_chunk = config.getInt(configPath() + ".unoptimized.max-per-chunk", 20, "The maximum amount of unoptimized villagers per chunk."); - this.non_optimized_removal_priority = config.getList("villager-chunk-limit.unoptimized.removal-priority", Arrays.asList( + this.non_optimized_removal_priority = config.getList(configPath() + ".unoptimized.removal-priority", Arrays.asList( "NONE", "NITWIT", "SHEPHERD", "FISHERMAN", "BUTCHER", "CARTOGRAPHER", "LEATHERWORKER", - "FLETCHER", "MASON", "FARMER", "ARMORER", "TOOLSMITH", "WEAPONSMITH", "CLERIC", "LIBRARIAN" - ), - "Professions that are in the top of the list are going to be scheduled for removal first.\n" + - "Use enums from https://jd.papermc.io/paper/1.20/org/bukkit/entity/Villager.Profession.html" - ).stream().map(configuredProfession -> { - try { - return Villager.Profession.valueOf(configuredProfession); - } catch (IllegalArgumentException e) { - VillagerOptimizer.getLog().warn("(villager-chunk-limit.unoptimized) Villager profession '"+configuredProfession + - "' not recognized. Make sure you're using the correct profession enums from " + - "https://jd.papermc.io/paper/1.20/org/bukkit/entity/Villager.Profession.html."); - return null; - } - }).filter(Objects::nonNull).collect(Collectors.toList()); - this.optimized_max_per_chunk = config.getInt("villager-chunk-limit.optimized.max-per-chunk", 60, + "FLETCHER", "MASON", "FARMER", "ARMORER", "TOOLSMITH", "WEAPONSMITH", "CLERIC", "LIBRARIAN"), + "Professions that are in the top of the list are going to be scheduled for removal first.\n" + + "Use enums from https://jd.papermc.io/paper/1.20/org/bukkit/entity/Villager.Profession.html") + .stream() + .map(configuredProfession -> { + try { + return Villager.Profession.valueOf(configuredProfession); + } catch (IllegalArgumentException e) { + warn("(unoptimized) Villager profession '" + configuredProfession + + "' not recognized. Make sure you're using the correct profession enums from " + + "https://jd.papermc.io/paper/1.20/org/bukkit/entity/Villager.Profession.html."); + return null; + } + }) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + this.optimized_max_per_chunk = config.getInt(configPath() + ".optimized.max-per-chunk", 60, "The maximum amount of optimized villagers per chunk."); - this.optimized_removal_priority = config.getList("villager-chunk-limit.optimized.removal-priority", Arrays.asList( + this.optimized_removal_priority = config.getList(configPath() + ".optimized.removal-priority", Arrays.asList( "NONE", "NITWIT", "SHEPHERD", "FISHERMAN", "BUTCHER", "CARTOGRAPHER", "LEATHERWORKER", - "FLETCHER", "MASON", "FARMER", "ARMORER", "TOOLSMITH", "WEAPONSMITH", "CLERIC", "LIBRARIAN" - )).stream().map(configuredProfession -> { - try { - return Villager.Profession.valueOf(configuredProfession); - } catch (IllegalArgumentException e) { - VillagerOptimizer.getLog().warn("(villager-chunk-limit.optimized) Villager profession '"+configuredProfession + - "' not recognized. Make sure you're using the correct profession enums from " + - "https://jd.papermc.io/paper/1.20/org/bukkit/entity/Villager.Profession.html."); - return null; - } - }).filter(Objects::nonNull).collect(Collectors.toList()); + "FLETCHER", "MASON", "FARMER", "ARMORER", "TOOLSMITH", "WEAPONSMITH", "CLERIC", "LIBRARIAN")) + .stream() + .map(configuredProfession -> { + try { + return Villager.Profession.valueOf(configuredProfession); + } catch (IllegalArgumentException e) { + warn("(optimized) Villager profession '" + configuredProfession + "' not recognized. " + + "Make sure you're using the correct profession enums from " + + "https://jd.papermc.io/paper/1.20/org/bukkit/entity/Villager.Profession.html."); + return null; + } + }) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } + + @Override + public String configPath() { + return "villager-chunk-limit"; } @Override @@ -102,7 +111,7 @@ public class VillagerChunkLimit implements VillagerOptimizerModule, Listener { @Override public boolean shouldEnable() { - return VillagerOptimizer.getConfiguration().getBoolean("villager-chunk-limit.enable", false); + return VillagerOptimizer.getConfiguration().getBoolean(configPath() + ".enable", false); } @Override @@ -155,9 +164,8 @@ public class VillagerChunkLimit implements VillagerOptimizerModule, Listener { scheduler.runAtEntity(villager, kill -> { villager.remove(); if (log_enabled) { - VillagerOptimizer.getLog().info(Component.text( - "Removed unoptimized villager with profession '" + villager.getProfession().name() + "' at " + - GenericUtil.formatLocation(villager.getLocation())).color(GenericUtil.COLOR)); + info("Removed unoptimized villager with profession '" + villager.getProfession() + "' at " + + GenericUtil.formatLocation(villager.getLocation())); } }); } @@ -178,9 +186,8 @@ public class VillagerChunkLimit implements VillagerOptimizerModule, Listener { villager.remove(); if (log_enabled) { - VillagerOptimizer.getLog().info(Component.text("Removed optimized villager with profession '" + - villager.getProfession().name() + "' at " + - GenericUtil.formatLocation(villager.getLocation())).color(GenericUtil.COLOR)); + info("Removed optimized villager with profession '" + villager.getProfession() + "' at " + + GenericUtil.formatLocation(villager.getLocation())); } }); } diff --git a/src/main/java/me/xginko/villageroptimizer/modules/VillagerOptimizerModule.java b/src/main/java/me/xginko/villageroptimizer/modules/VillagerOptimizerModule.java index 3f0bfe6..02b19d0 100644 --- a/src/main/java/me/xginko/villageroptimizer/modules/VillagerOptimizerModule.java +++ b/src/main/java/me/xginko/villageroptimizer/modules/VillagerOptimizerModule.java @@ -1,5 +1,6 @@ package me.xginko.villageroptimizer.modules; +import me.xginko.villageroptimizer.VillagerOptimizer; import me.xginko.villageroptimizer.modules.gameplay.*; import me.xginko.villageroptimizer.modules.optimization.OptimizeByBlock; import me.xginko.villageroptimizer.modules.optimization.OptimizeByNametag; @@ -9,6 +10,7 @@ import java.util.HashSet; public interface VillagerOptimizerModule { + String configPath(); void enable(); void disable(); boolean shouldEnable(); @@ -40,4 +42,26 @@ public interface VillagerOptimizerModule { if (module.shouldEnable()) module.enable(); }); } + + default void trace(String message, Throwable throwable) { + VillagerOptimizer.getPrefixedLogger().trace(logPrefix() + message, throwable); + } + + default void error(String message) { + VillagerOptimizer.getPrefixedLogger().error(logPrefix() + message); + } + + default void warn(String message) { + VillagerOptimizer.getPrefixedLogger().warn(logPrefix() + message); + } + + default void info(String message) { + VillagerOptimizer.getPrefixedLogger().info(logPrefix() + message); + } + + default String logPrefix() { + String[] split = configPath().split("\\."); + if (split.length <= 2) return "<" + configPath() + "> "; + return "<" + String.join(".", split[split.length - 2], split[split.length - 1]) + "> "; + } } diff --git a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/EnableLeashingVillagers.java b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/EnableLeashingVillagers.java index ad411e3..45f8ba4 100644 --- a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/EnableLeashingVillagers.java +++ b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/EnableLeashingVillagers.java @@ -6,7 +6,6 @@ import me.xginko.villageroptimizer.VillagerOptimizer; import me.xginko.villageroptimizer.config.Config; import me.xginko.villageroptimizer.modules.VillagerOptimizerModule; import me.xginko.villageroptimizer.utils.GenericUtil; -import net.kyori.adventure.text.Component; import org.bukkit.GameMode; import org.bukkit.Material; import org.bukkit.entity.EntityType; @@ -31,11 +30,16 @@ public class EnableLeashingVillagers implements VillagerOptimizerModule, Listene this.scheduler = VillagerOptimizer.getFoliaLib().getImpl(); this.villagerCache = VillagerOptimizer.getCache(); Config config = VillagerOptimizer.getConfiguration(); - config.master().addComment("gameplay.villagers-can-be-leashed.enable", + config.master().addComment(configPath() + ".enable", "Enable leashing of villagers, enabling players to easily move villagers to where they want them to be."); - this.only_optimized = config.getBoolean("gameplay.villagers-can-be-leashed.only-optimized", false, + this.only_optimized = config.getBoolean(configPath() + ".only-optimized", false, "If set to true, only optimized villagers can be leashed."); - this.log_enabled = config.getBoolean("gameplay.villagers-can-be-leashed.log", false); + this.log_enabled = config.getBoolean(configPath() + ".log", false); + } + + @Override + public String configPath() { + return "gameplay.villagers-can-be-leashed"; } @Override @@ -51,7 +55,7 @@ public class EnableLeashingVillagers implements VillagerOptimizerModule, Listene @Override public boolean shouldEnable() { - return VillagerOptimizer.getConfiguration().getBoolean("gameplay.villagers-can-be-leashed.enable", false); + return VillagerOptimizer.getConfiguration().getBoolean(configPath() + ".enable", false); } @SuppressWarnings("deprecation") @@ -86,8 +90,7 @@ public class EnableLeashingVillagers implements VillagerOptimizerModule, Listene handItem.subtract(1); // Manually consume for survival players if (log_enabled) { - VillagerOptimizer.getLog().info(Component.text(player.getName() + " leashed a villager at " + - GenericUtil.formatLocation(villager.getLocation())).color(GenericUtil.COLOR)); + info(player.getName() + " leashed a villager at " + GenericUtil.formatLocation(villager.getLocation())); } }); } diff --git a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/FixOptimisationAfterCure.java b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/FixOptimisationAfterCure.java index f650984..ff4c04a 100644 --- a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/FixOptimisationAfterCure.java +++ b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/FixOptimisationAfterCure.java @@ -17,6 +17,11 @@ public class FixOptimisationAfterCure implements VillagerOptimizerModule, Listen public FixOptimisationAfterCure() {} + @Override + public String configPath() { + return "post-cure-optimization-fix"; + } + @Override public void enable() { VillagerOptimizer plugin = VillagerOptimizer.getInstance(); diff --git a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/LevelOptimizedProfession.java b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/LevelOptimizedProfession.java index 0c17af7..b0d5f8d 100644 --- a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/LevelOptimizedProfession.java +++ b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/LevelOptimizedProfession.java @@ -35,17 +35,22 @@ public class LevelOptimizedProfession implements VillagerOptimizerModule, Listen this.scheduler = VillagerOptimizer.getFoliaLib().getImpl(); this.villagerCache = VillagerOptimizer.getCache(); Config config = VillagerOptimizer.getConfiguration(); - config.master().addComment("gameplay.level-optimized-profession", + config.master().addComment(configPath(), "This is needed to allow optimized villagers to level up.\n" + "Temporarily enables the villagers AI to allow it to level up and then disables it again."); this.cooldown_millis = TimeUnit.SECONDS.toMillis( - config.getInt("gameplay.level-optimized-profession.level-check-cooldown-seconds", 5, + config.getInt(configPath() + ".level-check-cooldown-seconds", 5, "Cooldown in seconds until the level of a villager will be checked and updated again.\n" + "Recommended to leave as is.")); - this.notify_player = config.getBoolean("gameplay.level-optimized-profession.notify-player", true, + this.notify_player = config.getBoolean(configPath() + ".notify-player", true, "Tell players to wait when a villager is leveling up."); } + @Override + public String configPath() { + return "gameplay.level-optimized-profession"; + } + @Override public void enable() { VillagerOptimizer plugin = VillagerOptimizer.getInstance(); diff --git a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/MakeVillagersSpawnAdult.java b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/MakeVillagersSpawnAdult.java index ccc345d..f9c8db5 100644 --- a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/MakeVillagersSpawnAdult.java +++ b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/MakeVillagersSpawnAdult.java @@ -14,6 +14,11 @@ public class MakeVillagersSpawnAdult implements VillagerOptimizerModule, Listene public MakeVillagersSpawnAdult() {} + @Override + public String configPath() { + return "gameplay.villagers-spawn-as-adults"; + } + @Override public void enable() { VillagerOptimizer plugin = VillagerOptimizer.getInstance(); @@ -27,7 +32,7 @@ public class MakeVillagersSpawnAdult implements VillagerOptimizerModule, Listene @Override public boolean shouldEnable() { - return VillagerOptimizer.getConfiguration().getBoolean("gameplay.villagers-spawn-as-adults.enable", false, + return VillagerOptimizer.getConfiguration().getBoolean(configPath() + ".enable", false, "Spawned villagers will immediately be adults.\n" + "This is to save some more resources as players don't have to keep unoptimized\n" + "villagers loaded because they have to wait for them to turn into adults before they can\n" + diff --git a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/PreventOptimizedDamage.java b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/PreventOptimizedDamage.java index 41c65b2..24339fc 100644 --- a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/PreventOptimizedDamage.java +++ b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/PreventOptimizedDamage.java @@ -29,25 +29,32 @@ public class PreventOptimizedDamage implements VillagerOptimizerModule, Listener shouldEnable(); this.villagerCache = VillagerOptimizer.getCache(); Config config = VillagerOptimizer.getConfiguration(); - config.master().addComment("gameplay.prevent-damage-to-optimized.enable", + config.master().addComment(configPath() + ".enable", "Configure what kind of damage you want to cancel for optimized villagers here."); - this.cancel_knockback = config.getBoolean("gameplay.prevent-damage-to-optimized.prevent-knockback-from-entity", true, + this.cancel_knockback = config.getBoolean(configPath() + ".prevent-knockback-from-entity", true, "Prevents optimized villagers from getting knocked back by an attacking entity"); - this.damage_causes_to_cancel = config.getList("gameplay.prevent-damage-to-optimized.damage-causes-to-cancel", + this.damage_causes_to_cancel = config.getList(configPath() + ".damage-causes-to-cancel", Arrays.stream(EntityDamageEvent.DamageCause.values()).map(Enum::name).sorted().collect(Collectors.toList()), "These are all current entries in the game. Remove what you do not need blocked.\n" + "If you want a description or need to add a previously removed type, refer to:\n" + - "https://jd.papermc.io/paper/1.20/org/bukkit/event/entity/EntityDamageEvent.DamageCause.html" - ).stream().map(configuredDamageCause -> { - try { - return EntityDamageEvent.DamageCause.valueOf(configuredDamageCause); - } catch (IllegalArgumentException e) { - VillagerOptimizer.getLog().warn("(prevent-damage-to-optimized) DamageCause '"+configuredDamageCause + - "' not recognized. Please use correct DamageCause enums from: " + - "https://jd.papermc.io/paper/1.20/org/bukkit/event/entity/EntityDamageEvent.DamageCause.html"); - return null; - } - }).filter(Objects::nonNull).collect(Collectors.toCollection(HashSet::new)); + "https://jd.papermc.io/paper/1.20/org/bukkit/event/entity/EntityDamageEvent.DamageCause.html") + .stream() + .map(configuredDamageCause -> { + try { + return EntityDamageEvent.DamageCause.valueOf(configuredDamageCause); + } catch (IllegalArgumentException e) { + warn("DamageCause '" + configuredDamageCause + "' not recognized. Please use correct DamageCause enums from: " + + "https://jd.papermc.io/paper/1.20/org/bukkit/event/entity/EntityDamageEvent.DamageCause.html"); + return null; + } + }) + .filter(Objects::nonNull) + .collect(Collectors.toCollection(HashSet::new)); + } + + @Override + public String configPath() { + return "gameplay.prevent-damage-to-optimized"; } @Override @@ -63,7 +70,7 @@ public class PreventOptimizedDamage implements VillagerOptimizerModule, Listener @Override public boolean shouldEnable() { - return VillagerOptimizer.getConfiguration().getBoolean("gameplay.prevent-damage-to-optimized.enable", true); + return VillagerOptimizer.getConfiguration().getBoolean(configPath() + ".enable", true); } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) diff --git a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/PreventOptimizedTargeting.java b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/PreventOptimizedTargeting.java index d66b70b..d0e860b 100644 --- a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/PreventOptimizedTargeting.java +++ b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/PreventOptimizedTargeting.java @@ -23,6 +23,11 @@ public class PreventOptimizedTargeting implements VillagerOptimizerModule, Liste this.villagerCache = VillagerOptimizer.getCache(); } + @Override + public String configPath() { + return "gameplay.prevent-entities-from-targeting-optimized"; + } + @Override public void enable() { VillagerOptimizer plugin = VillagerOptimizer.getInstance(); @@ -36,7 +41,7 @@ public class PreventOptimizedTargeting implements VillagerOptimizerModule, Liste @Override public boolean shouldEnable() { - return VillagerOptimizer.getConfiguration().getBoolean("gameplay.prevent-entities-from-targeting-optimized.enable", true, + return VillagerOptimizer.getConfiguration().getBoolean(configPath() + ".enable", true, "Prevents hostile entities from targeting optimized villagers."); } diff --git a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/PreventUnoptimizedTrading.java b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/PreventUnoptimizedTrading.java index 2591528..0575ed2 100644 --- a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/PreventUnoptimizedTrading.java +++ b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/PreventUnoptimizedTrading.java @@ -25,14 +25,19 @@ public class PreventUnoptimizedTrading implements VillagerOptimizerModule, Liste shouldEnable(); this.villagerCache = VillagerOptimizer.getCache(); Config config = VillagerOptimizer.getConfiguration(); - config.master().addComment("gameplay.prevent-trading-with-unoptimized.enable", + config.master().addComment(configPath() + ".enable", "Will prevent players from selecting and using trades of unoptimized villagers.\n" + "Use this if you have a lot of villagers and therefore want to force your players to optimize them.\n" + "Inventories can still be opened so players can move villagers around."); - this.notify_player = config.getBoolean("gameplay.prevent-trading-with-unoptimized.notify-player", true, + this.notify_player = config.getBoolean(configPath() + ".notify-player", true, "Sends players a message when they try to trade with an unoptimized villager."); } + @Override + public String configPath() { + return "gameplay.prevent-trading-with-unoptimized"; + } + @Override public void enable() { VillagerOptimizer plugin = VillagerOptimizer.getInstance(); @@ -46,7 +51,7 @@ public class PreventUnoptimizedTrading implements VillagerOptimizerModule, Liste @Override public boolean shouldEnable() { - return VillagerOptimizer.getConfiguration().getBoolean("gameplay.prevent-trading-with-unoptimized.enable", false); + return VillagerOptimizer.getConfiguration().getBoolean(configPath() + ".enable", false); } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) diff --git a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/RestockOptimizedTrades.java b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/RestockOptimizedTrades.java index 94b2e0b..b741ca3 100644 --- a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/RestockOptimizedTrades.java +++ b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/RestockOptimizedTrades.java @@ -8,7 +8,6 @@ import me.xginko.villageroptimizer.enums.Permissions; import me.xginko.villageroptimizer.modules.VillagerOptimizerModule; import me.xginko.villageroptimizer.utils.GenericUtil; import me.xginko.villageroptimizer.utils.KyoriUtil; -import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextReplacementConfig; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; @@ -31,14 +30,19 @@ public class RestockOptimizedTrades implements VillagerOptimizerModule, Listener shouldEnable(); this.villagerCache = VillagerOptimizer.getCache(); Config config = VillagerOptimizer.getConfiguration(); - config.master().addComment("gameplay.restock-optimized-trades", + config.master().addComment(configPath(), "This is for automatic restocking of trades for optimized villagers. Optimized Villagers\n" + "don't have enough AI to restock their trades naturally, so this is here as a workaround."); - this.restock_delay_millis = config.getInt("gameplay.restock-optimized-trades.delay-in-ticks", 1000, + this.restock_delay_millis = config.getInt(configPath() + ".delay-in-ticks", 1000, "1 second = 20 ticks. There are 24.000 ticks in a single minecraft day.") * 50L; - this.notify_player = config.getBoolean("gameplay.restock-optimized-trades.notify-player", true, + this.notify_player = config.getBoolean(configPath() + ".notify-player", true, "Sends the player a message when the trades were restocked on a clicked villager."); - this.log_enabled = config.getBoolean("gameplay.restock-optimized-trades.log", false); + this.log_enabled = config.getBoolean(configPath() + ".log", false); + } + + @Override + public String configPath() { + return "gameplay.restock-optimized-trades"; } @Override @@ -81,8 +85,7 @@ public class RestockOptimizedTrades implements VillagerOptimizerModule, Listener } if (log_enabled) { - VillagerOptimizer.getLog().info(Component.text("Restocked optimized villager at " + - GenericUtil.formatLocation(wVillager.villager().getLocation())).color(GenericUtil.COLOR)); + info("Restocked optimized villager at " + GenericUtil.formatLocation(wVillager.villager().getLocation())); } } } diff --git a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/UnoptimizeOnJobLoose.java b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/UnoptimizeOnJobLoose.java index bbab7f0..ecdc979 100644 --- a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/UnoptimizeOnJobLoose.java +++ b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/UnoptimizeOnJobLoose.java @@ -19,6 +19,11 @@ public class UnoptimizeOnJobLoose implements VillagerOptimizerModule, Listener { this.villagerCache = VillagerOptimizer.getCache(); } + @Override + public String configPath() { + return "gameplay.unoptimize-on-job-loose"; + } + @Override public void enable() { VillagerOptimizer plugin = VillagerOptimizer.getInstance(); @@ -32,7 +37,7 @@ public class UnoptimizeOnJobLoose implements VillagerOptimizerModule, Listener { @Override public boolean shouldEnable() { - return VillagerOptimizer.getConfiguration().getBoolean("gameplay.unoptimize-on-job-loose.enable", true, + return VillagerOptimizer.getConfiguration().getBoolean(configPath() + ".enable", true, "Villagers that get their jobs reset will become unoptimized again."); } diff --git a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/VisuallyHighlightOptimized.java b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/VisuallyHighlightOptimized.java index 58f8ba8..08ce57c 100644 --- a/src/main/java/me/xginko/villageroptimizer/modules/gameplay/VisuallyHighlightOptimized.java +++ b/src/main/java/me/xginko/villageroptimizer/modules/gameplay/VisuallyHighlightOptimized.java @@ -24,6 +24,11 @@ public class VisuallyHighlightOptimized implements VillagerOptimizerModule, List "Will make optimized villagers glow."); } + @Override + public String configPath() { + return "gameplay.outline-optimized-villagers"; + } + @Override public void enable() { VillagerOptimizer plugin = VillagerOptimizer.getInstance(); diff --git a/src/main/java/me/xginko/villageroptimizer/modules/optimization/OptimizeByBlock.java b/src/main/java/me/xginko/villageroptimizer/modules/optimization/OptimizeByBlock.java index 94324e0..d3622d5 100644 --- a/src/main/java/me/xginko/villageroptimizer/modules/optimization/OptimizeByBlock.java +++ b/src/main/java/me/xginko/villageroptimizer/modules/optimization/OptimizeByBlock.java @@ -45,34 +45,41 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener { shouldEnable(); this.villagerCache = VillagerOptimizer.getCache(); Config config = VillagerOptimizer.getConfiguration(); - config.master().addComment("optimization-methods.block-optimization.enable", + config.master().addComment(configPath() + ".enable", "When enabled, the closest villager standing near a configured block being placed will be optimized.\n" + "If a configured block is broken nearby, the closest villager will become unoptimized again."); - this.blocks_that_disable = config.getList("optimization-methods.block-optimization.materials", Arrays.asList( + this.blocks_that_disable = config.getList(configPath() + ".materials", Arrays.asList( "LAPIS_BLOCK", "GLOWSTONE", "IRON_BLOCK" - ), "Values here need to be valid bukkit Material enums for your server version." - ).stream().map(configuredMaterial -> { - try { - return Material.valueOf(configuredMaterial); - } catch (IllegalArgumentException e) { - VillagerOptimizer.getLog().warn("(block-optimization) Material '" + configuredMaterial + - "' not recognized. Please use correct Material enums from: " + - "https://jd.papermc.io/paper/1.20/org/bukkit/Material.html"); - return null; - } - }).filter(Objects::nonNull).collect(Collectors.toCollection(HashSet::new)); + ), "Values here need to be valid bukkit Material enums for your server version.") + .stream() + .map(configuredMaterial -> { + try { + return Material.valueOf(configuredMaterial); + } catch (IllegalArgumentException e) { + warn("Material '" + configuredMaterial + "' not recognized. Please use correct Material enums from: " + + "https://jd.papermc.io/paper/1.20/org/bukkit/Material.html"); + return null; + } + }) + .filter(Objects::nonNull) + .collect(Collectors.toCollection(HashSet::new)); this.cooldown_millis = TimeUnit.SECONDS.toMillis( - config.getInt("optimization-methods.block-optimization.optimize-cooldown-seconds", 600, + config.getInt(configPath() + ".optimize-cooldown-seconds", 600, "Cooldown in seconds until a villager can be optimized again by using specific blocks.\n" + "Here for configuration freedom. Recommended to leave as is to not enable any exploitable behavior.")); - this.search_radius = config.getDouble("optimization-methods.block-optimization.search-radius-in-blocks", 2.0, + this.search_radius = config.getDouble(configPath() + ".search-radius-in-blocks", 2.0, "The radius in blocks a villager can be away from the player when he places an optimize block.\n" + "The closest unoptimized villager to the player will be optimized.") / 2; - this.only_while_sneaking = config.getBoolean("optimization-methods.block-optimization.only-when-sneaking", true, + this.only_while_sneaking = config.getBoolean(configPath() + ".only-when-sneaking", true, "Only optimize/unoptimize by block when player is sneaking during place or break."); - this.notify_player = config.getBoolean("optimization-methods.block-optimization.notify-player", true, + this.notify_player = config.getBoolean(configPath() + ".notify-player", true, "Sends players a message when they successfully optimized or unoptimized a villager."); - this.log_enabled = config.getBoolean("optimization-methods.block-optimization.log", false); + this.log_enabled = config.getBoolean(configPath() + ".log", false); + } + + @Override + public String configPath() { + return "optimization-methods.block-optimization"; } @Override @@ -88,7 +95,7 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener { @Override public boolean shouldEnable() { - return VillagerOptimizer.getConfiguration().getBoolean("optimization-methods.block-optimization.enable", false); + return VillagerOptimizer.getConfiguration().getBoolean(configPath() + ".enable", false); } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @@ -144,8 +151,8 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener { } if (log_enabled) { - VillagerOptimizer.getLog().info(Component.text(player.getName() + " optimized villager by block at " + - GenericUtil.formatLocation(closestOptimizableVillager.villager().getLocation())).color(GenericUtil.COLOR)); + VillagerOptimizer.getPrefixedLogger().info(Component.text(player.getName() + " optimized villager by block at " + + GenericUtil.formatLocation(closestOptimizableVillager.villager().getLocation())).color(GenericUtil.COLOR)); } } else { closestOptimizableVillager.sayNo(); @@ -209,8 +216,8 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener { } if (log_enabled) { - VillagerOptimizer.getLog().info(Component.text(player.getName() + " unoptimized villager by block at " + - GenericUtil.formatLocation(closestOptimizedVillager.villager().getLocation())).color(GenericUtil.COLOR)); + VillagerOptimizer.getPrefixedLogger().info(Component.text(player.getName() + " unoptimized villager by block at " + + GenericUtil.formatLocation(closestOptimizedVillager.villager().getLocation())).color(GenericUtil.COLOR)); } } } \ No newline at end of file diff --git a/src/main/java/me/xginko/villageroptimizer/modules/optimization/OptimizeByNametag.java b/src/main/java/me/xginko/villageroptimizer/modules/optimization/OptimizeByNametag.java index 3dd9a35..0ecf471 100644 --- a/src/main/java/me/xginko/villageroptimizer/modules/optimization/OptimizeByNametag.java +++ b/src/main/java/me/xginko/villageroptimizer/modules/optimization/OptimizeByNametag.java @@ -43,21 +43,26 @@ public class OptimizeByNametag implements VillagerOptimizerModule, Listener { shouldEnable(); this.villagerCache = VillagerOptimizer.getCache(); Config config = VillagerOptimizer.getConfiguration(); - config.master().addComment("optimization-methods.nametag-optimization.enable", + config.master().addComment(configPath() + ".enable", "Enable optimization by naming villagers to one of the names configured below.\n" + "Nametag optimized villagers will be unoptimized again when they are renamed to something else."); - this.nametags = config.getList("optimization-methods.nametag-optimization.names", Arrays.asList("Optimize", "DisableAI"), + this.nametags = config.getList(configPath() + ".names", Arrays.asList("Optimize", "DisableAI"), "Names are case insensitive, capital letters won't matter.") .stream().map(String::toLowerCase).collect(Collectors.toCollection(HashSet::new)); - this.consume_nametag = config.getBoolean("optimization-methods.nametag-optimization.nametags-get-consumed", true, + this.consume_nametag = config.getBoolean(configPath() + ".nametags-get-consumed", true, "Enable or disable consumption of the used nametag item."); this.cooldown = TimeUnit.SECONDS.toMillis( - config.getInt("optimization-methods.nametag-optimization.optimize-cooldown-seconds", 600, + config.getInt(configPath() + ".optimize-cooldown-seconds", 600, "Cooldown in seconds until a villager can be optimized again using a nametag.\n" + "Here for configuration freedom. Recommended to leave as is to not enable any exploitable behavior.")); - this.notify_player = config.getBoolean("optimization-methods.nametag-optimization.notify-player", true, + this.notify_player = config.getBoolean(configPath() + ".notify-player", true, "Sends players a message when they successfully optimized a villager."); - this.log_enabled = config.getBoolean("optimization-methods.nametag-optimization.log", false); + this.log_enabled = config.getBoolean(configPath() + ".log", false); + } + + @Override + public String configPath() { + return "optimization-methods.nametag-optimization"; } @Override @@ -73,7 +78,7 @@ public class OptimizeByNametag implements VillagerOptimizerModule, Listener { @Override public boolean shouldEnable() { - return VillagerOptimizer.getConfiguration().getBoolean("optimization-methods.nametag-optimization.enable", true); + return VillagerOptimizer.getConfiguration().getBoolean(configPath() + ".enable", true); } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @@ -120,9 +125,8 @@ public class OptimizeByNametag implements VillagerOptimizerModule, Listener { } if (log_enabled) { - VillagerOptimizer.getLog().info(Component.text(player.getName() + - " optimized villager by nametag '" + nameTagPlainText + "' at " + - GenericUtil.formatLocation(wVillager.villager().getLocation())).color(GenericUtil.COLOR)); + info(player.getName() + " optimized villager using nametag '" + nameTagPlainText + "' at " + + GenericUtil.formatLocation(wVillager.villager().getLocation())); } } else { event.setCancelled(true); @@ -154,9 +158,8 @@ public class OptimizeByNametag implements VillagerOptimizerModule, Listener { } if (log_enabled) { - VillagerOptimizer.getLog().info(Component.text(player.getName() + - " unoptimized villager by nametag '" + nameTagPlainText + "' at " + - GenericUtil.formatLocation(wVillager.villager().getLocation())).color(GenericUtil.COLOR)); + info(player.getName() + " unoptimized villager using nametag '" + nameTagPlainText + "' at " + + GenericUtil.formatLocation(wVillager.villager().getLocation())); } } } diff --git a/src/main/java/me/xginko/villageroptimizer/modules/optimization/OptimizeByWorkstation.java b/src/main/java/me/xginko/villageroptimizer/modules/optimization/OptimizeByWorkstation.java index 25228f8..6be7899 100644 --- a/src/main/java/me/xginko/villageroptimizer/modules/optimization/OptimizeByWorkstation.java +++ b/src/main/java/me/xginko/villageroptimizer/modules/optimization/OptimizeByWorkstation.java @@ -12,7 +12,6 @@ import me.xginko.villageroptimizer.events.VillagerUnoptimizeEvent; import me.xginko.villageroptimizer.modules.VillagerOptimizerModule; import me.xginko.villageroptimizer.utils.GenericUtil; import me.xginko.villageroptimizer.utils.KyoriUtil; -import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextReplacementConfig; import org.bukkit.Location; import org.bukkit.block.Block; @@ -44,24 +43,29 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener this.scheduler = VillagerOptimizer.getFoliaLib().getImpl(); this.villagerCache = VillagerOptimizer.getCache(); Config config = VillagerOptimizer.getConfiguration(); - config.master().addComment("optimization-methods.workstation-optimization.enable", + config.master().addComment(configPath() + ".enable", "When enabled, villagers that have a job and have been traded with at least once will become optimized,\n" + "if near their workstation. If the workstation is broken, the villager will become unoptimized again."); - this.check_duration_ticks = Math.max(config.getInt("optimization-methods.workstation-optimization.check-linger-duration-ticks", 100, + this.check_duration_ticks = Math.max(config.getInt(configPath() + ".check-linger-duration-ticks", 100, "After a workstation has been placed, the plugin will wait for the configured amount of time in ticks\n" + "for a villager to claim that workstation. Not recommended to go below 100 ticks."), 1); - this.search_radius = config.getDouble("optimization-methods.workstation-optimization.search-radius-in-blocks", 2.0, + this.search_radius = config.getDouble(configPath() + ".search-radius-in-blocks", 2.0, "The radius in blocks a villager can be away from the player when he places a workstation.\n" + "The closest unoptimized villager to the player will be optimized."); this.cooldown_millis = TimeUnit.SECONDS.toMillis( - Math.max(1, config.getInt("optimization-methods.workstation-optimization.optimize-cooldown-seconds", 600, + Math.max(1, config.getInt(configPath() + ".optimize-cooldown-seconds", 600, "Cooldown in seconds until a villager can be optimized again using a workstation.\n" + "Here for configuration freedom. Recommended to leave as is to not enable any exploitable behavior."))); - this.only_while_sneaking = config.getBoolean("optimization-methods.workstation-optimization.only-when-sneaking", true, + this.only_while_sneaking = config.getBoolean(configPath() + ".only-when-sneaking", true, "Only optimize/unoptimize by workstation when player is sneaking during place or break. Useful for villager rolling."); - this.notify_player = config.getBoolean("optimization-methods.workstation-optimization.notify-player", true, + this.notify_player = config.getBoolean(configPath() + ".notify-player", true, "Sends players a message when they successfully optimized a villager."); - this.log_enabled = config.getBoolean("optimization-methods.workstation-optimization.log", false); + this.log_enabled = config.getBoolean(configPath() + ".log", false); + } + + @Override + public String configPath() { + return "optimization-methods.workstation-optimization"; } @Override @@ -77,7 +81,7 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener @Override public boolean shouldEnable() { - return VillagerOptimizer.getConfiguration().getBoolean("optimization-methods.workstation-optimization.enable", false); + return VillagerOptimizer.getConfiguration().getBoolean(configPath() + ".enable", false); } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @@ -146,9 +150,8 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener } if (log_enabled) { - VillagerOptimizer.getLog().info(Component.text(player.getName() + - " optimized villager by workstation (" + placed.getType().toString().toLowerCase() + ") at " + - GenericUtil.formatLocation(wrapped.villager().getLocation())).color(GenericUtil.COLOR)); + info(player.getName() + " optimized villager using workstation " + placed.getType() + " at " + + GenericUtil.formatLocation(wrapped.villager().getLocation())); } taskComplete.set(true); @@ -211,9 +214,8 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener } if (log_enabled) { - VillagerOptimizer.getLog().info(Component.text(player.getName() + - " unoptimized villager by workstation (" + broken.getType().toString().toLowerCase() + ") at " + - GenericUtil.formatLocation(closestOptimized.villager().getLocation())).color(GenericUtil.COLOR)); + info(player.getName() + " unoptimized villager using workstation " + broken.getType() + " at " + + GenericUtil.formatLocation(closestOptimized.villager().getLocation())); } } } \ No newline at end of file