wrapper data accuracy fixes and improvements

This commit is contained in:
xGinko 2024-03-31 18:34:08 +02:00
parent 2d1a9a4fec
commit 41d2bc4935
6 changed files with 40 additions and 38 deletions

View File

@ -22,6 +22,10 @@ public final class VillagerCache {
return this.villagerCache.asMap(); return this.villagerCache.asMap();
} }
public void clear() {
this.villagerCache.asMap().clear();
}
public @NotNull WrappedVillager getOrAdd(@NotNull Villager villager) { public @NotNull WrappedVillager getOrAdd(@NotNull Villager villager) {
WrappedVillager wrappedVillager = this.villagerCache.getIfPresent(villager.getUniqueId()); WrappedVillager wrappedVillager = this.villagerCache.getIfPresent(villager.getUniqueId());
return wrappedVillager == null ? this.add(new WrappedVillager(villager)) : this.add(wrappedVillager); return wrappedVillager == null ? this.add(new WrappedVillager(villager)) : this.add(wrappedVillager);

View File

@ -99,7 +99,7 @@ public final class VillagerOptimizer extends JavaPlugin {
foliaLib = null; foliaLib = null;
} }
if (villagerCache != null) { if (villagerCache != null) {
villagerCache.cacheMap().clear(); villagerCache.clear();
villagerCache = null; villagerCache = null;
} }
if (audiences != null) { if (audiences != null) {
@ -153,6 +153,7 @@ public final class VillagerOptimizer extends JavaPlugin {
private void reloadConfiguration() { private void reloadConfiguration() {
try { try {
config = new Config(); config = new Config();
if (villagerCache != null) villagerCache.clear();
villagerCache = new VillagerCache(config.cache_keep_time_seconds); villagerCache = new VillagerCache(config.cache_keep_time_seconds);
VillagerOptimizerCommand.reloadCommands(); VillagerOptimizerCommand.reloadCommands();
VillagerOptimizerModule.reloadModules(); VillagerOptimizerModule.reloadModules();

View File

@ -12,8 +12,8 @@ import java.util.concurrent.TimeUnit;
public class AVLVillagerDataHandlerImpl implements VillagerDataHandler { public class AVLVillagerDataHandlerImpl implements VillagerDataHandler {
private final @NotNull Villager villager; private @NotNull Villager villager;
private final @NotNull PersistentDataContainer dataContainer; private @NotNull PersistentDataContainer dataContainer;
AVLVillagerDataHandlerImpl(@NotNull Villager villager) { AVLVillagerDataHandlerImpl(@NotNull Villager villager) {
this.villager = villager; this.villager = villager;
@ -33,13 +33,13 @@ public class AVLVillagerDataHandlerImpl implements VillagerDataHandler {
} }
@Override @Override
public boolean canOptimize(final long cooldown_millis) { public boolean canOptimize(long cooldown_millis) {
return dataContainer.has(Keyring.AntiVillagerLag.NEXT_OPTIMIZATION_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG) return !dataContainer.has(Keyring.AntiVillagerLag.NEXT_OPTIMIZATION_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG)
&& System.currentTimeMillis() > TimeUnit.SECONDS.toMillis(dataContainer.get(Keyring.AntiVillagerLag.NEXT_OPTIMIZATION_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG)); || System.currentTimeMillis() > TimeUnit.SECONDS.toMillis(dataContainer.get(Keyring.AntiVillagerLag.NEXT_OPTIMIZATION_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG));
} }
@Override @Override
public void setOptimizationType(final OptimizationType type) { public void setOptimizationType(OptimizationType type) {
VillagerOptimizer.getFoliaLib().getImpl().runAtEntityTimer(villager, setOptimization -> { VillagerOptimizer.getFoliaLib().getImpl().runAtEntityTimer(villager, setOptimization -> {
// Keep repeating task until villager is no longer trading with a player // Keep repeating task until villager is no longer trading with a player
if (villager.isTrading()) return; if (villager.isTrading()) return;
@ -97,7 +97,7 @@ public class AVLVillagerDataHandlerImpl implements VillagerDataHandler {
} }
@Override @Override
public long getOptimizeCooldownMillis(final long cooldown_millis) { public long getOptimizeCooldownMillis(long cooldown_millis) {
if (dataContainer.has(Keyring.AntiVillagerLag.NEXT_OPTIMIZATION_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG)) { if (dataContainer.has(Keyring.AntiVillagerLag.NEXT_OPTIMIZATION_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG)) {
return Math.max(cooldown_millis, System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(dataContainer.get(Keyring.AntiVillagerLag.NEXT_OPTIMIZATION_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG))); return Math.max(cooldown_millis, System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(dataContainer.get(Keyring.AntiVillagerLag.NEXT_OPTIMIZATION_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG)));
} }
@ -105,7 +105,7 @@ public class AVLVillagerDataHandlerImpl implements VillagerDataHandler {
} }
@Override @Override
public boolean canRestock(final long cooldown_millis) { public boolean canRestock(long cooldown_millis) {
if (dataContainer.has(Keyring.AntiVillagerLag.LAST_RESTOCK_WORLDFULLTIME.getKey(), PersistentDataType.LONG)) { if (dataContainer.has(Keyring.AntiVillagerLag.LAST_RESTOCK_WORLDFULLTIME.getKey(), PersistentDataType.LONG)) {
return villager.getWorld().getFullTime() > dataContainer.get(Keyring.AntiVillagerLag.LAST_RESTOCK_WORLDFULLTIME.getKey(), PersistentDataType.LONG); return villager.getWorld().getFullTime() > dataContainer.get(Keyring.AntiVillagerLag.LAST_RESTOCK_WORLDFULLTIME.getKey(), PersistentDataType.LONG);
} }
@ -118,14 +118,14 @@ public class AVLVillagerDataHandlerImpl implements VillagerDataHandler {
} }
@Override @Override
public long getRestockCooldownMillis(final long cooldown_millis) { public long getRestockCooldownMillis(long cooldown_millis) {
if (dataContainer.has(Keyring.AntiVillagerLag.LAST_RESTOCK_WORLDFULLTIME.getKey(), PersistentDataType.LONG)) if (dataContainer.has(Keyring.AntiVillagerLag.LAST_RESTOCK_WORLDFULLTIME.getKey(), PersistentDataType.LONG))
return (villager.getWorld().getFullTime() - dataContainer.get(Keyring.AntiVillagerLag.LAST_RESTOCK_WORLDFULLTIME.getKey(), PersistentDataType.LONG)) * 50L; return (villager.getWorld().getFullTime() - dataContainer.get(Keyring.AntiVillagerLag.LAST_RESTOCK_WORLDFULLTIME.getKey(), PersistentDataType.LONG)) * 50L;
return cooldown_millis; return cooldown_millis;
} }
@Override @Override
public boolean canLevelUp(final long cooldown_millis) { public boolean canLevelUp(long cooldown_millis) {
return !dataContainer.has(Keyring.AntiVillagerLag.NEXT_LEVELUP_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG) return !dataContainer.has(Keyring.AntiVillagerLag.NEXT_LEVELUP_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG)
|| System.currentTimeMillis() > TimeUnit.SECONDS.toMillis(dataContainer.get(Keyring.AntiVillagerLag.NEXT_LEVELUP_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG)); || System.currentTimeMillis() > TimeUnit.SECONDS.toMillis(dataContainer.get(Keyring.AntiVillagerLag.NEXT_LEVELUP_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG));
} }
@ -136,7 +136,7 @@ public class AVLVillagerDataHandlerImpl implements VillagerDataHandler {
} }
@Override @Override
public long getLevelCooldownMillis(final long cooldown_millis) { public long getLevelCooldownMillis(long cooldown_millis) {
if (dataContainer.has(Keyring.AntiVillagerLag.NEXT_LEVELUP_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG)) if (dataContainer.has(Keyring.AntiVillagerLag.NEXT_LEVELUP_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG))
return System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(dataContainer.get(Keyring.AntiVillagerLag.NEXT_LEVELUP_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG)); return System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(dataContainer.get(Keyring.AntiVillagerLag.NEXT_LEVELUP_SYSTIME_SECONDS.getKey(), PersistentDataType.LONG));
return cooldown_millis; return cooldown_millis;

View File

@ -10,10 +10,10 @@ import org.jetbrains.annotations.NotNull;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public final class MainVillagerDataHandlerImpl implements VillagerDataHandler { public class MainVillagerDataHandlerImpl implements VillagerDataHandler {
private final @NotNull Villager villager; private @NotNull Villager villager;
private final @NotNull PersistentDataContainer dataContainer; private @NotNull PersistentDataContainer dataContainer;
MainVillagerDataHandlerImpl(@NotNull Villager villager) { MainVillagerDataHandlerImpl(@NotNull Villager villager) {
this.villager = villager; this.villager = villager;
@ -31,20 +31,19 @@ public final class MainVillagerDataHandlerImpl implements VillagerDataHandler {
} }
@Override @Override
public boolean canOptimize(final long cooldown_millis) { public boolean canOptimize(long cooldown_millis) {
return System.currentTimeMillis() > getLastOptimize() + cooldown_millis; return System.currentTimeMillis() > getLastOptimize() + cooldown_millis;
} }
@Override @Override
public void setOptimizationType(final OptimizationType type) { public void setOptimizationType(OptimizationType type) {
VillagerOptimizer.getFoliaLib().getImpl().runAtEntityTimer(villager, setOptimization -> { VillagerOptimizer.getFoliaLib().getImpl().runAtEntityTimer(villager, setOptimization -> {
// Keep repeating task until villager is no longer trading with a player // Keep repeating task until villager is no longer trading with a player
if (villager.isTrading()) return; if (villager.isTrading()) return;
if (type == OptimizationType.NONE) { if (type == OptimizationType.NONE) {
if (isOptimized()) { if (isOptimized())
dataContainer.remove(Keyring.VillagerOptimizer.OPTIMIZATION_TYPE.getKey()); dataContainer.remove(Keyring.VillagerOptimizer.OPTIMIZATION_TYPE.getKey());
}
villager.setAware(true); villager.setAware(true);
villager.setAI(true); villager.setAI(true);
} else { } else {
@ -82,12 +81,12 @@ public final class MainVillagerDataHandlerImpl implements VillagerDataHandler {
} }
@Override @Override
public long getOptimizeCooldownMillis(final long cooldown_millis) { public long getOptimizeCooldownMillis(long cooldown_millis) {
return Math.max(System.currentTimeMillis() - getLastOptimize(), cooldown_millis); return Math.max(System.currentTimeMillis() - getLastOptimize(), cooldown_millis);
} }
@Override @Override
public boolean canRestock(final long cooldown_millis) { public boolean canRestock(long cooldown_millis) {
return getLastRestock() + cooldown_millis <= System.currentTimeMillis(); return getLastRestock() + cooldown_millis <= System.currentTimeMillis();
} }
@ -100,22 +99,21 @@ public final class MainVillagerDataHandlerImpl implements VillagerDataHandler {
* @return The time when the entity was last restocked. * @return The time when the entity was last restocked.
*/ */
public long getLastRestock() { public long getLastRestock() {
long lastRestock = 0L;
if (dataContainer.has(Keyring.VillagerOptimizer.LAST_RESTOCK_SYSTIME_MILLIS.getKey(), PersistentDataType.LONG)) { if (dataContainer.has(Keyring.VillagerOptimizer.LAST_RESTOCK_SYSTIME_MILLIS.getKey(), PersistentDataType.LONG)) {
lastRestock = dataContainer.get(Keyring.VillagerOptimizer.LAST_RESTOCK_SYSTIME_MILLIS.getKey(), PersistentDataType.LONG); return dataContainer.get(Keyring.VillagerOptimizer.LAST_RESTOCK_SYSTIME_MILLIS.getKey(), PersistentDataType.LONG);
} }
return lastRestock; return 0L;
} }
@Override @Override
public long getRestockCooldownMillis(final long cooldown_millis) { public long getRestockCooldownMillis(long cooldown_millis) {
if (dataContainer.has(Keyring.VillagerOptimizer.LAST_RESTOCK_SYSTIME_MILLIS.getKey(), PersistentDataType.LONG)) if (dataContainer.has(Keyring.VillagerOptimizer.LAST_RESTOCK_SYSTIME_MILLIS.getKey(), PersistentDataType.LONG))
return System.currentTimeMillis() - (dataContainer.get(Keyring.VillagerOptimizer.LAST_RESTOCK_SYSTIME_MILLIS.getKey(), PersistentDataType.LONG) + cooldown_millis); return System.currentTimeMillis() - (dataContainer.get(Keyring.VillagerOptimizer.LAST_RESTOCK_SYSTIME_MILLIS.getKey(), PersistentDataType.LONG) + cooldown_millis);
return cooldown_millis; return cooldown_millis;
} }
@Override @Override
public boolean canLevelUp(final long cooldown_millis) { public boolean canLevelUp(long cooldown_millis) {
return System.currentTimeMillis() >= getLastLevelUpTime() + cooldown_millis; return System.currentTimeMillis() >= getLastLevelUpTime() + cooldown_millis;
} }
@ -134,7 +132,7 @@ public final class MainVillagerDataHandlerImpl implements VillagerDataHandler {
} }
@Override @Override
public long getLevelCooldownMillis(final long cooldown_millis) { public long getLevelCooldownMillis(long cooldown_millis) {
if (dataContainer.has(Keyring.VillagerOptimizer.LAST_LEVELUP_SYSTIME_MILLIS.getKey(), PersistentDataType.LONG)) if (dataContainer.has(Keyring.VillagerOptimizer.LAST_LEVELUP_SYSTIME_MILLIS.getKey(), PersistentDataType.LONG))
return System.currentTimeMillis() - (dataContainer.get(Keyring.VillagerOptimizer.LAST_LEVELUP_SYSTIME_MILLIS.getKey(), PersistentDataType.LONG) + cooldown_millis); return System.currentTimeMillis() - (dataContainer.get(Keyring.VillagerOptimizer.LAST_LEVELUP_SYSTIME_MILLIS.getKey(), PersistentDataType.LONG) + cooldown_millis);
return cooldown_millis; return cooldown_millis;

View File

@ -32,12 +32,12 @@ public interface VillagerDataHandler {
* @param cooldown_millis The configured cooldown in millis until the next optimization is allowed to occur. * @param cooldown_millis The configured cooldown in millis until the next optimization is allowed to occur.
* @return True if villager can be optimized again, otherwise false. * @return True if villager can be optimized again, otherwise false.
*/ */
boolean canOptimize(final long cooldown_millis); boolean canOptimize(long cooldown_millis);
/** /**
* @param type OptimizationType the villager should be set to. * @param type OptimizationType the villager should be set to.
*/ */
void setOptimizationType(final OptimizationType type); void setOptimizationType(OptimizationType type);
/** /**
* @return The current OptimizationType of the villager. * @return The current OptimizationType of the villager.
@ -57,7 +57,7 @@ public interface VillagerDataHandler {
* @param cooldown_millis The configured cooldown in milliseconds you want to check against. * @param cooldown_millis The configured cooldown in milliseconds you want to check against.
* @return The time left in millis until the villager can be optimized again. * @return The time left in millis until the villager can be optimized again.
*/ */
long getOptimizeCooldownMillis(final long cooldown_millis); long getOptimizeCooldownMillis(long cooldown_millis);
/** /**
* For convenience so the remaining millis since the last stored restock time * For convenience so the remaining millis since the last stored restock time
@ -66,7 +66,7 @@ public interface VillagerDataHandler {
* @param cooldown_millis The configured cooldown in milliseconds you want to check against. * @param cooldown_millis The configured cooldown in milliseconds you want to check against.
* @return True if the villager has been loaded long enough. * @return True if the villager has been loaded long enough.
*/ */
boolean canRestock(final long cooldown_millis); boolean canRestock(long cooldown_millis);
/** /**
* Saves the time of when the entity was last restocked. * Saves the time of when the entity was last restocked.
@ -81,13 +81,13 @@ public interface VillagerDataHandler {
* @param cooldown_millis The configured cooldown in milliseconds you want to check against. * @param cooldown_millis The configured cooldown in milliseconds you want to check against.
* @return The time left in millis until the villager can be restocked again. * @return The time left in millis until the villager can be restocked again.
*/ */
long getRestockCooldownMillis(final long cooldown_millis); long getRestockCooldownMillis(long cooldown_millis);
/** /**
* @param cooldown_millis The configured cooldown in milliseconds you want to check against. * @param cooldown_millis The configured cooldown in milliseconds you want to check against.
* @return Whether the villager can be leveled up or not with the checked milliseconds * @return Whether the villager can be leveled up or not with the checked milliseconds
*/ */
boolean canLevelUp(final long cooldown_millis); boolean canLevelUp(long cooldown_millis);
/** /**
* Saves the time of the in-game world when the entity was last leveled up. * Saves the time of the in-game world when the entity was last leveled up.
@ -100,6 +100,5 @@ public interface VillagerDataHandler {
* *
* @return The time of the in-game world when the entity was last leveled up. * @return The time of the in-game world when the entity was last leveled up.
*/ */
long getLevelCooldownMillis(final long cooldown_millis); long getLevelCooldownMillis(long cooldown_millis);
} }

View File

@ -86,11 +86,11 @@ public class WrappedVillager implements VillagerDataHandler {
@Override @Override
public boolean canOptimize(long cooldown_millis) { public boolean canOptimize(long cooldown_millis) {
for (VillagerDataHandler handler : dataHandlers) { for (VillagerDataHandler handler : dataHandlers) {
if (handler.canOptimize(cooldown_millis)) { if (!handler.canOptimize(cooldown_millis)) {
return true; return false;
} }
} }
return false; return true;
} }
@Override @Override