naming conventions

This commit is contained in:
xGinko 2023-10-02 23:52:28 +02:00
parent 48a3ed900f
commit 7eec1c23b0
19 changed files with 146 additions and 133 deletions

View File

@ -28,11 +28,11 @@ public class OptVillagersRadius implements VillagerOptimizerCommand, TabComplete
private final List<String> tabCompletes = List.of("5", "10", "25", "50"); private final List<String> tabCompletes = List.of("5", "10", "25", "50");
private final long cooldown; private final long cooldown;
private final int maxRadius; private final int max_radius;
public OptVillagersRadius() { public OptVillagersRadius() {
Config config = VillagerOptimizer.getConfiguration(); Config config = VillagerOptimizer.getConfiguration();
this.maxRadius = config.getInt("optimization-methods.commands.optimizevillagers.max-block-radius", 100); this.max_radius = config.getInt("optimization-methods.commands.optimizevillagers.max-block-radius", 100);
this.cooldown = config.getInt("optimization-methods.commands.optimizevillagers.cooldown-seconds", 600, """ this.cooldown = config.getInt("optimization-methods.commands.optimizevillagers.cooldown-seconds", 600, """
Cooldown in seconds until a villager can be optimized again using the command.\s Cooldown in seconds until a villager can be optimized again using the command.\s
Here for configuration freedom. Recommended to leave as is to not enable any exploitable behavior.""") * 1000L; Here for configuration freedom. Recommended to leave as is to not enable any exploitable behavior.""") * 1000L;
@ -65,10 +65,10 @@ public class OptVillagersRadius implements VillagerOptimizerCommand, TabComplete
try { try {
int specifiedRadius = Integer.parseInt(args[0]); int specifiedRadius = Integer.parseInt(args[0]);
if (specifiedRadius > maxRadius) { if (specifiedRadius > max_radius) {
final TextReplacementConfig limit = TextReplacementConfig.builder() final TextReplacementConfig limit = TextReplacementConfig.builder()
.matchLiteral("%distance%") .matchLiteral("%distance%")
.replacement(Integer.toString(maxRadius)) .replacement(Integer.toString(max_radius))
.build(); .build();
VillagerOptimizer.getLang(player.locale()).command_radius_limit_exceed.forEach(line -> player.sendMessage(line.replaceText(limit))); VillagerOptimizer.getLang(player.locale()).command_radius_limit_exceed.forEach(line -> player.sendMessage(line.replaceText(limit)));
return true; return true;

View File

@ -26,10 +26,10 @@ import java.util.List;
public class UnOptVillagersRadius implements VillagerOptimizerCommand, TabCompleter { public class UnOptVillagersRadius implements VillagerOptimizerCommand, TabCompleter {
private final List<String> tabCompletes = List.of("5", "10", "25", "50"); private final List<String> tabCompletes = List.of("5", "10", "25", "50");
private final int maxRadius; private final int max_radius;
public UnOptVillagersRadius() { public UnOptVillagersRadius() {
this.maxRadius = VillagerOptimizer.getConfiguration().getInt("optimization-methods.commands.unoptimizevillagers.max-block-radius", 100); this.max_radius = VillagerOptimizer.getConfiguration().getInt("optimization-methods.commands.unoptimizevillagers.max-block-radius", 100);
} }
@Override @Override
@ -59,10 +59,10 @@ public class UnOptVillagersRadius implements VillagerOptimizerCommand, TabComple
try { try {
int specifiedRadius = Integer.parseInt(args[0]); int specifiedRadius = Integer.parseInt(args[0]);
if (specifiedRadius > maxRadius) { if (specifiedRadius > max_radius) {
final TextReplacementConfig limit = TextReplacementConfig.builder() final TextReplacementConfig limit = TextReplacementConfig.builder()
.matchLiteral("%distance%") .matchLiteral("%distance%")
.replacement(Integer.toString(maxRadius)) .replacement(Integer.toString(max_radius))
.build(); .build();
VillagerOptimizer.getLang(player.locale()).command_radius_limit_exceed.forEach(line -> player.sendMessage(line.replaceText(limit))); VillagerOptimizer.getLang(player.locale()).command_radius_limit_exceed.forEach(line -> player.sendMessage(line.replaceText(limit)));
return true; return true;

View File

@ -8,6 +8,7 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.event.HandlerList;
public class DisableSubCmd extends SubCommand { public class DisableSubCmd extends SubCommand {
@ -30,7 +31,9 @@ public class DisableSubCmd extends SubCommand {
public void perform(CommandSender sender, String[] args) { public void perform(CommandSender sender, String[] args) {
if (sender.hasPermission(Permissions.Commands.DISABLE.get())) { if (sender.hasPermission(Permissions.Commands.DISABLE.get())) {
sender.sendMessage(Component.text("Disabling VillagerOptimizer...").color(NamedTextColor.RED)); sender.sendMessage(Component.text("Disabling VillagerOptimizer...").color(NamedTextColor.RED));
VillagerOptimizerModule.modules.forEach(VillagerOptimizerModule::disable); VillagerOptimizer plugin = VillagerOptimizer.getInstance();
HandlerList.unregisterAll(plugin);
plugin.getServer().getScheduler().cancelTasks(plugin);
VillagerOptimizerModule.modules.clear(); VillagerOptimizerModule.modules.clear();
VillagerOptimizer.getCache().cacheMap().clear(); VillagerOptimizer.getCache().cacheMap().clear();
sender.sendMessage(Component.text("Disabled all plugin listeners and tasks.").color(NamedTextColor.GREEN)); sender.sendMessage(Component.text("Disabled all plugin listeners and tasks.").color(NamedTextColor.GREEN));

View File

@ -25,10 +25,10 @@ public class VillagerChunkLimit implements VillagerOptimizerModule, Listener, Ru
private final Server server; private final Server server;
private final VillagerCache villagerCache; private final VillagerCache villagerCache;
private final List<Villager.Profession> removalPriority = new ArrayList<>(16); private final List<Villager.Profession> removal_priority = new ArrayList<>(16);
private final long check_period; private final long check_period;
private final int max_unoptimized_per_chunk, max_optimized_per_chunk; private final int max_unoptimized_per_chunk, max_optimized_per_chunk;
private final boolean logIsEnabled; private final boolean log_enabled;
protected VillagerChunkLimit() { protected VillagerChunkLimit() {
shouldEnable(); shouldEnable();
@ -47,7 +47,7 @@ public class VillagerChunkLimit implements VillagerOptimizerModule, Listener, Ru
Check all loaded chunks every X ticks. 1 second = 20 ticks\s Check all loaded chunks every X ticks. 1 second = 20 ticks\s
A shorter delay in between checks is more efficient but is also more resource intense.\s A shorter delay in between checks is more efficient but is also more resource intense.\s
A larger delay is less resource intense but could become inefficient."""); A larger delay is less resource intense but could become inefficient.""");
this.logIsEnabled = config.getBoolean("villager-chunk-limit.log-removals", false); this.log_enabled = config.getBoolean("villager-chunk-limit.log-removals", false);
config.getList("villager-chunk-limit.removal-priority", List.of( config.getList("villager-chunk-limit.removal-priority", List.of(
"NONE", "NITWIT", "SHEPHERD", "FISHERMAN", "BUTCHER", "CARTOGRAPHER", "LEATHERWORKER", "NONE", "NITWIT", "SHEPHERD", "FISHERMAN", "BUTCHER", "CARTOGRAPHER", "LEATHERWORKER",
"FLETCHER", "MASON", "FARMER", "ARMORER", "TOOLSMITH", "WEAPONSMITH", "CLERIC", "LIBRARIAN" "FLETCHER", "MASON", "FARMER", "ARMORER", "TOOLSMITH", "WEAPONSMITH", "CLERIC", "LIBRARIAN"
@ -57,7 +57,7 @@ public class VillagerChunkLimit implements VillagerOptimizerModule, Listener, Ru
).forEach(configuredProfession -> { ).forEach(configuredProfession -> {
try { try {
Villager.Profession profession = Villager.Profession.valueOf(configuredProfession); Villager.Profession profession = Villager.Profession.valueOf(configuredProfession);
this.removalPriority.add(profession); this.removal_priority.add(profession);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
LogUtil.moduleLog(Level.WARNING, "villager-chunk-limit", LogUtil.moduleLog(Level.WARNING, "villager-chunk-limit",
"Villager profession '"+configuredProfession+"' not recognized. Make sure you're using the correct profession enums."); "Villager profession '"+configuredProfession+"' not recognized. Make sure you're using the correct profession enums.");
@ -128,8 +128,9 @@ public class VillagerChunkLimit implements VillagerOptimizerModule, Listener, Ru
for (int i = 0; i < unoptimized_vils_too_many; i++) { for (int i = 0; i < unoptimized_vils_too_many; i++) {
Villager villager = unoptimized_villagers.get(i); Villager villager = unoptimized_villagers.get(i);
villager.remove(); villager.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, "villager-chunk-limit", if (log_enabled) LogUtil.moduleLog(Level.INFO, "villager-chunk-limit",
"Removed unoptimized villager of profession type '"+villager.getProfession().name()+"' at "+villager.getLocation()); "Removed unoptimized villager of profession type '"+villager.getProfession().name()+"' at "+villager.getLocation()
);
} }
} }
@ -142,14 +143,15 @@ public class VillagerChunkLimit implements VillagerOptimizerModule, Listener, Ru
for (int i = 0; i < optimized_vils_too_many; i++) { for (int i = 0; i < optimized_vils_too_many; i++) {
Villager villager = optimized_villagers.get(i); Villager villager = optimized_villagers.get(i);
villager.remove(); villager.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, "villager-chunk-limit", if (log_enabled) LogUtil.moduleLog(Level.INFO, "villager-chunk-limit",
"Removed optimized villager of profession type '"+villager.getProfession().name()+"' at "+villager.getLocation()); "Removed optimized villager of profession type '"+villager.getProfession().name()+"' at "+villager.getLocation()
);
} }
} }
} }
private int getProfessionPriority(Villager villager) { private int getProfessionPriority(Villager villager) {
final Villager.Profession profession = villager.getProfession(); final Villager.Profession profession = villager.getProfession();
return removalPriority.contains(profession) ? removalPriority.indexOf(profession) : Integer.MAX_VALUE; return removal_priority.contains(profession) ? removal_priority.indexOf(profession) : Integer.MAX_VALUE;
} }
} }

View File

@ -17,7 +17,7 @@ import org.bukkit.event.inventory.TradeSelectEvent;
public class PreventUnoptimizedTrading implements VillagerOptimizerModule, Listener { public class PreventUnoptimizedTrading implements VillagerOptimizerModule, Listener {
private final VillagerCache villagerCache; private final VillagerCache villagerCache;
private final boolean notifyPlayer; private final boolean notify_player;
public PreventUnoptimizedTrading() { public PreventUnoptimizedTrading() {
shouldEnable(); shouldEnable();
@ -27,7 +27,7 @@ public class PreventUnoptimizedTrading implements VillagerOptimizerModule, Liste
Will prevent players from selecting and using trades of unoptimized villagers.\s Will prevent players from selecting and using trades of unoptimized villagers.\s
Use this if you have a lot of villagers and therefore want to force your players to optimize them.\s Use this if you have a lot of villagers and therefore want to force your players to optimize them.\s
Inventories can still be opened so players can move villagers around."""); Inventories can still be opened so players can move villagers around.""");
this.notifyPlayer = config.getBoolean("gameplay.prevent-trading-with-unoptimized.notify-player", true, this.notify_player = config.getBoolean("gameplay.prevent-trading-with-unoptimized.notify-player", true,
"Sends players a message when they try to trade with an unoptimized villager."); "Sends players a message when they try to trade with an unoptimized villager.");
} }
@ -52,7 +52,7 @@ public class PreventUnoptimizedTrading implements VillagerOptimizerModule, Liste
&& !villagerCache.getOrAdd(villager).isOptimized() && !villagerCache.getOrAdd(villager).isOptimized()
) { ) {
event.setCancelled(true); event.setCancelled(true);
if (notifyPlayer) if (notify_player)
VillagerOptimizer.getLang(player.locale()).optimize_for_trading.forEach(player::sendMessage); VillagerOptimizer.getLang(player.locale()).optimize_for_trading.forEach(player::sendMessage);
} }
} }
@ -67,7 +67,7 @@ public class PreventUnoptimizedTrading implements VillagerOptimizerModule, Liste
&& !villagerCache.getOrAdd(villager).isOptimized() && !villagerCache.getOrAdd(villager).isOptimized()
) { ) {
event.setCancelled(true); event.setCancelled(true);
if (notifyPlayer) if (notify_player)
VillagerOptimizer.getLang(player.locale()).optimize_for_trading.forEach(player::sendMessage); VillagerOptimizer.getLang(player.locale()).optimize_for_trading.forEach(player::sendMessage);
} }
} }

View File

@ -21,7 +21,7 @@ public class LevelVillagers implements VillagerOptimizerModule, Listener {
private final VillagerOptimizer plugin; private final VillagerOptimizer plugin;
private final VillagerCache villagerCache; private final VillagerCache villagerCache;
private final boolean shouldNotify; private final boolean notify_player;
private final long cooldown; private final long cooldown;
public LevelVillagers() { public LevelVillagers() {
@ -35,7 +35,7 @@ public class LevelVillagers implements VillagerOptimizerModule, Listener {
this.cooldown = config.getInt("gameplay.villager-leveling.level-check-cooldown-seconds", 5, """ this.cooldown = config.getInt("gameplay.villager-leveling.level-check-cooldown-seconds", 5, """
Cooldown in seconds until the level of a villager will be checked and updated again.\s Cooldown in seconds until the level of a villager will be checked and updated again.\s
Recommended to leave as is.""") * 1000L; Recommended to leave as is.""") * 1000L;
this.shouldNotify = config.getBoolean("gameplay.villager-leveling.notify-player", true, this.notify_player = config.getBoolean("gameplay.villager-leveling.notify-player", true,
"Tell players to wait when a villager is leveling up."); "Tell players to wait when a villager is leveling up.");
} }
@ -70,7 +70,7 @@ public class LevelVillagers implements VillagerOptimizerModule, Listener {
}, 100L); }, 100L);
} }
} else { } else {
if (shouldNotify) { if (notify_player) {
Player player = (Player) event.getPlayer(); Player player = (Player) event.getPlayer();
final TextReplacementConfig timeLeft = TextReplacementConfig.builder() final TextReplacementConfig timeLeft = TextReplacementConfig.builder()
.matchLiteral("%time%") .matchLiteral("%time%")

View File

@ -20,7 +20,7 @@ public class RestockTrades implements VillagerOptimizerModule, Listener {
private final VillagerCache villagerCache; private final VillagerCache villagerCache;
private final long restock_delay_millis; private final long restock_delay_millis;
private final boolean shouldLog, notifyPlayer; private final boolean log_enabled, notify_player;
public RestockTrades() { public RestockTrades() {
shouldEnable(); shouldEnable();
@ -31,9 +31,9 @@ public class RestockTrades implements VillagerOptimizerModule, Listener {
Don't have enough AI to do trade restocks themselves, so this needs to always be enabled."""); Don't have enough AI to do trade restocks themselves, so this needs to always be enabled.""");
this.restock_delay_millis = config.getInt("gameplay.trade-restocking.delay-in-ticks", 1000, this.restock_delay_millis = config.getInt("gameplay.trade-restocking.delay-in-ticks", 1000,
"1 second = 20 ticks. There are 24.000 ticks in a single minecraft day.") * 50L; "1 second = 20 ticks. There are 24.000 ticks in a single minecraft day.") * 50L;
this.notifyPlayer = config.getBoolean("gameplay.trade-restocking.notify-player", true, this.notify_player = config.getBoolean("gameplay.trade-restocking.notify-player", true,
"Sends the player a message when the trades were restocked on a clicked villager."); "Sends the player a message when the trades were restocked on a clicked villager.");
this.shouldLog = config.getBoolean("gameplay.trade-restocking.log", false); this.log_enabled = config.getBoolean("gameplay.trade-restocking.log", false);
} }
@Override @Override
@ -60,14 +60,14 @@ public class RestockTrades implements VillagerOptimizerModule, Listener {
if (wVillager.canRestock(restock_delay_millis) || player_bypassing) { if (wVillager.canRestock(restock_delay_millis) || player_bypassing) {
wVillager.restock(); wVillager.restock();
wVillager.saveRestockTime(); wVillager.saveRestockTime();
if (notifyPlayer && !player_bypassing) { if (notify_player && !player_bypassing) {
final TextReplacementConfig timeLeft = TextReplacementConfig.builder() final TextReplacementConfig timeLeft = TextReplacementConfig.builder()
.matchLiteral("%time%") .matchLiteral("%time%")
.replacement(CommonUtil.formatTime(wVillager.getRestockCooldownMillis(restock_delay_millis))) .replacement(CommonUtil.formatTime(wVillager.getRestockCooldownMillis(restock_delay_millis)))
.build(); .build();
VillagerOptimizer.getLang(player.locale()).trades_restocked.forEach(line -> player.sendMessage(line.replaceText(timeLeft))); VillagerOptimizer.getLang(player.locale()).trades_restocked.forEach(line -> player.sendMessage(line.replaceText(timeLeft)));
} }
if (shouldLog) if (log_enabled)
VillagerOptimizer.getLog().info("Restocked optimized villager at "+ wVillager.villager().getLocation()); VillagerOptimizer.getLog().info("Restocked optimized villager at "+ wVillager.villager().getLocation());
} }
} }

View File

@ -34,7 +34,7 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
private final HashSet<Material> blocks_that_disable = new HashSet<>(4); private final HashSet<Material> blocks_that_disable = new HashSet<>(4);
private final long cooldown; private final long cooldown;
private final double search_radius; private final double search_radius;
private final boolean onlyWhileSneaking, shouldNotifyPlayer, shouldLog; private final boolean only_while_sneaking, notify_player, log_enabled;
public OptimizeByBlock() { public OptimizeByBlock() {
shouldEnable(); shouldEnable();
@ -60,11 +60,11 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
this.search_radius = config.getDouble("optimization-methods.block-optimization.search-radius-in-blocks", 2.0, """ this.search_radius = config.getDouble("optimization-methods.block-optimization.search-radius-in-blocks", 2.0, """
The radius in blocks a villager can be away from the player when he places an optimize block.\s The radius in blocks a villager can be away from the player when he places an optimize block.\s
The closest unoptimized villager to the player will be optimized.""") / 2; The closest unoptimized villager to the player will be optimized.""") / 2;
this.onlyWhileSneaking = config.getBoolean("optimization-methods.block-optimization.only-when-sneaking", true, this.only_while_sneaking = config.getBoolean("optimization-methods.block-optimization.only-when-sneaking", true,
"Only optimize/unoptimize by workstation when player is sneaking during place or break."); "Only optimize/unoptimize by workstation when player is sneaking during place or break.");
this.shouldNotifyPlayer = config.getBoolean("optimization-methods.block-optimization.notify-player", true, this.notify_player = config.getBoolean("optimization-methods.block-optimization.notify-player", true,
"Sends players a message when they successfully optimized or unoptimized a villager."); "Sends players a message when they successfully optimized or unoptimized a villager.");
this.shouldLog = config.getBoolean("optimization-methods.block-optimization.log", false); this.log_enabled = config.getBoolean("optimization-methods.block-optimization.log", false);
} }
@Override @Override
@ -84,7 +84,7 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
if (!blocks_that_disable.contains(placed.getType())) return; if (!blocks_that_disable.contains(placed.getType())) return;
Player player = event.getPlayer(); Player player = event.getPlayer();
if (!player.hasPermission(Permissions.Optimize.BLOCK.get())) return; if (!player.hasPermission(Permissions.Optimize.BLOCK.get())) return;
if (onlyWhileSneaking && !player.isSneaking()) return; if (only_while_sneaking && !player.isSneaking()) return;
final Location blockLoc = placed.getLocation(); final Location blockLoc = placed.getLocation();
WrappedVillager closestOptimizableVillager = null; WrappedVillager closestOptimizableVillager = null;
@ -115,7 +115,7 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
closestOptimizableVillager.setOptimization(optimizeEvent.getOptimizationType()); closestOptimizableVillager.setOptimization(optimizeEvent.getOptimizationType());
closestOptimizableVillager.saveOptimizeTime(); closestOptimizableVillager.saveOptimizeTime();
if (shouldNotifyPlayer) { if (notify_player) {
final TextReplacementConfig vilProfession = TextReplacementConfig.builder() final TextReplacementConfig vilProfession = TextReplacementConfig.builder()
.matchLiteral("%vil_profession%") .matchLiteral("%vil_profession%")
.replacement(closestOptimizableVillager.villager().getProfession().toString().toLowerCase()) .replacement(closestOptimizableVillager.villager().getProfession().toString().toLowerCase())
@ -129,10 +129,10 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
.replaceText(placedMaterial) .replaceText(placedMaterial)
)); ));
} }
if (shouldLog) if (log_enabled)
VillagerOptimizer.getLog().info("Villager was optimized by block at "+closestOptimizableVillager.villager().getLocation()); VillagerOptimizer.getLog().info("Villager was optimized by block at "+closestOptimizableVillager.villager().getLocation());
} else { } else {
if (shouldNotifyPlayer) { if (notify_player) {
final TextReplacementConfig timeLeft = TextReplacementConfig.builder() final TextReplacementConfig timeLeft = TextReplacementConfig.builder()
.matchLiteral("%time%") .matchLiteral("%time%")
.replacement(CommonUtil.formatTime(closestOptimizableVillager.getOptimizeCooldownMillis(cooldown))) .replacement(CommonUtil.formatTime(closestOptimizableVillager.getOptimizeCooldownMillis(cooldown)))
@ -148,7 +148,7 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
if (!blocks_that_disable.contains(broken.getType())) return; if (!blocks_that_disable.contains(broken.getType())) return;
Player player = event.getPlayer(); Player player = event.getPlayer();
if (!player.hasPermission(Permissions.Optimize.BLOCK.get())) return; if (!player.hasPermission(Permissions.Optimize.BLOCK.get())) return;
if (onlyWhileSneaking && !player.isSneaking()) return; if (only_while_sneaking && !player.isSneaking()) return;
final Location blockLoc = broken.getLocation(); final Location blockLoc = broken.getLocation();
WrappedVillager closestOptimizedVillager = null; WrappedVillager closestOptimizedVillager = null;
@ -175,7 +175,7 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
closestOptimizedVillager.setOptimization(OptimizationType.NONE); closestOptimizedVillager.setOptimization(OptimizationType.NONE);
if (shouldNotifyPlayer) { if (notify_player) {
final TextReplacementConfig vilProfession = TextReplacementConfig.builder() final TextReplacementConfig vilProfession = TextReplacementConfig.builder()
.matchLiteral("%vil_profession%") .matchLiteral("%vil_profession%")
.replacement(closestOptimizedVillager.villager().getProfession().toString().toLowerCase()) .replacement(closestOptimizedVillager.villager().getProfession().toString().toLowerCase())
@ -189,7 +189,7 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
.replaceText(brokenMaterial) .replaceText(brokenMaterial)
)); ));
} }
if (shouldLog) if (log_enabled)
VillagerOptimizer.getLog().info("Villager unoptimized because nearby optimization block broken at: "+closestOptimizedVillager.villager().getLocation()); VillagerOptimizer.getLog().info("Villager unoptimized because nearby optimization block broken at: "+closestOptimizedVillager.villager().getLocation());
} }
} }

View File

@ -32,7 +32,7 @@ public class OptimizeByNametag implements VillagerOptimizerModule, Listener {
private final VillagerCache villagerCache; private final VillagerCache villagerCache;
private final HashSet<String> nametags = new HashSet<>(4); private final HashSet<String> nametags = new HashSet<>(4);
private final long cooldown; private final long cooldown;
private final boolean consumeNametag, shouldNotifyPlayer, shouldLog; private final boolean consume_nametag, notify_player, log_enabled;
public OptimizeByNametag() { public OptimizeByNametag() {
shouldEnable(); shouldEnable();
@ -43,14 +43,14 @@ public class OptimizeByNametag implements VillagerOptimizerModule, Listener {
Nametag optimized villagers will be unoptimized again when they are renamed to something else."""); Nametag optimized villagers will be unoptimized again when they are renamed to something else.""");
this.nametags.addAll(config.getList("optimization-methods.nametag-optimization.names", List.of("Optimize", "DisableAI"), this.nametags.addAll(config.getList("optimization-methods.nametag-optimization.names", List.of("Optimize", "DisableAI"),
"Names are case insensitive, capital letters won't matter.").stream().map(String::toLowerCase).toList()); "Names are case insensitive, capital letters won't matter.").stream().map(String::toLowerCase).toList());
this.consumeNametag = config.getBoolean("optimization-methods.nametag-optimization.nametags-get-consumed", true, this.consume_nametag = config.getBoolean("optimization-methods.nametag-optimization.nametags-get-consumed", true,
"Enable or disable consumption of the used nametag item."); "Enable or disable consumption of the used nametag item.");
this.cooldown = config.getInt("optimization-methods.nametag-optimization.optimize-cooldown-seconds", 600, """ this.cooldown = config.getInt("optimization-methods.nametag-optimization.optimize-cooldown-seconds", 600, """
Cooldown in seconds until a villager can be optimized again using a nametag.\s Cooldown in seconds until a villager can be optimized again using a nametag.\s
Here for configuration freedom. Recommended to leave as is to not enable any exploitable behavior.""") * 1000L; Here for configuration freedom. Recommended to leave as is to not enable any exploitable behavior.""") * 1000L;
this.shouldNotifyPlayer = config.getBoolean("optimization-methods.nametag-optimization.notify-player", true, this.notify_player = config.getBoolean("optimization-methods.nametag-optimization.notify-player", true,
"Sends players a message when they successfully optimized a villager."); "Sends players a message when they successfully optimized a villager.");
this.shouldLog = config.getBoolean("optimization-methods.nametag-optimization.log", false); this.log_enabled = config.getBoolean("optimization-methods.nametag-optimization.log", false);
} }
@Override @Override
@ -88,7 +88,7 @@ public class OptimizeByNametag implements VillagerOptimizerModule, Listener {
VillagerOptimizer.callEvent(optimizeEvent); VillagerOptimizer.callEvent(optimizeEvent);
if (optimizeEvent.isCancelled()) return; if (optimizeEvent.isCancelled()) return;
if (!consumeNametag) { if (!consume_nametag) {
event.setCancelled(true); event.setCancelled(true);
villager.customName(newVillagerName); villager.customName(newVillagerName);
} }
@ -96,13 +96,13 @@ public class OptimizeByNametag implements VillagerOptimizerModule, Listener {
wVillager.setOptimization(optimizeEvent.getOptimizationType()); wVillager.setOptimization(optimizeEvent.getOptimizationType());
wVillager.saveOptimizeTime(); wVillager.saveOptimizeTime();
if (shouldNotifyPlayer) if (notify_player)
VillagerOptimizer.getLang(player.locale()).nametag_optimize_success.forEach(player::sendMessage); VillagerOptimizer.getLang(player.locale()).nametag_optimize_success.forEach(player::sendMessage);
if (shouldLog) if (log_enabled)
VillagerOptimizer.getLog().info(player.getName() + " optimized a villager using nametag: '" + name + "'"); VillagerOptimizer.getLog().info(player.getName() + " optimized a villager using nametag: '" + name + "'");
} else { } else {
event.setCancelled(true); event.setCancelled(true);
if (shouldNotifyPlayer) { if (notify_player) {
final TextReplacementConfig timeLeft = TextReplacementConfig.builder() final TextReplacementConfig timeLeft = TextReplacementConfig.builder()
.matchLiteral("%time%") .matchLiteral("%time%")
.replacement(CommonUtil.formatTime(wVillager.getOptimizeCooldownMillis(cooldown))) .replacement(CommonUtil.formatTime(wVillager.getOptimizeCooldownMillis(cooldown)))
@ -118,9 +118,9 @@ public class OptimizeByNametag implements VillagerOptimizerModule, Listener {
wVillager.setOptimization(OptimizationType.NONE); wVillager.setOptimization(OptimizationType.NONE);
if (shouldNotifyPlayer) if (notify_player)
VillagerOptimizer.getLang(player.locale()).nametag_unoptimize_success.forEach(player::sendMessage); VillagerOptimizer.getLang(player.locale()).nametag_unoptimize_success.forEach(player::sendMessage);
if (shouldLog) if (log_enabled)
VillagerOptimizer.getLog().info(event.getPlayer().getName() + " disabled optimizations for a villager using nametag: '" + name + "'"); VillagerOptimizer.getLog().info(event.getPlayer().getName() + " disabled optimizations for a villager using nametag: '" + name + "'");
} }
} }

View File

@ -29,7 +29,7 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
private final VillagerCache villagerCache; private final VillagerCache villagerCache;
private final long cooldown; private final long cooldown;
private final double search_radius; private final double search_radius;
private final boolean onlyWhileSneaking, shouldLog, shouldNotifyPlayer; private final boolean only_while_sneaking, log_enabled, notify_player;
public OptimizeByWorkstation() { public OptimizeByWorkstation() {
shouldEnable(); shouldEnable();
@ -44,11 +44,11 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
this.cooldown = config.getInt("optimization-methods.workstation-optimization.optimize-cooldown-seconds", 600, """ this.cooldown = config.getInt("optimization-methods.workstation-optimization.optimize-cooldown-seconds", 600, """
Cooldown in seconds until a villager can be optimized again using a workstation.\s Cooldown in seconds until a villager can be optimized again using a workstation.\s
Here for configuration freedom. Recommended to leave as is to not enable any exploitable behavior.""") * 1000L; Here for configuration freedom. Recommended to leave as is to not enable any exploitable behavior.""") * 1000L;
this.onlyWhileSneaking = config.getBoolean("optimization-methods.workstation-optimization.only-when-sneaking", true, this.only_while_sneaking = config.getBoolean("optimization-methods.workstation-optimization.only-when-sneaking", true,
"Only optimize/unoptimize by workstation when player is sneaking during place or break"); "Only optimize/unoptimize by workstation when player is sneaking during place or break");
this.shouldNotifyPlayer = config.getBoolean("optimization-methods.workstation-optimization.notify-player", true, this.notify_player = config.getBoolean("optimization-methods.workstation-optimization.notify-player", true,
"Sends players a message when they successfully optimized a villager."); "Sends players a message when they successfully optimized a villager.");
this.shouldLog = config.getBoolean("optimization-methods.workstation-optimization.log", false); this.log_enabled = config.getBoolean("optimization-methods.workstation-optimization.log", false);
} }
@Override @Override
@ -69,7 +69,7 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
if (workstationProfession.equals(Villager.Profession.NONE)) return; if (workstationProfession.equals(Villager.Profession.NONE)) return;
Player player = event.getPlayer(); Player player = event.getPlayer();
if (!player.hasPermission(Permissions.Optimize.WORKSTATION.get())) return; if (!player.hasPermission(Permissions.Optimize.WORKSTATION.get())) return;
if (onlyWhileSneaking && !player.isSneaking()) return; if (only_while_sneaking && !player.isSneaking()) return;
final Location workstationLoc = placed.getLocation(); final Location workstationLoc = placed.getLocation();
WrappedVillager closestOptimizableVillager = null; WrappedVillager closestOptimizableVillager = null;
@ -99,7 +99,7 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
closestOptimizableVillager.setOptimization(optimizeEvent.getOptimizationType()); closestOptimizableVillager.setOptimization(optimizeEvent.getOptimizationType());
closestOptimizableVillager.saveOptimizeTime(); closestOptimizableVillager.saveOptimizeTime();
if (shouldNotifyPlayer) { if (notify_player) {
final TextReplacementConfig vilProfession = TextReplacementConfig.builder() final TextReplacementConfig vilProfession = TextReplacementConfig.builder()
.matchLiteral("%vil_profession%") .matchLiteral("%vil_profession%")
.replacement(closestOptimizableVillager.villager().getProfession().toString().toLowerCase()) .replacement(closestOptimizableVillager.villager().getProfession().toString().toLowerCase())
@ -113,10 +113,10 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
.replaceText(placedWorkstation) .replaceText(placedWorkstation)
)); ));
} }
if (shouldLog) if (log_enabled)
VillagerOptimizer.getLog().info(player.getName() + " optimized a villager using workstation: '" + placed.getType().toString().toLowerCase() + "'"); VillagerOptimizer.getLog().info(player.getName() + " optimized a villager using workstation: '" + placed.getType().toString().toLowerCase() + "'");
} else { } else {
if (shouldNotifyPlayer) { if (notify_player) {
final TextReplacementConfig timeLeft = TextReplacementConfig.builder() final TextReplacementConfig timeLeft = TextReplacementConfig.builder()
.matchLiteral("%time%") .matchLiteral("%time%")
.replacement(CommonUtil.formatTime(closestOptimizableVillager.getOptimizeCooldownMillis(cooldown))) .replacement(CommonUtil.formatTime(closestOptimizableVillager.getOptimizeCooldownMillis(cooldown)))
@ -135,7 +135,7 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
if (workstationProfession.equals(Villager.Profession.NONE)) return; if (workstationProfession.equals(Villager.Profession.NONE)) return;
Player player = event.getPlayer(); Player player = event.getPlayer();
if (!player.hasPermission(Permissions.Optimize.WORKSTATION.get())) return; if (!player.hasPermission(Permissions.Optimize.WORKSTATION.get())) return;
if (onlyWhileSneaking && !player.isSneaking()) return; if (only_while_sneaking && !player.isSneaking()) return;
final Location workstationLoc = broken.getLocation(); final Location workstationLoc = broken.getLocation();
WrappedVillager closestOptimizedVillager = null; WrappedVillager closestOptimizedVillager = null;
@ -163,7 +163,7 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
closestOptimizedVillager.setOptimization(OptimizationType.NONE); closestOptimizedVillager.setOptimization(OptimizationType.NONE);
if (shouldNotifyPlayer) { if (notify_player) {
final TextReplacementConfig vilProfession = TextReplacementConfig.builder() final TextReplacementConfig vilProfession = TextReplacementConfig.builder()
.matchLiteral("%vil_profession%") .matchLiteral("%vil_profession%")
.replacement(closestOptimizedVillager.villager().getProfession().toString().toLowerCase()) .replacement(closestOptimizedVillager.villager().getProfession().toString().toLowerCase())
@ -177,7 +177,7 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
.replaceText(brokenWorkstation) .replaceText(brokenWorkstation)
)); ));
} }
if (shouldLog) if (log_enabled)
VillagerOptimizer.getLog().info(player.getName() + " unoptimized a villager by breaking workstation: '" + broken.getType().toString().toLowerCase() + "'"); VillagerOptimizer.getLog().info(player.getName() + " unoptimized a villager by breaking workstation: '" + broken.getType().toString().toLowerCase() + "'");
} }

View File

@ -28,11 +28,11 @@ public class OptVillagersRadius implements VillagerOptimizerCommand, TabComplete
private final List<String> tabCompletes = List.of("5", "10", "25", "50"); private final List<String> tabCompletes = List.of("5", "10", "25", "50");
private final long cooldown; private final long cooldown;
private final int maxRadius; private final int max_radius;
public OptVillagersRadius() { public OptVillagersRadius() {
Config config = VillagerOptimizer.getConfiguration(); Config config = VillagerOptimizer.getConfiguration();
this.maxRadius = config.getInt("optimization-methods.commands.optimizevillagers.max-block-radius", 100); this.max_radius = config.getInt("optimization-methods.commands.optimizevillagers.max-block-radius", 100);
this.cooldown = config.getInt("optimization-methods.commands.optimizevillagers.cooldown-seconds", 600, """ this.cooldown = config.getInt("optimization-methods.commands.optimizevillagers.cooldown-seconds", 600, """
Cooldown in seconds until a villager can be optimized again using the command.\s Cooldown in seconds until a villager can be optimized again using the command.\s
Here for configuration freedom. Recommended to leave as is to not enable any exploitable behavior.""") * 1000L; Here for configuration freedom. Recommended to leave as is to not enable any exploitable behavior.""") * 1000L;
@ -65,10 +65,10 @@ public class OptVillagersRadius implements VillagerOptimizerCommand, TabComplete
try { try {
int specifiedRadius = Integer.parseInt(args[0]); int specifiedRadius = Integer.parseInt(args[0]);
if (specifiedRadius > maxRadius) { if (specifiedRadius > max_radius) {
final TextReplacementConfig limit = TextReplacementConfig.builder() final TextReplacementConfig limit = TextReplacementConfig.builder()
.matchLiteral("%distance%") .matchLiteral("%distance%")
.replacement(Integer.toString(maxRadius)) .replacement(Integer.toString(max_radius))
.build(); .build();
VillagerOptimizer.getLang(player.locale()).command_radius_limit_exceed.forEach(line -> player.sendMessage(line.replaceText(limit))); VillagerOptimizer.getLang(player.locale()).command_radius_limit_exceed.forEach(line -> player.sendMessage(line.replaceText(limit)));
return true; return true;

View File

@ -26,10 +26,10 @@ import java.util.List;
public class UnOptVillagersRadius implements VillagerOptimizerCommand, TabCompleter { public class UnOptVillagersRadius implements VillagerOptimizerCommand, TabCompleter {
private final List<String> tabCompletes = List.of("5", "10", "25", "50"); private final List<String> tabCompletes = List.of("5", "10", "25", "50");
private final int maxRadius; private final int max_radius;
public UnOptVillagersRadius() { public UnOptVillagersRadius() {
this.maxRadius = VillagerOptimizer.getConfiguration().getInt("optimization-methods.commands.unoptimizevillagers.max-block-radius", 100); this.max_radius = VillagerOptimizer.getConfiguration().getInt("optimization-methods.commands.unoptimizevillagers.max-block-radius", 100);
} }
@Override @Override
@ -59,10 +59,10 @@ public class UnOptVillagersRadius implements VillagerOptimizerCommand, TabComple
try { try {
int specifiedRadius = Integer.parseInt(args[0]); int specifiedRadius = Integer.parseInt(args[0]);
if (specifiedRadius > maxRadius) { if (specifiedRadius > max_radius) {
final TextReplacementConfig limit = TextReplacementConfig.builder() final TextReplacementConfig limit = TextReplacementConfig.builder()
.matchLiteral("%distance%") .matchLiteral("%distance%")
.replacement(Integer.toString(maxRadius)) .replacement(Integer.toString(max_radius))
.build(); .build();
VillagerOptimizer.getLang(player.locale()).command_radius_limit_exceed.forEach(line -> player.sendMessage(line.replaceText(limit))); VillagerOptimizer.getLang(player.locale()).command_radius_limit_exceed.forEach(line -> player.sendMessage(line.replaceText(limit)));
return true; return true;

View File

@ -6,6 +6,8 @@ import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.config.Config; import me.xginko.villageroptimizer.config.Config;
import me.xginko.villageroptimizer.utils.LogUtil; import me.xginko.villageroptimizer.utils.LogUtil;
import org.bukkit.Chunk; import org.bukkit.Chunk;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Villager; import org.bukkit.entity.Villager;
@ -23,17 +25,17 @@ import java.util.logging.Level;
public class VillagerChunkLimit implements VillagerOptimizerModule, Listener { public class VillagerChunkLimit implements VillagerOptimizerModule, Listener {
private final VillagerOptimizer plugin; private final Server server;
private final VillagerCache villagerCache; private final VillagerCache villagerCache;
private ScheduledTask scheduledTask; private ScheduledTask periodic_chunk_check;
private final List<Villager.Profession> removalPriority = new ArrayList<>(16); private final List<Villager.Profession> removal_priority = new ArrayList<>(16);
private final long check_period; private final long check_period;
private final int max_unoptimized_per_chunk, max_optimized_per_chunk; private final int max_unoptimized_per_chunk, max_optimized_per_chunk;
private final boolean logIsEnabled; private final boolean log_enabled;
protected VillagerChunkLimit() { protected VillagerChunkLimit() {
shouldEnable(); shouldEnable();
this.plugin = VillagerOptimizer.getInstance(); this.server = VillagerOptimizer.getInstance().getServer();
this.villagerCache = VillagerOptimizer.getCache(); this.villagerCache = VillagerOptimizer.getCache();
Config config = VillagerOptimizer.getConfiguration(); Config config = VillagerOptimizer.getConfiguration();
config.addComment("villager-chunk-limit.enable", """ config.addComment("villager-chunk-limit.enable", """
@ -48,7 +50,7 @@ public class VillagerChunkLimit implements VillagerOptimizerModule, Listener {
Check all loaded chunks every X ticks. 1 second = 20 ticks\s Check all loaded chunks every X ticks. 1 second = 20 ticks\s
A shorter delay in between checks is more efficient but is also more resource intense.\s A shorter delay in between checks is more efficient but is also more resource intense.\s
A larger delay is less resource intense but could become inefficient."""); A larger delay is less resource intense but could become inefficient.""");
this.logIsEnabled = config.getBoolean("villager-chunk-limit.log-removals", false); this.log_enabled = config.getBoolean("villager-chunk-limit.log-removals", false);
config.getList("villager-chunk-limit.removal-priority", List.of( config.getList("villager-chunk-limit.removal-priority", List.of(
"NONE", "NITWIT", "SHEPHERD", "FISHERMAN", "BUTCHER", "CARTOGRAPHER", "LEATHERWORKER", "NONE", "NITWIT", "SHEPHERD", "FISHERMAN", "BUTCHER", "CARTOGRAPHER", "LEATHERWORKER",
"FLETCHER", "MASON", "FARMER", "ARMORER", "TOOLSMITH", "WEAPONSMITH", "CLERIC", "LIBRARIAN" "FLETCHER", "MASON", "FARMER", "ARMORER", "TOOLSMITH", "WEAPONSMITH", "CLERIC", "LIBRARIAN"
@ -58,7 +60,7 @@ public class VillagerChunkLimit implements VillagerOptimizerModule, Listener {
).forEach(configuredProfession -> { ).forEach(configuredProfession -> {
try { try {
Villager.Profession profession = Villager.Profession.valueOf(configuredProfession); Villager.Profession profession = Villager.Profession.valueOf(configuredProfession);
this.removalPriority.add(profession); this.removal_priority.add(profession);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
LogUtil.moduleLog(Level.WARNING, "villager-chunk-limit", LogUtil.moduleLog(Level.WARNING, "villager-chunk-limit",
"Villager profession '"+configuredProfession+"' not recognized. Make sure you're using the correct profession enums."); "Villager profession '"+configuredProfession+"' not recognized. Make sure you're using the correct profession enums.");
@ -68,12 +70,16 @@ public class VillagerChunkLimit implements VillagerOptimizerModule, Listener {
@Override @Override
public void enable() { public void enable() {
plugin.getServer().getPluginManager().registerEvents(this, plugin); VillagerOptimizer plugin = VillagerOptimizer.getInstance();
this.scheduledTask = plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, periodic_chunk_check -> { server.getPluginManager().registerEvents(this, plugin);
plugin.getServer().getWorlds().forEach(world -> { this.periodic_chunk_check = server.getGlobalRegionScheduler().runAtFixedRate(plugin, periodic_chunk_check -> {
for (Chunk chunk : world.getLoadedChunks()) for (World world : server.getWorlds()) {
plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), check_chunk -> checkVillagersInChunk(chunk)); for (Chunk chunk : world.getLoadedChunks()) {
}); plugin.getServer().getRegionScheduler().run(
plugin, world, chunk.getX(), chunk.getZ(), check_chunk -> checkVillagersInChunk(chunk)
);
}
}
}, check_period, check_period); }, check_period, check_period);
} }
@ -85,7 +91,7 @@ public class VillagerChunkLimit implements VillagerOptimizerModule, Listener {
@Override @Override
public void disable() { public void disable() {
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
if (scheduledTask != null) scheduledTask.cancel(); if (periodic_chunk_check != null) periodic_chunk_check.cancel();
} }
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
@ -130,8 +136,9 @@ public class VillagerChunkLimit implements VillagerOptimizerModule, Listener {
for (int i = 0; i < unoptimized_vils_too_many; i++) { for (int i = 0; i < unoptimized_vils_too_many; i++) {
Villager villager = unoptimized_villagers.get(i); Villager villager = unoptimized_villagers.get(i);
villager.remove(); villager.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, "villager-chunk-limit", if (log_enabled) LogUtil.moduleLog(Level.INFO, "villager-chunk-limit",
"Removed unoptimized villager of profession type '"+villager.getProfession().name()+"' at "+villager.getLocation()); "Removed unoptimized villager of profession type '"+villager.getProfession().name()+"' at "+villager.getLocation()
);
} }
} }
@ -144,14 +151,15 @@ public class VillagerChunkLimit implements VillagerOptimizerModule, Listener {
for (int i = 0; i < optimized_vils_too_many; i++) { for (int i = 0; i < optimized_vils_too_many; i++) {
Villager villager = optimized_villagers.get(i); Villager villager = optimized_villagers.get(i);
villager.remove(); villager.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, "villager-chunk-limit", if (log_enabled) LogUtil.moduleLog(Level.INFO, "villager-chunk-limit",
"Removed optimized villager of profession type '"+villager.getProfession().name()+"' at "+villager.getLocation()); "Removed optimized villager of profession type '"+villager.getProfession().name()+"' at "+villager.getLocation()
);
} }
} }
} }
private int getProfessionPriority(Villager villager) { private int getProfessionPriority(Villager villager) {
final Villager.Profession profession = villager.getProfession(); final Villager.Profession profession = villager.getProfession();
return removalPriority.contains(profession) ? removalPriority.indexOf(profession) : Integer.MAX_VALUE; return removal_priority.contains(profession) ? removal_priority.indexOf(profession) : Integer.MAX_VALUE;
} }
} }

View File

@ -18,7 +18,7 @@ import org.bukkit.event.inventory.TradeSelectEvent;
public class PreventUnoptimizedTrading implements VillagerOptimizerModule, Listener { public class PreventUnoptimizedTrading implements VillagerOptimizerModule, Listener {
private final VillagerCache villagerCache; private final VillagerCache villagerCache;
private final boolean notifyPlayer; private final boolean notify_player;
public PreventUnoptimizedTrading() { public PreventUnoptimizedTrading() {
shouldEnable(); shouldEnable();
@ -28,7 +28,7 @@ public class PreventUnoptimizedTrading implements VillagerOptimizerModule, Liste
Will prevent players from selecting and using trades of unoptimized villagers.\s Will prevent players from selecting and using trades of unoptimized villagers.\s
Use this if you have a lot of villagers and therefore want to force your players to optimize them.\s Use this if you have a lot of villagers and therefore want to force your players to optimize them.\s
Inventories can still be opened so players can move villagers around."""); Inventories can still be opened so players can move villagers around.""");
this.notifyPlayer = config.getBoolean("gameplay.prevent-trading-with-unoptimized.notify-player", true, this.notify_player = config.getBoolean("gameplay.prevent-trading-with-unoptimized.notify-player", true,
"Sends players a message when they try to trade with an unoptimized villager."); "Sends players a message when they try to trade with an unoptimized villager.");
} }
@ -58,7 +58,7 @@ public class PreventUnoptimizedTrading implements VillagerOptimizerModule, Liste
&& !villagerCache.getOrAdd(villager).isOptimized() && !villagerCache.getOrAdd(villager).isOptimized()
) { ) {
event.setCancelled(true); event.setCancelled(true);
if (notifyPlayer) if (notify_player)
VillagerOptimizer.getLang(player.locale()).optimize_for_trading.forEach(player::sendMessage); VillagerOptimizer.getLang(player.locale()).optimize_for_trading.forEach(player::sendMessage);
} }
} }
@ -73,7 +73,7 @@ public class PreventUnoptimizedTrading implements VillagerOptimizerModule, Liste
&& !villagerCache.getOrAdd(villager).isOptimized() && !villagerCache.getOrAdd(villager).isOptimized()
) { ) {
event.setCancelled(true); event.setCancelled(true);
if (notifyPlayer) if (notify_player)
VillagerOptimizer.getLang(player.locale()).optimize_for_trading.forEach(player::sendMessage); VillagerOptimizer.getLang(player.locale()).optimize_for_trading.forEach(player::sendMessage);
} }
} }

View File

@ -22,7 +22,7 @@ public class LevelVillagers implements VillagerOptimizerModule, Listener {
private final VillagerOptimizer plugin; private final VillagerOptimizer plugin;
private final VillagerCache villagerCache; private final VillagerCache villagerCache;
private final boolean shouldNotify; private final boolean notify_player;
private final long cooldown; private final long cooldown;
public LevelVillagers() { public LevelVillagers() {
@ -36,7 +36,7 @@ public class LevelVillagers implements VillagerOptimizerModule, Listener {
this.cooldown = config.getInt("gameplay.villager-leveling.level-check-cooldown-seconds", 5, """ this.cooldown = config.getInt("gameplay.villager-leveling.level-check-cooldown-seconds", 5, """
Cooldown in seconds until the level of a villager will be checked and updated again.\s Cooldown in seconds until the level of a villager will be checked and updated again.\s
Recommended to leave as is.""") * 1000L; Recommended to leave as is.""") * 1000L;
this.shouldNotify = config.getBoolean("gameplay.villager-leveling.notify-player", true, this.notify_player = config.getBoolean("gameplay.villager-leveling.notify-player", true,
"Tell players to wait when a villager is leveling up."); "Tell players to wait when a villager is leveling up.");
} }
@ -77,7 +77,7 @@ public class LevelVillagers implements VillagerOptimizerModule, Listener {
}, null, 100L); }, null, 100L);
} }
} else { } else {
if (shouldNotify) { if (notify_player) {
Player player = (Player) event.getPlayer(); Player player = (Player) event.getPlayer();
final TextReplacementConfig timeLeft = TextReplacementConfig.builder() final TextReplacementConfig timeLeft = TextReplacementConfig.builder()
.matchLiteral("%time%") .matchLiteral("%time%")

View File

@ -21,7 +21,7 @@ public class RestockTrades implements VillagerOptimizerModule, Listener {
private final VillagerCache villagerCache; private final VillagerCache villagerCache;
private final long restock_delay_millis; private final long restock_delay_millis;
private final boolean shouldLog, notifyPlayer; private final boolean log_enabled, notify_player;
public RestockTrades() { public RestockTrades() {
shouldEnable(); shouldEnable();
@ -32,9 +32,9 @@ public class RestockTrades implements VillagerOptimizerModule, Listener {
Don't have enough AI to do trade restocks themselves, so this needs to always be enabled."""); Don't have enough AI to do trade restocks themselves, so this needs to always be enabled.""");
this.restock_delay_millis = config.getInt("gameplay.trade-restocking.delay-in-ticks", 1000, this.restock_delay_millis = config.getInt("gameplay.trade-restocking.delay-in-ticks", 1000,
"1 second = 20 ticks. There are 24.000 ticks in a single minecraft day.") * 50L; "1 second = 20 ticks. There are 24.000 ticks in a single minecraft day.") * 50L;
this.notifyPlayer = config.getBoolean("gameplay.trade-restocking.notify-player", true, this.notify_player = config.getBoolean("gameplay.trade-restocking.notify-player", true,
"Sends the player a message when the trades were restocked on a clicked villager."); "Sends the player a message when the trades were restocked on a clicked villager.");
this.shouldLog = config.getBoolean("gameplay.trade-restocking.log", false); this.log_enabled = config.getBoolean("gameplay.trade-restocking.log", false);
} }
@Override @Override
@ -66,14 +66,14 @@ public class RestockTrades implements VillagerOptimizerModule, Listener {
if (wVillager.canRestock(restock_delay_millis) || player_bypassing) { if (wVillager.canRestock(restock_delay_millis) || player_bypassing) {
wVillager.restock(); wVillager.restock();
wVillager.saveRestockTime(); wVillager.saveRestockTime();
if (notifyPlayer && !player_bypassing) { if (notify_player && !player_bypassing) {
final TextReplacementConfig timeLeft = TextReplacementConfig.builder() final TextReplacementConfig timeLeft = TextReplacementConfig.builder()
.matchLiteral("%time%") .matchLiteral("%time%")
.replacement(CommonUtil.formatTime(wVillager.getRestockCooldownMillis(restock_delay_millis))) .replacement(CommonUtil.formatTime(wVillager.getRestockCooldownMillis(restock_delay_millis)))
.build(); .build();
VillagerOptimizer.getLang(player.locale()).trades_restocked.forEach(line -> player.sendMessage(line.replaceText(timeLeft))); VillagerOptimizer.getLang(player.locale()).trades_restocked.forEach(line -> player.sendMessage(line.replaceText(timeLeft)));
} }
if (shouldLog) if (log_enabled)
VillagerOptimizer.getLog().info("Restocked optimized villager at "+ wVillager.villager().getLocation()); VillagerOptimizer.getLog().info("Restocked optimized villager at "+ wVillager.villager().getLocation());
} }
} }

View File

@ -35,7 +35,7 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
private final HashSet<Material> blocks_that_disable = new HashSet<>(4); private final HashSet<Material> blocks_that_disable = new HashSet<>(4);
private final long cooldown; private final long cooldown;
private final double search_radius; private final double search_radius;
private final boolean onlyWhileSneaking, shouldNotifyPlayer, shouldLog; private final boolean only_while_sneaking, notify_player, log_enabled;
public OptimizeByBlock() { public OptimizeByBlock() {
shouldEnable(); shouldEnable();
@ -61,11 +61,11 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
this.search_radius = config.getDouble("optimization-methods.block-optimization.search-radius-in-blocks", 2.0, """ this.search_radius = config.getDouble("optimization-methods.block-optimization.search-radius-in-blocks", 2.0, """
The radius in blocks a villager can be away from the player when he places an optimize block.\s The radius in blocks a villager can be away from the player when he places an optimize block.\s
The closest unoptimized villager to the player will be optimized.""") / 2; The closest unoptimized villager to the player will be optimized.""") / 2;
this.onlyWhileSneaking = config.getBoolean("optimization-methods.block-optimization.only-when-sneaking", true, this.only_while_sneaking = config.getBoolean("optimization-methods.block-optimization.only-when-sneaking", true,
"Only optimize/unoptimize by workstation when player is sneaking during place or break."); "Only optimize/unoptimize by workstation when player is sneaking during place or break.");
this.shouldNotifyPlayer = config.getBoolean("optimization-methods.block-optimization.notify-player", true, this.notify_player = config.getBoolean("optimization-methods.block-optimization.notify-player", true,
"Sends players a message when they successfully optimized or unoptimized a villager."); "Sends players a message when they successfully optimized or unoptimized a villager.");
this.shouldLog = config.getBoolean("optimization-methods.block-optimization.log", false); this.log_enabled = config.getBoolean("optimization-methods.block-optimization.log", false);
} }
@Override @Override
@ -90,7 +90,7 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
if (!blocks_that_disable.contains(placed.getType())) return; if (!blocks_that_disable.contains(placed.getType())) return;
Player player = event.getPlayer(); Player player = event.getPlayer();
if (!player.hasPermission(Permissions.Optimize.BLOCK.get())) return; if (!player.hasPermission(Permissions.Optimize.BLOCK.get())) return;
if (onlyWhileSneaking && !player.isSneaking()) return; if (only_while_sneaking && !player.isSneaking()) return;
final Location blockLoc = placed.getLocation(); final Location blockLoc = placed.getLocation();
WrappedVillager closestOptimizableVillager = null; WrappedVillager closestOptimizableVillager = null;
@ -121,7 +121,7 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
closestOptimizableVillager.setOptimization(optimizeEvent.getOptimizationType()); closestOptimizableVillager.setOptimization(optimizeEvent.getOptimizationType());
closestOptimizableVillager.saveOptimizeTime(); closestOptimizableVillager.saveOptimizeTime();
if (shouldNotifyPlayer) { if (notify_player) {
final TextReplacementConfig vilProfession = TextReplacementConfig.builder() final TextReplacementConfig vilProfession = TextReplacementConfig.builder()
.matchLiteral("%vil_profession%") .matchLiteral("%vil_profession%")
.replacement(closestOptimizableVillager.villager().getProfession().toString().toLowerCase()) .replacement(closestOptimizableVillager.villager().getProfession().toString().toLowerCase())
@ -135,11 +135,11 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
.replaceText(placedMaterial) .replaceText(placedMaterial)
)); ));
} }
if (shouldLog) if (log_enabled)
VillagerOptimizer.getLog().info("Villager was optimized by block at "+closestOptimizableVillager.villager().getLocation()); VillagerOptimizer.getLog().info("Villager was optimized by block at "+closestOptimizableVillager.villager().getLocation());
} else { } else {
closestOptimizableVillager.villager().shakeHead(); closestOptimizableVillager.villager().shakeHead();
if (shouldNotifyPlayer) { if (notify_player) {
final TextReplacementConfig timeLeft = TextReplacementConfig.builder() final TextReplacementConfig timeLeft = TextReplacementConfig.builder()
.matchLiteral("%time%") .matchLiteral("%time%")
.replacement(CommonUtil.formatTime(closestOptimizableVillager.getOptimizeCooldownMillis(cooldown))) .replacement(CommonUtil.formatTime(closestOptimizableVillager.getOptimizeCooldownMillis(cooldown)))
@ -155,7 +155,7 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
if (!blocks_that_disable.contains(broken.getType())) return; if (!blocks_that_disable.contains(broken.getType())) return;
Player player = event.getPlayer(); Player player = event.getPlayer();
if (!player.hasPermission(Permissions.Optimize.BLOCK.get())) return; if (!player.hasPermission(Permissions.Optimize.BLOCK.get())) return;
if (onlyWhileSneaking && !player.isSneaking()) return; if (only_while_sneaking && !player.isSneaking()) return;
final Location blockLoc = broken.getLocation(); final Location blockLoc = broken.getLocation();
WrappedVillager closestOptimizedVillager = null; WrappedVillager closestOptimizedVillager = null;
@ -182,7 +182,7 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
closestOptimizedVillager.setOptimization(OptimizationType.NONE); closestOptimizedVillager.setOptimization(OptimizationType.NONE);
if (shouldNotifyPlayer) { if (notify_player) {
final TextReplacementConfig vilProfession = TextReplacementConfig.builder() final TextReplacementConfig vilProfession = TextReplacementConfig.builder()
.matchLiteral("%vil_profession%") .matchLiteral("%vil_profession%")
.replacement(closestOptimizedVillager.villager().getProfession().toString().toLowerCase()) .replacement(closestOptimizedVillager.villager().getProfession().toString().toLowerCase())
@ -196,7 +196,7 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
.replaceText(brokenMaterial) .replaceText(brokenMaterial)
)); ));
} }
if (shouldLog) if (log_enabled)
VillagerOptimizer.getLog().info("Villager unoptimized because nearby optimization block broken at: "+closestOptimizedVillager.villager().getLocation()); VillagerOptimizer.getLog().info("Villager unoptimized because nearby optimization block broken at: "+closestOptimizedVillager.villager().getLocation());
} }
} }

