minor optimizations

This commit is contained in:
xGinko 2024-01-23 09:21:10 +01:00
parent 325b55d874
commit e9e2bfb48b
10 changed files with 56 additions and 44 deletions

View File

@ -6,7 +6,7 @@
<groupId>me.xginko.VillagerOptimizer</groupId>
<artifactId>VillagerOptimizer</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
<packaging>jar</packaging>
<name>VillagerOptimizer</name>

View File

@ -10,13 +10,21 @@ import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
public interface VillagerOptimizerCommand extends CommandExecutor {
String label();
@Override
boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args);
List<String> NO_TABCOMPLETES = Collections.emptyList();
List<String> RADIUS_TABCOMPLETES = List.of("5", "10", "25", "50");
HashSet<VillagerOptimizerCommand> commands = new HashSet<>();
static void reloadCommands() {
VillagerOptimizer plugin = VillagerOptimizer.getInstance();
CommandMap commandMap = plugin.getServer().getCommandMap();
@ -29,7 +37,4 @@ public interface VillagerOptimizerCommand extends CommandExecutor {
commands.forEach(command -> plugin.getCommand(command.label()).setExecutor(command));
}
@Override
boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args);
}

View File

@ -27,7 +27,6 @@ import java.util.List;
public class OptVillagersRadius implements VillagerOptimizerCommand, TabCompleter {
private final List<String> radiusSuggestions = List.of("5", "10", "25", "50");
private final long cooldown;
private final int max_radius;
@ -46,7 +45,7 @@ public class OptVillagersRadius implements VillagerOptimizerCommand, TabComplete
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
return args.length == 1 ? radiusSuggestions : null;
return args.length == 1 ? RADIUS_TABCOMPLETES : NO_TABCOMPLETES;
}
@Override

View File

@ -25,7 +25,6 @@ import java.util.List;
public class UnOptVillagersRadius implements VillagerOptimizerCommand, TabCompleter {
private final List<String> radiusSuggestions = List.of("5", "10", "25", "50");
private final int max_radius;
public UnOptVillagersRadius() {
@ -39,7 +38,7 @@ public class UnOptVillagersRadius implements VillagerOptimizerCommand, TabComple
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
return args.length == 1 ? radiusSuggestions : null;
return args.length == 1 ? RADIUS_TABCOMPLETES : NO_TABCOMPLETES;
}
@Override

View File

@ -33,7 +33,7 @@ public class VillagerOptimizerCmd implements TabCompleter, VillagerOptimizerComm
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
return args.length == 1 ? tabCompleter : null;
return args.length == 1 ? tabCompleter : NO_TABCOMPLETES;
}
@Override

View File

