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();
}
public void clear() {
this.villagerCache.asMap().clear();
}
public @NotNull WrappedVillager getOrAdd(@NotNull Villager villager) {
WrappedVillager wrappedVillager = this.villagerCache.getIfPresent(villager.getUniqueId());
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;
}
if (villagerCache != null) {
villagerCache.cacheMap().clear();
villagerCache.clear();
villagerCache = null;
}
if (audiences != null) {
@ -153,6 +153,7 @@ public final class VillagerOptimizer extends JavaPlugin {
private void reloadConfiguration() {
try {
config = new Config();
if (villagerCache != null) villagerCache.clear();
villagerCache = new VillagerCache(config.cache_keep_time_seconds);
VillagerOptimizerCommand.reloadCommands();
VillagerOptimizerModule.reloadModules();

View File

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

View File

@ -10,10 +10,10 @@ import org.jetbrains.annotations.NotNull;
import java.util.concurrent.TimeUnit;
public final class MainVillagerDataHandlerImpl implements VillagerDataHandler {
public class MainVillagerDataHandlerImpl implements VillagerDataHandler {
private final @NotNull Villager villager;
private final @NotNull PersistentDataContainer dataContainer;
private @NotNull Villager villager;
private @NotNull PersistentDataContainer dataContainer;
MainVillagerDataHandlerImpl(@NotNull Villager villager) {
this.villager = villager;
@ -31,20 +31,19 @@ public final class MainVillagerDataHandlerImpl implements VillagerDataHandler {
}
@Override
public boolean canOptimize(final long cooldown_millis) {
public boolean canOptimize(long cooldown_millis) {
return System.currentTimeMillis() > getLastOptimize() + cooldown_millis;
}
@Override
public void setOptimizationType(final OptimizationType type) {
public void setOptimizationType(OptimizationType type) {
VillagerOptimizer.getFoliaLib().getImpl().runAtEntityTimer(villager, setOptimization -> {
// Keep repeating task until villager is no longer trading with a player
if (villager.isTrading()) return;
if (type == OptimizationType.NONE) {
if (isOptimized()) {
if (isOptimized())
dataContainer.remove(Keyring.VillagerOptimizer.OPTIMIZATION_TYPE.getKey());
}
villager.setAware(true);
villager.setAI(true);
} else {
@ -82,12 +81,12 @@ public final class MainVillagerDataHandlerImpl implements VillagerDataHandler {
}
@Override
public long getOptimizeCooldownMillis(final long cooldown_millis) {
public long getOptimizeCooldownMillis(long cooldown_millis) {
return Math.max(System.currentTimeMillis() - getLastOptimize(), cooldown_millis);
}
@Override
public boolean canRestock(final long cooldown_millis) {
public boolean canRestock(long cooldown_millis) {
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.
*/
public long getLastRestock() {
long lastRestock = 0L;
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
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))
return System.currentTimeMillis() - (dataContainer.get(Keyring.VillagerOptimizer.LAST_RESTOCK_SYSTIME_MILLIS.getKey(), PersistentDataType.LONG) + cooldown_millis);
return cooldown_millis;
}
@Override
public boolean canLevelUp(final long cooldown_millis) {
public boolean canLevelUp(long cooldown_millis) {
return System.currentTimeMillis() >= getLastLevelUpTime() + cooldown_millis;
}
@ -134,7 +132,7 @@ public final class MainVillagerDataHandlerImpl implements VillagerDataHandler {
}
@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))
return System.currentTimeMillis() - (dataContainer.get(Keyring.VillagerOptimizer.LAST_LEVELUP_SYSTIME_MILLIS.getKey(), PersistentDataType.LONG) + 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.
* @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.
*/
void setOptimizationType(final OptimizationType type);
void setOptimizationType(OptimizationType type);
/**
* @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.
* @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
@ -66,7 +66,7 @@ public interface VillagerDataHandler {
* @param cooldown_millis The configured cooldown in milliseconds you want to check against.
* @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.
@ -81,13 +81,13 @@ public interface VillagerDataHandler {
* @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.
*/
long getRestockCooldownMillis(final long cooldown_millis);
long getRestockCooldownMillis(long cooldown_millis);
/**
* @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
*/
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.
@ -100,6 +100,5 @@ public interface VillagerDataHandler {
*
* @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,12 +86,12 @@ public class WrappedVillager implements VillagerDataHandler {
@Override
public boolean canOptimize(long cooldown_millis) {
for (VillagerDataHandler handler : dataHandlers) {
if (handler.canOptimize(cooldown_millis)) {
return true;
}
}
if (!handler.canOptimize(cooldown_millis)) {
return false;
}
}
return true;
}
@Override
public void setOptimizationType(OptimizationType type) {