View File

@ -33,7 +33,7 @@ public class OptimizeByNametag implements VillagerOptimizerModule, Listener {
private final VillagerCache villagerCache; private final VillagerCache villagerCache;
private final HashSet<String> nametags = new HashSet<>(4); private final HashSet<String> nametags = new HashSet<>(4);
private final long cooldown; private final long cooldown;
private final boolean consumeNametag, shouldNotifyPlayer, shouldLog; private final boolean consume_nametag, notify_player, log_enabled;
public OptimizeByNametag() { public OptimizeByNametag() {
shouldEnable(); shouldEnable();
@ -44,14 +44,14 @@ public class OptimizeByNametag implements VillagerOptimizerModule, Listener {
Nametag optimized villagers will be unoptimized again when they are renamed to something else."""); Nametag optimized villagers will be unoptimized again when they are renamed to something else.""");
this.nametags.addAll(config.getList("optimization-methods.nametag-optimization.names", List.of("Optimize", "DisableAI"), this.nametags.addAll(config.getList("optimization-methods.nametag-optimization.names", List.of("Optimize", "DisableAI"),
"Names are case insensitive, capital letters won't matter.").stream().map(String::toLowerCase).toList()); "Names are case insensitive, capital letters won't matter.").stream().map(String::toLowerCase).toList());
this.consumeNametag = config.getBoolean("optimization-methods.nametag-optimization.nametags-get-consumed", true, this.consume_nametag = config.getBoolean("optimization-methods.nametag-optimization.nametags-get-consumed", true,
"Enable or disable consumption of the used nametag item."); "Enable or disable consumption of the used nametag item.");
this.cooldown = config.getInt("optimization-methods.nametag-optimization.optimize-cooldown-seconds", 600, """ this.cooldown = config.getInt("optimization-methods.nametag-optimization.optimize-cooldown-seconds", 600, """
Cooldown in seconds until a villager can be optimized again using a nametag.\s Cooldown in seconds until a villager can be optimized again using a nametag.\s
Here for configuration freedom. Recommended to leave as is to not enable any exploitable behavior.""") * 1000L; Here for configuration freedom. Recommended to leave as is to not enable any exploitable behavior.""") * 1000L;
this.shouldNotifyPlayer = config.getBoolean("optimization-methods.nametag-optimization.notify-player", true, this.notify_player = config.getBoolean("optimization-methods.nametag-optimization.notify-player", true,
"Sends players a message when they successfully optimized a villager."); "Sends players a message when they successfully optimized a villager.");
this.shouldLog = config.getBoolean("optimization-methods.nametag-optimization.log", false); this.log_enabled = config.getBoolean("optimization-methods.nametag-optimization.log", false);
} }
@Override @Override
@ -94,7 +94,7 @@ public class OptimizeByNametag implements VillagerOptimizerModule, Listener {
VillagerOptimizer.callEvent(optimizeEvent); VillagerOptimizer.callEvent(optimizeEvent);
if (optimizeEvent.isCancelled()) return; if (optimizeEvent.isCancelled()) return;
if (!consumeNametag) { if (!consume_nametag) {
event.setCancelled(true); event.setCancelled(true);
villager.customName(newVillagerName); villager.customName(newVillagerName);
} }
@ -102,14 +102,14 @@ public class OptimizeByNametag implements VillagerOptimizerModule, Listener {
wVillager.setOptimization(optimizeEvent.getOptimizationType()); wVillager.setOptimization(optimizeEvent.getOptimizationType());
wVillager.saveOptimizeTime(); wVillager.saveOptimizeTime();
if (shouldNotifyPlayer) if (notify_player)
VillagerOptimizer.getLang(player.locale()).nametag_optimize_success.forEach(player::sendMessage); VillagerOptimizer.getLang(player.locale()).nametag_optimize_success.forEach(player::sendMessage);
if (shouldLog) if (log_enabled)
VillagerOptimizer.getLog().info(player.getName() + " optimized a villager using nametag: '" + name + "'"); VillagerOptimizer.getLog().info(player.getName() + " optimized a villager using nametag: '" + name + "'");
} else { } else {
event.setCancelled(true); event.setCancelled(true);
villager.shakeHead(); villager.shakeHead();
if (shouldNotifyPlayer) { if (notify_player) {
final TextReplacementConfig timeLeft = TextReplacementConfig.builder() final TextReplacementConfig timeLeft = TextReplacementConfig.builder()
.matchLiteral("%time%") .matchLiteral("%time%")
.replacement(CommonUtil.formatTime(wVillager.getOptimizeCooldownMillis(cooldown))) .replacement(CommonUtil.formatTime(wVillager.getOptimizeCooldownMillis(cooldown)))
@ -125,9 +125,9 @@ public class OptimizeByNametag implements VillagerOptimizerModule, Listener {
wVillager.setOptimization(OptimizationType.NONE); wVillager.setOptimization(OptimizationType.NONE);
if (shouldNotifyPlayer) if (notify_player)
VillagerOptimizer.getLang(player.locale()).nametag_unoptimize_success.forEach(player::sendMessage); VillagerOptimizer.getLang(player.locale()).nametag_unoptimize_success.forEach(player::sendMessage);
if (shouldLog) if (log_enabled)
VillagerOptimizer.getLog().info(event.getPlayer().getName() + " disabled optimizations for a villager using nametag: '" + name + "'"); VillagerOptimizer.getLog().info(event.getPlayer().getName() + " disabled optimizations for a villager using nametag: '" + name + "'");
} }
} }

View File

@ -30,7 +30,7 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
private final VillagerCache villagerCache; private final VillagerCache villagerCache;
private final long cooldown; private final long cooldown;
private final double search_radius; private final double search_radius;
private final boolean onlyWhileSneaking, shouldLog, shouldNotifyPlayer; private final boolean only_while_sneaking, log_enabled, notify_player;
public OptimizeByWorkstation() { public OptimizeByWorkstation() {
shouldEnable(); shouldEnable();
@ -45,11 +45,11 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
this.cooldown = config.getInt("optimization-methods.workstation-optimization.optimize-cooldown-seconds", 600, """ this.cooldown = config.getInt("optimization-methods.workstation-optimization.optimize-cooldown-seconds", 600, """
Cooldown in seconds until a villager can be optimized again using a workstation.\s Cooldown in seconds until a villager can be optimized again using a workstation.\s
Here for configuration freedom. Recommended to leave as is to not enable any exploitable behavior.""") * 1000L; Here for configuration freedom. Recommended to leave as is to not enable any exploitable behavior.""") * 1000L;
this.onlyWhileSneaking = config.getBoolean("optimization-methods.workstation-optimization.only-when-sneaking", true, this.only_while_sneaking = config.getBoolean("optimization-methods.workstation-optimization.only-when-sneaking", true,
"Only optimize/unoptimize by workstation when player is sneaking during place or break"); "Only optimize/unoptimize by workstation when player is sneaking during place or break");
this.shouldNotifyPlayer = config.getBoolean("optimization-methods.workstation-optimization.notify-player", true, this.notify_player = config.getBoolean("optimization-methods.workstation-optimization.notify-player", true,
"Sends players a message when they successfully optimized a villager."); "Sends players a message when they successfully optimized a villager.");
this.shouldLog = config.getBoolean("optimization-methods.workstation-optimization.log", false); this.log_enabled = config.getBoolean("optimization-methods.workstation-optimization.log", false);
} }
@Override @Override
@ -75,7 +75,7 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
if (workstationProfession.equals(Villager.Profession.NONE)) return; if (workstationProfession.equals(Villager.Profession.NONE)) return;
Player player = event.getPlayer(); Player player = event.getPlayer();
if (!player.hasPermission(Permissions.Optimize.WORKSTATION.get())) return; if (!player.hasPermission(Permissions.Optimize.WORKSTATION.get())) return;
if (onlyWhileSneaking && !player.isSneaking()) return; if (only_while_sneaking && !player.isSneaking()) return;
final Location workstationLoc = placed.getLocation(); final Location workstationLoc = placed.getLocation();
WrappedVillager closestOptimizableVillager = null; WrappedVillager closestOptimizableVillager = null;
@ -105,7 +105,7 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
closestOptimizableVillager.setOptimization(optimizeEvent.getOptimizationType()); closestOptimizableVillager.setOptimization(optimizeEvent.getOptimizationType());
closestOptimizableVillager.saveOptimizeTime(); closestOptimizableVillager.saveOptimizeTime();
if (shouldNotifyPlayer) { if (notify_player) {
final TextReplacementConfig vilProfession = TextReplacementConfig.builder() final TextReplacementConfig vilProfession = TextReplacementConfig.builder()
.matchLiteral("%vil_profession%") .matchLiteral("%vil_profession%")
.replacement(closestOptimizableVillager.villager().getProfession().toString().toLowerCase()) .replacement(closestOptimizableVillager.villager().getProfession().toString().toLowerCase())
@ -119,11 +119,11 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
.replaceText(placedWorkstation) .replaceText(placedWorkstation)
)); ));
} }
if (shouldLog) if (log_enabled)
VillagerOptimizer.getLog().info(player.getName() + " optimized a villager using workstation: '" + placed.getType().toString().toLowerCase() + "'"); VillagerOptimizer.getLog().info(player.getName() + " optimized a villager using workstation: '" + placed.getType().toString().toLowerCase() + "'");
} else { } else {
closestOptimizableVillager.villager().shakeHead(); closestOptimizableVillager.villager().shakeHead();
if (shouldNotifyPlayer) { if (notify_player) {
final TextReplacementConfig timeLeft = TextReplacementConfig.builder() final TextReplacementConfig timeLeft = TextReplacementConfig.builder()
.matchLiteral("%time%") .matchLiteral("%time%")
.replacement(CommonUtil.formatTime(closestOptimizableVillager.getOptimizeCooldownMillis(cooldown))) .replacement(CommonUtil.formatTime(closestOptimizableVillager.getOptimizeCooldownMillis(cooldown)))
@ -142,7 +142,7 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
if (workstationProfession.equals(Villager.Profession.NONE)) return; if (workstationProfession.equals(Villager.Profession.NONE)) return;
Player player = event.getPlayer(); Player player = event.getPlayer();
if (!player.hasPermission(Permissions.Optimize.WORKSTATION.get())) return; if (!player.hasPermission(Permissions.Optimize.WORKSTATION.get())) return;
if (onlyWhileSneaking && !player.isSneaking()) return; if (only_while_sneaking && !player.isSneaking()) return;
final Location workstationLoc = broken.getLocation(); final Location workstationLoc = broken.getLocation();
WrappedVillager closestOptimizedVillager = null; WrappedVillager closestOptimizedVillager = null;
@ -170,7 +170,7 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
closestOptimizedVillager.setOptimization(OptimizationType.NONE); closestOptimizedVillager.setOptimization(OptimizationType.NONE);
if (shouldNotifyPlayer) { if (notify_player) {
final TextReplacementConfig vilProfession = TextReplacementConfig.builder() final TextReplacementConfig vilProfession = TextReplacementConfig.builder()
.matchLiteral("%vil_profession%") .matchLiteral("%vil_profession%")
.replacement(closestOptimizedVillager.villager().getProfession().toString().toLowerCase()) .replacement(closestOptimizedVillager.villager().getProfession().toString().toLowerCase())
@ -184,7 +184,7 @@ public class OptimizeByWorkstation implements VillagerOptimizerModule, Listener
.replaceText(brokenWorkstation) .replaceText(brokenWorkstation)
)); ));
} }
if (shouldLog) if (log_enabled)
VillagerOptimizer.getLog().info(player.getName() + " unoptimized a villager by breaking workstation: '" + broken.getType().toString().toLowerCase() + "'"); VillagerOptimizer.getLog().info(player.getName() + " unoptimized a villager by breaking workstation: '" + broken.getType().toString().toLowerCase() + "'");
} }