Add predicate caching

This commit is contained in:
Eclipse 2024-12-19 11:09:43 +00:00
parent 885c4d9d3b
commit 25109b71b3
No known key found for this signature in database
GPG key ID: 95E6998F82EC938A

View file

@ -26,6 +26,8 @@
package org.geysermc.geyser.translator.item;
import com.google.common.collect.Multimap;
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
import net.kyori.adventure.key.Key;
import org.cloudburstmc.protocol.bedrock.data.TrimMaterial;
import org.geysermc.geyser.api.item.custom.v2.predicate.CustomItemPredicate;
@ -78,10 +80,13 @@ public final class CustomItemTranslator {
return null;
}
// Cache predicate values so they're not recalculated every time when there are multiple item definitions using the same predicates
Object2BooleanMap<CustomItemPredicate> calculatedPredicates = new Object2BooleanOpenHashMap<>();
for (GeyserCustomMappingData customMapping : customItems) {
boolean allMatch = true;
for (CustomItemPredicate predicate : customMapping.definition().predicates()) {
if (!predicateMatches(session, predicate, stackSize, components)) {
boolean value = calculatedPredicates.computeIfAbsent(predicate, x -> predicateMatches(session, predicate, stackSize, components));
if (!value) {
allMatch = false;
break;
}