From 32f667f6f3edb139ab1d5acf314a8f12a6af83ca Mon Sep 17 00:00:00 2001 From: Traben Date: Fri, 24 Nov 2023 22:31:14 +1000 Subject: [PATCH] rewrite #11 rewrote spawn condition handling --- .../features/ETFManager.java | 6 ++-- .../PropertiesRandomProvider.java | 31 ++++++++++++++---- .../property_reading/RandomPropertyRule.java | 26 ++++++++------- .../properties/RandomProperty.java | 27 ++++++++-------- .../utils/EntityBooleanLRU.java | 32 +++++++++++++++++++ 5 files changed, 88 insertions(+), 34 deletions(-) create mode 100644 common/src/main/java/traben/entity_texture_features/utils/EntityBooleanLRU.java diff --git a/common/src/main/java/traben/entity_texture_features/features/ETFManager.java b/common/src/main/java/traben/entity_texture_features/features/ETFManager.java index cfd21e27..cbe71a6d 100644 --- a/common/src/main/java/traben/entity_texture_features/features/ETFManager.java +++ b/common/src/main/java/traben/entity_texture_features/features/ETFManager.java @@ -15,7 +15,6 @@ import traben.entity_texture_features.config.screens.skin.ETFConfigScreenSkinTool; import traben.entity_texture_features.features.player.ETFPlayerEntity; import traben.entity_texture_features.features.player.ETFPlayerTexture; -import traben.entity_texture_features.features.property_reading.properties.RandomProperty; import traben.entity_texture_features.features.texture_handlers.ETFDirectory; import traben.entity_texture_features.features.texture_handlers.ETFTexture; import traben.entity_texture_features.features.texture_handlers.ETFTextureVariator; @@ -45,7 +44,7 @@ public class ETFManager { public final Object2IntOpenHashMap> ENTITY_TYPE_VANILLA_BRIGHTNESS_OVERRIDE_VALUE = new Object2IntOpenHashMap<>(); public final ObjectOpenHashSet> ENTITY_TYPE_IGNORE_PARTICLES = new ObjectOpenHashSet<>(); public final Object2IntOpenHashMap> ENTITY_TYPE_RENDER_LAYER = new Object2IntOpenHashMap<>(); - public final ETFLruCache> ENTITY_SPAWN_CONDITIONS_CACHE = new ETFLruCache<>(); +// public final ETFLruCache> ENTITY_SPAWN_CONDITIONS_CACHE = new ETFLruCache<>(); //this is a cache of all known ETFTexture versions of any existing resource-pack texture, used to prevent remaking objects public final Object2ReferenceOpenHashMap<@NotNull Identifier, @Nullable ETFTexture> ETF_TEXTURE_CACHE = new Object2ReferenceOpenHashMap<>(); // public final Object2BooleanOpenHashMap ENTITY_IS_UPDATABLE = new Object2BooleanOpenHashMap<>(); @@ -178,7 +177,7 @@ public void grabSpecialProperties(Properties props, ETFEntity entity) { public void removeThisEntityDataFromAllStorage(UUID uuid) { //todo still needed? expand? - ENTITY_SPAWN_CONDITIONS_CACHE.removeEntryOnly(uuid); +// ENTITY_SPAWN_CONDITIONS_CACHE.removeEntryOnly(uuid); // ENTITY_IS_UPDATABLE.removeBoolean(uuid); ENTITY_DEBUG_QUEUE.remove(uuid); ENTITY_BLINK_TIME.removeLong(uuid); @@ -306,4 +305,5 @@ public int put(UUID uuid, int v) { return this.putAndMoveToFirst(uuid, v); } } + } diff --git a/common/src/main/java/traben/entity_texture_features/features/property_reading/PropertiesRandomProvider.java b/common/src/main/java/traben/entity_texture_features/features/property_reading/PropertiesRandomProvider.java index ffb11d73..d63e3cc7 100644 --- a/common/src/main/java/traben/entity_texture_features/features/property_reading/PropertiesRandomProvider.java +++ b/common/src/main/java/traben/entity_texture_features/features/property_reading/PropertiesRandomProvider.java @@ -1,7 +1,6 @@ package traben.entity_texture_features.features.property_reading; import it.unimi.dsi.fastutil.ints.IntOpenHashSet; -import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap; import net.minecraft.client.MinecraftClient; import net.minecraft.resource.Resource; import net.minecraft.resource.ResourceManager; @@ -15,6 +14,7 @@ import traben.entity_texture_features.features.texture_handlers.ETFDirectory; import traben.entity_texture_features.utils.ETFEntity; import traben.entity_texture_features.utils.ETFUtils2; +import traben.entity_texture_features.utils.EntityBooleanLRU; import java.util.*; @@ -23,7 +23,7 @@ public class PropertiesRandomProvider implements ETFApi.ETFVariantSuffixProvider protected final List propertyRules; - protected final Object2BooleanOpenHashMap entityCanUpdate = new Object2BooleanOpenHashMap<>(); + protected final EntityBooleanLRU entityCanUpdate = new EntityBooleanLRU(1000); protected final String packname; @@ -158,11 +158,30 @@ public int size() { public int getSuffixForETFEntity(ETFEntity entityToBeTested) { if (entityToBeTested == null) return 0; UUID id = entityToBeTested.etf$getUuid(); - boolean entityTestedBefore = entityCanUpdate.containsKey(id); - for (RandomPropertyRule testCase : propertyRules) { - if (testCase.doesEntityMeetConditionsOfThisCase(entityToBeTested, entityTestedBefore, entityCanUpdate)) { - return testCase.getVariantSuffixFromThisCase(id); + boolean entityHasBeenTestedBefore = entityCanUpdate.containsKey(id); + if(entityHasBeenTestedBefore){ + //return andNothingElse + for (RandomPropertyRule testCase : propertyRules) { + if (testCase.doesEntityMeetConditionsOfThisCase(entityToBeTested, true, entityCanUpdate)) { + return testCase.getVariantSuffixFromThisCase(id); + } + } + } else { + //return but capture spawn conditions of first time entity + int foundSuffix = -1; + for (RandomPropertyRule rule : propertyRules) { + if (rule.doesEntityMeetConditionsOfThisCase(entityToBeTested, false, entityCanUpdate)) { + foundSuffix = rule.getVariantSuffixFromThisCase(id); + break; + } + } + if(entityCanUpdate.getBoolean(entityToBeTested.etf$getUuid())) { + for (RandomPropertyRule rule : propertyRules) { + //cache entity spawns + rule.cacheEntityInitialResultsOfNonUpdatingProperties(entityToBeTested); + } } + return foundSuffix == -1 ? 0 : foundSuffix; } return 0; } diff --git a/common/src/main/java/traben/entity_texture_features/features/property_reading/RandomPropertyRule.java b/common/src/main/java/traben/entity_texture_features/features/property_reading/RandomPropertyRule.java index 982de28f..869f1f22 100644 --- a/common/src/main/java/traben/entity_texture_features/features/property_reading/RandomPropertyRule.java +++ b/common/src/main/java/traben/entity_texture_features/features/property_reading/RandomPropertyRule.java @@ -1,10 +1,10 @@ package traben.entity_texture_features.features.property_reading; -import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap; import traben.entity_texture_features.features.ETFManager; import traben.entity_texture_features.features.property_reading.properties.RandomProperty; import traben.entity_texture_features.utils.ETFEntity; import traben.entity_texture_features.utils.ETFUtils2; +import traben.entity_texture_features.utils.EntityBooleanLRU; import java.util.*; @@ -59,20 +59,12 @@ public Set getSuffixSet() { return new HashSet<>(List.of(SUFFIX_NUMBERS_WEIGHTED)); } - public boolean doesEntityMeetConditionsOfThisCase(ETFEntity etfEntity, boolean isUpdate, Object2BooleanOpenHashMap UUID_CaseHasUpdateablesCustom) { + public boolean doesEntityMeetConditionsOfThisCase(ETFEntity etfEntity, boolean isUpdate, EntityBooleanLRU UUID_CaseHasUpdateablesCustom) { if (RULE_ALWAYS_APPROVED) return true; if (etfEntity == null) return false; UUID id = etfEntity.etf$getUuid(); - Object2BooleanOpenHashMap spawnConditions; - if (ETFManager.getInstance().ENTITY_SPAWN_CONDITIONS_CACHE.containsKey(id)) { - spawnConditions = (ETFManager.getInstance().ENTITY_SPAWN_CONDITIONS_CACHE.get(id)); - } else { - spawnConditions = new Object2BooleanOpenHashMap<>(); - ETFManager.getInstance().ENTITY_SPAWN_CONDITIONS_CACHE.put(id, spawnConditions); - } - boolean wasEntityTestedByAnUpdatableProperty = false; boolean entityMetRequirements = true; try { @@ -81,7 +73,7 @@ public boolean doesEntityMeetConditionsOfThisCase(ETFEntity etfEntity, boolean i if (!entityMetRequirements) break; if (property.isPropertyUpdatable()) wasEntityTestedByAnUpdatableProperty = true; - entityMetRequirements = property.testEntity(etfEntity, isUpdate, spawnConditions); + entityMetRequirements = property.testEntity(etfEntity, isUpdate); } } catch (Exception e) { ETFUtils2.logWarn("Random Property file [" + @@ -105,4 +97,16 @@ public int getVariantSuffixFromThisCase(UUID uuid) { return SUFFIX_NUMBERS_WEIGHTED[randomSeededByUUID % SUFFIX_NUMBERS_WEIGHTED.length]; } + + public void cacheEntityInitialResultsOfNonUpdatingProperties(ETFEntity entity){ + try { + for (RandomProperty property : + PROPERTIES_TO_TEST) { + if (!property.isPropertyUpdatable()) { + property.cacheEntityInitialResult(entity); + } + } + } catch (Exception ignored) {} + } + } diff --git a/common/src/main/java/traben/entity_texture_features/features/property_reading/properties/RandomProperty.java b/common/src/main/java/traben/entity_texture_features/features/property_reading/properties/RandomProperty.java index 80a093e9..22828fa4 100644 --- a/common/src/main/java/traben/entity_texture_features/features/property_reading/properties/RandomProperty.java +++ b/common/src/main/java/traben/entity_texture_features/features/property_reading/properties/RandomProperty.java @@ -1,8 +1,8 @@ package traben.entity_texture_features.features.property_reading.properties; -import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap; import org.jetbrains.annotations.NotNull; import traben.entity_texture_features.utils.ETFEntity; +import traben.entity_texture_features.utils.EntityBooleanLRU; import java.util.Properties; @@ -44,28 +44,21 @@ public static String readPropertiesOrThrow(Properties properties, int propertyNu * * @param entity the ETFEntity being tested by this property * @param isUpdate flags if this test is part of an update - * @param spawnConditions the original spawn conditions map of this entity, possibly holding a prior value for this test * @return true if the entity meets the requirements of this property */ - public boolean testEntity(ETFEntity entity, boolean isUpdate, Object2BooleanOpenHashMap spawnConditions) { + public boolean testEntity(ETFEntity entity, boolean isUpdate) { + if (isUpdate && !isPropertyUpdatable()) { + return entityCachedInitialResult.getBoolean(entity.etf$getUuid());//false default value + } try { - if (isUpdate && !isPropertyUpdatable()) { - if (spawnConditions.containsKey(this)) { - return spawnConditions.getBoolean(this); - } - return false; - } - boolean result = testEntityInternal(entity); - if (!isPropertyUpdatable()) - spawnConditions.put(this, result); - return result; + return testEntityInternal(entity); } catch (Exception ignored) { return false; } } /** - * Internal abstract method functionality for {@link RandomProperty#testEntity(ETFEntity, boolean, Object2BooleanOpenHashMap)} + * Internal abstract method functionality for {@link RandomProperty#testEntity(ETFEntity, boolean)} */ protected abstract boolean testEntityInternal(ETFEntity entity); @@ -132,4 +125,10 @@ public RandomPropertyException(String reason) { super("[ETF] " + reason); } } + + protected EntityBooleanLRU entityCachedInitialResult = new EntityBooleanLRU(); + + public void cacheEntityInitialResult(ETFEntity entity){ + entityCachedInitialResult.put(entity.etf$getUuid(),testEntityInternal(entity)); + } } diff --git a/common/src/main/java/traben/entity_texture_features/utils/EntityBooleanLRU.java b/common/src/main/java/traben/entity_texture_features/utils/EntityBooleanLRU.java new file mode 100644 index 00000000..c8d129aa --- /dev/null +++ b/common/src/main/java/traben/entity_texture_features/utils/EntityBooleanLRU.java @@ -0,0 +1,32 @@ +package traben.entity_texture_features.utils; + +import it.unimi.dsi.fastutil.objects.Object2BooleanLinkedOpenHashMap; + +import java.util.UUID; + +public class EntityBooleanLRU extends Object2BooleanLinkedOpenHashMap { + { + defaultReturnValue(false); + } + + final int capacity; + + public EntityBooleanLRU(int capacity) { + this.capacity = capacity; + } + + public EntityBooleanLRU() { + this.capacity = 2048; + } + + @Override + public boolean put(UUID uuid, boolean v) { + if (size() >= capacity) { + UUID lastKey = lastKey(); + if (!lastKey.equals(uuid)) { + removeBoolean(lastKey); + } + } + return this.putAndMoveToFirst(uuid, v); + } +}