add ability to disable nametag consumption

This commit is contained in:
xGinko 2023-09-08 21:14:23 +02:00
parent edf396cdab
commit 50c734eccd
4 changed files with 34 additions and 36 deletions

View File

@ -30,19 +30,15 @@ public class Config {
public Config() throws Exception {
this.config = loadConfig(new File(VillagerOptimizer.getInstance().getDataFolder(), "config.yml"));
structureConfig();
/**
* Language
*/
this.default_lang = Locale.forLanguageTag(
getString("language.default-language", "en_us",
"The default language that will be used if auto-language is false or no matching language file was found.")
.replace("_", "-"));
this.auto_lang = getBoolean("language.auto-language", true, "If set to true, will display messages based on client language");
/**
* General
*/
this.cache_enabled = getBoolean("general.plugin-cache.enable", true, "Leave enabled if you don't know what you're doing and or don't experience any issues.");
this.cache_keep_time_seconds = getInt("general.plugin-cache.expire-after-x-seconds", 30, "The amount of time in seconds a villager will be kept in cache.");
this.default_lang = Locale.forLanguageTag(
getString("general.default-language", "en_us",
"The default language that will be used if auto-language is false or no matching language file was found.")
.replace("_", "-"));
this.auto_lang = getBoolean("general.auto-language", true, "If set to true, will display messages based on client language");
this.cache_keep_time_seconds = getInt("general.cache-keep-time-seconds", 30, "The amount of time in seconds a villager will be kept in the plugin's cache.");
/**
* Optimization
*/
@ -52,12 +48,10 @@ public class Config {
this.nametags.addAll(getList("optimization.methods.by-nametag.names", List.of("Optimize", "DisableAI"), "Names are case insensitive")
.stream().map(String::toLowerCase).toList());
// Workstations
this.enable_workstation_optimization = getBoolean("optimization.methods.by-workstation.enable", true,
"""
this.enable_workstation_optimization = getBoolean("optimization.methods.by-workstation.enable", true, """
Optimize villagers that are standing near their acquired workstations /s
Values here need to be valid bukkit Material enums for your server version.
"""
);
""");
this.workstation_max_distance = getDouble("optimization.methods.by-workstation.disable-range-in-blocks", 4.0,
"How close in blocks a villager needs to be to get optimized by its workstation");
this.getList("optimization.methods.by-workstation.workstation-materials", List.of(
@ -72,12 +66,10 @@ public class Config {
}
});
// Blocks
this.enable_block_optimization = getBoolean("optimization.methods.by-specific-block.enable", true,
"""
this.enable_block_optimization = getBoolean("optimization.methods.by-specific-block.enable", true, """
Optimize villagers that are standing on these specific block materials /s
Values here need to be valid bukkit Material enums for your server version.
"""
);
""");
this.getList("optimization.methods.by-specific-block.materials", List.of(
"LAPIS_BLOCK", "GLOWSTONE", "IRON_BLOCK"
)).forEach(configuredMaterial -> {
@ -109,7 +101,7 @@ public class Config {
private void structureConfig() {
config.addDefault("config-version", 1.00);
createTitledSection("Language", "language");
createTitledSection("General", "general");
createTitledSection("Optimization", "optimization");
}

View File

@ -9,7 +9,9 @@ import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
public final class WrappedVillager {
/*
* TODO: Refresh cache when information is read or written (but efficiently)
* */
private final @NotNull Villager villager;
private final @NotNull PersistentDataContainer dataContainer;

View File

@ -2,14 +2,15 @@ package me.xginko.villageroptimizer.modules;
import io.papermc.paper.event.player.PlayerNameEntityEvent;
import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.cache.VillagerManager;
import me.xginko.villageroptimizer.config.Config;
import me.xginko.villageroptimizer.enums.OptimizationType;
import me.xginko.villageroptimizer.cache.VillagerManager;
import me.xginko.villageroptimizer.models.WrappedVillager;
import me.xginko.villageroptimizer.utils.CommonUtils;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
@ -17,22 +18,22 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
public class NametagOptimization implements VillagerOptimizerModule, Listener {
private final VillagerManager villagerManager;
private final Config config;
private final boolean shouldLog, shouldNotifyPlayer;
private final boolean shouldLog, shouldNotifyPlayer, consumeNametag;
protected NametagOptimization() {
this.villagerManager = VillagerOptimizer.getVillagerManager();
this.config = VillagerOptimizer.getConfiguration();
this.config.addComment("optimization.methods.by-nametag.enable",
"""
this.config.addComment("optimization.methods.by-nametag.enable", """
Enable optimization by naming villagers to one of the names configured below.\s
Nametag optimized villagers will be unoptimized again when they are renamed to something else.
"""
);
""");
this.consumeNametag = config.getBoolean("optimization.methods.by-nametag.nametags-get-consumed", true);
this.shouldLog = config.getBoolean("optimization.methods.by-nametag.log", false);
this.shouldNotifyPlayer = config.getBoolean("optimization.methods.by-nametag.notify-player", true);
}
@ -61,19 +62,24 @@ public class NametagOptimization implements VillagerOptimizerModule, Listener {
final String nameTag = PlainTextComponentSerializer.plainText().serialize(name);
WrappedVillager wVillager = villagerManager.getOrAdd((Villager) event.getEntity());
Player player = event.getPlayer();
if (config.nametags.contains(nameTag.toLowerCase())) {
if (!wVillager.isOptimized()) {
if (wVillager.setOptimization(OptimizationType.NAMETAG)) {
if (shouldNotifyPlayer) {
Player player = event.getPlayer();
VillagerOptimizer.getLang(player.locale()).nametag_optimize_success.forEach(player::sendMessage);
if (!consumeNametag) {
ItemStack mainHand = player.getInventory().getItemInMainHand();
ItemStack offHand = player.getInventory().getItemInOffHand();
if (mainHand.getType().equals(Material.NAME_TAG)) mainHand.add();
else if (offHand.getType().equals(Material.NAME_TAG)) offHand.add();
}
if (shouldNotifyPlayer)
VillagerOptimizer.getLang(player.locale()).nametag_optimize_success.forEach(player::sendMessage);
if (shouldLog)
VillagerOptimizer.getLog().info(event.getPlayer().getName() + " optimized a villager using nametag: '" + nameTag + "'");
VillagerOptimizer.getLog().info(player.getName() + " optimized a villager using nametag: '" + nameTag + "'");
} else {
event.setCancelled(true);
if (shouldNotifyPlayer) {
Player player = event.getPlayer();
VillagerOptimizer.getLang(player.locale()).nametag_on_optimize_cooldown.forEach(line -> player.sendMessage(line
.replaceText(TextReplacementConfig.builder().matchLiteral("%time%").replacement(CommonUtils.formatTime(wVillager.getOptimizeCooldown())).build())));
}
@ -82,10 +88,8 @@ public class NametagOptimization implements VillagerOptimizerModule, Listener {
} else {
if (wVillager.getOptimizationType().equals(OptimizationType.NAMETAG)) {
wVillager.setOptimization(OptimizationType.OFF);
if (shouldNotifyPlayer) {
Player player = event.getPlayer();
if (shouldNotifyPlayer)
VillagerOptimizer.getLang(player.locale()).nametag_unoptimize_success.forEach(player::sendMessage);
}
if (shouldLog)
VillagerOptimizer.getLog().info(event.getPlayer().getName() + " disabled optimizations for a villager using nametag: '" + nameTag + "'");
}

View File

@ -70,7 +70,7 @@ public class WorkstationOptimization implements VillagerOptimizerModule, Listene
for (Entity entity : workstationLoc.getNearbyEntities(search_radius, search_radius, search_radius)) {
if (!entity.getType().equals(EntityType.VILLAGER)) continue;
Villager villager = (Villager) entity;
Villager.Profession profession = villager.getProfession();
final Villager.Profession profession = villager.getProfession();
if (profession.equals(Villager.Profession.NONE) || profession.equals(Villager.Profession.NITWIT)) continue;
WrappedVillager wVillager = villagerManager.getOrAdd(villager);
@ -116,7 +116,7 @@ public class WorkstationOptimization implements VillagerOptimizerModule, Listene
for (Entity entity : workstationLoc.getNearbyEntities(search_radius, search_radius, search_radius)) {
if (!entity.getType().equals(EntityType.VILLAGER)) continue;
Villager villager = (Villager) entity;
Villager.Profession profession = villager.getProfession();
final Villager.Profession profession = villager.getProfession();
if (profession.equals(Villager.Profession.NONE) || profession.equals(Villager.Profession.NITWIT)) continue;
WrappedVillager wVillager = villagerManager.getOrAdd(villager);