@ -29,8 +29,10 @@ public class ReloadSubCmd extends SubCommand {
public void perform(CommandSender sender, String[] args) {
if (sender.hasPermission(Commands.RELOAD.get())) {
sender.sendMessage(Component.text("Reloading VillagerOptimizer...").color(NamedTextColor.WHITE));
VillagerOptimizer.getInstance().reloadPlugin();
sender.sendMessage(Component.text("Reload complete.").color(NamedTextColor.GREEN));
VillagerOptimizer.getScheduler().runNextTick(reload -> { // Reload in sync with the server
VillagerOptimizer.getInstance().reloadPlugin();
sender.sendMessage(Component.text("Reload complete.").color(NamedTextColor.GREEN));
});
} else {
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
}

View File

@ -21,18 +21,17 @@ import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.*;
import java.util.logging.Level;
import java.util.stream.Stream;
public class VillagerChunkLimit implements VillagerOptimizerModule, Listener {
private final ServerImplementation scheduler;
private final VillagerCache villagerCache;
private WrappedTask periodic_chunk_check;
private final List<Villager.Profession> non_optimized_removal_priority = new ArrayList<>(16);
private final List<Villager.Profession> optimized_removal_priority = new ArrayList<>(16);
private final List<Villager.Profession> non_optimized_removal_priority;
private final List<Villager.Profession> optimized_removal_priority;
private final long check_period;
private final int non_optimized_max_per_chunk, optimized_max_per_chunk;
private final boolean log_enabled, skip_unloaded_entity_chunks;
@ -53,37 +52,37 @@ public class VillagerChunkLimit implements VillagerOptimizerModule, Listener {
this.log_enabled = config.getBoolean("villager-chunk-limit.log-removals", false);
this.non_optimized_max_per_chunk = config.getInt("villager-chunk-limit.unoptimized.max-per-chunk", 20,
"The maximum amount of unoptimized villagers per chunk.");
config.getList("villager-chunk-limit.unoptimized.removal-priority", List.of(
this.non_optimized_removal_priority = config.getList("villager-chunk-limit.unoptimized.removal-priority", List.of(
"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.\s
Use enums from https://jd.papermc.io/paper/1.20/org/bukkit/entity/Villager.Profession.html"""
).forEach(configuredProfession -> {
).stream().map(configuredProfession -> {
try {
Villager.Profession profession = Villager.Profession.valueOf(configuredProfession);
this.non_optimized_removal_priority.add(profession);
return Villager.Profession.valueOf(configuredProfession);
} catch (IllegalArgumentException e) {
LogUtil.moduleLog(Level.WARNING, "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).toList();
this.optimized_max_per_chunk = config.getInt("villager-chunk-limit.optimized.max-per-chunk", 60,
"The maximum amount of optimized villagers per chunk.");
config.getList("villager-chunk-limit.optimized.removal-priority", List.of(
this.optimized_removal_priority = config.getList("villager-chunk-limit.optimized.removal-priority", List.of(
"NONE", "NITWIT", "SHEPHERD", "FISHERMAN", "BUTCHER", "CARTOGRAPHER", "LEATHERWORKER",
"FLETCHER", "MASON", "FARMER", "ARMORER", "TOOLSMITH", "WEAPONSMITH", "CLERIC", "LIBRARIAN"
)).forEach(configuredProfession -> {
)).stream().map(configuredProfession -> {
try {
Villager.Profession profession = Villager.Profession.valueOf(configuredProfession);
this.optimized_removal_priority.add(profession);
return Villager.Profession.valueOf(configuredProfession);
} catch (IllegalArgumentException e) {
LogUtil.moduleLog(Level.WARNING, "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).toList();
}
@Override

View File

@ -16,12 +16,14 @@ import org.bukkit.event.entity.EntityDamageEvent;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
public class PreventOptimizedDamage implements VillagerOptimizerModule, Listener {
private final VillagerCache villagerCache;
private final Set<EntityDamageEvent.DamageCause> damage_causes_to_cancel = new HashSet<>();
private final Set<EntityDamageEvent.DamageCause> damage_causes_to_cancel;
private final boolean cancelKnockback;
public PreventOptimizedDamage() {
@ -32,19 +34,19 @@ public class PreventOptimizedDamage implements VillagerOptimizerModule, Listener
"Configure what kind of damage you want to cancel for optimized villagers here.");
this.cancelKnockback = config.getBoolean("gameplay.prevent-damage-to-optimized.prevent-knockback-from-entity", true,
"Prevents optimized villagers from getting knocked back by an attacking entity");
config.getList("gameplay.prevent-damage-to-optimized.damage-causes-to-cancel",
this.damage_causes_to_cancel = config.getList("gameplay.prevent-damage-to-optimized.damage-causes-to-cancel",
Arrays.stream(EntityDamageEvent.DamageCause.values()).map(Enum::name).sorted().toList(), """
These are all current entries in the game. Remove what you do not need blocked.\s
If you want a description or need to add a previously removed type, refer to:\s
https://jd.papermc.io/paper/1.20/org/bukkit/event/entity/EntityDamageEvent.DamageCause.html"""
).forEach(configuredDamageCause -> {
).stream().map(configuredDamageCause -> {
try {
EntityDamageEvent.DamageCause damageCause = EntityDamageEvent.DamageCause.valueOf(configuredDamageCause);
this.damage_causes_to_cancel.add(damageCause);
return EntityDamageEvent.DamageCause.valueOf(configuredDamageCause);
} catch (IllegalArgumentException e) {
LogUtil.damageCauseNotRecognized("prevent-damage-to-optimized", configuredDamageCause);
return null;
}
});
}).filter(Objects::nonNull).collect(Collectors.toCollection(HashSet::new));
}
@Override

View File

@ -50,31 +50,35 @@ public class PreventUnoptimizedTrading implements VillagerOptimizerModule, Liste
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
private void onTradeOpen(TradeSelectEvent event) {
Player player = (Player) event.getWhoClicked();
if (player.hasPermission(Bypass.TRADE_PREVENTION.get())) return;
if (event.getWhoClicked().hasPermission(Bypass.TRADE_PREVENTION.get())) return;
if (
event.getInventory().getType().equals(InventoryType.MERCHANT)
&& event.getInventory().getHolder() instanceof Villager villager
&& !villagerCache.getOrAdd(villager).isOptimized()
) {
event.setCancelled(true);
if (notify_player)
if (notify_player) {
Player player = (Player) event.getWhoClicked();
VillagerOptimizer.getLang(player.locale()).optimize_for_trading.forEach(player::sendMessage);
}
}
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
private void onInventoryClick(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
if (player.hasPermission(Bypass.TRADE_PREVENTION.get())) return;
if (event.getWhoClicked().hasPermission(Bypass.TRADE_PREVENTION.get())) return;
if (
event.getInventory().getType().equals(InventoryType.MERCHANT)
&& event.getInventory().getHolder() instanceof Villager villager
&& !villagerCache.getOrAdd(villager).isOptimized()
) {
event.setCancelled(true);
if (notify_player)
if (notify_player) {
Player player = (Player) event.getWhoClicked();
VillagerOptimizer.getLang(player.locale()).optimize_for_trading.forEach(player::sendMessage);
}
}
}
}

View File

@ -29,13 +29,15 @@ import org.bukkit.event.block.BlockPlaceEvent;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
private final VillagerCache villagerCache;
private final Set<Material> blocks_that_disable = new HashSet<>();
private final Set<Material> blocks_that_disable;
private final long cooldown_millis;
private final double search_radius;
private final boolean only_while_sneaking, notify_player, log_enabled;
@ -47,17 +49,17 @@ public class OptimizeByBlock implements VillagerOptimizerModule, Listener {
config.master().addComment("optimization-methods.block-optimization.enable", """
When enabled, the closest villager standing near a configured block being placed will be optimized.\s
If a configured block is broken nearby, the closest villager will become unoptimized again.""");
config.getList("optimization-methods.block-optimization.materials", List.of(
this.blocks_that_disable = config.getList("optimization-methods.block-optimization.materials", List.of(
"LAPIS_BLOCK", "GLOWSTONE", "IRON_BLOCK"
), "Values here need to be valid bukkit Material enums for your server version."
).forEach(configuredMaterial -> {
).stream().map(configuredMaterial -> {
try {
Material disableBlock = Material.valueOf(configuredMaterial);
this.blocks_that_disable.add(disableBlock);
return Material.valueOf(configuredMaterial);
} catch (IllegalArgumentException e) {
LogUtil.materialNotRecognized("block-optimization", configuredMaterial);
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, """
Cooldown in seconds until a villager can be optimized again by using specific blocks.\s