From 4d4f4243788267392de84c19e08e1a7e58316199 Mon Sep 17 00:00:00 2001 From: Anastasia Chrysanthemum Date: Fri, 22 May 2026 01:43:23 +1000 Subject: [PATCH] refactor: Bind components early then unbind to allow for early obtaining, similar to Fabric and NeoForge APIs. --- .../DefaultBlockEntityComponentsImpl.java | 39 +++--------------- .../DefaultEntityComponentsImpl.java | 41 +++---------------- 2 files changed, 10 insertions(+), 70 deletions(-) diff --git a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/component/DefaultBlockEntityComponentsImpl.java b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/component/DefaultBlockEntityComponentsImpl.java index 6d57290..2300f05 100644 --- a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/component/DefaultBlockEntityComponentsImpl.java +++ b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/component/DefaultBlockEntityComponentsImpl.java @@ -20,7 +20,6 @@ import java.util.*; import java.util.function.Predicate; public class DefaultBlockEntityComponentsImpl implements CowponentsDefaultBlockEntityComponentEvents.ModifyContext { - private static final Map>, Set>> COWPONENTS_ORIGINATED = new HashMap<>(); private final DataComponentMap.Builder builder; private final HolderLookup.Provider lookupProvider; private final BlockEntityType blockEntityType; @@ -31,46 +30,18 @@ public class DefaultBlockEntityComponentsImpl implements CowponentsDefaultBlockE this.blockEntityType = blockEntityType; } - public static Set> getCowponentsOriginatedComponents(Holder> holder) { - return COWPONENTS_ORIGINATED.getOrDefault(holder, Collections.emptySet()); - } - - public static void addCowponentsOriginatedComponent(Holder> holder, DataComponentType componentType) { - getCowponentsOriginatedComponents(holder).add(componentType); - } - @Override public void modify(Predicate> entityTypePredicate, CowponentsDefaultBlockEntityComponentEvents.ModifyConsumer builderConsumer) { if (!entityTypePredicate.test(blockEntityType)) return; //noinspection deprecation - Holder> holder = blockEntityType.builtInRegistryHolder(); - - DataComponentMap oldComponents = builder.build(); + Holder.Reference> holder = blockEntityType.builtInRegistryHolder(); + // Bind the current components early so they can be referenced directly from the block entity type. + holder.bindComponents(builder.build()); builderConsumer.modify(builder, lookupProvider, blockEntityType); - - DataComponentMap newComponents = builder.build(); - - Set> originated = new HashSet<>(COWPONENTS_ORIGINATED.getOrDefault(holder, Collections.emptySet())); - - for (TypedDataComponent component : oldComponents - .stream() - .filter(typed -> !newComponents.has(typed.type())) - .toList()) { - originated.remove(component.type()); - } - - for (TypedDataComponent component : newComponents - .stream() - .filter(typed -> !oldComponents.has(typed.type()) || !oldComponents.get(typed.type()).equals(typed.value())) - .toList()) { - originated.add(component.type()); - } - - if (!originated.isEmpty()) { - COWPONENTS_ORIGINATED.put(holder, originated); - } + //noinspection DataFlowIssue + holder.bindComponents(null); } } diff --git a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/DefaultEntityComponentsImpl.java b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/DefaultEntityComponentsImpl.java index f3452d3..bb2bb44 100644 --- a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/DefaultEntityComponentsImpl.java +++ b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/DefaultEntityComponentsImpl.java @@ -12,15 +12,12 @@ import lgbt.greenhouse.cowponents.entities.api.v1.event.CowponentsDefaultEntityC import net.minecraft.core.Holder; import net.minecraft.core.HolderLookup; import net.minecraft.core.component.DataComponentMap; -import net.minecraft.core.component.DataComponentType; -import net.minecraft.core.component.TypedDataComponent; import net.minecraft.world.entity.EntityType; import java.util.*; import java.util.function.Predicate; public class DefaultEntityComponentsImpl implements CowponentsDefaultEntityComponentEvents.ModifyContext { - private static final Map>, Set>> COWPONENTS_ORIGINATED = new HashMap<>(); private final DataComponentMap.Builder builder; private final HolderLookup.Provider lookupProvider; private final EntityType entityType; @@ -31,46 +28,18 @@ public class DefaultEntityComponentsImpl implements CowponentsDefaultEntityCompo this.entityType = entityType; } - public static Set> getCowponentsOriginatedComponents(Holder> holder) { - return COWPONENTS_ORIGINATED.getOrDefault(holder, Collections.emptySet()); - } - - public static void addCowponentsOriginatedComponent(Holder> holder, DataComponentType componentType) { - getCowponentsOriginatedComponents(holder).add(componentType); - } - @Override public void modify(Predicate> entityTypePredicate, CowponentsDefaultEntityComponentEvents.ModifyConsumer builderConsumer) { if (!entityTypePredicate.test(entityType)) return; //noinspection deprecation - Holder> holder = entityType.builtInRegistryHolder(); - - DataComponentMap oldComponents = builder.build(); + Holder.Reference> holder = entityType.builtInRegistryHolder(); + // Bind the current components early so they can be referenced directly from the block entity type. + holder.bindComponents(builder.build()); builderConsumer.modify(builder, lookupProvider, entityType); - - DataComponentMap newComponents = builder.build(); - - Set> originated = new HashSet<>(COWPONENTS_ORIGINATED.getOrDefault(holder, Collections.emptySet())); - - for (TypedDataComponent component : oldComponents - .stream() - .filter(typed -> !newComponents.has(typed.type())) - .toList()) { - originated.remove(component.type()); - } - - for (TypedDataComponent component : newComponents - .stream() - .filter(typed -> !oldComponents.has(typed.type()) || !oldComponents.get(typed.type()).equals(typed.value())) - .toList()) { - originated.add(component.type()); - } - - if (!originated.isEmpty()) { - COWPONENTS_ORIGINATED.put(holder, Collections.unmodifiableSet(originated)); - } + //noinspection DataFlowIssue + holder.bindComponents(null); } } -- 2.51.2