From 65a4298a72a5702ad71a08ddd910a6250df5f484 Mon Sep 17 00:00:00 2001 From: Anastasia Chrysanthemum Date: Mon, 11 May 2026 08:58:21 +1000 Subject: [PATCH] refactor!: Refactor inheritance events to use a component patch builder. --- ...BlockEntityComponentInheritanceEvents.java | 65 ++++++++++++ .../BlockEntityComponentValidationEvents.java | 16 +++ .../v1/event/CowponentsBlockEntityEvents.java | 79 --------------- ...entsDefaultBlockEntityComponentEvents.java | 6 +- .../impl/CowponentsBlockEntities.java | 3 +- ...ckEntityComponentInheritanceInternals.java | 24 +++++ .../BlockEntityComponentInternals.java | 24 +---- .../CowponentsBlockEntitiesEventsImpl.java | 27 ----- .../impl/event/package-info.java | 6 -- .../api/v1/DataComponentGetterSetter.java | 3 +- .../impl/util/ComponentCodecUtils.java | 3 +- .../CowponentsEntityInheritanceEvents.java | 97 ------------------ .../EntityComponentInheritanceEvents.java | 76 ++++++++++++++ .../EntityComponentValidationEvents.java | 1 - .../entities/impl/CowponentsEntities.java | 4 +- .../EntityComponentInheritanceInternals.java | 24 +++++ .../component/EntityInheritanceInternals.java | 26 ----- .../event/CowponentsEntitiesEventsImpl.java | 27 ----- .../v1/event/InheritComponentCallback.java | 7 +- .../InheritanceImplementationEvents.java | 1 - .../ItemStackComponentInheritanceEvents.java | 63 ++++++++++++ .../v1/event/ItemStackInheritanceEvents.java | 99 ------------------- .../ComponentInheritanceInternals.java | 54 +++++----- ...temStackComponentInheritanceInternals.java | 22 ++--- .../Duck_InheritableDataComponentMap.java | 2 + .../mixin/Mixin_PatchedDataComponentMap.java | 20 ++-- .../cowponents/test/CowponentsTest.java | 54 +++++----- 27 files changed, 360 insertions(+), 473 deletions(-) create mode 100644 block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/api/v1/event/BlockEntityComponentInheritanceEvents.java create mode 100644 block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/api/v1/event/BlockEntityComponentValidationEvents.java delete mode 100644 block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/api/v1/event/CowponentsBlockEntityEvents.java create mode 100644 block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/component/BlockEntityComponentInheritanceInternals.java delete mode 100644 block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/event/CowponentsBlockEntitiesEventsImpl.java delete mode 100644 block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/event/package-info.java delete mode 100644 entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/CowponentsEntityInheritanceEvents.java create mode 100644 entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/EntityComponentInheritanceEvents.java create mode 100644 entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/EntityComponentInheritanceInternals.java delete mode 100644 entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/EntityInheritanceInternals.java delete mode 100644 entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/event/CowponentsEntitiesEventsImpl.java create mode 100644 extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/ItemStackComponentInheritanceEvents.java delete mode 100644 extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/ItemStackInheritanceEvents.java diff --git a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/api/v1/event/BlockEntityComponentInheritanceEvents.java b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/api/v1/event/BlockEntityComponentInheritanceEvents.java new file mode 100644 index 0000000..16d022e --- /dev/null +++ b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/api/v1/event/BlockEntityComponentInheritanceEvents.java @@ -0,0 +1,65 @@ +package lgbt.greenhouse.cowponents.block_entities.api.v1.event; + +import dev.yumi.commons.event.Event; +import dev.yumi.mc.core.api.YumiEvents; +import lgbt.greenhouse.cowponents.extensions.api.v1.event.InheritComponentCallback; +import lgbt.greenhouse.cowponents.extensions.impl.component.ComponentInheritanceInternals; +import lgbt.greenhouse.cowponents.impl.Cowponents; +import net.minecraft.resources.Identifier; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.entity.BlockEntityType; + +import java.util.Arrays; +import java.util.function.Supplier; + +public class BlockEntityComponentInheritanceEvents { + public static final Event> INHERIT_COMPONENTS = YumiEvents.EVENTS.create(InheritComponentCallback.class, callbacks -> (builder, blockEntity, registries) -> { + for (var callback : callbacks) { + callback.inherit(builder, blockEntity, registries); + } + }); + + @SafeVarargs + public static void registerForTypes(InheritComponentCallback callback, BlockEntityType... allowedTypes) { + INHERIT_COMPONENTS.register(ComponentInheritanceInternals.filteredEventResult(callback, blockEntity -> { + if (allowedTypes.length == 0) + return true; + + for (BlockEntityType type : allowedTypes) { + if (blockEntity.is(type)) { + return true; + } + } + return false; + })); + } + + @SafeVarargs + public static void registerForTypes(Identifier phase, InheritComponentCallback callback, BlockEntityType... allowedTypes) { + INHERIT_COMPONENTS.register(phase, ComponentInheritanceInternals.filteredEventResult(callback, blockEntity -> { + if (allowedTypes.length == 0) + return true; + + for (BlockEntityType type : allowedTypes) { + if (blockEntity.is(type)) { + return true; + } + } + return false; + })); + } + + @SafeVarargs + public static void registerForTypes(InheritComponentCallback callback, Supplier>... allowedTypes) { + //noinspection unchecked + Cowponents.getPlatformHelper().deferToRegisterTime(() -> + registerForTypes(callback, Arrays.stream(allowedTypes).map(Supplier::get).toArray(BlockEntityType[]::new))); + } + + @SafeVarargs + public static void registerForTypes(Identifier phase, InheritComponentCallback callback, Supplier>... allowedTypes) { + //noinspection unchecked + Cowponents.getPlatformHelper().deferToRegisterTime(() -> + registerForTypes(phase, callback, Arrays.stream(allowedTypes).map(Supplier::get).toArray(BlockEntityType[]::new))); + } +} diff --git a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/api/v1/event/BlockEntityComponentValidationEvents.java b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/api/v1/event/BlockEntityComponentValidationEvents.java new file mode 100644 index 0000000..26d72ed --- /dev/null +++ b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/api/v1/event/BlockEntityComponentValidationEvents.java @@ -0,0 +1,16 @@ +package lgbt.greenhouse.cowponents.block_entities.api.v1.event; + +import dev.yumi.commons.event.Event; +import dev.yumi.mc.core.api.YumiEvents; +import lgbt.greenhouse.cowponents.api.v1.event.ComponentValidationEvents; +import lgbt.greenhouse.cowponents.impl.util.ComponentValidationUtils; +import net.minecraft.resources.Identifier; + +public class BlockEntityComponentValidationEvents { + /** + * An event used to validate components alongside other components specifically in a block entity context. + *

+ * Returning a DataResult#error within this event will roll back any components to what they were previously. + */ + public static final Event VALIDATE_BLOCK_ENTITY_COMPONENTS = YumiEvents.EVENTS.create(ComponentValidationEvents.ValidateComponentsCallback.class, ComponentValidationUtils::validationCallback); +} diff --git a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/api/v1/event/CowponentsBlockEntityEvents.java b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/api/v1/event/CowponentsBlockEntityEvents.java deleted file mode 100644 index 685667c..0000000 --- a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/api/v1/event/CowponentsBlockEntityEvents.java +++ /dev/null @@ -1,79 +0,0 @@ -package lgbt.greenhouse.cowponents.block_entities.api.v1.event; - -import dev.yumi.commons.event.Event; -import dev.yumi.commons.event.FilteredEvent; -import dev.yumi.mc.core.api.YumiEvents; -import lgbt.greenhouse.cowponents.api.v1.event.ComponentValidationEvents; -import lgbt.greenhouse.cowponents.extensions.api.v1.event.InheritComponentCallback; -import lgbt.greenhouse.cowponents.extensions.impl.component.ComponentInheritanceInternals; -import lgbt.greenhouse.cowponents.impl.util.ComponentValidationUtils; -import net.minecraft.core.component.DataComponentType; -import net.minecraft.resources.Identifier; -import net.minecraft.world.level.block.entity.BlockEntity; -import net.minecraft.world.level.block.entity.BlockEntityType; - -import static lgbt.greenhouse.cowponents.block_entities.impl.event.CowponentsBlockEntitiesEventsImpl.INHERIT_FROM_BLOCK_ENTITY_MAP; - -public class CowponentsBlockEntityEvents { - /** - * An event used to validate components alongside other components specifically in a block entity context. - *

- * Returning a DataResult#error within this event will roll back any components to what they were previously. - */ - public static final Event VALIDATE_BLOCK_ENTITY_COMPONENTS = YumiEvents.EVENTS.create(ComponentValidationEvents.ValidateComponentsCallback.class, ComponentValidationUtils::validationCallback); - - public static final FilteredEvent, DataComponentType> INHERIT_FROM_BLOCK_ENTITY = YumiEvents.EVENTS.createFiltered(InheritComponentCallback.class, DataComponentType.class, callbacks -> (blockEntity, registries) -> { - for (var callback : callbacks) { - Object value = callback.inherit(blockEntity, registries); - if (value != null) { - return value; - } - } - return null; - }); - - public static void register(DataComponentType componentType, InheritComponentCallback callback) { - INHERIT_FROM_BLOCK_ENTITY_MAP.computeIfAbsent(componentType, INHERIT_FROM_BLOCK_ENTITY::forContext) - .register(ComponentInheritanceInternals.unsafeCastEvent(callback)); - } - - public static void register(Identifier phase, DataComponentType componentType, InheritComponentCallback callback) { - INHERIT_FROM_BLOCK_ENTITY_MAP.computeIfAbsent(componentType, INHERIT_FROM_BLOCK_ENTITY::forContext) - .register(phase, ComponentInheritanceInternals.unsafeCastEvent(callback)); - } - - public static void registerForTypes(DataComponentType componentType, InheritComponentCallback callback, BlockEntityType... allowedTypes) { - INHERIT_FROM_BLOCK_ENTITY_MAP.computeIfAbsent(componentType, INHERIT_FROM_BLOCK_ENTITY::forContext) - .register(ComponentInheritanceInternals.filteredEventResult(callback, blockEntity -> { - if (allowedTypes.length == 0) - return true; - - for (BlockEntityType type : allowedTypes) { - if (blockEntity.is(type)) { - return true; - } - } - return false; - })); - } - - public static void registerForTypes(Identifier phase, DataComponentType componentType, InheritComponentCallback callback, BlockEntityType... allowedTypes) { - INHERIT_FROM_BLOCK_ENTITY_MAP.computeIfAbsent(componentType, INHERIT_FROM_BLOCK_ENTITY::forContext) - .register(phase, ComponentInheritanceInternals.filteredEventResult(callback, blockEntity -> { - if (allowedTypes.length == 0) - return true; - - for (BlockEntityType type : allowedTypes) { - if (blockEntity.is(type)) { - return true; - } - } - return false; - })); - } - - public static void addPhaseOrdering(Identifier firstPhase, Identifier secondPhase, DataComponentType componentType) { - INHERIT_FROM_BLOCK_ENTITY_MAP.computeIfAbsent(componentType, INHERIT_FROM_BLOCK_ENTITY::forContext) - .addPhaseOrdering(firstPhase, secondPhase); - } -} diff --git a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/api/v1/event/CowponentsDefaultBlockEntityComponentEvents.java b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/api/v1/event/CowponentsDefaultBlockEntityComponentEvents.java index 43ae946..921fc73 100644 --- a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/api/v1/event/CowponentsDefaultBlockEntityComponentEvents.java +++ b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/api/v1/event/CowponentsDefaultBlockEntityComponentEvents.java @@ -15,7 +15,7 @@ public class CowponentsDefaultBlockEntityComponentEvents { public interface ModifyContext { /** - * Modify the default data components of the specified item. + * Modify the default data components of the specified block entity. * * @param entityTypePredicate A predicate to match entity types to modify * @param builderConsumer A consumer that provides a {@link DataComponentMap.Builder} and a {@link HolderLookup.Provider} to modify the entity's components. @@ -23,7 +23,7 @@ public class CowponentsDefaultBlockEntityComponentEvents { void modify(Predicate> entityTypePredicate, ModifyConsumer builderConsumer); /** - * Modify the default data components of the specified item. + * Modify the default data components of the specified block entity. * * @param entityType The entity types to modify * @param builderConsumer A consumer that provides a {@link DataComponentMap.Builder} to modify the item's components. @@ -33,7 +33,7 @@ public class CowponentsDefaultBlockEntityComponentEvents { } /** - * Modify the default data components of the specified items. + * Modify the default data components of the specified block entities. * * @param entityTypes The entity types to modify * @param builderConsumer A consumer that provides a {@link DataComponentMap.Builder} to modify the item's components. diff --git a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/CowponentsBlockEntities.java b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/CowponentsBlockEntities.java index 4f57c0d..2b37c29 100644 --- a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/CowponentsBlockEntities.java +++ b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/CowponentsBlockEntities.java @@ -1,5 +1,6 @@ package lgbt.greenhouse.cowponents.block_entities.impl; +import lgbt.greenhouse.cowponents.block_entities.impl.component.BlockEntityComponentInheritanceInternals; import lgbt.greenhouse.cowponents.extensions.api.v1.ComponentInheritanceIdRegistry; import lgbt.greenhouse.cowponents.extensions.api.v1.event.InheritanceImplementationEvents; import lgbt.greenhouse.cowponents.impl.Cowponents; @@ -26,7 +27,7 @@ public class CowponentsBlockEntities { return null; }); - InheritanceImplementationEvents.registerInstancedInheritEvent(CowponentsInstancesInheritanceIds.BLOCK_ENTITY, BlockEntityComponentInternals::refreshInherited); + InheritanceImplementationEvents.registerInstancedInheritEvent(CowponentsInstancesInheritanceIds.BLOCK_ENTITY, BlockEntityComponentInheritanceInternals::refreshInheritedFromEntity); } } diff --git a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/component/BlockEntityComponentInheritanceInternals.java b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/component/BlockEntityComponentInheritanceInternals.java new file mode 100644 index 0000000..0b6330a --- /dev/null +++ b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/component/BlockEntityComponentInheritanceInternals.java @@ -0,0 +1,24 @@ +package lgbt.greenhouse.cowponents.block_entities.impl.component; + +import lgbt.greenhouse.cowponents.api.v1.DataComponentGetterSetter; +import lgbt.greenhouse.cowponents.block_entities.api.v1.event.BlockEntityComponentInheritanceEvents; +import net.minecraft.core.HolderLookup; +import net.minecraft.core.component.DataComponentPatch; +import net.minecraft.core.component.DataComponentType; +import net.minecraft.world.level.block.entity.BlockEntity; + +public class BlockEntityComponentInheritanceInternals { + public static void refreshInheritedFromEntity(BlockEntity entity, HolderLookup.Provider lookupProvider, DataComponentGetterSetter getterSetter) { + DataComponentPatch.Builder builder = DataComponentPatch.builder(); + BlockEntityComponentInheritanceEvents.INHERIT_COMPONENTS + .invoker() + .inherit(builder, entity, lookupProvider); + DataComponentPatch patch = builder.build(); + + for (var entry : patch.entrySet()) { + DataComponentType type = entry.getKey(); + Object value = entry.getValue().orElse(null); + getterSetter.set(type, value); + } + } +} diff --git a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/component/BlockEntityComponentInternals.java b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/component/BlockEntityComponentInternals.java index efbeb74..247e09c 100644 --- a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/component/BlockEntityComponentInternals.java +++ b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/component/BlockEntityComponentInternals.java @@ -1,15 +1,11 @@ package lgbt.greenhouse.cowponents.block_entities.impl.component; import com.mojang.serialization.DataResult; -import lgbt.greenhouse.cowponents.api.v1.DataComponentGetterSetter; -import lgbt.greenhouse.cowponents.block_entities.api.v1.event.CowponentsBlockEntityEvents; +import lgbt.greenhouse.cowponents.block_entities.api.v1.event.BlockEntityComponentValidationEvents; import lgbt.greenhouse.cowponents.impl.util.ComponentValidationUtils; import lgbt.greenhouse.cowponents.block_entities.api.v1.CowponentsBlockEntityHelper; import lgbt.greenhouse.cowponents.block_entities.impl.CowponentsBlockEntities; -import lgbt.greenhouse.cowponents.block_entities.impl.event.CowponentsBlockEntitiesEventsImpl; -import net.minecraft.core.HolderLookup; import net.minecraft.core.component.DataComponentMap; -import net.minecraft.core.component.DataComponentType; import net.minecraft.util.Unit; import net.minecraft.world.level.block.entity.BlockEntity; import org.slf4j.Logger; @@ -25,25 +21,9 @@ public class BlockEntityComponentInternals { return globalValidation.map(_ -> blockEntity); } - return CowponentsBlockEntityEvents.VALIDATE_BLOCK_ENTITY_COMPONENTS + return BlockEntityComponentValidationEvents.VALIDATE_BLOCK_ENTITY_COMPONENTS .invoker() .validate(components) .map(_ -> blockEntity); } - - public static void refreshInherited(BlockEntity blockEntity, HolderLookup.Provider lookupProvider, DataComponentGetterSetter getterSetter) { - for (var type : CowponentsBlockEntitiesEventsImpl.INHERIT_FROM_BLOCK_ENTITY_MAP.keySet()) { - inheritValue(lookupProvider, blockEntity, type, getterSetter); - } - } - - private static void inheritValue(HolderLookup.Provider lookupProvider, - BlockEntity blockEntity, - DataComponentType type, - DataComponentGetterSetter getterSetter) { - T toValue = CowponentsBlockEntitiesEventsImpl.invokeComponentForBlockEntityEvent(type, blockEntity, lookupProvider); - if (toValue != null) { - getterSetter.set(type, toValue); - } - } } diff --git a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/event/CowponentsBlockEntitiesEventsImpl.java b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/event/CowponentsBlockEntitiesEventsImpl.java deleted file mode 100644 index d37472b..0000000 --- a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/event/CowponentsBlockEntitiesEventsImpl.java +++ /dev/null @@ -1,27 +0,0 @@ -package lgbt.greenhouse.cowponents.block_entities.impl.event; - -import dev.yumi.commons.event.ContextualizedEvent; -import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap; -import lgbt.greenhouse.cowponents.extensions.api.v1.event.InheritComponentCallback; -import net.minecraft.core.HolderLookup; -import net.minecraft.core.component.DataComponentType; -import net.minecraft.resources.Identifier; -import net.minecraft.world.level.block.entity.BlockEntity; -import org.jspecify.annotations.Nullable; - -import java.util.Map; - -public class CowponentsBlockEntitiesEventsImpl { - public static final Map, ContextualizedEvent, DataComponentType>> INHERIT_FROM_BLOCK_ENTITY_MAP = new Object2ObjectArrayMap<>(); - - @SuppressWarnings("unchecked") - @Nullable - public static T invokeComponentForBlockEntityEvent(DataComponentType type, BlockEntity blockEntity, HolderLookup.Provider lookupProvider) { - if (blockEntity.getLevel() != null && INHERIT_FROM_BLOCK_ENTITY_MAP.containsKey(type)) { - return (T) INHERIT_FROM_BLOCK_ENTITY_MAP.get(type) - .invoker() - .inherit(blockEntity, lookupProvider); - } - return null; - } -} diff --git a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/event/package-info.java b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/event/package-info.java deleted file mode 100644 index 462473b..0000000 --- a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/event/package-info.java +++ /dev/null @@ -1,6 +0,0 @@ -@ApiStatus.Internal -@NullMarked -package lgbt.greenhouse.cowponents.block_entities.impl.event; - -import org.jetbrains.annotations.ApiStatus; -import org.jspecify.annotations.NullMarked; diff --git a/core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/DataComponentGetterSetter.java b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/DataComponentGetterSetter.java index f830b60..e46a121 100644 --- a/core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/DataComponentGetterSetter.java +++ b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/DataComponentGetterSetter.java @@ -2,7 +2,8 @@ package lgbt.greenhouse.cowponents.api.v1; import net.minecraft.core.component.DataComponentGetter; import net.minecraft.core.component.DataComponentType; +import org.jspecify.annotations.Nullable; public interface DataComponentGetterSetter extends DataComponentGetter { - void set(DataComponentType type, T value); + void set(DataComponentType type, @Nullable T value); } diff --git a/core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/util/ComponentCodecUtils.java b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/util/ComponentCodecUtils.java index b39cb13..93752d4 100644 --- a/core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/util/ComponentCodecUtils.java +++ b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/util/ComponentCodecUtils.java @@ -30,8 +30,7 @@ public class ComponentCodecUtils { public static OptionalInt getNonPersistentNetworkableComponentHash(TypedDataComponent component, RegistryAccess registryAccess) { - //noinspection ConstantValue - if (component.type().codec() == null && component.type().streamCodec() != null) { + if (component.type().isTransient()) { RegistryFriendlyByteBuf buf = new RegistryFriendlyByteBuf(new FriendlyByteBuf(Unpooled.buffer()), registryAccess); component.type().streamCodec().encode(buf, component.value()); return OptionalInt.of(Hashing.crc32c().hashBytes(buf.array()).asInt()); diff --git a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/CowponentsEntityInheritanceEvents.java b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/CowponentsEntityInheritanceEvents.java deleted file mode 100644 index 562a85f..0000000 --- a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/CowponentsEntityInheritanceEvents.java +++ /dev/null @@ -1,97 +0,0 @@ -package lgbt.greenhouse.cowponents.entities.api.v1.event; - -import dev.yumi.commons.event.FilteredEvent; -import dev.yumi.mc.core.api.YumiEvents; -import lgbt.greenhouse.cowponents.extensions.api.v1.event.InheritComponentCallback; -import lgbt.greenhouse.cowponents.extensions.impl.component.ComponentInheritanceInternals; -import lgbt.greenhouse.cowponents.impl.Cowponents; -import net.minecraft.core.component.DataComponentType; -import net.minecraft.resources.Identifier; -import net.minecraft.world.entity.Entity; -import net.minecraft.world.entity.EntityType; - -import java.util.function.Supplier; - -import static lgbt.greenhouse.cowponents.entities.impl.event.CowponentsEntitiesEventsImpl.INHERIT_FROM_ENTITY_MAP; - -public class CowponentsEntityInheritanceEvents { - public static final FilteredEvent, DataComponentType> INHERIT_FROM_ENTITY = YumiEvents.EVENTS.createFiltered(InheritComponentCallback.class, DataComponentType.class, callbacks -> (entity, registries) -> { - for (var callback : callbacks) { - Object value = callback.inherit(entity, registries); - if (value != null) { - return value; - } - } - return null; - }); - - public static void register(DataComponentType componentType, InheritComponentCallback callback) { - INHERIT_FROM_ENTITY_MAP.computeIfAbsent(componentType, INHERIT_FROM_ENTITY::forContext) - .register(ComponentInheritanceInternals.unsafeCastEvent(callback)); - } - - public static void register(Identifier phase, DataComponentType componentType, InheritComponentCallback callback) { - INHERIT_FROM_ENTITY_MAP.computeIfAbsent(componentType, INHERIT_FROM_ENTITY::forContext) - .register(phase, ComponentInheritanceInternals.unsafeCastEvent(callback)); - } - - public static void registerForTypes(DataComponentType componentType, InheritComponentCallback callback, EntityType... allowedTypes) { - INHERIT_FROM_ENTITY_MAP.computeIfAbsent(componentType, INHERIT_FROM_ENTITY::forContext) - .register(ComponentInheritanceInternals.filteredEventResult(callback, entity -> { - if (allowedTypes.length == 0) - return true; - - for (EntityType type : allowedTypes) { - if (entity.is(type)) { - return true; - } - } - return false; - })); - } - - public static void registerForTypes(Identifier phase, DataComponentType componentType, InheritComponentCallback callback, EntityType... allowedTypes) { - INHERIT_FROM_ENTITY_MAP.computeIfAbsent(componentType, INHERIT_FROM_ENTITY::forContext) - .register(phase, ComponentInheritanceInternals.filteredEventResult(callback, entity -> { - if (allowedTypes.length == 0) - return true; - - for (EntityType type : allowedTypes) { - if (entity.is(type)) { - return true; - } - } - return false; - })); - } - - public static void addPhaseOrdering(Identifier firstPhase, Identifier secondPhase, DataComponentType componentType) { - INHERIT_FROM_ENTITY_MAP.computeIfAbsent(componentType, INHERIT_FROM_ENTITY::forContext) - .addPhaseOrdering(firstPhase, secondPhase); - } - - public static void register(Supplier> componentType, InheritComponentCallback callback) { - Cowponents.getPlatformHelper().deferToRegisterTime(() -> - register(componentType.get(), callback)); - } - - public static void register(Identifier phase, Supplier> componentType, InheritComponentCallback callback) { - Cowponents.getPlatformHelper().deferToRegisterTime(() -> - register(phase, componentType.get(), callback)); - } - - public static void registerForTypes(Identifier phase, Supplier> supplier, InheritComponentCallback callback, EntityType... allowedTypes) { - Cowponents.getPlatformHelper().deferToRegisterTime(() -> - registerForTypes(phase, supplier.get(), callback, allowedTypes)); - } - - public static void registerForTypes(Supplier> componentType, InheritComponentCallback callback, EntityType... allowedTypes) { - Cowponents.getPlatformHelper().deferToRegisterTime(() -> - registerForTypes(componentType.get(), callback, allowedTypes)); - } - - public static void addPhaseOrdering(Identifier firstPhase, Identifier secondPhase, Supplier> supplier) { - Cowponents.getPlatformHelper().deferToRegisterTime(() -> - addPhaseOrdering(firstPhase, secondPhase, supplier.get())); - } -} diff --git a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/EntityComponentInheritanceEvents.java b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/EntityComponentInheritanceEvents.java new file mode 100644 index 0000000..1ebe69a --- /dev/null +++ b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/EntityComponentInheritanceEvents.java @@ -0,0 +1,76 @@ +package lgbt.greenhouse.cowponents.entities.api.v1.event; + +import dev.yumi.commons.event.Event; +import dev.yumi.mc.core.api.YumiEvents; +import lgbt.greenhouse.cowponents.extensions.api.v1.event.InheritComponentCallback; +import lgbt.greenhouse.cowponents.extensions.impl.component.ComponentInheritanceInternals; +import lgbt.greenhouse.cowponents.impl.Cowponents; +import net.minecraft.resources.Identifier; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EntityType; + +import java.util.Arrays; +import java.util.function.Supplier; + +public class EntityComponentInheritanceEvents { + public static final Event> INHERIT_COMPONENT = YumiEvents.EVENTS.create(InheritComponentCallback.class, callbacks -> (builder, entity, registries) -> { + for (var callback : callbacks) { + callback.inherit(builder, entity, registries); + } + }); + + @SafeVarargs + public static void registerForTypes(InheritComponentCallback callback, EntityType... allowedTypes) { + //noinspection unchecked + INHERIT_COMPONENT.register( + ComponentInheritanceInternals.filteredEventResult( + (InheritComponentCallback) callback, + entity -> { + if (allowedTypes.length == 0) + return true; + + for (EntityType type : allowedTypes) { + if (entity.is(type)) { + return true; + } + } + return false; + }) + ); + } + + @SafeVarargs + public static void registerForTypes(Identifier phase, InheritComponentCallback callback, EntityType... allowedTypes) { + //noinspection unchecked + INHERIT_COMPONENT.register( + phase, + ComponentInheritanceInternals.filteredEventResult( + (InheritComponentCallback) callback, + entity -> { + if (allowedTypes.length == 0) + return true; + + for (EntityType type : allowedTypes) { + if (entity.is(type)) { + return true; + } + } + return false; + }) + ); + } + + @SafeVarargs + public static void registerForTypes(InheritComponentCallback callback, Supplier>... allowedTypes) { + //noinspection unchecked + Cowponents.getPlatformHelper().deferToRegisterTime(() -> + registerForTypes(callback, Arrays.stream(allowedTypes).map(Supplier::get).toArray(EntityType[]::new))); + } + + @SafeVarargs + public static void registerForTypes(Identifier phase, InheritComponentCallback callback, Supplier>... allowedTypes) { + //noinspection unchecked + Cowponents.getPlatformHelper().deferToRegisterTime(() -> + registerForTypes(phase, callback, Arrays.stream(allowedTypes).map(Supplier::get).toArray(EntityType[]::new))); + } +} diff --git a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/EntityComponentValidationEvents.java b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/EntityComponentValidationEvents.java index 1bdbb49..87e3c79 100644 --- a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/EntityComponentValidationEvents.java +++ b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/EntityComponentValidationEvents.java @@ -13,5 +13,4 @@ public class EntityComponentValidationEvents { * Returning a DataResult#error within this event will roll back any components to what they were previously. */ public static final Event VALIDATE = YumiEvents.EVENTS.create(ComponentValidationEvents.ValidateComponentsCallback.class, ComponentValidationUtils::validationCallback); - } diff --git a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/CowponentsEntities.java b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/CowponentsEntities.java index 101c0dd..84a150a 100644 --- a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/CowponentsEntities.java +++ b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/CowponentsEntities.java @@ -1,7 +1,7 @@ package lgbt.greenhouse.cowponents.entities.impl; import lgbt.greenhouse.cowponents.entities.api.v1.CowponentsEntitiesInheritanceIds; -import lgbt.greenhouse.cowponents.entities.impl.component.EntityInheritanceInternals; +import lgbt.greenhouse.cowponents.entities.impl.component.EntityComponentInheritanceInternals; import lgbt.greenhouse.cowponents.extensions.api.v1.ComponentInheritanceIdRegistry; import lgbt.greenhouse.cowponents.extensions.api.v1.event.InheritanceImplementationEvents; import lgbt.greenhouse.cowponents.impl.Cowponents; @@ -22,7 +22,7 @@ public class CowponentsEntities { InheritanceImplementationEvents.registerObtainLookupEvent(CowponentsEntitiesInheritanceIds.ENTITY, instance -> instance.level().registryAccess()); - InheritanceImplementationEvents.registerInstancedInheritEvent(CowponentsEntitiesInheritanceIds.ENTITY, EntityInheritanceInternals::refreshInheritedFromEntity); + InheritanceImplementationEvents.registerInstancedInheritEvent(CowponentsEntitiesInheritanceIds.ENTITY, EntityComponentInheritanceInternals::refreshInheritedFromEntity); } } diff --git a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/EntityComponentInheritanceInternals.java b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/EntityComponentInheritanceInternals.java new file mode 100644 index 0000000..f2dd7ba --- /dev/null +++ b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/EntityComponentInheritanceInternals.java @@ -0,0 +1,24 @@ +package lgbt.greenhouse.cowponents.entities.impl.component; + +import lgbt.greenhouse.cowponents.api.v1.DataComponentGetterSetter; +import lgbt.greenhouse.cowponents.entities.api.v1.event.EntityComponentInheritanceEvents; +import net.minecraft.core.HolderLookup; +import net.minecraft.core.component.DataComponentPatch; +import net.minecraft.core.component.DataComponentType; +import net.minecraft.world.entity.Entity; + +public class EntityComponentInheritanceInternals { + public static void refreshInheritedFromEntity(Entity entity, HolderLookup.Provider lookupProvider, DataComponentGetterSetter getterSetter) { + DataComponentPatch.Builder builder = DataComponentPatch.builder(); + EntityComponentInheritanceEvents.INHERIT_COMPONENT + .invoker() + .inherit(builder, entity, lookupProvider); + DataComponentPatch patch = builder.build(); + + for (var entry : patch.entrySet()) { + DataComponentType type = entry.getKey(); + Object value = entry.getValue().orElse(null); + getterSetter.set(type, value); + } + } +} diff --git a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/EntityInheritanceInternals.java b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/EntityInheritanceInternals.java deleted file mode 100644 index 31f3f8f..0000000 --- a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/EntityInheritanceInternals.java +++ /dev/null @@ -1,26 +0,0 @@ -package lgbt.greenhouse.cowponents.entities.impl.component; - -import lgbt.greenhouse.cowponents.api.v1.DataComponentGetterSetter; -import lgbt.greenhouse.cowponents.entities.impl.event.CowponentsEntitiesEventsImpl; -import lgbt.greenhouse.cowponents.extensions.impl.component.ComponentInheritanceInternals; -import net.minecraft.core.HolderLookup; -import net.minecraft.core.component.DataComponentType; -import net.minecraft.world.entity.Entity; - -public class EntityInheritanceInternals { - public static void refreshInheritedFromEntity(Entity entity, HolderLookup.Provider lookupProvider, DataComponentGetterSetter getterSetter) { - for (var type : ComponentInheritanceInternals.INHERIT_FROM_ITEM_STACK_MAP.keySet()) { - inheritValueFromEntity(lookupProvider, entity, type, getterSetter); - } - } - - private static void inheritValueFromEntity(HolderLookup.Provider lookupProvider, - Entity entity, - DataComponentType type, - DataComponentGetterSetter getterSetter) { - T toValue = CowponentsEntitiesEventsImpl.invokeComponentForEntityEvent(type, entity, lookupProvider); - if (toValue != null) { - getterSetter.set(type, toValue); - } - } -} diff --git a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/event/CowponentsEntitiesEventsImpl.java b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/event/CowponentsEntitiesEventsImpl.java deleted file mode 100644 index 7e587cf..0000000 --- a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/event/CowponentsEntitiesEventsImpl.java +++ /dev/null @@ -1,27 +0,0 @@ -package lgbt.greenhouse.cowponents.entities.impl.event; - -import dev.yumi.commons.event.ContextualizedEvent; -import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap; -import lgbt.greenhouse.cowponents.extensions.api.v1.event.InheritComponentCallback; -import net.minecraft.core.HolderLookup; -import net.minecraft.core.component.DataComponentType; -import net.minecraft.resources.Identifier; -import net.minecraft.world.entity.Entity; -import org.jspecify.annotations.Nullable; - -import java.util.Map; - -public class CowponentsEntitiesEventsImpl { - public static final Map, ContextualizedEvent, DataComponentType>> INHERIT_FROM_ENTITY_MAP = new Object2ObjectArrayMap<>(); - - @SuppressWarnings("unchecked") - @Nullable - public static T invokeComponentForEntityEvent(DataComponentType type, Entity entity, HolderLookup.Provider lookupProvider) { - if (INHERIT_FROM_ENTITY_MAP.containsKey(type)) { - return (T) INHERIT_FROM_ENTITY_MAP.get(type) - .invoker() - .inherit(entity, lookupProvider); - } - return null; - } -} diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/InheritComponentCallback.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/InheritComponentCallback.java index cf6c832..1387246 100644 --- a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/InheritComponentCallback.java +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/InheritComponentCallback.java @@ -1,10 +1,9 @@ package lgbt.greenhouse.cowponents.extensions.api.v1.event; import net.minecraft.core.HolderLookup; -import org.jspecify.annotations.Nullable; +import net.minecraft.core.component.DataComponentPatch; @FunctionalInterface -public interface InheritComponentCallback { - @Nullable - T inherit(I value, HolderLookup.Provider lookupProvider); +public interface InheritComponentCallback { + void inherit(DataComponentPatch.Builder componentBuilder, I value, HolderLookup.Provider lookupProvider); } diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/InheritanceImplementationEvents.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/InheritanceImplementationEvents.java index 4c92adc..da9c6b9 100644 --- a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/InheritanceImplementationEvents.java +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/InheritanceImplementationEvents.java @@ -24,7 +24,6 @@ public class InheritanceImplementationEvents { } return Cowponents.getSideHelper().getHolderLookupProvider(); }); - private static final FilteredEvent, Identifier> INSTANCED_INHERIT = YumiEvents.EVENTS.createFiltered(InheritCallback.class, Identifier.class, callbacks -> (instance, registries, getterSetter) -> { for (var callback : callbacks) { callback.inherit(instance, registries, getterSetter); diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/ItemStackComponentInheritanceEvents.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/ItemStackComponentInheritanceEvents.java new file mode 100644 index 0000000..1175e79 --- /dev/null +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/ItemStackComponentInheritanceEvents.java @@ -0,0 +1,63 @@ +package lgbt.greenhouse.cowponents.extensions.api.v1.event; + +import dev.yumi.commons.event.Event; +import dev.yumi.mc.core.api.YumiEvents; +import lgbt.greenhouse.cowponents.extensions.impl.component.ComponentInheritanceInternals; +import lgbt.greenhouse.cowponents.impl.Cowponents; +import net.minecraft.resources.Identifier; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.ItemLike; + +import java.util.Arrays; +import java.util.function.Supplier; + +public class ItemStackComponentInheritanceEvents { + private ItemStackComponentInheritanceEvents() { + } + + public static final Event> INHERIT_COMPONENT = YumiEvents.EVENTS.create(InheritComponentCallback.class, callbacks -> (patch, stack, registries) -> { + for (var callback : callbacks) { + callback.inherit(patch, stack, registries); + } + }); + + public static void registerForItems(InheritComponentCallback callback, ItemLike... allowedItems) { + INHERIT_COMPONENT.register(ComponentInheritanceInternals.filteredEventResult(callback, stack -> { + if (allowedItems.length == 0) + return true; + + for (ItemLike itemLike : allowedItems) { + if (stack.is(itemLike.asItem())) { + return true; + } + } + return false; + })); + } + + public static void registerForItems(Identifier phase, InheritComponentCallback callback, ItemLike... allowedItems) { + INHERIT_COMPONENT.register(phase, ComponentInheritanceInternals.filteredEventResult(callback, stack -> { + if (allowedItems.length == 0) + return true; + + for (ItemLike itemLike : allowedItems) { + if (stack.is(itemLike.asItem())) { + return true; + } + } + return false; + })); + } + + @SafeVarargs + public static void registerForItems(InheritComponentCallback callback, Supplier... allowedTypes) { + Cowponents.getPlatformHelper().deferToRegisterTime(() -> + registerForItems(callback, Arrays.stream(allowedTypes).map(Supplier::get).toArray(ItemLike[]::new))); + } + + @SafeVarargs + public static void registerForItems(Identifier phase, InheritComponentCallback callback, Supplier... allowedTypes) { + Cowponents.getPlatformHelper().deferToRegisterTime(() -> + registerForItems(phase, callback, Arrays.stream(allowedTypes).map(Supplier::get).toArray(ItemLike[]::new))); + } +} diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/ItemStackInheritanceEvents.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/ItemStackInheritanceEvents.java deleted file mode 100644 index c3b8db1..0000000 --- a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/ItemStackInheritanceEvents.java +++ /dev/null @@ -1,99 +0,0 @@ -package lgbt.greenhouse.cowponents.extensions.api.v1.event; - -import dev.yumi.commons.event.FilteredEvent; -import dev.yumi.mc.core.api.YumiEvents; -import lgbt.greenhouse.cowponents.extensions.impl.component.ComponentInheritanceInternals; -import lgbt.greenhouse.cowponents.impl.Cowponents; -import net.minecraft.core.component.DataComponentType; -import net.minecraft.resources.Identifier; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.ItemLike; - -import java.util.function.Supplier; - -import static lgbt.greenhouse.cowponents.extensions.impl.component.ComponentInheritanceInternals.INHERIT_FROM_ITEM_STACK_MAP; - -public class ItemStackInheritanceEvents { - private ItemStackInheritanceEvents() { - } - - private static final FilteredEvent, DataComponentType> INHERIT_FROM_ITEM_STACK = YumiEvents.EVENTS.createFiltered(InheritComponentCallback.class, DataComponentType.class, callbacks -> (stack, registries) -> { - for (var callback : callbacks) { - Object value = callback.inherit(stack, registries); - if (value != null) { - return value; - } - } - return null; - }); - - public static void register(DataComponentType componentType, InheritComponentCallback callback) { - INHERIT_FROM_ITEM_STACK_MAP.computeIfAbsent(componentType, INHERIT_FROM_ITEM_STACK::forContext) - .register(ComponentInheritanceInternals.unsafeCastEvent(callback)); - } - - public static void register(Identifier phase, DataComponentType componentType, InheritComponentCallback callback) { - INHERIT_FROM_ITEM_STACK_MAP.computeIfAbsent(componentType, INHERIT_FROM_ITEM_STACK::forContext) - .register(phase, ComponentInheritanceInternals.unsafeCastEvent(callback)); - } - - public static void registerForItems(DataComponentType componentType, InheritComponentCallback callback, ItemLike... allowedItems) { - INHERIT_FROM_ITEM_STACK_MAP.computeIfAbsent(componentType, INHERIT_FROM_ITEM_STACK::forContext) - .register(ComponentInheritanceInternals.filteredEventResult(callback, stack -> { - if (allowedItems.length == 0) - return true; - - for (ItemLike itemLike : allowedItems) { - if (stack.is(itemLike.asItem())) { - return true; - } - } - return false; - })); - } - - public static void registerForItems(Identifier phase, DataComponentType componentType, InheritComponentCallback callback, ItemLike... allowedItems) { - INHERIT_FROM_ITEM_STACK_MAP.computeIfAbsent(componentType, INHERIT_FROM_ITEM_STACK::forContext) - .register(phase, ComponentInheritanceInternals.filteredEventResult(callback, stack -> { - if (allowedItems.length == 0) - return true; - - for (ItemLike itemLike : allowedItems) { - if (stack.is(itemLike.asItem())) { - return true; - } - } - return false; - })); - } - - public static void addPhaseOrdering(Identifier firstPhase, Identifier secondPhase, DataComponentType componentType) { - INHERIT_FROM_ITEM_STACK_MAP.computeIfAbsent(componentType, INHERIT_FROM_ITEM_STACK::forContext) - .addPhaseOrdering(firstPhase, secondPhase); - } - - public static void register(Supplier> componentType, InheritComponentCallback callback) { - Cowponents.getPlatformHelper().deferToRegisterTime(() -> - register(componentType.get(), callback)); - } - - public static void register(Identifier phase, Supplier> componentType, InheritComponentCallback callback) { - Cowponents.getPlatformHelper().deferToRegisterTime(() -> - register(phase, componentType.get(), callback)); - } - - public static void registerForItems(Identifier phase, Supplier> supplier, InheritComponentCallback callback, ItemLike... allowedTypes) { - Cowponents.getPlatformHelper().deferToRegisterTime(() -> - registerForItems(phase, supplier.get(), callback, allowedTypes)); - } - - public static void registerForItems(Supplier> componentType, InheritComponentCallback callback, ItemLike... allowedTypes) { - Cowponents.getPlatformHelper().deferToRegisterTime(() -> - registerForItems(componentType.get(), callback, allowedTypes)); - } - - public static void addPhaseOrdering(Identifier firstPhase, Identifier secondPhase, Supplier> supplier) { - Cowponents.getPlatformHelper().deferToRegisterTime(() -> - addPhaseOrdering(firstPhase, secondPhase, supplier.get())); - } -} diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/ComponentInheritanceInternals.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/ComponentInheritanceInternals.java index 5aa3362..034adc8 100644 --- a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/ComponentInheritanceInternals.java +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/ComponentInheritanceInternals.java @@ -2,6 +2,7 @@ package lgbt.greenhouse.cowponents.extensions.impl.component; import dev.yumi.commons.event.ContextualizedEvent; import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap; +import it.unimi.dsi.fastutil.objects.Reference2ObjectMap; import lgbt.greenhouse.cowponents.api.v1.DataComponentGetterSetter; import lgbt.greenhouse.cowponents.extensions.api.v1.event.InheritComponentCallback; import lgbt.greenhouse.cowponents.extensions.api.v1.event.InheritanceImplementationEvents; @@ -12,7 +13,6 @@ import net.minecraft.core.HolderLookup; import net.minecraft.core.component.DataComponentType; import net.minecraft.core.component.PatchedDataComponentMap; import net.minecraft.resources.Identifier; -import net.minecraft.world.item.ItemStack; import org.jspecify.annotations.Nullable; import org.slf4j.Logger; @@ -26,10 +26,8 @@ public class ComponentInheritanceInternals { public static final Logger OBTAIN_REGISTRIES_LOGGER = CowponentsExtensions.getLogger("Obtain Registries Event"); public static final Set WARNED_IDS = new HashSet<>(); - public static final Map, Identifier>> INSTANCED_INHERIT_MAP = new Object2ObjectArrayMap<>(); public static final Map, Identifier>> OBTAIN_LOOKUP_MAP = new Object2ObjectArrayMap<>(); - - public static final Map, ContextualizedEvent, DataComponentType>> INHERIT_FROM_ITEM_STACK_MAP = new Object2ObjectArrayMap<>(); + public static final Map, Identifier>> INSTANCED_INHERIT_MAP = new Object2ObjectArrayMap<>(); public static HolderLookup.@Nullable Provider invokeObtainHolderLookupProvider(@Nullable T instance) { if (instance == null) @@ -55,55 +53,49 @@ public class ComponentInheritanceInternals { Identifier inheritanceId = ComponentInheritanceIdRegistryImpl.get(instance); if (inheritanceId == null || !INSTANCED_INHERIT_MAP.containsKey(inheritanceId)) return; + INSTANCED_INHERIT_MAP.get(inheritanceId) .invoker() .inherit(instance, registries, getterSetter); } - @SuppressWarnings("unchecked") - @Nullable - public static T invokeInheritFromItemStackEvent(DataComponentType type, - ItemStack stack, - HolderLookup.Provider lookupProvider) { - if (INHERIT_FROM_ITEM_STACK_MAP.containsKey(type)) { - return (T) INHERIT_FROM_ITEM_STACK_MAP.get(type) - .invoker() - .inherit(stack, lookupProvider); - } - return null; - } - - public static InheritComponentCallback filteredEventResult(InheritComponentCallback callback, Predicate predicate) { - return (instance, lookupProvider) -> { - if (!predicate.test(instance)) { - return null; - } - return callback.inherit(instance, lookupProvider); + public static InheritComponentCallback filteredEventResult(InheritComponentCallback callback, Predicate predicate) { + return (componentBuilder, instance, lookupProvider) -> { + if (!predicate.test(instance)) + return; + callback.inherit(componentBuilder, instance, lookupProvider); }; } - public static InheritComponentCallback unsafeCastEvent(InheritComponentCallback callback) { - return callback::inherit; + public static void refreshInheritedValues(PatchedDataComponentMap components, + Object instance) { + refreshInheritedValues(components, instance, ((Duck_InheritableDataComponentMap) (Object) components).cowponents_extensions$getInherited()); } public static void refreshInheritedValues(PatchedDataComponentMap components, - Object instance) { + Object instance, + Reference2ObjectMap, Object> inherited) { HolderLookup.Provider lookupProvider = ComponentInheritanceInternals.invokeObtainHolderLookupProvider(instance); if (lookupProvider != null) { - ((Duck_InheritableDataComponentMap) (Object) components).cowponents_extensions$getInherited().clear(); + inherited.clear(); ComponentInheritanceInternals.invokeInstancedInherit( instance, lookupProvider, - getterSetterForInstanceInherited(components) + getterSetterForInstanceInherited(components, inherited) ); } } - public static DataComponentGetterSetter getterSetterForInstanceInherited(PatchedDataComponentMap components) { + public static DataComponentGetterSetter getterSetterForInstanceInherited(PatchedDataComponentMap components, + Reference2ObjectMap, Object> inherited) { return new DataComponentGetterSetter() { @Override - public void set(DataComponentType type, T value) { - ((Duck_InheritableDataComponentMap) (Object) components).cowponents_extensions$getInherited().put(type, value); + public void set(DataComponentType type, @Nullable T value) { + if (value == null) { + inherited.remove(type); + return; + } + inherited.put(type, value); } @Override diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/ItemStackComponentInheritanceInternals.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/ItemStackComponentInheritanceInternals.java index 7ec75cf..ebd29a3 100644 --- a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/ItemStackComponentInheritanceInternals.java +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/ItemStackComponentInheritanceInternals.java @@ -1,7 +1,9 @@ package lgbt.greenhouse.cowponents.extensions.impl.component; import lgbt.greenhouse.cowponents.api.v1.DataComponentGetterSetter; +import lgbt.greenhouse.cowponents.extensions.api.v1.event.ItemStackComponentInheritanceEvents; import net.minecraft.core.HolderLookup; +import net.minecraft.core.component.DataComponentPatch; import net.minecraft.core.component.DataComponentType; import net.minecraft.world.item.ItemStack; @@ -10,18 +12,16 @@ public class ItemStackComponentInheritanceInternals { if (stack.isEmpty()) return; - for (var type : ComponentInheritanceInternals.INHERIT_FROM_ITEM_STACK_MAP.keySet()) { - inheritValueFromItemStack(lookupProvider, stack, type, getterSetter); - } - } + DataComponentPatch.Builder builder = DataComponentPatch.builder(); + ItemStackComponentInheritanceEvents.INHERIT_COMPONENT + .invoker() + .inherit(builder, stack, lookupProvider); + DataComponentPatch patch = builder.build(); - private static void inheritValueFromItemStack(HolderLookup.Provider lookupProvider, - ItemStack stack, - DataComponentType type, - DataComponentGetterSetter getterSetter) { - T toValue = ComponentInheritanceInternals.invokeInheritFromItemStackEvent(type, stack, lookupProvider); - if (toValue != null) { - getterSetter.set(type, toValue); + for (var entry : patch.entrySet()) { + DataComponentType type = entry.getKey(); + Object value = entry.getValue().orElse(null); + getterSetter.set(type, value); } } } diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/duck/Duck_InheritableDataComponentMap.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/duck/Duck_InheritableDataComponentMap.java index af8b8ff..53507fd 100644 --- a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/duck/Duck_InheritableDataComponentMap.java +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/duck/Duck_InheritableDataComponentMap.java @@ -2,8 +2,10 @@ package lgbt.greenhouse.cowponents.extensions.impl.duck; import it.unimi.dsi.fastutil.objects.Reference2ObjectMap; import net.minecraft.core.component.DataComponentType; +import org.jspecify.annotations.Nullable; public interface Duck_InheritableDataComponentMap { + @Nullable Object cowponents_extensions$getInstance(); void cowponents_extensions$setInstance(Object object); diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/mixin/Mixin_PatchedDataComponentMap.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/mixin/Mixin_PatchedDataComponentMap.java index b47069b..ff1d7ff 100644 --- a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/mixin/Mixin_PatchedDataComponentMap.java +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/mixin/Mixin_PatchedDataComponentMap.java @@ -13,6 +13,7 @@ import net.minecraft.core.component.DataComponentMap; import net.minecraft.core.component.DataComponentType; import net.minecraft.core.component.PatchedDataComponentMap; import net.minecraft.core.component.TypedDataComponent; +import org.jspecify.annotations.NonNull; import org.jspecify.annotations.Nullable; import org.objectweb.asm.Opcodes; import org.spongepowered.asm.mixin.Final; @@ -37,6 +38,7 @@ public class Mixin_PatchedDataComponentMap implements Duck_InheritableDataCompon private DataComponentMap prototype; @Unique + @Nullable private Object cowponents_extensions$instance; @Unique private final Reference2ObjectMap, Object> cowponents_extensions$inherited = new Reference2ObjectArrayMap<>(); @@ -68,28 +70,28 @@ public class Mixin_PatchedDataComponentMap implements Duck_InheritableDataCompon @Inject(method = "set(Lnet/minecraft/core/component/DataComponentType;Ljava/lang/Object;)Ljava/lang/Object;", at = @At("RETURN")) private void cowponents_extensions$refreshInheritedValuesCacheUponSet(DataComponentType type, @Nullable T value, CallbackInfoReturnable cir) { - ComponentInheritanceInternals.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance); + ComponentInheritanceInternals.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance, cowponents_extensions$inherited); } @Inject(method = "remove", at = @At("RETURN")) private void cowponents_extensions$refreshInheritedValuesCacheUponRemove(DataComponentType type, CallbackInfoReturnable cir) { - ComponentInheritanceInternals.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance); + ComponentInheritanceInternals.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance, cowponents_extensions$inherited); } @SuppressWarnings("OptionalUsedAsFieldOrParameterType") @Inject(method = "applyPatch(Lnet/minecraft/core/component/DataComponentType;Ljava/util/Optional;)V", at = @At("TAIL")) private void cowponents_extensions$refreshInheritedValuesCacheUponApplyPatch(DataComponentType type, Optional value, CallbackInfo ci) { - ComponentInheritanceInternals.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance); + ComponentInheritanceInternals.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance, cowponents_extensions$inherited); } @Inject(method = "restorePatch", at = @At("TAIL")) private void cowponents_extensions$refreshInheritedValuesCacheUponRestorePatch(CallbackInfo ci) { - ComponentInheritanceInternals.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance); + ComponentInheritanceInternals.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance, cowponents_extensions$inherited); } @Inject(method = "clearPatch", at = @At("TAIL")) private void cowponents_extensions$clearInheritedValuesCacheUponClearPatch(CallbackInfo ci) { - ComponentInheritanceInternals.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance); + ComponentInheritanceInternals.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance, cowponents_extensions$inherited); } @ModifyReturnValue(method = "keySet", at = @At("RETURN")) @@ -160,22 +162,22 @@ public class Mixin_PatchedDataComponentMap implements Duck_InheritableDataCompon } @Override - public Object cowponents_extensions$getInstance() { + public @Nullable Object cowponents_extensions$getInstance() { return cowponents_extensions$instance; } @Override - public void cowponents_extensions$setInstance(Object object) { + public void cowponents_extensions$setInstance(@NonNull Object object) { cowponents_extensions$instance = object; } @Override - public Reference2ObjectMap, Object> cowponents_extensions$getInherited() { + public @NonNull Reference2ObjectMap, Object> cowponents_extensions$getInherited() { return cowponents_extensions$inherited; } @Override - public void cowponents_extensions$setInherited(Reference2ObjectMap, Object> value) { + public void cowponents_extensions$setInherited(@NonNull Reference2ObjectMap, Object> value) { cowponents_extensions$inherited.clear(); cowponents_extensions$inherited.putAll(value); } diff --git a/test/xplat/src/test/java/lgbt/greenhouse/cowponents/test/CowponentsTest.java b/test/xplat/src/test/java/lgbt/greenhouse/cowponents/test/CowponentsTest.java index 7d92557..6e893d9 100644 --- a/test/xplat/src/test/java/lgbt/greenhouse/cowponents/test/CowponentsTest.java +++ b/test/xplat/src/test/java/lgbt/greenhouse/cowponents/test/CowponentsTest.java @@ -3,17 +3,19 @@ package lgbt.greenhouse.cowponents.test; import com.mojang.serialization.DataResult; import lgbt.greenhouse.cowponents.api.v1.event.ItemStackComponentValidationEvents; import lgbt.greenhouse.cowponents.entities.api.v1.event.CowponentsDefaultEntityComponentEvents; -import lgbt.greenhouse.cowponents.entities.api.v1.event.CowponentsEntityInheritanceEvents; +import lgbt.greenhouse.cowponents.entities.api.v1.event.EntityComponentInheritanceEvents; import lgbt.greenhouse.cowponents.entities.api.v1.event.EntityComponentValidationEvents; -import lgbt.greenhouse.cowponents.extensions.api.v1.event.ItemStackInheritanceEvents; +import lgbt.greenhouse.cowponents.extensions.api.v1.event.ItemStackComponentInheritanceEvents; import lgbt.greenhouse.cowponents.test.registry.CowponentsTestDataComponents; import net.minecraft.core.Holder; import net.minecraft.core.component.DataComponents; +import net.minecraft.network.chat.Component; import net.minecraft.resources.Identifier; import net.minecraft.util.RandomSource; import net.minecraft.util.Unit; import net.minecraft.world.entity.EntityType; import net.minecraft.world.item.Items; +import net.minecraft.world.item.component.ItemLore; import net.minecraft.world.item.enchantment.Enchantment; import net.minecraft.world.item.enchantment.Enchantments; import net.minecraft.world.item.enchantment.ItemEnchantments; @@ -54,44 +56,48 @@ public class CowponentsTest { } public static void initItemStackInheritanceEvents() { - ItemStackInheritanceEvents.addPhaseOrdering(DEFAULT_EVENT_PHASE, CYCLING_ENCHANTMENTS_EVENT_PHASE, DataComponents.ENCHANTMENTS); - ItemStackInheritanceEvents.registerForItems( + ItemStackComponentInheritanceEvents.INHERIT_COMPONENT.addPhaseOrdering(DEFAULT_EVENT_PHASE, CYCLING_ENCHANTMENTS_EVENT_PHASE); + ItemStackComponentInheritanceEvents.registerForItems( DEFAULT_EVENT_PHASE, - DataComponents.ENCHANTMENTS, - (_, registryAccess) -> { - ItemEnchantments.Mutable mutable = new ItemEnchantments.Mutable(ItemEnchantments.EMPTY); - mutable.set(registryAccess.getOrThrow(Enchantments.FIRE_ASPECT), 2); - return mutable.toImmutable(); + (builder, stack, lookupProvider) -> { + if (!stack.getOrDefault(DataComponents.LORE, ItemLore.EMPTY).lines().isEmpty()) { + ItemEnchantments.Mutable enchantments = new ItemEnchantments.Mutable(ItemEnchantments.EMPTY); + enchantments.set(lookupProvider.getOrThrow(Enchantments.FIRE_ASPECT), 2); + builder.set(DataComponents.ENCHANTMENTS, enchantments.toImmutable()); + } }, Items.NETHERITE_AXE ); - ItemStackInheritanceEvents.register( + ItemStackComponentInheritanceEvents.INHERIT_COMPONENT.register( CYCLING_ENCHANTMENTS_EVENT_PHASE, - DataComponents.ENCHANTMENTS, - (value, _) -> value.get(CowponentsTestDataComponents.CYCLING_ENCHANTMENTS.get()) + (builder, stack, _) -> { + if (stack.has(CowponentsTestDataComponents.CYCLING_ENCHANTMENTS.get())) { + //noinspection DataFlowIssue + builder.set(DataComponents.ENCHANTMENTS, stack.get(CowponentsTestDataComponents.CYCLING_ENCHANTMENTS.get())); + } + } ); - ItemStackInheritanceEvents.register( - DataComponents.ITEM_MODEL, - (value, lookupProvider) -> { + ItemStackComponentInheritanceEvents.INHERIT_COMPONENT.register( + (builder, stack, lookupProvider) -> { Holder baneOfArthropods = lookupProvider.getOrThrow(Enchantments.BANE_OF_ARTHROPODS); - if (value.getOrDefault(DataComponents.ENCHANTMENTS, ItemEnchantments.EMPTY).getLevel(baneOfArthropods) > 0) { - return id("smiley_face"); + if (stack.getOrDefault(DataComponents.ENCHANTMENTS, ItemEnchantments.EMPTY).getLevel(baneOfArthropods) > 0) { + builder.set(DataComponents.ITEM_MODEL, id("smiley_face")); } - return null; } ); } public static void initEntityInheritanceEvents() { - CowponentsEntityInheritanceEvents.registerForTypes( - CowponentsTestDataComponents.ENTITY_TINT, - (value, _) -> { - RandomSource random = new LegacyRandomSource(value.getUUID().hashCode()); - return random.nextIntBetweenInclusive(0, 0xFFFFFF); + EntityComponentInheritanceEvents.registerForTypes( + (builder, player, _) -> { + RandomSource random = new LegacyRandomSource(player.getUUID().hashCode()); + int color = random.nextIntBetweenInclusive(0, 0xFFFFFF); + builder.set(CowponentsTestDataComponents.ENTITY_TINT.get(), color); }, - EntityType.PLAYER); + EntityType.PLAYER + ); } public static Identifier id(String path) { -- 2.51.2