remove usage of OldEnum for backwards compatibility

make reloadModules method simple again
This commit is contained in:
xGinko 2024-07-30 02:17:00 +02:00
parent 7969f75a3a
commit aecd669638
3 changed files with 26 additions and 28 deletions

View File

@ -48,9 +48,13 @@ public class VillagerChunkLimit extends VillagerOptimizerModule implements Runna
"A larger delay is less resource intense but could become inefficient."); "A larger delay is less resource intense but could become inefficient.");
this.skip_unloaded_chunks = config.getBoolean(configPath + ".skip-not-fully-loaded-chunks", true, this.skip_unloaded_chunks = config.getBoolean(configPath + ".skip-not-fully-loaded-chunks", true,
"Does not check chunks that don't have their entities loaded."); "Does not check chunks that don't have their entities loaded.");
this.checked_chunks = new ExpiringSet<>(Duration.ofSeconds(
Math.max(1, config.getInt(configPath + ".chunk-check-cooldown-seconds", 5,
"The delay in seconds a chunk will not be checked again after the first time.\n" +
"Reduces chances to lag the server due to overchecking."))));
this.log_enabled = config.getBoolean(configPath + ".log-removals", true); this.log_enabled = config.getBoolean(configPath + ".log-removals", true);
final List<String> defaults = Stream.of( List<String> defaults = Stream.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")
.filter(profession -> { .filter(profession -> {
try { try {
@ -61,9 +65,11 @@ public class VillagerChunkLimit extends VillagerOptimizerModule implements Runna
return false; return false;
} }
}).collect(Collectors.toList()); }).collect(Collectors.toList());
this.use_whitelist = config.getBoolean(configPath + ".whitelist-certain-professions", false, this.use_whitelist = config.getBoolean(configPath + ".whitelist.enable", false,
"Enable if you only want to manage villager counts for certain profession types."); "Enable if you only want to manage villager counts for certain profession types.");
this.profession_whitelist = config.getList(configPath + ".removal-whitelist", defaults, this.profession_whitelist = config.getList(configPath + ".whitelist.professions", defaults.stream()
.filter(prof -> !prof.equalsIgnoreCase("NONE") && !prof.equalsIgnoreCase("NITWIT"))
.collect(Collectors.toList()),
"Only professions in this list will count for the cap.") "Only professions in this list will count for the cap.")
.stream() .stream()
.map(configuredProfession -> { .map(configuredProfession -> {
@ -80,10 +86,6 @@ public class VillagerChunkLimit extends VillagerOptimizerModule implements Runna
.collect(Collectors.toCollection(HashSet::new)); .collect(Collectors.toCollection(HashSet::new));
this.non_optimized_max_per_chunk = config.getInt(configPath + ".unoptimized.max-per-chunk", 20, this.non_optimized_max_per_chunk = config.getInt(configPath + ".unoptimized.max-per-chunk", 20,
"The maximum amount of unoptimized villagers per chunk."); "The maximum amount of unoptimized villagers per chunk.");
this.checked_chunks = new ExpiringSet<>(Duration.ofSeconds(
Math.max(1, config.getInt(configPath + ".chunk-check-cooldown-seconds", 5,
"The delay in seconds a chunk will not be checked again after the first time.\n" +
"Reduces chances to lag the server due to overchecking."))));
this.non_optimized_removal_priority = config.getList(configPath + ".unoptimized.removal-priority", defaults, this.non_optimized_removal_priority = config.getList(configPath + ".unoptimized.removal-priority", defaults,
"Professions that are in the top of the list are going to be scheduled for removal first.\n" + "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") "Use enums from https://jd.papermc.io/paper/1.20/org/bukkit/entity/Villager.Profession.html")

View File

@ -1,7 +1,7 @@
package me.xginko.villageroptimizer.modules; package me.xginko.villageroptimizer.modules;
import me.xginko.villageroptimizer.WrapperCache;
import me.xginko.villageroptimizer.VillagerOptimizer; import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.WrapperCache;
import me.xginko.villageroptimizer.config.Config; import me.xginko.villageroptimizer.config.Config;
import me.xginko.villageroptimizer.utils.Disableable; import me.xginko.villageroptimizer.utils.Disableable;
import me.xginko.villageroptimizer.utils.Enableable; import me.xginko.villageroptimizer.utils.Enableable;
@ -9,9 +9,9 @@ import org.reflections.Reflections;
import org.reflections.scanners.Scanners; import org.reflections.scanners.Scanners;
import space.arim.morepaperlib.scheduling.GracefulScheduling; import space.arim.morepaperlib.scheduling.GracefulScheduling;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.HashSet; import java.util.HashSet;
import java.util.Objects;
import java.util.Set; import java.util.Set;
public abstract class VillagerOptimizerModule implements Enableable, Disableable { public abstract class VillagerOptimizerModule implements Enableable, Disableable {
@ -47,20 +47,18 @@ public abstract class VillagerOptimizerModule implements Enableable, Disableable
ENABLED_MODULES.forEach(VillagerOptimizerModule::disable); ENABLED_MODULES.forEach(VillagerOptimizerModule::disable);
ENABLED_MODULES.clear(); ENABLED_MODULES.clear();
MODULES_PACKAGE.get(Scanners.SubTypes.of(VillagerOptimizerModule.class).asClass()) for (Class<?> clazz : MODULES_PACKAGE.get(Scanners.SubTypes.of(VillagerOptimizerModule.class).asClass())) {
.stream() if (clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) continue;
.filter(clazz -> !clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers()))
.map(clazz -> { try {
try { VillagerOptimizerModule module = (VillagerOptimizerModule) clazz.getDeclaredConstructor().newInstance();
return (VillagerOptimizerModule) clazz.getDeclaredConstructor().newInstance(); if (module.shouldEnable()) {
} catch (Throwable t) { ENABLED_MODULES.add(module);
VillagerOptimizer.logger().error("Failed initialising module '{}'.", clazz.getSimpleName(), t); }
return null; } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
} VillagerOptimizer.logger().error("Failed initialising module class '{}'.", clazz.getSimpleName(), e);
}) }
.filter(Objects::nonNull) }
.filter(VillagerOptimizerModule::shouldEnable)
.forEach(ENABLED_MODULES::add);
ENABLED_MODULES.forEach(VillagerOptimizerModule::enable); ENABLED_MODULES.forEach(VillagerOptimizerModule::enable);
} }

View File

@ -7,7 +7,6 @@ import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Chunk; import org.bukkit.Chunk;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Villager; import org.bukkit.entity.Villager;
import org.bukkit.util.OldEnum;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -26,6 +25,7 @@ public class Util {
static { static {
PL_COLOR = TextColor.color(102,255,230); PL_COLOR = TextColor.color(102,255,230);
PL_STYLE = Style.style(PL_COLOR, TextDecoration.BOLD); PL_STYLE = Style.style(PL_COLOR, TextDecoration.BOLD);
PROFESSION_MAP = new HashMap<>(); PROFESSION_MAP = new HashMap<>();
PROFESSION_MAP.put(XMaterial.LOOM.parseMaterial(), Villager.Profession.SHEPHERD); PROFESSION_MAP.put(XMaterial.LOOM.parseMaterial(), Villager.Profession.SHEPHERD);
PROFESSION_MAP.put(XMaterial.BARREL.parseMaterial(), Villager.Profession.FISHERMAN); PROFESSION_MAP.put(XMaterial.BARREL.parseMaterial(), Villager.Profession.FISHERMAN);
@ -40,6 +40,7 @@ public class Util {
PROFESSION_MAP.put(XMaterial.SMITHING_TABLE.parseMaterial(), Villager.Profession.TOOLSMITH); PROFESSION_MAP.put(XMaterial.SMITHING_TABLE.parseMaterial(), Villager.Profession.TOOLSMITH);
PROFESSION_MAP.put(XMaterial.FLETCHING_TABLE.parseMaterial(), Villager.Profession.FLETCHER); PROFESSION_MAP.put(XMaterial.FLETCHING_TABLE.parseMaterial(), Villager.Profession.FLETCHER);
PROFESSION_MAP.put(XMaterial.CARTOGRAPHY_TABLE.parseMaterial(), Villager.Profession.CARTOGRAPHER); PROFESSION_MAP.put(XMaterial.CARTOGRAPHY_TABLE.parseMaterial(), Villager.Profession.CARTOGRAPHER);
try { try {
Chunk.class.getMethod("isEntitiesLoaded"); Chunk.class.getMethod("isEntitiesLoaded");
canUseIsEntitiesLoaded = true; canUseIsEntitiesLoaded = true;
@ -72,14 +73,11 @@ public class Util {
} }
} }
@SuppressWarnings({"deprecation", "UnstableApiUsage"})
public static @NotNull String toNiceString(@NotNull Object input) { public static @NotNull String toNiceString(@NotNull Object input) {
// Get name // Get name
String name; String name;
if (input instanceof Enum<?>) { if (input instanceof Enum<?>) {
name = ((Enum<?>) input).name(); name = ((Enum<?>) input).name();
} else if (input instanceof OldEnum<?>) {
name = ((OldEnum<?>) input).name();
} else { } else {
name = input.toString(); name = input.toString();
} }