finish trade restocker and issues

This commit is contained in:
xGinko 2023-09-08 22:00:22 +02:00
parent 2e68170b4f
commit e089a416ea
7 changed files with 46 additions and 26 deletions

View File

@ -18,9 +18,7 @@ public class Config {
public final Locale default_lang; public final Locale default_lang;
public final boolean auto_lang, enable_nametag_optimization, enable_workstation_optimization, enable_block_optimization; public final boolean auto_lang, enable_nametag_optimization, enable_workstation_optimization, enable_block_optimization;
public final int state_change_cooldown; public final long cache_keep_time_seconds, optimize_cooldown_millis;
public final double workstation_max_distance;
public final long cache_keep_time_seconds;
public final HashSet<String> nametags = new HashSet<>(4); public final HashSet<String> nametags = new HashSet<>(4);
public final HashSet<Material> blocks_that_disable = new HashSet<>(4); public final HashSet<Material> blocks_that_disable = new HashSet<>(4);
@ -41,7 +39,7 @@ public class Config {
/** /**
* Optimization * Optimization
*/ */
this.state_change_cooldown = getInt("optimization.state-change-cooldown-in-seconds", 600); this.optimize_cooldown_millis = getInt("optimization.state-change-cooldown-in-seconds", 600) * 1000L;
// Nametags // Nametags
this.enable_nametag_optimization = getBoolean("optimization.methods.by-nametag.enable", true); this.enable_nametag_optimization = getBoolean("optimization.methods.by-nametag.enable", true);
this.nametags.addAll(getList("optimization.methods.by-nametag.names", List.of("Optimize", "DisableAI"), "Names are case insensitive") this.nametags.addAll(getList("optimization.methods.by-nametag.names", List.of("Optimize", "DisableAI"), "Names are case insensitive")
@ -51,8 +49,6 @@ public class Config {
Optimize villagers that are standing near their acquired workstations /s Optimize villagers that are standing near their acquired workstations /s
Values here need to be valid bukkit Material enums for your server version. 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( this.getList("optimization.methods.by-workstation.workstation-materials", List.of(
"COMPOSTER", "SMOKER", "BARREL", "LOOM", "BLAST_FURNACE", "BREWING_STAND", "CAULDRON", "COMPOSTER", "SMOKER", "BARREL", "LOOM", "BLAST_FURNACE", "BREWING_STAND", "CAULDRON",
"FLETCHING_TABLE", "CARTOGRAPHY_TABLE", "LECTERN", "SMITHING_TABLE", "STONECUTTER", "GRINDSTONE" "FLETCHING_TABLE", "CARTOGRAPHY_TABLE", "LECTERN", "SMITHING_TABLE", "STONECUTTER", "GRINDSTONE"

View File

@ -16,13 +16,15 @@ public class LanguageCache {
public final Component no_permission; public final Component no_permission;
public final List<Component> nametag_optimize_success, nametag_on_optimize_cooldown, nametag_unoptimize_success, public final List<Component> nametag_optimize_success, nametag_on_optimize_cooldown, nametag_unoptimize_success,
block_optimize_success, block_on_optimize_cooldown, block_unoptimize_success, block_optimize_success, block_on_optimize_cooldown, block_unoptimize_success,
workstation_optimize_success, workstation_on_optimize_cooldown, workstation_unoptimize_success; workstation_optimize_success, workstation_on_optimize_cooldown, workstation_unoptimize_success,
trades_restocked;
public LanguageCache(String lang) throws Exception { public LanguageCache(String lang) throws Exception {
this.lang = loadLang(new File(VillagerOptimizer.getInstance().getDataFolder() + File.separator + "lang", lang + ".yml")); this.lang = loadLang(new File(VillagerOptimizer.getInstance().getDataFolder() + File.separator + "lang", lang + ".yml"));
this.miniMessage = MiniMessage.miniMessage(); this.miniMessage = MiniMessage.miniMessage();
this.no_permission = getTranslation("messages.no-permission", "<red>You don't have permission to use this command."); this.no_permission = getTranslation("messages.no-permission", "<red>You don't have permission to use this command.");
this.trades_restocked = getListTranslation("messages.restock-success", List.of("<green>All trades restocked!"));
this.nametag_optimize_success = getListTranslation("messages.nametag.optimize-success", List.of("<green>Successfully optimized villager by using a nametag.")); this.nametag_optimize_success = getListTranslation("messages.nametag.optimize-success", List.of("<green>Successfully optimized villager by using a nametag."));
this.nametag_on_optimize_cooldown = getListTranslation("messages.nametag.optimize-on-cooldown", List.of("<gray>You need to wait %time% until you can optimize this villager again.")); this.nametag_on_optimize_cooldown = getListTranslation("messages.nametag.optimize-on-cooldown", List.of("<gray>You need to wait %time% until you can optimize this villager again."));
this.nametag_unoptimize_success = getListTranslation("messages.nametag.unoptimize-success", List.of("<green>Successfully unoptimized villager by using a nametag.")); this.nametag_unoptimize_success = getListTranslation("messages.nametag.unoptimize-success", List.of("<green>Successfully unoptimized villager by using a nametag."));

View File

@ -6,9 +6,9 @@ import org.bukkit.NamespacedKey;
public enum Keys { public enum Keys {
OPTIMIZED(VillagerOptimizer.getKey("optimized")), OPTIMIZED(VillagerOptimizer.getKey("optimized")),
COOLDOWN_OPTIMIZE(VillagerOptimizer.getKey("optimize-state-change-cooldown")), COOLDOWN_OPTIMIZE(VillagerOptimizer.getKey("optimize-cooldown")),
COOLDOWN_EXPERIENCE(VillagerOptimizer.getKey("experience-cooldown")), COOLDOWN_EXPERIENCE(VillagerOptimizer.getKey("experience-cooldown")),
WORLDTIME(VillagerOptimizer.getKey("world-time")); WORLDTIME(VillagerOptimizer.getKey("last-restock-time"));
private final NamespacedKey key; private final NamespacedKey key;

View File

@ -41,7 +41,7 @@ public final class WrappedVillager {
if (isOnOptimizeCooldown()) return false; if (isOnOptimizeCooldown()) return false;
dataContainer.set(Keys.OPTIMIZED.key(), PersistentDataType.STRING, type.name()); dataContainer.set(Keys.OPTIMIZED.key(), PersistentDataType.STRING, type.name());
villager.setAware(false); villager.setAware(false);
setOptimizeCooldown(VillagerOptimizer.getConfiguration().state_change_cooldown); setOptimizeCooldown(VillagerOptimizer.getConfiguration().optimize_cooldown_millis);
} }
return true; return true;
} }
@ -62,10 +62,6 @@ public final class WrappedVillager {
return dataContainer.has(Keys.COOLDOWN_OPTIMIZE.key(), PersistentDataType.LONG) && dataContainer.get(Keys.COOLDOWN_OPTIMIZE.key(), PersistentDataType.LONG) <= System.currentTimeMillis(); return dataContainer.has(Keys.COOLDOWN_OPTIMIZE.key(), PersistentDataType.LONG) && dataContainer.get(Keys.COOLDOWN_OPTIMIZE.key(), PersistentDataType.LONG) <= System.currentTimeMillis();
} }
public void restock() {
villager.getRecipes().forEach(recipe -> recipe.setUses(0));
}
public void setExpCooldown(long milliseconds) { public void setExpCooldown(long milliseconds) {
dataContainer.set(Keys.COOLDOWN_EXPERIENCE.key(), PersistentDataType.LONG, System.currentTimeMillis() + milliseconds); dataContainer.set(Keys.COOLDOWN_EXPERIENCE.key(), PersistentDataType.LONG, System.currentTimeMillis() + milliseconds);
} }
@ -74,13 +70,22 @@ public final class WrappedVillager {
return dataContainer.has(Keys.COOLDOWN_EXPERIENCE.key(), PersistentDataType.LONG) && dataContainer.get(Keys.COOLDOWN_EXPERIENCE.key(), PersistentDataType.LONG) <= System.currentTimeMillis(); return dataContainer.has(Keys.COOLDOWN_EXPERIENCE.key(), PersistentDataType.LONG) && dataContainer.get(Keys.COOLDOWN_EXPERIENCE.key(), PersistentDataType.LONG) <= System.currentTimeMillis();
} }
public long saveWorldTime() { public boolean canRestock(final long cooldown_millis) {
final long worldTime = villager.getWorld().getFullTime(); final long lastRestock = getRestockTimestamp();
dataContainer.set(Keys.WORLDTIME.key(), PersistentDataType.LONG, worldTime); if (lastRestock == 0L) return true;
return worldTime; return lastRestock + cooldown_millis <= villager.getWorld().getFullTime();
} }
public long getSavedWorldTime() { public void restock() {
return dataContainer.has(Keys.WORLDTIME.key(), PersistentDataType.LONG) ? dataContainer.get(Keys.WORLDTIME.key(), PersistentDataType.LONG) : saveWorldTime(); villager.getRecipes().forEach(recipe -> recipe.setUses(0));
saveRestockTimestamp();
}
public void saveRestockTimestamp() {
dataContainer.set(Keys.WORLDTIME.key(), PersistentDataType.LONG, villager.getWorld().getFullTime());
}
public long getRestockTimestamp() {
return dataContainer.has(Keys.WORLDTIME.key(), PersistentDataType.LONG) ? dataContainer.get(Keys.WORLDTIME.key(), PersistentDataType.LONG) : 0L;
} }
} }

View File

@ -1,10 +1,13 @@
package me.xginko.villageroptimizer.modules; package me.xginko.villageroptimizer.modules;
import me.xginko.villageroptimizer.VillagerOptimizer; import me.xginko.villageroptimizer.VillagerOptimizer;
import me.xginko.villageroptimizer.config.Config;
import me.xginko.villageroptimizer.cache.VillagerManager; import me.xginko.villageroptimizer.cache.VillagerManager;
import me.xginko.villageroptimizer.config.Config;
import me.xginko.villageroptimizer.models.WrappedVillager; import me.xginko.villageroptimizer.models.WrappedVillager;
import me.xginko.villageroptimizer.utils.CommonUtils;
import net.kyori.adventure.text.TextReplacementConfig;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager; import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@ -16,7 +19,7 @@ public class RestockTrades implements VillagerOptimizerModule, Listener {
private final VillagerManager villagerManager; private final VillagerManager villagerManager;
private final long restock_delay; private final long restock_delay;
private final boolean shouldLog; private final boolean shouldLog, notifyPlayer;
protected RestockTrades() { protected RestockTrades() {
this.villagerManager = VillagerOptimizer.getVillagerManager(); this.villagerManager = VillagerOptimizer.getVillagerManager();
@ -25,8 +28,10 @@ public class RestockTrades implements VillagerOptimizerModule, Listener {
This is for automatic restocking of trades for optimized villagers. Optimized Villagers\s This is for automatic restocking of trades for optimized villagers. Optimized Villagers\s
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 = config.getInt("optimization.trade-restocking.delay-in-ticks", 1200); this.restock_delay = config.getInt("optimization.trade-restocking.delay-in-ticks", 1200) * 50L;
this.shouldLog = config.getBoolean("optimization.trade-restocking.log", false); this.shouldLog = config.getBoolean("optimization.trade-restocking.log", false);
this.notifyPlayer = config.getBoolean("optimization.trade-restocking.notify-player", true,
"Sends the player a message when the trades were restocked on a clicked villager.");
} }
@Override @Override
@ -48,9 +53,19 @@ public class RestockTrades implements VillagerOptimizerModule, Listener {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
private void onInteract(PlayerInteractEntityEvent event) { private void onInteract(PlayerInteractEntityEvent event) {
if (!event.getRightClicked().getType().equals(EntityType.VILLAGER)) return; if (!event.getRightClicked().getType().equals(EntityType.VILLAGER)) return;
WrappedVillager wVillager = villagerManager.getOrAdd((Villager) event.getRightClicked()); WrappedVillager wVillager = villagerManager.getOrAdd((Villager) event.getRightClicked());
if (!wVillager.isOptimized()) return;
if (wVillager.isOptimized() && wVillager.canRestock(restock_delay)) {
wVillager.restock();
if (notifyPlayer) {
Player player = event.getPlayer();
VillagerOptimizer.getLang(player.locale()).trades_restocked.forEach(line -> player.sendMessage(line
.replaceText(TextReplacementConfig.builder().matchLiteral("%time%").replacement(CommonUtils.formatTime(restock_delay)).build()))
);
}
if (shouldLog)
VillagerOptimizer.getLog().info("Restocked optimized villager at "+ wVillager.villager().getLocation());
}
} }
} }

View File

@ -7,7 +7,7 @@ import java.util.logging.Level;
public class LogUtils { public class LogUtils {
public static void moduleLog(Level logLevel, String path, String logMessage) { public static void moduleLog(Level logLevel, String path, String logMessage) {
VillagerOptimizer.getLog().log(logLevel, "<" + path + "> " + logMessage); VillagerOptimizer.getLog().log(logLevel, "(" + path + ") " + logMessage);
} }
public static void materialNotRecognized(String path, String material) { public static void materialNotRecognized(String path, String material) {

View File

@ -1,5 +1,7 @@
messages: messages:
no-permission: "<red>You don't have permission to use this command." no-permission: "<red>You don't have permission to use this command."
restock-success:
- "<green>All trades restocked! Next restock in %time%"
nametag: nametag:
optimize-success: optimize-success:
- "<green>Successfully optimized villager by using a nametag." - "<green>Successfully optimized villager by using a nametag."