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,8 +48,12 @@ public class VillagerChunkLimit extends VillagerOptimizerModule implements Runna
"A larger delay is less resource intense but could become inefficient.");
this.skip_unloaded_chunks = config.getBoolean(configPath + ".skip-not-fully-loaded-chunks", true,
"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);
final List<String> defaults = Stream.of(
List<String> defaults = Stream.of(
"NONE", "NITWIT", "SHEPHERD", "FISHERMAN", "BUTCHER", "CARTOGRAPHER", "LEATHERWORKER",
"FLETCHER", "MASON", "FARMER", "ARMORER", "TOOLSMITH", "WEAPONSMITH", "CLERIC", "LIBRARIAN")
.filter(profession -> {
@ -61,9 +65,11 @@ public class VillagerChunkLimit extends VillagerOptimizerModule implements Runna
return false;
}
}).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.");
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.")
.stream()
.map(configuredProfession -> {
@ -80,10 +86,6 @@ public class VillagerChunkLimit extends VillagerOptimizerModule implements Runna
.collect(Collectors.toCollection(HashSet::new));
this.non_optimized_max_per_chunk = config.getInt(configPath + ".unoptimized.max-per-chunk", 20,
"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,
"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")

View File

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

View File

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