From 0d6520189088a2c31d9d237a4b200bad9017740e Mon Sep 17 00:00:00 2001 From: Anastasia Chrysanthemum Date: Thu, 7 May 2026 06:14:05 +1000 Subject: [PATCH] feat: Remove cross-component inheritance, refactor codebase. --- .../neoforge/CowponentsInstancesNeoForge.java | 68 -------- .../v1/event/CowponentsBlockEntityEvents.java | 14 +- .../impl/CowponentsBlockEntities.java | 10 +- .../impl/mixin/Mixin_BlockEntity.java | 4 +- .../Mixin_DataComponentInitializers.java | 2 +- ...ts.java => ComponentValidationEvents.java} | 11 +- .../ItemStackComponentValidationEvents.java | 16 ++ .../cowponents/api/v1/event/package-info.java | 4 + .../cowponents/api/v1/package-info.java | 5 + .../side/CowponentsClientSideHelper.java | 26 +-- .../cowponents/impl/duck/package-info.java | 6 + ...ainerComponentSynchronizerCacheLoader.java | 3 +- .../impl/util/ComponentValidationUtils.java | 11 +- .../src/main/resources/cowponents.mixins.json | 9 +- .../impl/fabric/CowponentsEntitiesFabric.java | 12 +- .../neoforge/CowponentsInstancesNeoForge.java | 17 +- ...=> CowponentsEntityInheritanceEvents.java} | 22 +-- .../EntityComponentValidationEvents.java | 17 ++ .../entities/api/v1/event/package-info.java | 4 + .../CowponentsEntityDataComponentType.java | 6 +- .../api/v1/extension/package-info.java | 4 + .../entities/api/v1/package-info.java | 4 + .../entities/impl/CowponentsEntities.java | 12 +- .../DefaultEntityComponentsImpl.java | 2 +- .../EntityComponentInternals.java} | 8 +- .../component/EntityInheritanceInternals.java | 28 ++++ .../entities/impl/component/package-info.java | 6 + .../Mixin_DataComponentInitializers.java | 4 +- ...in_DataComponentTypeBuilderSimpleType.java | 8 +- .../entities/impl/mixin/Mixin_Entity.java | 136 ++++++++-------- .../resources/cowponents_entities.mixins.json | 33 ++-- .../CowponentsExtensionsClientFabric.java | 4 +- .../CowponentsExtensionsDedicatedFabric.java | 4 +- .../CowponentsExtensionsNeoForge.java | 4 +- .../v1/ComponentInheritanceIdRegistry.java | 12 ++ ...nsionsDataComponentTypeBuilderWrapper.java | 107 ------------- .../CowponentsExtensionsInheritanceIds.java | 2 + .../v1/CowponentsInheritanceIdRegistry.java | 10 -- .../CowponentsComponentInheritanceEvents.java | 40 ----- ...a => InheritanceImplementationEvents.java} | 12 +- ...s.java => ItemStackInheritanceEvents.java} | 16 +- .../extensions/api/v1/event/package-info.java | 4 + .../extensions/api/v1/package-info.java | 4 + .../extensions/impl/CowponentsExtensions.java | 10 +- .../ComponentInheritanceIdRegistryImpl.java} | 6 +- .../InheritanceInternals.java} | 67 ++++---- .../ItemStackInheritanceInternals.java | 27 ++++ .../impl/component/package-info.java | 6 + .../Duck_InheritableDataComponentMap.java | 8 +- .../extensions/impl/duck/package-info.java | 6 + .../Accessor_PatchedDataComponentMap.java | 19 --- .../impl/mixin/Mixin_ItemStack.java | 4 +- .../mixin/Mixin_PatchedDataComponentMap.java | 94 ++++------- .../impl/util/InheritedValueUtils.java | 151 ------------------ .../cowponents_extensions.mixins.json | 5 +- gradle/libs.versions.toml | 8 +- .../test/fabric/CowponentsTestFabric.java | 5 +- .../test/neoforge/CowponentsTestNeoForge.java | 5 +- .../cowponents/test/CowponentsTest.java | 89 ++++++++--- .../CowponentsTestDataComponents.java | 9 +- .../resources/cowponents_test.mixins.json | 3 + 61 files changed, 504 insertions(+), 749 deletions(-) rename core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/event/{CowponentsValidationEvents.java => ComponentValidationEvents.java} (62%) create mode 100644 core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/event/ItemStackComponentValidationEvents.java create mode 100644 core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/event/package-info.java create mode 100644 core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/package-info.java create mode 100644 core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/duck/package-info.java rename entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/{CowponentsEntityEvents.java => CowponentsEntityInheritanceEvents.java} (69%) create mode 100644 entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/EntityComponentValidationEvents.java create mode 100644 entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/package-info.java create mode 100644 entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/extension/package-info.java create mode 100644 entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/package-info.java rename entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/{world/entity => component}/DefaultEntityComponentsImpl.java (97%) rename entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/{world/entity/EntityComponentUtils.java => component/EntityComponentInternals.java} (94%) create mode 100644 entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/EntityInheritanceInternals.java create mode 100644 entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/package-info.java create mode 100644 extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/ComponentInheritanceIdRegistry.java delete mode 100644 extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/CowponentsExtensionsDataComponentTypeBuilderWrapper.java delete mode 100644 extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/CowponentsInheritanceIdRegistry.java delete mode 100644 extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/CowponentsComponentInheritanceEvents.java rename extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/{CowponentsInheritanceImplementationEvents.java => InheritanceImplementationEvents.java} (87%) rename extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/{CowponentsItemStackInheritanceEvents.java => ItemStackInheritanceEvents.java} (80%) create mode 100644 extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/package-info.java create mode 100644 extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/package-info.java rename extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/{event/CowponentsInheritanceIdImpl.java => component/ComponentInheritanceIdRegistryImpl.java} (83%) rename extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/{event/CowponentsInheritanceEventsImpl.java => component/InheritanceInternals.java} (61%) create mode 100644 extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/ItemStackInheritanceInternals.java create mode 100644 extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/package-info.java create mode 100644 extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/duck/package-info.java delete mode 100644 extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/mixin/Accessor_PatchedDataComponentMap.java delete mode 100644 extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/util/InheritedValueUtils.java diff --git a/block_entities/neoforge/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/neoforge/CowponentsInstancesNeoForge.java b/block_entities/neoforge/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/neoforge/CowponentsInstancesNeoForge.java index 3b96d25..8bc92ea 100644 --- a/block_entities/neoforge/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/neoforge/CowponentsInstancesNeoForge.java +++ b/block_entities/neoforge/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/neoforge/CowponentsInstancesNeoForge.java @@ -1,82 +1,14 @@ package lgbt.greenhouse.cowponents.block_entities.impl.neoforge; -import lgbt.greenhouse.cowponents.entities.api.v1.extension.CowponentsInstancesDataComponentType; import lgbt.greenhouse.cowponents.block_entities.impl.CowponentsBlockEntities; -import lgbt.greenhouse.cowponents.entities.impl.network.clientbound.SyncEntityComponentPacketClientbound; -import lgbt.greenhouse.cowponents.entities.impl.world.entity.EntityComponentUtils; -import net.minecraft.core.component.DataComponentPatch; -import net.minecraft.server.level.ServerPlayer; -import net.minecraft.world.entity.player.Player; import net.neoforged.bus.api.IEventBus; -import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.fml.common.Mod; -import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; -import net.neoforged.neoforge.event.entity.player.PlayerEvent; -import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent; - -import java.util.ArrayList; -import java.util.List; - -import static lgbt.greenhouse.cowponents.impl.util.ComponentCastUtils.castComponentType; @Mod(CowponentsBlockEntities.MOD_ID) @EventBusSubscriber(modid = CowponentsBlockEntities.MOD_ID) public class CowponentsInstancesNeoForge { - public static final List DEFERRED_RUNNABLES = new ArrayList<>(); - public CowponentsInstancesNeoForge(IEventBus eventBus) { CowponentsBlockEntities.init(); } - - @SubscribeEvent - public static void onPlayerClone(PlayerEvent.Clone event) { - Player oldPlayer = event.getOriginal(); - Player newPlayer = event.getEntity(); - boolean wasDeath = event.isWasDeath(); - - if (CowponentsEntityHelper.getComponentsPatch(oldPlayer).isEmpty()) - return; - - DataComponentPatch.Builder patch = DataComponentPatch.builder(); - for (var entry : CowponentsEntityHelper.getComponentsPatch(oldPlayer).entrySet()) { - if (wasDeath && !((CowponentsInstancesDataComponentType)entry.getKey()).cowponents$transferOnDeath()) - continue; - - entry.getValue().ifPresentOrElse( - value -> patch.set(castComponentType(entry.getKey()), value), - () -> patch.remove(entry.getKey()) - ); - } - CowponentsEntityHelper.applyComponentsAndValidate(newPlayer, patch.build()); - EntityComponentUtils.syncComponents(newPlayer); - } - - @SubscribeEvent - public static void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) { - if (event.getEntity() instanceof ServerPlayer serverPlayer) { - EntityComponentUtils.syncComponentsToPlayer(serverPlayer, serverPlayer); - } - } - - @SubscribeEvent - public static void onPlayerStartTracking(PlayerEvent.StartTracking event) { - if (event.getEntity() instanceof ServerPlayer serverPlayer) { - EntityComponentUtils.syncComponentsToPlayer(event.getTarget(), serverPlayer); - } - } - - @SubscribeEvent - public static void onFmlCommonSetup(FMLCommonSetupEvent event) { - DEFERRED_RUNNABLES.forEach(event::enqueueWork); - } - - @SubscribeEvent - public static void registerPayloadHandlers(RegisterPayloadHandlersEvent event) { - event.registrar("1.0.0") - .playToClient( - SyncEntityComponentPacketClientbound.TYPE, - SyncEntityComponentPacketClientbound.STREAM_CODEC - ); - } } 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 index aa7560a..33a3974 100644 --- 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 @@ -3,9 +3,9 @@ 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.CowponentsValidationEvents; +import lgbt.greenhouse.cowponents.api.v1.event.ComponentValidationEvents; import lgbt.greenhouse.cowponents.extensions.api.v1.event.InheritComponentCallback; -import lgbt.greenhouse.cowponents.extensions.impl.event.CowponentsInheritanceEventsImpl; +import lgbt.greenhouse.cowponents.extensions.impl.component.InheritanceInternals; import lgbt.greenhouse.cowponents.impl.util.ComponentValidationUtils; import net.minecraft.core.component.DataComponentType; import net.minecraft.resources.Identifier; @@ -20,7 +20,7 @@ public class CowponentsBlockEntityEvents { *

* Returning a DataResult#error within this event will roll back any addedComponents to what they were previously. */ - public static final Event VALIDATE_BLOCK_ENTITY_COMPONENTS = YumiEvents.EVENTS.create(CowponentsValidationEvents.ValidateComponentsCallback.class, ComponentValidationUtils::validationCallback); + 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) { @@ -34,17 +34,17 @@ public class CowponentsBlockEntityEvents { public static void register(DataComponentType componentType, InheritComponentCallback callback) { INHERIT_FROM_BLOCK_ENTITY_MAP.computeIfAbsent(componentType, INHERIT_FROM_BLOCK_ENTITY::forContext) - .register(CowponentsInheritanceEventsImpl.unsafeCastEvent(callback)); + .register(InheritanceInternals.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, CowponentsInheritanceEventsImpl.unsafeCastEvent(callback)); + .register(phase, InheritanceInternals.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(CowponentsInheritanceEventsImpl.filteredEventResult(callback, blockEntity -> { + .register(InheritanceInternals.filteredEventResult(callback, blockEntity -> { if (allowedTypes.length == 0) return true; @@ -59,7 +59,7 @@ public class CowponentsBlockEntityEvents { 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, CowponentsInheritanceEventsImpl.filteredEventResult(callback, blockEntity -> { + .register(phase, InheritanceInternals.filteredEventResult(callback, blockEntity -> { if (allowedTypes.length == 0) return true; 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 ba3d090..3ad5dcf 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,7 +1,7 @@ package lgbt.greenhouse.cowponents.block_entities.impl; -import lgbt.greenhouse.cowponents.extensions.api.v1.CowponentsInheritanceIdRegistry; -import lgbt.greenhouse.cowponents.extensions.api.v1.event.CowponentsInheritanceImplementationEvents; +import lgbt.greenhouse.cowponents.extensions.api.v1.ComponentInheritanceIdRegistry; +import lgbt.greenhouse.cowponents.extensions.api.v1.event.InheritanceImplementationEvents; import lgbt.greenhouse.cowponents.impl.Cowponents; import lgbt.greenhouse.cowponents.block_entities.api.v1.CowponentsInstancesInheritanceIds; import lgbt.greenhouse.cowponents.block_entities.impl.world.block_entity.BlockEntityComponentHelper; @@ -17,16 +17,16 @@ public class CowponentsBlockEntities { public static void init() { if (Cowponents.getPlatformHelper().isModLoaded("cowponents_extensions")) { - CowponentsInheritanceIdRegistry.register(BlockEntity.class, CowponentsInstancesInheritanceIds.BLOCK_ENTITY); + ComponentInheritanceIdRegistry.register(BlockEntity.class, CowponentsInstancesInheritanceIds.BLOCK_ENTITY); - CowponentsInheritanceImplementationEvents.registerObtainLookupEvent(CowponentsInstancesInheritanceIds.BLOCK_ENTITY, instance -> { + InheritanceImplementationEvents.registerObtainLookupEvent(CowponentsInstancesInheritanceIds.BLOCK_ENTITY, instance -> { if (instance.getLevel() != null) { return instance.getLevel().registryAccess(); } return null; }); - CowponentsInheritanceImplementationEvents.registerInstancedInheritEvent(CowponentsInstancesInheritanceIds.BLOCK_ENTITY, BlockEntityComponentHelper::refreshInherited); + InheritanceImplementationEvents.registerInstancedInheritEvent(CowponentsInstancesInheritanceIds.BLOCK_ENTITY, BlockEntityComponentHelper::refreshInherited); } } diff --git a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/mixin/Mixin_BlockEntity.java b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/mixin/Mixin_BlockEntity.java index b463fc8..42f22b8 100644 --- a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/mixin/Mixin_BlockEntity.java +++ b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/mixin/Mixin_BlockEntity.java @@ -3,8 +3,8 @@ package lgbt.greenhouse.cowponents.block_entities.impl.mixin; import com.mojang.serialization.DataResult; import lgbt.greenhouse.cowponents.block_entities.api.v1.extension.CowponentsBlockEntityComponentHolder; import lgbt.greenhouse.cowponents.block_entities.impl.duck.Duck_HolderCowponentsBlockEntityOriginated; +import lgbt.greenhouse.cowponents.extensions.impl.component.InheritanceInternals; import lgbt.greenhouse.cowponents.extensions.impl.duck.Duck_InheritableDataComponentMap; -import lgbt.greenhouse.cowponents.extensions.impl.util.InheritedValueUtils; import lgbt.greenhouse.cowponents.impl.Cowponents; import lgbt.greenhouse.cowponents.impl.util.ComponentCastUtils; import lgbt.greenhouse.cowponents.block_entities.impl.world.block_entity.BlockEntityComponentHelper; @@ -40,7 +40,7 @@ public abstract class Mixin_BlockEntity implements CowponentsBlockEntityComponen if (Cowponents.getPlatformHelper().isModLoaded("cowponents_extensions")) { PatchedDataComponentMap components = (PatchedDataComponentMap) cowponents$getComponents(); ((Duck_InheritableDataComponentMap) (Object) components).cowponents_extensions$setInstance(this); - InheritedValueUtils.refreshInheritedValues(components, this); + InheritanceInternals.refreshInheritedValues(components, this); } } diff --git a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/mixin/Mixin_DataComponentInitializers.java b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/mixin/Mixin_DataComponentInitializers.java index 203eeb7..b967820 100644 --- a/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/mixin/Mixin_DataComponentInitializers.java +++ b/block_entities/xplat/src/main/java/lgbt/greenhouse/cowponents/block_entities/impl/mixin/Mixin_DataComponentInitializers.java @@ -18,7 +18,7 @@ import java.util.Map; @Mixin(DataComponentInitializers.class) public abstract class Mixin_DataComponentInitializers { @ModifyReturnValue(method = "runInitializers", at = @At(value = "RETURN")) - private static Map, DataComponentMap.Builder> cowponents$runDefaultEntityComponents(Map, DataComponentMap.Builder> original, @Local(argsOnly = true) HolderLookup.Provider lookupProvider) { + private static Map, DataComponentMap.Builder> cowponents_block_entities$runDefaultBlockEntityComponents(Map, DataComponentMap.Builder> original, @Local(argsOnly = true, name = "context") HolderLookup.Provider lookupProvider) { for (BlockEntityType blockEntityType : BuiltInRegistries.BLOCK_ENTITY_TYPE) { //noinspection deprecation ResourceKey> key = blockEntityType.builtInRegistryHolder().key(); diff --git a/core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/event/CowponentsValidationEvents.java b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/event/ComponentValidationEvents.java similarity index 62% rename from core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/event/CowponentsValidationEvents.java rename to core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/event/ComponentValidationEvents.java index 9cfb4c5..77ea6a6 100644 --- a/core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/event/CowponentsValidationEvents.java +++ b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/event/ComponentValidationEvents.java @@ -8,20 +8,13 @@ import net.minecraft.core.component.DataComponentMap; import net.minecraft.resources.Identifier; import net.minecraft.util.Unit; -public class CowponentsValidationEvents { +public class ComponentValidationEvents { /** * An event used to validate components alongside other components. *

* Returning a DataResult#error within this event will roll back any components to what they were previously. */ - public static final Event VALIDATE_COMPONENTS = YumiEvents.EVENTS.create(ValidateComponentsCallback.class, ComponentValidationUtils::validationCallback); - - /** - * An event used to validate components alongside other components specifically in an item stack context. - *

- * Returning a DataResult#error within this event will roll back any components to what they were previously. - */ - public static final Event VALIDATE_ITEM_STACK_COMPONENTS = YumiEvents.EVENTS.create(CowponentsValidationEvents.ValidateComponentsCallback.class, ComponentValidationUtils::validationCallback); + public static final Event VALIDATE = YumiEvents.EVENTS.create(ValidateComponentsCallback.class, ComponentValidationUtils::validationCallback); @FunctionalInterface public interface ValidateComponentsCallback { diff --git a/core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/event/ItemStackComponentValidationEvents.java b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/event/ItemStackComponentValidationEvents.java new file mode 100644 index 0000000..2073d9b --- /dev/null +++ b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/event/ItemStackComponentValidationEvents.java @@ -0,0 +1,16 @@ +package lgbt.greenhouse.cowponents.api.v1.event; + +import dev.yumi.commons.event.Event; +import dev.yumi.mc.core.api.YumiEvents; +import lgbt.greenhouse.cowponents.impl.util.ComponentValidationUtils; +import net.minecraft.resources.Identifier; + +public class ItemStackComponentValidationEvents { + /** + * An event used to validate components alongside other components specifically in an item stack context. + *

+ * 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/core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/event/package-info.java b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/event/package-info.java new file mode 100644 index 0000000..9a5406f --- /dev/null +++ b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/event/package-info.java @@ -0,0 +1,4 @@ +@NullMarked +package lgbt.greenhouse.cowponents.api.v1.event; + +import org.jspecify.annotations.NullMarked; diff --git a/core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/package-info.java b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/package-info.java new file mode 100644 index 0000000..1919c82 --- /dev/null +++ b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/api/v1/package-info.java @@ -0,0 +1,5 @@ +@NullMarked +package lgbt.greenhouse.cowponents.api.v1; + +import org.jetbrains.annotations.ApiStatus; +import org.jspecify.annotations.NullMarked; diff --git a/core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/client/platform/side/CowponentsClientSideHelper.java b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/client/platform/side/CowponentsClientSideHelper.java index d94ab58..4a6def5 100644 --- a/core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/client/platform/side/CowponentsClientSideHelper.java +++ b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/client/platform/side/CowponentsClientSideHelper.java @@ -10,7 +10,9 @@ import net.minecraft.resources.ResourceKey; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; +import java.util.HashSet; import java.util.Optional; +import java.util.Set; import java.util.stream.Stream; public class CowponentsClientSideHelper implements CowponentsSideHelper { @@ -19,25 +21,29 @@ public class CowponentsClientSideHelper implements CowponentsSideHelper>> listRegistryKeys() { + Set>> registryKeys = new HashSet<>(); + ClientLevel level = Minecraft.getInstance().level; - if (level == null) { - throw new IllegalStateException("Attempted to obtain registries without a ClientLevel"); + + if (level != null) { + registryKeys.addAll(level.registryAccess().listRegistryKeys().toList()); } + if (Cowponents.server != null && Cowponents.server.isSingleplayer()) { - return Stream.concat(level.registryAccess().listRegistryKeys(), Cowponents.server.registryAccess().listRegistryKeys()); + registryKeys.addAll(Cowponents.server.registryAccess().listRegistryKeys().toList()); } - return level.registryAccess().listRegistryKeys(); + return registryKeys.stream(); } @Override public Optional> lookup(ResourceKey> resourceKey) { ClientLevel level = Minecraft.getInstance().level; - if (level == null) { - throw new IllegalStateException("Attempted to obtain registries without a ClientLevel"); - } - Optional> lookup = level.registryAccess().lookup(resourceKey); - if (lookup.isPresent()) { - return lookup; + + if (level != null) { + Optional> lookup = level.registryAccess().lookup(resourceKey); + if (lookup.isPresent()) { + return lookup; + } } if (Cowponents.server != null && Cowponents.server.isSingleplayer()) { diff --git a/core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/duck/package-info.java b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/duck/package-info.java new file mode 100644 index 0000000..9313c1b --- /dev/null +++ b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/duck/package-info.java @@ -0,0 +1,6 @@ +@ApiStatus.Internal +@NullMarked +package lgbt.greenhouse.cowponents.impl.duck; + +import org.jetbrains.annotations.ApiStatus; +import org.jspecify.annotations.NullMarked; diff --git a/core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/mixin/Mixin_ServerPlayerContainerComponentSynchronizerCacheLoader.java b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/mixin/Mixin_ServerPlayerContainerComponentSynchronizerCacheLoader.java index 26e00d4..368a5f1 100644 --- a/core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/mixin/Mixin_ServerPlayerContainerComponentSynchronizerCacheLoader.java +++ b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/mixin/Mixin_ServerPlayerContainerComponentSynchronizerCacheLoader.java @@ -4,6 +4,7 @@ import lgbt.greenhouse.cowponents.impl.duck.Duck_ServerPlayerContainerComponentS import lgbt.greenhouse.cowponents.impl.util.ComponentCodecUtils; import net.minecraft.core.RegistryAccess; import net.minecraft.core.component.TypedDataComponent; +import org.jspecify.annotations.NonNull; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; @@ -24,7 +25,7 @@ public class Mixin_ServerPlayerContainerComponentSynchronizerCacheLoader impleme } @Override - public void cowponents$setRegistryAccess(RegistryAccess access) { + public void cowponents$setRegistryAccess(@NonNull RegistryAccess access) { cowponents$registryAccess = access; } } diff --git a/core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/util/ComponentValidationUtils.java b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/util/ComponentValidationUtils.java index 708fe01..3c7469f 100644 --- a/core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/util/ComponentValidationUtils.java +++ b/core/xplat/src/main/java/lgbt/greenhouse/cowponents/impl/util/ComponentValidationUtils.java @@ -1,14 +1,15 @@ package lgbt.greenhouse.cowponents.impl.util; import com.mojang.serialization.DataResult; -import lgbt.greenhouse.cowponents.api.v1.event.CowponentsValidationEvents; +import lgbt.greenhouse.cowponents.api.v1.event.ComponentValidationEvents; +import lgbt.greenhouse.cowponents.api.v1.event.ItemStackComponentValidationEvents; import net.minecraft.core.component.DataComponentMap; import net.minecraft.util.Unit; public class ComponentValidationUtils { - public static CowponentsValidationEvents.ValidateComponentsCallback validationCallback(CowponentsValidationEvents.ValidateComponentsCallback[] callbacks) { + public static ComponentValidationEvents.ValidateComponentsCallback validationCallback(ComponentValidationEvents.ValidateComponentsCallback[] callbacks) { return components -> { - for (CowponentsValidationEvents.ValidateComponentsCallback callback : callbacks) { + for (ComponentValidationEvents.ValidateComponentsCallback callback : callbacks) { var dataResult = callback.validate(components); if (dataResult.isError()) { return dataResult; @@ -19,13 +20,13 @@ public class ComponentValidationUtils { } public static DataResult validateGeneric(DataComponentMap components) { - return CowponentsValidationEvents.VALIDATE_COMPONENTS + return ComponentValidationEvents.VALIDATE .invoker() .validate(components); } public static DataResult validateItemStack(DataComponentMap components) { - return CowponentsValidationEvents.VALIDATE_ITEM_STACK_COMPONENTS + return ItemStackComponentValidationEvents.VALIDATE .invoker() .validate(components); } diff --git a/core/xplat/src/main/resources/cowponents.mixins.json b/core/xplat/src/main/resources/cowponents.mixins.json index a6d418c..a57ba28 100644 --- a/core/xplat/src/main/resources/cowponents.mixins.json +++ b/core/xplat/src/main/resources/cowponents.mixins.json @@ -8,10 +8,13 @@ "Mixin_ServerPlayerContainerComponentSynchronizerCache", "Mixin_ServerPlayerContainerComponentSynchronizerCacheLoader" ], + "client": [ + "client.Mixin_ClientPacketLIstener" + ], "injectors": { "defaultRequire": 1 }, - "client": [ - "client.Mixin_ClientPacketLIstener" - ] + "mixinextras": { + "minVersion": "0.5.0" + } } diff --git a/entities/fabric/src/main/java/lgbt/greenhouse/cowponents/entities/impl/fabric/CowponentsEntitiesFabric.java b/entities/fabric/src/main/java/lgbt/greenhouse/cowponents/entities/impl/fabric/CowponentsEntitiesFabric.java index 8fff10b..867fbed 100644 --- a/entities/fabric/src/main/java/lgbt/greenhouse/cowponents/entities/impl/fabric/CowponentsEntitiesFabric.java +++ b/entities/fabric/src/main/java/lgbt/greenhouse/cowponents/entities/impl/fabric/CowponentsEntitiesFabric.java @@ -4,7 +4,7 @@ import lgbt.greenhouse.cowponents.entities.api.v1.CowponentsEntityHelper; import lgbt.greenhouse.cowponents.entities.api.v1.extension.CowponentsEntityDataComponentType; import lgbt.greenhouse.cowponents.entities.impl.CowponentsEntities; import lgbt.greenhouse.cowponents.entities.impl.network.clientbound.SyncEntityComponentPacketClientbound; -import lgbt.greenhouse.cowponents.entities.impl.world.entity.EntityComponentUtils; +import lgbt.greenhouse.cowponents.entities.impl.component.EntityComponentInternals; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.entity.event.v1.ServerEntityLevelChangeEvents; import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents; @@ -23,8 +23,8 @@ public class CowponentsEntitiesFabric implements ModInitializer { } public static void registerEvents() { - EntityTrackingEvents.START_TRACKING.register(EntityComponentUtils::syncComponentsToPlayer); - ServerPlayerEvents.JOIN.register(player -> EntityComponentUtils.syncComponentsToPlayer(player, player)); + EntityTrackingEvents.START_TRACKING.register(EntityComponentInternals::syncComponentsToPlayer); + ServerPlayerEvents.JOIN.register(player -> EntityComponentInternals.syncComponentsToPlayer(player, player)); ServerPlayerEvents.COPY_FROM.register((oldPlayer, newPlayer, alive) -> { if (CowponentsEntityHelper.getComponentsPatch(oldPlayer).isEmpty()) @@ -32,7 +32,7 @@ public class CowponentsEntitiesFabric implements ModInitializer { DataComponentPatch.Builder patch = DataComponentPatch.builder(); for (var entry : CowponentsEntityHelper.getComponentsPatch(oldPlayer).entrySet()) { - if (!alive && !((CowponentsEntityDataComponentType)entry.getKey()).cowponents$transferOnDeath()) + if (!alive && !((CowponentsEntityDataComponentType)entry.getKey()).cowponents$transferOnDeath()) continue; entry.getValue().ifPresentOrElse( @@ -44,10 +44,10 @@ public class CowponentsEntitiesFabric implements ModInitializer { }); ServerPlayerEvents.AFTER_RESPAWN.register((_, newPlayer, _) -> - EntityComponentUtils.syncComponents(newPlayer) + EntityComponentInternals.syncComponents(newPlayer) ); ServerEntityLevelChangeEvents.AFTER_PLAYER_CHANGE_LEVEL.register((player, _, _) -> { - EntityComponentUtils.syncComponents(player); + EntityComponentInternals.syncComponents(player); }); } diff --git a/entities/neoforge/src/main/java/lgbt/greenhouse/cowponents/entities/impl/neoforge/CowponentsInstancesNeoForge.java b/entities/neoforge/src/main/java/lgbt/greenhouse/cowponents/entities/impl/neoforge/CowponentsInstancesNeoForge.java index ad8798c..0773fe6 100644 --- a/entities/neoforge/src/main/java/lgbt/greenhouse/cowponents/entities/impl/neoforge/CowponentsInstancesNeoForge.java +++ b/entities/neoforge/src/main/java/lgbt/greenhouse/cowponents/entities/impl/neoforge/CowponentsInstancesNeoForge.java @@ -4,7 +4,7 @@ import lgbt.greenhouse.cowponents.entities.api.v1.CowponentsEntityHelper; import lgbt.greenhouse.cowponents.entities.api.v1.extension.CowponentsEntityDataComponentType; import lgbt.greenhouse.cowponents.entities.impl.CowponentsEntities; import lgbt.greenhouse.cowponents.entities.impl.network.clientbound.SyncEntityComponentPacketClientbound; -import lgbt.greenhouse.cowponents.entities.impl.world.entity.EntityComponentUtils; +import lgbt.greenhouse.cowponents.entities.impl.component.EntityComponentInternals; import net.minecraft.core.component.DataComponentPatch; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.player.Player; @@ -24,8 +24,6 @@ import static lgbt.greenhouse.cowponents.impl.util.ComponentCastUtils.castCompon @Mod(CowponentsEntities.MOD_ID) @EventBusSubscriber(modid = CowponentsEntities.MOD_ID) public class CowponentsInstancesNeoForge { - public static final List DEFERRED_RUNNABLES = new ArrayList<>(); - public CowponentsInstancesNeoForge(IEventBus eventBus) { CowponentsEntities.init(); } @@ -41,7 +39,7 @@ public class CowponentsInstancesNeoForge { DataComponentPatch.Builder patch = DataComponentPatch.builder(); for (var entry : CowponentsEntityHelper.getComponentsPatch(oldPlayer).entrySet()) { - if (wasDeath && !((CowponentsEntityDataComponentType)entry.getKey()).cowponents$transferOnDeath()) + if (wasDeath && !((CowponentsEntityDataComponentType)entry.getKey()).cowponents$transferOnDeath()) continue; entry.getValue().ifPresentOrElse( @@ -50,28 +48,23 @@ public class CowponentsInstancesNeoForge { ); } CowponentsEntityHelper.applyComponentsAndValidate(newPlayer, patch.build()); - EntityComponentUtils.syncComponents(newPlayer); + EntityComponentInternals.syncComponents(newPlayer); } @SubscribeEvent public static void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) { if (event.getEntity() instanceof ServerPlayer serverPlayer) { - EntityComponentUtils.syncComponentsToPlayer(serverPlayer, serverPlayer); + EntityComponentInternals.syncComponentsToPlayer(serverPlayer, serverPlayer); } } @SubscribeEvent public static void onPlayerStartTracking(PlayerEvent.StartTracking event) { if (event.getEntity() instanceof ServerPlayer serverPlayer) { - EntityComponentUtils.syncComponentsToPlayer(event.getTarget(), serverPlayer); + EntityComponentInternals.syncComponentsToPlayer(event.getTarget(), serverPlayer); } } - @SubscribeEvent - public static void onFmlCommonSetup(FMLCommonSetupEvent event) { - DEFERRED_RUNNABLES.forEach(event::enqueueWork); - } - @SubscribeEvent public static void registerPayloadHandlers(RegisterPayloadHandlersEvent event) { event.registrar("1.0.0") diff --git a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/CowponentsEntityEvents.java b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/CowponentsEntityInheritanceEvents.java similarity index 69% rename from entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/CowponentsEntityEvents.java rename to entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/CowponentsEntityInheritanceEvents.java index 6a09ca2..061e263 100644 --- a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/CowponentsEntityEvents.java +++ b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/CowponentsEntityInheritanceEvents.java @@ -1,12 +1,9 @@ package lgbt.greenhouse.cowponents.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.CowponentsValidationEvents; import lgbt.greenhouse.cowponents.extensions.api.v1.event.InheritComponentCallback; -import lgbt.greenhouse.cowponents.extensions.impl.event.CowponentsInheritanceEventsImpl; -import lgbt.greenhouse.cowponents.impl.util.ComponentValidationUtils; +import lgbt.greenhouse.cowponents.extensions.impl.component.InheritanceInternals; import net.minecraft.core.component.DataComponentType; import net.minecraft.resources.Identifier; import net.minecraft.world.entity.Entity; @@ -14,14 +11,7 @@ import net.minecraft.world.entity.EntityType; import static lgbt.greenhouse.cowponents.entities.impl.event.CowponentsEntitiesEventsImpl.INHERIT_FROM_ENTITY_MAP; -public class CowponentsEntityEvents { - /** - * An event used to validate addedComponents alongside other addedComponents specifically in an entity context. - *

- * Returning a DataResult#error within this event will roll back any addedComponents to what they were previously. - */ - public static final Event VALIDATE_ENTITY_COMPONENTS = YumiEvents.EVENTS.create(CowponentsValidationEvents.ValidateComponentsCallback.class, ComponentValidationUtils::validationCallback); - +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); @@ -34,17 +24,17 @@ public class CowponentsEntityEvents { public static void register(DataComponentType componentType, InheritComponentCallback callback) { INHERIT_FROM_ENTITY_MAP.computeIfAbsent(componentType, INHERIT_FROM_ENTITY::forContext) - .register(CowponentsInheritanceEventsImpl.unsafeCastEvent(callback)); + .register(InheritanceInternals.unsafeCastEvent(callback)); } public static void register(Identifier phase, DataComponentType componentType, InheritComponentCallback callback) { INHERIT_FROM_ENTITY_MAP.computeIfAbsent(componentType, INHERIT_FROM_ENTITY::forContext) - .register(phase, CowponentsInheritanceEventsImpl.unsafeCastEvent(callback)); + .register(phase, InheritanceInternals.unsafeCastEvent(callback)); } public static void registerForTypes(DataComponentType componentType, InheritComponentCallback callback, EntityType... allowedTypes) { INHERIT_FROM_ENTITY_MAP.computeIfAbsent(componentType, INHERIT_FROM_ENTITY::forContext) - .register(CowponentsInheritanceEventsImpl.filteredEventResult(callback, entity -> { + .register(InheritanceInternals.filteredEventResult(callback, entity -> { if (allowedTypes.length == 0) return true; @@ -59,7 +49,7 @@ public class CowponentsEntityEvents { public static void registerForTypes(Identifier phase, DataComponentType componentType, InheritComponentCallback callback, EntityType... allowedTypes) { INHERIT_FROM_ENTITY_MAP.computeIfAbsent(componentType, INHERIT_FROM_ENTITY::forContext) - .register(phase, CowponentsInheritanceEventsImpl.filteredEventResult(callback, entity -> { + .register(phase, InheritanceInternals.filteredEventResult(callback, entity -> { if (allowedTypes.length == 0) return true; 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 new file mode 100644 index 0000000..fa64ae9 --- /dev/null +++ b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/EntityComponentValidationEvents.java @@ -0,0 +1,17 @@ +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.api.v1.event.ComponentValidationEvents; +import lgbt.greenhouse.cowponents.impl.util.ComponentValidationUtils; +import net.minecraft.resources.Identifier; + +public class EntityComponentValidationEvents { + /** + * An event used to validate addedComponents alongside other addedComponents specifically in an entity context. + *

+ * Returning a DataResult#error within this event will roll back any addedComponents 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/api/v1/event/package-info.java b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/package-info.java new file mode 100644 index 0000000..2f1bd5e --- /dev/null +++ b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/event/package-info.java @@ -0,0 +1,4 @@ +@NullMarked +package lgbt.greenhouse.cowponents.entities.api.v1.event; + +import org.jspecify.annotations.NullMarked; diff --git a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/extension/CowponentsEntityDataComponentType.java b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/extension/CowponentsEntityDataComponentType.java index e2a340c..346b576 100644 --- a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/extension/CowponentsEntityDataComponentType.java +++ b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/extension/CowponentsEntityDataComponentType.java @@ -5,11 +5,9 @@ import net.minecraft.core.component.DataComponentType; /** * An extension to {@link DataComponentType} for Cowponents: Instances specific fields. */ -public interface CowponentsEntityDataComponentType { +public interface CowponentsEntityDataComponentType { /** - * Whether to transfer this component back to the new instance of a player when they die. - * - * @return This builder. + * @return Whether to transfer this component back to the new instance of a player when they die. */ default boolean cowponents$transferOnDeath() { throw new AssertionError("Implemented via mixin."); diff --git a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/extension/package-info.java b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/extension/package-info.java new file mode 100644 index 0000000..7771cd2 --- /dev/null +++ b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/extension/package-info.java @@ -0,0 +1,4 @@ +@NullMarked +package lgbt.greenhouse.cowponents.entities.api.v1.extension; + +import org.jspecify.annotations.NullMarked; diff --git a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/package-info.java b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/package-info.java new file mode 100644 index 0000000..b7de455 --- /dev/null +++ b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/api/v1/package-info.java @@ -0,0 +1,4 @@ +@NullMarked +package lgbt.greenhouse.cowponents.entities.api.v1; + +import org.jspecify.annotations.NullMarked; 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 8ee193d..18d9021 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,10 +1,10 @@ package lgbt.greenhouse.cowponents.entities.impl; -import lgbt.greenhouse.cowponents.extensions.api.v1.CowponentsInheritanceIdRegistry; -import lgbt.greenhouse.cowponents.extensions.api.v1.event.CowponentsInheritanceImplementationEvents; +import lgbt.greenhouse.cowponents.extensions.api.v1.ComponentInheritanceIdRegistry; +import lgbt.greenhouse.cowponents.extensions.api.v1.event.InheritanceImplementationEvents; import lgbt.greenhouse.cowponents.impl.Cowponents; import lgbt.greenhouse.cowponents.entities.api.v1.CowponentsEntitiesInheritanceIds; -import lgbt.greenhouse.cowponents.entities.impl.world.entity.EntityComponentUtils; +import lgbt.greenhouse.cowponents.entities.impl.component.EntityComponentInternals; import net.minecraft.resources.Identifier; import net.minecraft.world.entity.Entity; import org.slf4j.Logger; @@ -17,12 +17,12 @@ public class CowponentsEntities { public static void init() { if (Cowponents.getPlatformHelper().isModLoaded("cowponents_extensions")) { - CowponentsInheritanceIdRegistry.register(Entity.class, CowponentsEntitiesInheritanceIds.ENTITY); + ComponentInheritanceIdRegistry.register(Entity.class, CowponentsEntitiesInheritanceIds.ENTITY); - CowponentsInheritanceImplementationEvents.registerObtainLookupEvent(CowponentsEntitiesInheritanceIds.ENTITY, instance -> + InheritanceImplementationEvents.registerObtainLookupEvent(CowponentsEntitiesInheritanceIds.ENTITY, instance -> instance.level().registryAccess()); - CowponentsInheritanceImplementationEvents.registerInstancedInheritEvent(CowponentsEntitiesInheritanceIds.ENTITY, EntityComponentUtils::refreshInherited); + InheritanceImplementationEvents.registerInstancedInheritEvent(CowponentsEntitiesInheritanceIds.ENTITY, EntityComponentInternals::refreshInherited); } } diff --git a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/world/entity/DefaultEntityComponentsImpl.java b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/DefaultEntityComponentsImpl.java similarity index 97% rename from entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/world/entity/DefaultEntityComponentsImpl.java rename to entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/DefaultEntityComponentsImpl.java index 58cec07..a9b7121 100644 --- a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/world/entity/DefaultEntityComponentsImpl.java +++ b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/DefaultEntityComponentsImpl.java @@ -1,4 +1,4 @@ -package lgbt.greenhouse.cowponents.entities.impl.world.entity; +package lgbt.greenhouse.cowponents.entities.impl.component; import lgbt.greenhouse.cowponents.entities.api.v1.event.CowponentsDefaultEntityComponentEvents; import lgbt.greenhouse.cowponents.entities.impl.duck.Duck_HolderCowponentsEntityOriginated; diff --git a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/world/entity/EntityComponentUtils.java b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/EntityComponentInternals.java similarity index 94% rename from entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/world/entity/EntityComponentUtils.java rename to entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/EntityComponentInternals.java index 3b0fb56..fee9d82 100644 --- a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/world/entity/EntityComponentUtils.java +++ b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/EntityComponentInternals.java @@ -1,8 +1,8 @@ -package lgbt.greenhouse.cowponents.entities.impl.world.entity; +package lgbt.greenhouse.cowponents.entities.impl.component; import com.mojang.serialization.DataResult; import lgbt.greenhouse.cowponents.api.v1.DataComponentGetterSetter; -import lgbt.greenhouse.cowponents.entities.api.v1.event.CowponentsEntityEvents; +import lgbt.greenhouse.cowponents.entities.api.v1.event.EntityComponentValidationEvents; import lgbt.greenhouse.cowponents.impl.Cowponents; import lgbt.greenhouse.cowponents.impl.util.ComponentValidationUtils; import lgbt.greenhouse.cowponents.entities.api.v1.CowponentsEntityHelper; @@ -27,7 +27,7 @@ import java.util.Optional; import static lgbt.greenhouse.cowponents.impl.util.ComponentCastUtils.castComponentType; -public class EntityComponentUtils { +public class EntityComponentInternals { public static final Logger LOGGER = CowponentsEntities.getLogger("Entity Components"); public static DataResult validateEntity(Entity entity) { @@ -38,7 +38,7 @@ public class EntityComponentUtils { return globalValidation.map(_ -> entity); } - return CowponentsEntityEvents.VALIDATE_ENTITY_COMPONENTS + return EntityComponentValidationEvents.VALIDATE .invoker() .validate(components) .map(_ -> entity); 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 new file mode 100644 index 0000000..e07644e --- /dev/null +++ b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/EntityInheritanceInternals.java @@ -0,0 +1,28 @@ +package lgbt.greenhouse.cowponents.entities.impl.component; + +import lgbt.greenhouse.cowponents.api.v1.DataComponentGetterSetter; +import lgbt.greenhouse.cowponents.extensions.impl.component.InheritanceInternals; +import net.minecraft.core.HolderLookup; +import net.minecraft.core.component.DataComponentType; +import net.minecraft.world.item.ItemStack; + +public class EntityInheritanceInternals { + public static void refreshInheritedFromItemStack(ItemStack stack, HolderLookup.Provider lookupProvider, DataComponentGetterSetter getterSetter) { + if (stack.isEmpty()) + return; + + for (var type : InheritanceInternals.INHERIT_FROM_ITEM_STACK_MAP.keySet()) { + inheritValueFromItemStack(lookupProvider, stack, type, getterSetter); + } + } + + private static void inheritValueFromItemStack(HolderLookup.Provider lookupProvider, + ItemStack stack, + DataComponentType type, + DataComponentGetterSetter getterSetter) { + T toValue = InheritanceInternals.invokeInheritFromItemStackEvent(type, stack, lookupProvider); + if (toValue != null) { + getterSetter.set(type, toValue); + } + } +} diff --git a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/package-info.java b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/package-info.java new file mode 100644 index 0000000..828d433 --- /dev/null +++ b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/component/package-info.java @@ -0,0 +1,6 @@ +@ApiStatus.Internal +@NullMarked +package lgbt.greenhouse.cowponents.entities.impl.component; + +import org.jetbrains.annotations.ApiStatus; +import org.jspecify.annotations.NullMarked; diff --git a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/mixin/Mixin_DataComponentInitializers.java b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/mixin/Mixin_DataComponentInitializers.java index 8dc5cb6..1868ff1 100644 --- a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/mixin/Mixin_DataComponentInitializers.java +++ b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/mixin/Mixin_DataComponentInitializers.java @@ -3,7 +3,7 @@ package lgbt.greenhouse.cowponents.entities.impl.mixin; import com.llamalad7.mixinextras.injector.ModifyReturnValue; import com.llamalad7.mixinextras.sugar.Local; import lgbt.greenhouse.cowponents.entities.api.v1.event.CowponentsDefaultEntityComponentEvents; -import lgbt.greenhouse.cowponents.entities.impl.world.entity.DefaultEntityComponentsImpl; +import lgbt.greenhouse.cowponents.entities.impl.component.DefaultEntityComponentsImpl; import net.minecraft.core.HolderLookup; import net.minecraft.core.component.DataComponentInitializers; import net.minecraft.core.component.DataComponentMap; @@ -18,7 +18,7 @@ import java.util.Map; @Mixin(DataComponentInitializers.class) public abstract class Mixin_DataComponentInitializers { @ModifyReturnValue(method = "runInitializers", at = @At(value = "RETURN")) - private static Map, DataComponentMap.Builder> cowponents$runDefaultEntityComponents(Map, DataComponentMap.Builder> original, @Local(argsOnly = true) HolderLookup.Provider lookupProvider) { + private static Map, DataComponentMap.Builder> cowponents_entities$runDefaultEntityComponents(Map, DataComponentMap.Builder> original, @Local(argsOnly = true) HolderLookup.Provider lookupProvider) { for (EntityType entityType : BuiltInRegistries.ENTITY_TYPE) { //noinspection deprecation ResourceKey> key = entityType.builtInRegistryHolder().key(); diff --git a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/mixin/Mixin_DataComponentTypeBuilderSimpleType.java b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/mixin/Mixin_DataComponentTypeBuilderSimpleType.java index 28a9d32..11970fd 100644 --- a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/mixin/Mixin_DataComponentTypeBuilderSimpleType.java +++ b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/mixin/Mixin_DataComponentTypeBuilderSimpleType.java @@ -6,17 +6,17 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Unique; @Mixin(targets = "net/minecraft/core/component/DataComponentType$Builder$SimpleType") -public class Mixin_DataComponentTypeBuilderSimpleType implements CowponentsEntityDataComponentType, Duck_TransferrableOnDeath { +public class Mixin_DataComponentTypeBuilderSimpleType implements CowponentsEntityDataComponentType, Duck_TransferrableOnDeath { @Unique - private boolean cowponents_instances$transferOnDeath = false; + private boolean cowponents_entities$transferOnDeath = false; @Override public boolean cowponents$transferOnDeath() { - return cowponents_instances$transferOnDeath; + return cowponents_entities$transferOnDeath; } @Override public void cowponents_entities$setTransferOnDeath(boolean value) { - cowponents_instances$transferOnDeath = value; + cowponents_entities$transferOnDeath = value; } } diff --git a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/mixin/Mixin_Entity.java b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/mixin/Mixin_Entity.java index ceeb5a2..3953e92 100644 --- a/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/mixin/Mixin_Entity.java +++ b/entities/xplat/src/main/java/lgbt/greenhouse/cowponents/entities/impl/mixin/Mixin_Entity.java @@ -7,15 +7,15 @@ import com.llamalad7.mixinextras.injector.ModifyExpressionValue; import com.llamalad7.mixinextras.injector.ModifyReturnValue; import com.llamalad7.mixinextras.sugar.Local; import com.mojang.serialization.DataResult; +import lgbt.greenhouse.cowponents.extensions.impl.component.InheritanceInternals; import lgbt.greenhouse.cowponents.extensions.impl.duck.Duck_InheritableDataComponentMap; -import lgbt.greenhouse.cowponents.extensions.impl.util.InheritedValueUtils; import lgbt.greenhouse.cowponents.impl.Cowponents; import lgbt.greenhouse.cowponents.impl.util.ComponentCastUtils; import lgbt.greenhouse.cowponents.impl.util.ComponentCodecUtils; import lgbt.greenhouse.cowponents.entities.api.v1.CowponentsEntityHelper; import lgbt.greenhouse.cowponents.entities.api.v1.extension.CowponentsEntityComponentHolder; import lgbt.greenhouse.cowponents.entities.impl.duck.Duck_HolderCowponentsEntityOriginated; -import lgbt.greenhouse.cowponents.entities.impl.world.entity.EntityComponentUtils; +import lgbt.greenhouse.cowponents.entities.impl.component.EntityComponentInternals; import net.minecraft.core.Holder; import net.minecraft.core.RegistryAccess; import net.minecraft.core.component.*; @@ -41,12 +41,12 @@ import static lgbt.greenhouse.cowponents.impl.util.ComponentCastUtils.castCompon @Mixin(Entity.class) public abstract class Mixin_Entity implements CowponentsEntityComponentHolder { @Unique - private PatchedDataComponentMap cowponents$components; + private PatchedDataComponentMap cowponents_entities$components; @Unique - private HashedPatchMap cowponents$hashedPatchMap; + private HashedPatchMap cowponents_entities$hashedPatchMap; @Unique - private LoadingCache, Integer> cowponents$componentCache = CacheBuilder.newBuilder() + private LoadingCache, Integer> cowponents_entities$componentCache = CacheBuilder.newBuilder() .maximumSize(256L) .build(new CacheLoader<>() { public @NonNull Integer load(@NonNull TypedDataComponent component) { @@ -61,44 +61,44 @@ public abstract class Mixin_Entity implements CowponentsEntityComponentHolder { public abstract RegistryAccess registryAccess(); @Inject(method = "", at = @At("TAIL")) - private void cowponents_instances$setEntityInstance(EntityType type, Level level, CallbackInfo ci) { - cowponents$components = new PatchedDataComponentMap(cowponents$getPrototype()); - cowponents$hashedPatchMap = HashedPatchMap.create( - cowponents$components.asPatch(), - cowponents$componentCache::getUnchecked + private void cowponents_entities$setEntityInstance(EntityType type, Level level, CallbackInfo ci) { + cowponents_entities$components = new PatchedDataComponentMap(cowponents$getPrototype()); + cowponents_entities$hashedPatchMap = HashedPatchMap.create( + cowponents_entities$components.asPatch(), + cowponents_entities$componentCache::getUnchecked ); if (Cowponents.getPlatformHelper().isModLoaded("cowponents_extensions")) { PatchedDataComponentMap components = (PatchedDataComponentMap) cowponents$getComponents(); ((Duck_InheritableDataComponentMap) (Object) components).cowponents_extensions$setInstance(this); - InheritedValueUtils.refreshInheritedValues(components, this); + InheritanceInternals.refreshInheritedValues(components, this); } } @Inject(method = "saveWithoutId", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;addAdditionalSaveData(Lnet/minecraft/world/level/storage/ValueOutput;)V")) - private void cowponents_instances$saveComponentPatch(ValueOutput output, CallbackInfo ci) { + private void cowponents_entities$saveComponentPatch(ValueOutput output, CallbackInfo ci) { DataComponentPatch patch = cowponents$getComponentsPatch(); output.storeNullable("cowponents:components", DataComponentPatch.CODEC, patch.isEmpty() ? null : patch); } @Inject(method = "load", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;readAdditionalSaveData(Lnet/minecraft/world/level/storage/ValueInput;)V")) - private void cowponents_instances$loadComponentPatch(ValueInput input, CallbackInfo ci) { + private void cowponents_entities$loadComponentPatch(ValueInput input, CallbackInfo ci) { input.read("cowponents:components", DataComponentPatch.CODEC).ifPresent(patch -> { DataComponentPatch oldPatch = cowponents$getComponentsPatch(); - cowponents$components.clearPatch(); + cowponents_entities$components.clearPatch(); cowponents$applyComponentsAndValidate(patch, oldPatch); }); } @ModifyExpressionValue(method = "get", at = @At(value = "INVOKE", target = "Lnet/minecraft/core/component/DataComponentMap;get(Lnet/minecraft/core/component/DataComponentType;)Ljava/lang/Object;")) - private T cowponents_instances$getEntityCowponent(@Nullable T original, @Local(argsOnly = true) DataComponentType componentType) { - if (cowponents$getComponents().has(componentType)) { - return cowponents$getComponents().get(componentType); + private T cowponents_entities$getEntityCowponent(@Nullable T original, @Local(argsOnly = true) DataComponentType type) { + if (cowponents$getComponents().has(type)) { + return cowponents$getComponents().get(type); } return original; } @ModifyReturnValue(method = "teleportCrossDimension", at = @At(value = "RETURN", ordinal = 1)) - private Entity cowponents$restoreComponents(Entity original) { + private Entity cowponents_entities$restoreComponents(Entity original) { DataComponentPatch.Builder patch = DataComponentPatch.builder(); for (var entry : CowponentsEntityHelper.getComponentsPatch(original).entrySet()) { entry.getValue().ifPresentOrElse( @@ -107,92 +107,92 @@ public abstract class Mixin_Entity implements CowponentsEntityComponentHolder { ); } CowponentsEntityHelper.applyComponentsAndValidate((Entity) (Object) this, patch.build()); - EntityComponentUtils.syncComponents(original); + EntityComponentInternals.syncComponents(original); return original; } @Override - public DataComponentMap cowponents$getComponents() { - return cowponents$components; + public @NonNull DataComponentMap cowponents$getComponents() { + return cowponents_entities$components; } @Override - public @Nullable T cowponents$set(DataComponentType type, @Nullable T value) { - T setValue = cowponents$components.set(ComponentCastUtils.castComponentType(type), value); - if (!cowponents$hashedPatchMap.matches(cowponents$getComponentsPatch(), cowponents$componentCache::getUnchecked)) { - EntityComponentUtils.syncComponentChanges( + public @Nullable T cowponents$set(@NonNull DataComponentType type, @Nullable T value) { + T setValue = cowponents_entities$components.set(ComponentCastUtils.castComponentType(type), value); + if (!cowponents_entities$hashedPatchMap.matches(cowponents$getComponentsPatch(), cowponents_entities$componentCache::getUnchecked)) { + EntityComponentInternals.syncComponentChanges( (Entity) (Object) this, - cowponents$components.asPatch(), - cowponents$hashedPatchMap, - cowponents$componentCache::getUnchecked + cowponents_entities$components.asPatch(), + cowponents_entities$hashedPatchMap, + cowponents_entities$componentCache::getUnchecked ); - cowponents$hashedPatchMap = HashedPatchMap.create( - cowponents$components.asPatch(), - cowponents$componentCache::getUnchecked + cowponents_entities$hashedPatchMap = HashedPatchMap.create( + cowponents_entities$components.asPatch(), + cowponents_entities$componentCache::getUnchecked ); } return setValue; } @Override - public @Nullable T cowponents$set(TypedDataComponent value) { - T setValue = cowponents$components.set(value); - if (!cowponents$hashedPatchMap.matches(cowponents$getComponentsPatch(), cowponents$componentCache::getUnchecked)) { - EntityComponentUtils.syncComponentChanges( + public @Nullable T cowponents$set(@NonNull TypedDataComponent value) { + T setValue = cowponents_entities$components.set(value); + if (!cowponents_entities$hashedPatchMap.matches(cowponents$getComponentsPatch(), cowponents_entities$componentCache::getUnchecked)) { + EntityComponentInternals.syncComponentChanges( (Entity) (Object) this, - cowponents$components.asPatch(), - cowponents$hashedPatchMap, - cowponents$componentCache::getUnchecked + cowponents_entities$components.asPatch(), + cowponents_entities$hashedPatchMap, + cowponents_entities$componentCache::getUnchecked ); - cowponents$hashedPatchMap = HashedPatchMap.create( - cowponents$components.asPatch(), - cowponents$componentCache::getUnchecked + cowponents_entities$hashedPatchMap = HashedPatchMap.create( + cowponents_entities$components.asPatch(), + cowponents_entities$componentCache::getUnchecked ); } return setValue; } @Override - public @Nullable T cowponents$remove(DataComponentType type) { - T removedValue = cowponents$components.remove(type); - if (!cowponents$hashedPatchMap.matches(cowponents$getComponentsPatch(), cowponents$componentCache::getUnchecked)) { - EntityComponentUtils.syncComponentChanges( + public @Nullable T cowponents$remove(@NonNull DataComponentType type) { + T removedValue = cowponents_entities$components.remove(type); + if (!cowponents_entities$hashedPatchMap.matches(cowponents$getComponentsPatch(), cowponents_entities$componentCache::getUnchecked)) { + EntityComponentInternals.syncComponentChanges( (Entity) (Object) this, - cowponents$components.asPatch(), - cowponents$hashedPatchMap, - cowponents$componentCache::getUnchecked + cowponents_entities$components.asPatch(), + cowponents_entities$hashedPatchMap, + cowponents_entities$componentCache::getUnchecked ); - cowponents$hashedPatchMap = HashedPatchMap.create( - cowponents$components.asPatch(), - cowponents$componentCache::getUnchecked + cowponents_entities$hashedPatchMap = HashedPatchMap.create( + cowponents_entities$components.asPatch(), + cowponents_entities$componentCache::getUnchecked ); } return removedValue; } @Override - public DataComponentPatch cowponents$getComponentsPatch() { - return cowponents$components.asPatch(); + public @NonNull DataComponentPatch cowponents$getComponentsPatch() { + return cowponents_entities$components.asPatch(); } @Override - public DataComponentMap cowponents$immutableComponents() { - return cowponents$components.toImmutableMap(); + public @NonNull DataComponentMap cowponents$immutableComponents() { + return cowponents_entities$components.toImmutableMap(); } @Override - public boolean cowponents$hasNonDefault(DataComponentType type) { - return cowponents$components.hasNonDefault(type); + public boolean cowponents$hasNonDefault(@NonNull DataComponentType type) { + return cowponents_entities$components.hasNonDefault(type); } @Override - public void cowponents$applyComponentsAndValidate(DataComponentPatch patch) { + public void cowponents$applyComponentsAndValidate(@NonNull DataComponentPatch patch) { DataComponentPatch oldPatch = cowponents$getComponentsPatch(); cowponents$applyComponentsAndValidate(patch, oldPatch); } @Override - public DataComponentMap cowponents$getPrototype() { + public @NonNull DataComponentMap cowponents$getPrototype() { Holder> holder = typeHolder(); var cowponentsOriginated = ((Duck_HolderCowponentsEntityOriginated) holder).cowponents_entities$getCowponentsOriginated(); @@ -208,20 +208,20 @@ public abstract class Mixin_Entity implements CowponentsEntityComponentHolder { private void cowponents$applyComponentsAndValidate(DataComponentPatch patch, DataComponentPatch oldPatch) { PatchedDataComponentMap components = (PatchedDataComponentMap) cowponents$getComponents(); components.applyPatch(patch); - Optional> validationError = EntityComponentUtils.validateEntity((Entity) (Object) this).error(); + Optional> validationError = EntityComponentInternals.validateEntity((Entity) (Object) this).error(); if (validationError.isPresent()) { - EntityComponentUtils.LOGGER.error("Failed to apply component patch '{}' to entity: '{}'", patch, validationError.get().message()); + EntityComponentInternals.LOGGER.error("Failed to apply component patch '{}' to entity: '{}'", patch, validationError.get().message()); components.restorePatch(oldPatch); } - EntityComponentUtils.syncComponentChanges( + EntityComponentInternals.syncComponentChanges( (Entity) (Object) this, - cowponents$components.asPatch(), - cowponents$hashedPatchMap, - cowponents$componentCache::getUnchecked + cowponents_entities$components.asPatch(), + cowponents_entities$hashedPatchMap, + cowponents_entities$componentCache::getUnchecked ); - cowponents$hashedPatchMap = HashedPatchMap.create( - cowponents$components.asPatch(), - cowponents$componentCache::getUnchecked + cowponents_entities$hashedPatchMap = HashedPatchMap.create( + cowponents_entities$components.asPatch(), + cowponents_entities$componentCache::getUnchecked ); } } diff --git a/entities/xplat/src/main/resources/cowponents_entities.mixins.json b/entities/xplat/src/main/resources/cowponents_entities.mixins.json index 056b859..db9a37b 100644 --- a/entities/xplat/src/main/resources/cowponents_entities.mixins.json +++ b/entities/xplat/src/main/resources/cowponents_entities.mixins.json @@ -1,17 +1,20 @@ { - "required": true, - "minVersion": "0.8", - "package": "lgbt.greenhouse.cowponents.entities.impl.mixin", - "compatibilityLevel": "JAVA_25", - "mixins": [ - "Mixin_DataComponentInitializers", - "Mixin_DataComponentTypeBuilder", - "Mixin_DataComponentTypeBuilderSimpleType", - "Mixin_Entity", - "Mixin_EntityTypeBuilder", - "Mixin_HolderReference" - ], - "injectors": { - "defaultRequire": 1 + "required": true, + "minVersion": "0.8", + "package": "lgbt.greenhouse.cowponents.entities.impl.mixin", + "compatibilityLevel": "JAVA_25", + "mixins": [ + "Mixin_DataComponentInitializers", + "Mixin_DataComponentTypeBuilder", + "Mixin_DataComponentTypeBuilderSimpleType", + "Mixin_Entity", + "Mixin_EntityTypeBuilder", + "Mixin_HolderReference" + ], + "injectors": { + "defaultRequire": 1 + }, + "mixinextras": { + "minVersion": "0.5.0" } -} +} \ No newline at end of file diff --git a/extensions/fabric/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/fabric/client/CowponentsExtensionsClientFabric.java b/extensions/fabric/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/fabric/client/CowponentsExtensionsClientFabric.java index 4cd2f89..0216308 100644 --- a/extensions/fabric/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/fabric/client/CowponentsExtensionsClientFabric.java +++ b/extensions/fabric/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/fabric/client/CowponentsExtensionsClientFabric.java @@ -1,6 +1,6 @@ package lgbt.greenhouse.cowponents.extensions.impl.fabric.client; -import lgbt.greenhouse.cowponents.extensions.impl.event.CowponentsInheritanceIdImpl; +import lgbt.greenhouse.cowponents.extensions.impl.component.ComponentInheritanceIdRegistryImpl; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; @@ -12,6 +12,6 @@ public class CowponentsExtensionsClientFabric implements ClientModInitializer { private static void registerEvents() { ClientLifecycleEvents.CLIENT_STARTED.register(_ -> - CowponentsInheritanceIdImpl.makeImmutable()); + ComponentInheritanceIdRegistryImpl.makeImmutable()); } } diff --git a/extensions/fabric/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/fabric/server/dedicated/CowponentsExtensionsDedicatedFabric.java b/extensions/fabric/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/fabric/server/dedicated/CowponentsExtensionsDedicatedFabric.java index 02676b9..d8e844d 100644 --- a/extensions/fabric/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/fabric/server/dedicated/CowponentsExtensionsDedicatedFabric.java +++ b/extensions/fabric/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/fabric/server/dedicated/CowponentsExtensionsDedicatedFabric.java @@ -1,6 +1,6 @@ package lgbt.greenhouse.cowponents.extensions.impl.fabric.server.dedicated; -import lgbt.greenhouse.cowponents.extensions.impl.event.CowponentsInheritanceIdImpl; +import lgbt.greenhouse.cowponents.extensions.impl.component.ComponentInheritanceIdRegistryImpl; import net.fabricmc.api.DedicatedServerModInitializer; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; @@ -12,6 +12,6 @@ public class CowponentsExtensionsDedicatedFabric implements DedicatedServerModIn private static void registerEvents() { ServerLifecycleEvents.SERVER_STARTED.register(_ -> - CowponentsInheritanceIdImpl.makeImmutable()); + ComponentInheritanceIdRegistryImpl.makeImmutable()); } } diff --git a/extensions/neoforge/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/neoforge/CowponentsExtensionsNeoForge.java b/extensions/neoforge/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/neoforge/CowponentsExtensionsNeoForge.java index 57733d0..954ff16 100644 --- a/extensions/neoforge/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/neoforge/CowponentsExtensionsNeoForge.java +++ b/extensions/neoforge/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/neoforge/CowponentsExtensionsNeoForge.java @@ -1,7 +1,7 @@ package lgbt.greenhouse.cowponents.extensions.impl.neoforge; import lgbt.greenhouse.cowponents.extensions.impl.CowponentsExtensions; -import lgbt.greenhouse.cowponents.extensions.impl.event.CowponentsInheritanceIdImpl; +import lgbt.greenhouse.cowponents.extensions.impl.component.ComponentInheritanceIdRegistryImpl; import net.neoforged.bus.api.IEventBus; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.Mod; @@ -15,6 +15,6 @@ public class CowponentsExtensionsNeoForge { @SubscribeEvent public static void onFmlCommonSetup(FMLCommonSetupEvent event) { - event.enqueueWork(CowponentsInheritanceIdImpl::makeImmutable); + event.enqueueWork(ComponentInheritanceIdRegistryImpl::makeImmutable); } } diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/ComponentInheritanceIdRegistry.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/ComponentInheritanceIdRegistry.java new file mode 100644 index 0000000..012b389 --- /dev/null +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/ComponentInheritanceIdRegistry.java @@ -0,0 +1,12 @@ +package lgbt.greenhouse.cowponents.extensions.api.v1; + +import lgbt.greenhouse.cowponents.extensions.impl.component.ComponentInheritanceIdRegistryImpl; +import net.minecraft.resources.Identifier; + +public class ComponentInheritanceIdRegistry { + private ComponentInheritanceIdRegistry() {} + + public static void register(Class clazz, Identifier id) { + ComponentInheritanceIdRegistryImpl.register(clazz, id); + } +} diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/CowponentsExtensionsDataComponentTypeBuilderWrapper.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/CowponentsExtensionsDataComponentTypeBuilderWrapper.java deleted file mode 100644 index 45367f0..0000000 --- a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/CowponentsExtensionsDataComponentTypeBuilderWrapper.java +++ /dev/null @@ -1,107 +0,0 @@ -package lgbt.greenhouse.cowponents.extensions.api.v1; - -import com.mojang.serialization.Codec; -import lgbt.greenhouse.cowponents.extensions.api.v1.event.InheritComponentCallback; -import lgbt.greenhouse.cowponents.extensions.api.v1.extension.CowponentsExtensionsDataComponentTypeBuilder; -import net.minecraft.core.component.DataComponentType; -import org.jspecify.annotations.NonNull; - -import java.util.function.Supplier; - -@SuppressWarnings("unchecked") -public class CowponentsExtensionsDataComponentTypeBuilderWrapper { - public static CowponentsExtensionsDataComponentTypeBuilderWrapper wrap(DataComponentType.Builder toWrap) { - return new CowponentsExtensionsDataComponentTypeBuilderWrapper<>(toWrap); - } - - private final DataComponentType.Builder wrapped; - - private CowponentsExtensionsDataComponentTypeBuilderWrapper(DataComponentType.Builder wrapped) { - this.wrapped = wrapped; - } - - /** - * Allows this data component type to inherit a value from another data component type. - *

- * If the this data component type has a value via {@link DataComponentType.Builder#persistent(Codec)}, that value will be used instead. - * - * @param from Another data component type that this component type will inherit from. - * @param valueConstructor A constructor to create a value for this component from the 'from' component type's value. - * @param The value class of the 'from' data component type. - * @return This builder. - */ - @NonNull - public CowponentsExtensionsDataComponentTypeBuilderWrapper inheritFrom(DataComponentType from, InheritComponentCallback valueConstructor) { - ((CowponentsExtensionsDataComponentTypeBuilder) wrapped).cowponents$inheritFrom(from, valueConstructor); - return this; - } - - /** - * Allows this data component type to inherit a value from another data component type. - *

- * If the this data component type has a value via {@link DataComponentType.Builder#persistent(Codec)}, that value will be used instead. - * - * @param from Another data component type that this component type will inherit from. - * @param valueConstructor A constructor to create a value for this component from the 'from' component type's value. - * @param The value class of the 'from' data component type. - * @return This builder. - */ - @NonNull - public CowponentsExtensionsDataComponentTypeBuilderWrapper inheritFrom(Supplier> from, InheritComponentCallback valueConstructor) { - ((CowponentsExtensionsDataComponentTypeBuilder) wrapped).cowponents$inheritFrom(from, valueConstructor); - return this; - } - - /** - * Allows another data component type to inherit a value from this data component type. - *

- * If the 'to' data component type has a value of its own, that value will be used instead. - * - * @param to Another data component type that will inherit from this component type. - * @param valueConstructor A constructor to create a value for the other component from this component type's value. - * @param The value class of the 'to' data component type. - * @return This builder. - */ - @NonNull - public CowponentsExtensionsDataComponentTypeBuilderWrapper inheritTo(DataComponentType to, InheritComponentCallback valueConstructor) { - ((CowponentsExtensionsDataComponentTypeBuilder) wrapped).cowponents$inheritTo(to, valueConstructor); - return this; - } - - /** - * Allows another data component type to inherit a value from this data component type. - *

- * If the 'to' data component type has a value of its own, that value will be used instead. - * - * @param to Another data component type that will inherit from this component type. - * @param valueConstructor A constructor to create a value for the other component from this component type's value. - * @param The value class of the 'to' data component type. - * @return This builder. - */ - @NonNull - public CowponentsExtensionsDataComponentTypeBuilderWrapper inheritTo(Supplier> to, InheritComponentCallback valueConstructor) { - ((CowponentsExtensionsDataComponentTypeBuilder) wrapped).cowponents$inheritTo(to, valueConstructor); - return this; - } - - - /** - * Unwraps the {@link DataComponentType.Builder}. - * - * @return The wrapped data component type builder. - */ - public DataComponentType.Builder unwrap() { - return wrapped; - } - - /** - * Builds the wrapped builder. - *

- * Provided for convenience to avoid unnecessary unwrapping. - * - * @return A built {@link DataComponentType} - */ - public DataComponentType build() { - return wrapped.build(); - } -} diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/CowponentsExtensionsInheritanceIds.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/CowponentsExtensionsInheritanceIds.java index c652e2d..1e1540a 100644 --- a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/CowponentsExtensionsInheritanceIds.java +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/CowponentsExtensionsInheritanceIds.java @@ -4,4 +4,6 @@ import net.minecraft.resources.Identifier; public class CowponentsExtensionsInheritanceIds { public static final Identifier ITEM_STACK = Identifier.withDefaultNamespace("item_stack"); + + private CowponentsExtensionsInheritanceIds() {} } diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/CowponentsInheritanceIdRegistry.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/CowponentsInheritanceIdRegistry.java deleted file mode 100644 index 461fc43..0000000 --- a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/CowponentsInheritanceIdRegistry.java +++ /dev/null @@ -1,10 +0,0 @@ -package lgbt.greenhouse.cowponents.extensions.api.v1; - -import lgbt.greenhouse.cowponents.extensions.impl.event.CowponentsInheritanceIdImpl; -import net.minecraft.resources.Identifier; - -public class CowponentsInheritanceIdRegistry { - public static void register(Class clazz, Identifier id) { - CowponentsInheritanceIdImpl.register(clazz, id); - } -} diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/CowponentsComponentInheritanceEvents.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/CowponentsComponentInheritanceEvents.java deleted file mode 100644 index f39fb26..0000000 --- a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/CowponentsComponentInheritanceEvents.java +++ /dev/null @@ -1,40 +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.CowponentsExtensions; -import lgbt.greenhouse.cowponents.extensions.impl.event.CowponentsInheritanceEventsImpl; -import net.minecraft.core.component.DataComponentType; -import net.minecraft.resources.Identifier; - -import static lgbt.greenhouse.cowponents.extensions.impl.event.CowponentsInheritanceEventsImpl.INHERIT_FROM_OTHER_COMPONENT_MAP; -import static lgbt.greenhouse.cowponents.extensions.impl.event.CowponentsInheritanceEventsImpl.unsafeCastEventObjects; - -public class CowponentsComponentInheritanceEvents { - public static final Identifier CONSTRUCTOR_PHASE = CowponentsExtensions.id("constructor"); - - private static final FilteredEvent, CowponentsInheritanceEventsImpl.InheritanceComponents> INHERIT_FROM_OTHER_COMPONENT = YumiEvents.EVENTS.createFiltered(InheritComponentCallback.class, CowponentsInheritanceEventsImpl.InheritanceComponents.class, callbacks -> (value, registries) -> { - for (var callback : callbacks) { - Object returnValue = callback.inherit(value, registries); - if (returnValue != null) { - return returnValue; - } - } - return null; - }); - - public static void register(DataComponentType fromType, DataComponentType toType, InheritComponentCallback callback) { - INHERIT_FROM_OTHER_COMPONENT_MAP.computeIfAbsent(new CowponentsInheritanceEventsImpl.InheritanceComponents<>(fromType, toType), INHERIT_FROM_OTHER_COMPONENT::forContext) - .register(unsafeCastEventObjects(callback)); - } - - public static void register(Identifier phase, DataComponentType fromType, DataComponentType toType, InheritComponentCallback callback) { - INHERIT_FROM_OTHER_COMPONENT_MAP.computeIfAbsent(new CowponentsInheritanceEventsImpl.InheritanceComponents<>(fromType, toType), INHERIT_FROM_OTHER_COMPONENT::forContext) - .register(phase, unsafeCastEventObjects(callback)); - } - - public static void addPhaseOrdering(Identifier firstPhase, Identifier secondPhase, DataComponentType fromType, DataComponentType toType) { - INHERIT_FROM_OTHER_COMPONENT_MAP.computeIfAbsent(new CowponentsInheritanceEventsImpl.InheritanceComponents<>(fromType, toType), INHERIT_FROM_OTHER_COMPONENT::forContext) - .addPhaseOrdering(firstPhase, secondPhase); - } -} diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/CowponentsInheritanceImplementationEvents.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/InheritanceImplementationEvents.java similarity index 87% rename from extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/CowponentsInheritanceImplementationEvents.java rename to extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/InheritanceImplementationEvents.java index 63bff25..22ad9e3 100644 --- a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/CowponentsInheritanceImplementationEvents.java +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/InheritanceImplementationEvents.java @@ -3,15 +3,17 @@ 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.api.v1.DataComponentGetterSetter; -import lgbt.greenhouse.cowponents.extensions.impl.event.CowponentsInheritanceIdImpl; +import lgbt.greenhouse.cowponents.extensions.impl.component.ComponentInheritanceIdRegistryImpl; import lgbt.greenhouse.cowponents.impl.Cowponents; import net.minecraft.core.HolderLookup; import net.minecraft.resources.Identifier; import org.jspecify.annotations.Nullable; -import static lgbt.greenhouse.cowponents.extensions.impl.event.CowponentsInheritanceEventsImpl.*; +import static lgbt.greenhouse.cowponents.extensions.impl.component.InheritanceInternals.*; + +public class InheritanceImplementationEvents { + private InheritanceImplementationEvents() {} -public class CowponentsInheritanceImplementationEvents { private static final FilteredEvent, Identifier> OBTAIN_REGISTRIES = YumiEvents.EVENTS.createFiltered(ObtainLookupCallback.class, Identifier.class, callbacks -> (instance) -> { for (var callback : callbacks) { HolderLookup.Provider access = callback.getHolderLookupProvider(instance); @@ -32,7 +34,7 @@ public class CowponentsInheritanceImplementationEvents { OBTAIN_LOOKUP_MAP.remove(inheritanceId); OBTAIN_LOOKUP_MAP.computeIfAbsent(inheritanceId, OBTAIN_REGISTRIES::forContext) .register(instance -> { - Class clazz = CowponentsInheritanceIdImpl.getClass(inheritanceId); + Class clazz = ComponentInheritanceIdRegistryImpl.getClass(inheritanceId); try { if (clazz != null && clazz.isInstance(instance)) { //noinspection unchecked @@ -52,7 +54,7 @@ public class CowponentsInheritanceImplementationEvents { INSTANCED_INHERIT_MAP.remove(inheritanceId); INSTANCED_INHERIT_MAP.computeIfAbsent(inheritanceId, INSTANCED_INHERIT::forContext) .register((instance, registries, getterSetter) -> { - Class clazz = CowponentsInheritanceIdImpl.getClass(inheritanceId); + Class clazz = ComponentInheritanceIdRegistryImpl.getClass(inheritanceId); try { if (clazz != null && clazz.isInstance(instance)) { //noinspection unchecked diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/CowponentsItemStackInheritanceEvents.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/ItemStackInheritanceEvents.java similarity index 80% rename from extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/CowponentsItemStackInheritanceEvents.java rename to extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/ItemStackInheritanceEvents.java index 1654849..f6f8eb0 100644 --- a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/CowponentsItemStackInheritanceEvents.java +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/ItemStackInheritanceEvents.java @@ -2,15 +2,17 @@ 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.event.CowponentsInheritanceEventsImpl; +import lgbt.greenhouse.cowponents.extensions.impl.component.InheritanceInternals; import net.minecraft.core.component.DataComponentType; import net.minecraft.resources.Identifier; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.ItemLike; -import static lgbt.greenhouse.cowponents.extensions.impl.event.CowponentsInheritanceEventsImpl.INHERIT_FROM_ITEM_STACK_MAP; +import static lgbt.greenhouse.cowponents.extensions.impl.component.InheritanceInternals.INHERIT_FROM_ITEM_STACK_MAP; + +public class ItemStackInheritanceEvents { + private ItemStackInheritanceEvents() {} -public class CowponentsItemStackInheritanceEvents { 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); @@ -23,17 +25,17 @@ public class CowponentsItemStackInheritanceEvents { public static void register(DataComponentType componentType, InheritComponentCallback callback) { INHERIT_FROM_ITEM_STACK_MAP.computeIfAbsent(componentType, INHERIT_FROM_ITEM_STACK::forContext) - .register(CowponentsInheritanceEventsImpl.unsafeCastEvent(callback)); + .register(InheritanceInternals.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, CowponentsInheritanceEventsImpl.unsafeCastEvent(callback)); + .register(phase, InheritanceInternals.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(CowponentsInheritanceEventsImpl.filteredEventResult(callback, stack -> { + .register(InheritanceInternals.filteredEventResult(callback, stack -> { if (allowedItems.length == 0) return true; @@ -48,7 +50,7 @@ public class CowponentsItemStackInheritanceEvents { 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, CowponentsInheritanceEventsImpl.filteredEventResult(callback, stack -> { + .register(phase, InheritanceInternals.filteredEventResult(callback, stack -> { if (allowedItems.length == 0) return true; diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/package-info.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/package-info.java new file mode 100644 index 0000000..ee28efd --- /dev/null +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/event/package-info.java @@ -0,0 +1,4 @@ +@NullMarked +package lgbt.greenhouse.cowponents.extensions.api.v1.event; + +import org.jspecify.annotations.NullMarked; diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/package-info.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/package-info.java new file mode 100644 index 0000000..7ca3d8e --- /dev/null +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/api/v1/package-info.java @@ -0,0 +1,4 @@ +@NullMarked +package lgbt.greenhouse.cowponents.extensions.api.v1; + +import org.jspecify.annotations.NullMarked; diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/CowponentsExtensions.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/CowponentsExtensions.java index e982998..3a40a0b 100644 --- a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/CowponentsExtensions.java +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/CowponentsExtensions.java @@ -1,9 +1,9 @@ package lgbt.greenhouse.cowponents.extensions.impl; import lgbt.greenhouse.cowponents.extensions.api.v1.CowponentsExtensionsInheritanceIds; -import lgbt.greenhouse.cowponents.extensions.api.v1.CowponentsInheritanceIdRegistry; -import lgbt.greenhouse.cowponents.extensions.api.v1.event.CowponentsInheritanceImplementationEvents; -import lgbt.greenhouse.cowponents.extensions.impl.util.InheritedValueUtils; +import lgbt.greenhouse.cowponents.extensions.api.v1.ComponentInheritanceIdRegistry; +import lgbt.greenhouse.cowponents.extensions.api.v1.event.InheritanceImplementationEvents; +import lgbt.greenhouse.cowponents.extensions.impl.component.ItemStackInheritanceInternals; import net.minecraft.resources.Identifier; import net.minecraft.world.item.ItemStack; import org.slf4j.Logger; @@ -15,8 +15,8 @@ public class CowponentsExtensions { public static final Logger LOGGER = LoggerFactory.getLogger(MOD_NAME); public static void init() { - CowponentsInheritanceIdRegistry.register(ItemStack.class, CowponentsExtensionsInheritanceIds.ITEM_STACK); - CowponentsInheritanceImplementationEvents.registerInstancedInheritEvent(CowponentsExtensionsInheritanceIds.ITEM_STACK, InheritedValueUtils::refreshInheritedFromItemStack); + ComponentInheritanceIdRegistry.register(ItemStack.class, CowponentsExtensionsInheritanceIds.ITEM_STACK); + InheritanceImplementationEvents.registerInstancedInheritEvent(CowponentsExtensionsInheritanceIds.ITEM_STACK, ItemStackInheritanceInternals::refreshInheritedFromItemStack); } public static Identifier id(String path) { diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/event/CowponentsInheritanceIdImpl.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/ComponentInheritanceIdRegistryImpl.java similarity index 83% rename from extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/event/CowponentsInheritanceIdImpl.java rename to extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/ComponentInheritanceIdRegistryImpl.java index ea47199..9cd0371 100644 --- a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/event/CowponentsInheritanceIdImpl.java +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/ComponentInheritanceIdRegistryImpl.java @@ -1,4 +1,4 @@ -package lgbt.greenhouse.cowponents.extensions.impl.event; +package lgbt.greenhouse.cowponents.extensions.impl.component; import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap; import net.minecraft.resources.Identifier; @@ -7,11 +7,11 @@ import org.jspecify.annotations.Nullable; import java.util.Collections; import java.util.Map; -public class CowponentsInheritanceIdImpl { +public class ComponentInheritanceIdRegistryImpl { private static Map, Identifier> REGISTRY = new Object2ObjectArrayMap<>(); public static void register(Class clazz, Identifier id) { - CowponentsInheritanceIdImpl.REGISTRY.put(clazz, id); + ComponentInheritanceIdRegistryImpl.REGISTRY.put(clazz, id); } public static void makeImmutable() { diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/event/CowponentsInheritanceEventsImpl.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/InheritanceInternals.java similarity index 61% rename from extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/event/CowponentsInheritanceEventsImpl.java rename to extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/InheritanceInternals.java index 90c22d4..584e45f 100644 --- a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/event/CowponentsInheritanceEventsImpl.java +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/InheritanceInternals.java @@ -1,16 +1,19 @@ -package lgbt.greenhouse.cowponents.extensions.impl.event; +package lgbt.greenhouse.cowponents.extensions.impl.component; import dev.yumi.commons.event.ContextualizedEvent; import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap; import lgbt.greenhouse.cowponents.api.v1.DataComponentGetterSetter; -import lgbt.greenhouse.cowponents.extensions.api.v1.event.CowponentsInheritanceImplementationEvents; +import lgbt.greenhouse.cowponents.extensions.api.v1.event.InheritanceImplementationEvents; import lgbt.greenhouse.cowponents.extensions.api.v1.event.InheritComponentCallback; import lgbt.greenhouse.cowponents.extensions.impl.CowponentsExtensions; +import lgbt.greenhouse.cowponents.extensions.impl.duck.Duck_InheritableDataComponentMap; import lgbt.greenhouse.cowponents.impl.Cowponents; 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.NonNull; import org.jspecify.annotations.Nullable; import org.slf4j.Logger; @@ -19,21 +22,21 @@ import java.util.Map; import java.util.Set; import java.util.function.Predicate; -public class CowponentsInheritanceEventsImpl { - public static final Logger ON_INHERIT_LOGGER = CowponentsExtensions.getLogger("On Inherit Event"); +public class InheritanceInternals { + public static final Logger ON_INHERIT_LOGGER = CowponentsExtensions.getLogger("Inheritance Instance Event"); public static final Logger OBTAIN_REGISTRIES_LOGGER = CowponentsExtensions.getLogger("Obtain Registries Event"); public static final Set WARNED_IDS = new HashSet<>(); - public static final Map, ContextualizedEvent, InheritanceComponents>> INHERIT_FROM_OTHER_COMPONENT_MAP = new Object2ObjectArrayMap<>(); + 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 final Map, Identifier>> OBTAIN_LOOKUP_MAP = new Object2ObjectArrayMap<>(); public static HolderLookup.Provider invokeObtainHolderLookupProvider(@Nullable T instance) { if (instance == null) return Cowponents.getSideHelper().getHolderLookupProvider(); - Identifier inheritanceId = CowponentsInheritanceIdImpl.get(instance); + Identifier inheritanceId = ComponentInheritanceIdRegistryImpl.get(instance); if (inheritanceId == null || !OBTAIN_LOOKUP_MAP.containsKey(inheritanceId)) return Cowponents.getSideHelper().getHolderLookupProvider(); @@ -50,7 +53,7 @@ public class CowponentsInheritanceEventsImpl { public static void invokeInstancedInherit(T instance, HolderLookup.Provider registries, DataComponentGetterSetter getterSetter) { - Identifier inheritanceId = CowponentsInheritanceIdImpl.get(instance); + Identifier inheritanceId = ComponentInheritanceIdRegistryImpl.get(instance); if (inheritanceId == null || !INSTANCED_INHERIT_MAP.containsKey(inheritanceId)) return; INSTANCED_INHERIT_MAP.get(inheritanceId) @@ -58,18 +61,6 @@ public class CowponentsInheritanceEventsImpl { .inherit(instance, registries, getterSetter); } - @SuppressWarnings("unchecked") - @Nullable - public static T invokeInheritFromOtherComponentEvent(DataComponentType fromType, DataComponentType toType, F fromValue, HolderLookup.Provider lookupProvider) { - InheritanceComponents key = new InheritanceComponents<>(fromType, toType); - if (INHERIT_FROM_OTHER_COMPONENT_MAP.containsKey(key)) { - return (T) INHERIT_FROM_OTHER_COMPONENT_MAP.get(key) - .invoker() - .inherit(fromValue, lookupProvider); - } - return null; - } - @SuppressWarnings("unchecked") @Nullable public static T invokeInheritFromItemStackEvent(DataComponentType type, ItemStack stack, HolderLookup.Provider lookupProvider) { @@ -94,20 +85,30 @@ public class CowponentsInheritanceEventsImpl { return callback::inherit; } - public static InheritComponentCallback unsafeCastEventInstance(InheritComponentCallback callback) { - return (value, registries) -> { - //noinspection unchecked - return callback.inherit((I) value, registries); - }; + public static void refreshInheritedValues(PatchedDataComponentMap components, + Object instance) { + HolderLookup.Provider lookupProvider = InheritanceInternals.invokeObtainHolderLookupProvider(instance); + if (instance != null && lookupProvider != null) { + ((Duck_InheritableDataComponentMap) (Object) components).cowponents_extensions$getInherited().clear(); + InheritanceInternals.invokeInstancedInherit( + instance, + lookupProvider, + getterSetterForInstanceInherited(components) + ); + } } - public static InheritComponentCallback unsafeCastEventObjects(InheritComponentCallback callback) { - return (value, registries) -> { - //noinspection unchecked - return callback.inherit((I) value, registries); - }; - } + public static DataComponentGetterSetter getterSetterForInstanceInherited(PatchedDataComponentMap components) { + return new DataComponentGetterSetter() { + @Override + public void set(DataComponentType type, T value) { + ((Duck_InheritableDataComponentMap) (Object) components).cowponents_extensions$getInherited().put(type, value); + } - public record InheritanceComponents(DataComponentType from, DataComponentType to) { + @Override + public @Nullable T get(@NonNull DataComponentType type) { + return components.get(type); + } + }; } } diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/ItemStackInheritanceInternals.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/ItemStackInheritanceInternals.java new file mode 100644 index 0000000..06fff59 --- /dev/null +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/ItemStackInheritanceInternals.java @@ -0,0 +1,27 @@ +package lgbt.greenhouse.cowponents.extensions.impl.component; + +import lgbt.greenhouse.cowponents.api.v1.DataComponentGetterSetter; +import net.minecraft.core.HolderLookup; +import net.minecraft.core.component.DataComponentType; +import net.minecraft.world.item.ItemStack; + +public class ItemStackInheritanceInternals { + public static void refreshInheritedFromItemStack(ItemStack stack, HolderLookup.Provider lookupProvider, DataComponentGetterSetter getterSetter) { + if (stack.isEmpty()) + return; + + for (var type : InheritanceInternals.INHERIT_FROM_ITEM_STACK_MAP.keySet()) { + inheritValueFromItemStack(lookupProvider, stack, type, getterSetter); + } + } + + private static void inheritValueFromItemStack(HolderLookup.Provider lookupProvider, + ItemStack stack, + DataComponentType type, + DataComponentGetterSetter getterSetter) { + T toValue = InheritanceInternals.invokeInheritFromItemStackEvent(type, stack, lookupProvider); + if (toValue != null) { + getterSetter.set(type, toValue); + } + } +} diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/package-info.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/package-info.java new file mode 100644 index 0000000..05a332e --- /dev/null +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/component/package-info.java @@ -0,0 +1,6 @@ +@ApiStatus.Internal +@NullMarked +package lgbt.greenhouse.cowponents.extensions.impl.component; + +import org.jetbrains.annotations.ApiStatus; +import org.jspecify.annotations.NullMarked; 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 f860b23..af8b8ff 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 @@ -8,11 +8,7 @@ public interface Duck_InheritableDataComponentMap { void cowponents_extensions$setInstance(Object object); - Reference2ObjectMap, Object> cowponents_extensions$getFromComponentsInherited(); + Reference2ObjectMap, Object> cowponents_extensions$getInherited(); - void cowponents_extensions$setFromComponentsInherited(Reference2ObjectMap, Object> value); - - Reference2ObjectMap, Object> cowponents_extensions$getFromInstanceInherited(); - - void cowponents_extensions$setFromInstanceInherited(Reference2ObjectMap, Object> value); + void cowponents_extensions$setInherited(Reference2ObjectMap, Object> value); } diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/duck/package-info.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/duck/package-info.java new file mode 100644 index 0000000..ed75238 --- /dev/null +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/duck/package-info.java @@ -0,0 +1,6 @@ +@ApiStatus.Internal +@NullMarked +package lgbt.greenhouse.cowponents.extensions.impl.duck; + +import org.jetbrains.annotations.ApiStatus; +import org.jspecify.annotations.NullMarked; diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/mixin/Accessor_PatchedDataComponentMap.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/mixin/Accessor_PatchedDataComponentMap.java deleted file mode 100644 index 68a8104..0000000 --- a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/mixin/Accessor_PatchedDataComponentMap.java +++ /dev/null @@ -1,19 +0,0 @@ -package lgbt.greenhouse.cowponents.extensions.impl.mixin; - -import it.unimi.dsi.fastutil.objects.Reference2ObjectMap; -import net.minecraft.core.component.DataComponentMap; -import net.minecraft.core.component.DataComponentType; -import net.minecraft.core.component.PatchedDataComponentMap; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Accessor; - -import java.util.Optional; - -@Mixin(PatchedDataComponentMap.class) -public interface Accessor_PatchedDataComponentMap { - @Accessor("prototype") - DataComponentMap cowponents_extensions$getPrototype(); - - @Accessor("patch") - Reference2ObjectMap, Optional> cowponents_extensions$getPatch(); -} diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/mixin/Mixin_ItemStack.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/mixin/Mixin_ItemStack.java index de15b46..13db904 100644 --- a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/mixin/Mixin_ItemStack.java +++ b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/mixin/Mixin_ItemStack.java @@ -1,7 +1,7 @@ package lgbt.greenhouse.cowponents.extensions.impl.mixin; +import lgbt.greenhouse.cowponents.extensions.impl.component.InheritanceInternals; import lgbt.greenhouse.cowponents.extensions.impl.duck.Duck_InheritableDataComponentMap; -import lgbt.greenhouse.cowponents.extensions.impl.util.InheritedValueUtils; import net.minecraft.core.Holder; import net.minecraft.core.component.PatchedDataComponentMap; import net.minecraft.world.item.Item; @@ -19,6 +19,6 @@ public class Mixin_ItemStack { PatchedDataComponentMap components, CallbackInfo ci) { ((Duck_InheritableDataComponentMap) (Object) components).cowponents_extensions$setInstance(this); - InheritedValueUtils.refreshInheritedValues(components, this); + InheritanceInternals.refreshInheritedValues(components, this); } } 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 e4b03f1..549b1f5 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 @@ -7,8 +7,8 @@ import it.unimi.dsi.fastutil.objects.Reference2ObjectArrayMap; import it.unimi.dsi.fastutil.objects.Reference2ObjectMap; import it.unimi.dsi.fastutil.objects.Reference2ObjectMaps; import it.unimi.dsi.fastutil.objects.ReferenceArraySet; +import lgbt.greenhouse.cowponents.extensions.impl.component.InheritanceInternals; import lgbt.greenhouse.cowponents.extensions.impl.duck.Duck_InheritableDataComponentMap; -import lgbt.greenhouse.cowponents.extensions.impl.util.InheritedValueUtils; import net.minecraft.core.component.DataComponentMap; import net.minecraft.core.component.DataComponentType; import net.minecraft.core.component.PatchedDataComponentMap; @@ -27,7 +27,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import java.util.*; -import java.util.stream.Stream; @Mixin(PatchedDataComponentMap.class) public class Mixin_PatchedDataComponentMap implements Duck_InheritableDataComponentMap { @@ -40,83 +39,69 @@ public class Mixin_PatchedDataComponentMap implements Duck_InheritableDataCompon @Unique private Object cowponents_extensions$instance; @Unique - private final Reference2ObjectMap, Object> cowponents_extensions$fromComponentInherited = new Reference2ObjectArrayMap<>(); - @Unique - private final Reference2ObjectMap, Object> cowponents_extensions$fromInstanceInherited = new Reference2ObjectArrayMap<>(); + private final Reference2ObjectMap, Object> cowponents_extensions$inherited = new Reference2ObjectArrayMap<>(); @ModifyReturnValue(method = "fromPatch", at = @At("RETURN")) private static PatchedDataComponentMap cowponents_extensions$refreshInheritedFromPatch(PatchedDataComponentMap original) { - InheritedValueUtils.refreshInheritedValues(original, ((Duck_InheritableDataComponentMap) (Object) original).cowponents_extensions$getInstance()); + InheritanceInternals.refreshInheritedValues(original, ((Duck_InheritableDataComponentMap) (Object) original).cowponents_extensions$getInstance()); return original; } @SuppressWarnings({"unchecked", "OptionalAssignedToNull"}) @ModifyReturnValue(method = "get", at = @At("RETURN")) private T cowponents_extensions$inheritComponentValues(@Nullable T original, @Local(argsOnly = true) DataComponentType type) { - if (cowponents_extensions$fromComponentInherited.containsKey(type)) { - Optional patchValue = (Optional) patch.get(type); - return (T) (patchValue != null - ? patchValue.orElse(null) - : cowponents_extensions$fromComponentInherited.get(type)); - } - if (cowponents_extensions$fromInstanceInherited.containsKey(type)) { + if (cowponents_extensions$inherited.containsKey(type)) { Optional patchValue = (Optional) patch.get(type); return (T) (patchValue != null ? patchValue.orElse(null) - : cowponents_extensions$fromInstanceInherited.get(type)); + : cowponents_extensions$inherited.get(type)); } return original; } @ModifyReturnValue(method = "hasNonDefault", at = @At("RETURN")) private boolean cowponents_extensions$setNonDefaultIfInherited(boolean original, @Local(argsOnly = true) DataComponentType type) { - if (cowponents_extensions$fromComponentInherited.containsKey(type)) - return true; - if (cowponents_extensions$fromInstanceInherited.containsKey(type)) + if (cowponents_extensions$inherited.containsKey(type)) return true; return original; } @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) { - InheritedValueUtils.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance); + InheritanceInternals.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance); } @Inject(method = "remove", at = @At("RETURN")) private void cowponents_extensions$refreshInheritedValuesCacheUponRemove(DataComponentType type, CallbackInfoReturnable cir) { - InheritedValueUtils.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance); + InheritanceInternals.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance); } @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) { - InheritedValueUtils.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance); + InheritanceInternals.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance); } @Inject(method = "restorePatch", at = @At("TAIL")) private void cowponents_extensions$refreshInheritedValuesCacheUponRestorePatch(CallbackInfo ci) { - InheritedValueUtils.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance); + InheritanceInternals.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance); } @Inject(method = "clearPatch", at = @At("TAIL")) private void cowponents_extensions$clearInheritedValuesCacheUponClearPatch(CallbackInfo ci) { - InheritedValueUtils.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance); + InheritanceInternals.refreshInheritedValues((PatchedDataComponentMap) (Object) this, cowponents_extensions$instance); } @ModifyReturnValue(method = "keySet", at = @At("RETURN")) private Set> cowponents_extensions$modifyKeySet(Set> original) { Set> set = new ReferenceArraySet<>(original); - set.addAll(cowponents_extensions$fromInstanceInherited.keySet()); - set.addAll(cowponents_extensions$fromComponentInherited.keySet()); + set.addAll(cowponents_extensions$inherited.keySet()); return set; } @ModifyReturnValue(method = "size", at = @At("RETURN")) private int cowponents_extensions$modifySize(int original) { - for (var key : Stream.concat( - cowponents_extensions$fromInstanceInherited.keySet().stream(), - cowponents_extensions$fromComponentInherited.keySet().stream() - ).toList()) { + for (var key : cowponents_extensions$inherited.keySet()) { if (patch.containsKey(key) || prototype.has(key)) continue; original += 1; @@ -126,25 +111,18 @@ public class Mixin_PatchedDataComponentMap implements Duck_InheritableDataCompon @ModifyArg(method = "iterator", at = @At(value = "INVOKE", target = "Ljava/util/ArrayList;(I)V")) private int cowponents_extensions$addInheritedToIteratorList(int initialCapacity) { - return initialCapacity + cowponents_extensions$fromInstanceInherited.size() + cowponents_extensions$fromComponentInherited.size(); + return initialCapacity + cowponents_extensions$inherited.size(); } @ModifyExpressionValue(method = "iterator", at = @At(value = "INVOKE", target = "Lit/unimi/dsi/fastutil/objects/Reference2ObjectMap;containsKey(Ljava/lang/Object;)Z")) private boolean cowponents_extensions$addInheritedToIteratorList(boolean original, @Local(name = "component") TypedDataComponent component) { - return original && !cowponents_extensions$fromInstanceInherited.containsKey(component.type()) && !cowponents_extensions$fromComponentInherited.containsKey(component.type()); + return original && !cowponents_extensions$inherited.containsKey(component.type()); } @ModifyVariable(method = "iterator", at = @At(value = "FIELD", target = "Lnet/minecraft/core/component/PatchedDataComponentMap;prototype:Lnet/minecraft/core/component/DataComponentMap;", ordinal = 2, opcode = Opcodes.GETFIELD)) private List> cowponents_extensions$addInheritedToIterator(List> list) { - if (!cowponents_extensions$fromInstanceInherited.isEmpty()) { - for (var entry : Reference2ObjectMaps.fastIterable(cowponents_extensions$fromInstanceInherited)) { - if (cowponents_extensions$fromComponentInherited.containsKey(entry.getKey()) || patch.containsKey(entry.getKey())) - continue; - list.add(TypedDataComponent.createUnchecked(entry.getKey(), entry.getValue())); - } - } - if (!cowponents_extensions$fromComponentInherited.isEmpty()) { - for (var entry : Reference2ObjectMaps.fastIterable(cowponents_extensions$fromComponentInherited)) { + if (!cowponents_extensions$inherited.isEmpty()) { + for (var entry : Reference2ObjectMaps.fastIterable(cowponents_extensions$inherited)) { if (patch.containsKey(entry.getKey())) continue; list.add(TypedDataComponent.createUnchecked(entry.getKey(), entry.getValue())); @@ -155,24 +133,18 @@ public class Mixin_PatchedDataComponentMap implements Duck_InheritableDataCompon @ModifyReturnValue(method = "iterator", at = @At(value = "RETURN", ordinal = 0)) private Iterator> cowponents_extensions$addInheritedToPrototypeOnlyIterator(Iterator> original) { - if (cowponents_extensions$fromComponentInherited.isEmpty()) { + if (cowponents_extensions$inherited.isEmpty()) { return original; } - List> list = new ArrayList<>(this.cowponents_extensions$fromComponentInherited.size() + this.cowponents_extensions$fromInstanceInherited.size() + this.prototype.size()); + List> list = new ArrayList<>(this.cowponents_extensions$inherited.size() + this.prototype.size()); - for (var entry : Reference2ObjectMaps.fastIterable(cowponents_extensions$fromComponentInherited)) { + for (var entry : Reference2ObjectMaps.fastIterable(cowponents_extensions$inherited)) { list.add(TypedDataComponent.createUnchecked(entry.getKey(), entry.getValue())); } - for (var entry : Reference2ObjectMaps.fastIterable(cowponents_extensions$fromInstanceInherited)) { - if (!this.cowponents_extensions$fromComponentInherited.containsKey(entry.getKey())) { - list.add(TypedDataComponent.createUnchecked(entry.getKey(), entry.getValue())); - } - } - for (TypedDataComponent component : this.prototype) { - if (!this.cowponents_extensions$fromComponentInherited.containsKey(component.type()) || !this.cowponents_extensions$fromInstanceInherited.containsKey(component.type())) { + if (!this.cowponents_extensions$inherited.containsKey(component.type())) { list.add(component); } } @@ -183,8 +155,7 @@ public class Mixin_PatchedDataComponentMap implements Duck_InheritableDataCompon @SuppressWarnings("DataFlowIssue") @ModifyReturnValue(method = "copy", at = @At("RETURN")) private PatchedDataComponentMap cowponents_extensions$copyInherited(PatchedDataComponentMap original) { - ((Duck_InheritableDataComponentMap) (Object) original).cowponents_extensions$setFromInstanceInherited(cowponents_extensions$fromInstanceInherited); - ((Duck_InheritableDataComponentMap) (Object) original).cowponents_extensions$setFromComponentsInherited(cowponents_extensions$fromComponentInherited); + ((Duck_InheritableDataComponentMap) (Object) original).cowponents_extensions$setInherited(cowponents_extensions$inherited); return original; } @@ -199,24 +170,13 @@ public class Mixin_PatchedDataComponentMap implements Duck_InheritableDataCompon } @Override - public Reference2ObjectMap, Object> cowponents_extensions$getFromComponentsInherited() { - return cowponents_extensions$fromComponentInherited; - } - - @Override - public void cowponents_extensions$setFromComponentsInherited(Reference2ObjectMap, Object> value) { - cowponents_extensions$fromComponentInherited.clear(); - cowponents_extensions$fromComponentInherited.putAll(value); - } - - @Override - public Reference2ObjectMap, Object> cowponents_extensions$getFromInstanceInherited() { - return cowponents_extensions$fromInstanceInherited; + public Reference2ObjectMap, Object> cowponents_extensions$getInherited() { + return cowponents_extensions$inherited; } @Override - public void cowponents_extensions$setFromInstanceInherited(Reference2ObjectMap, Object> value) { - cowponents_extensions$fromInstanceInherited.clear(); - cowponents_extensions$fromInstanceInherited.putAll(value); + public void cowponents_extensions$setInherited(Reference2ObjectMap, Object> value) { + cowponents_extensions$inherited.clear(); + cowponents_extensions$inherited.putAll(value); } } diff --git a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/util/InheritedValueUtils.java b/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/util/InheritedValueUtils.java deleted file mode 100644 index da12e95..0000000 --- a/extensions/xplat/src/main/java/lgbt/greenhouse/cowponents/extensions/impl/util/InheritedValueUtils.java +++ /dev/null @@ -1,151 +0,0 @@ -package lgbt.greenhouse.cowponents.extensions.impl.util; - -import it.unimi.dsi.fastutil.objects.Reference2ObjectMap; -import it.unimi.dsi.fastutil.objects.Reference2ObjectMaps; -import it.unimi.dsi.fastutil.objects.ReferenceArraySet; -import it.unimi.dsi.fastutil.objects.ReferenceSet; -import lgbt.greenhouse.cowponents.api.v1.DataComponentGetterSetter; -import lgbt.greenhouse.cowponents.extensions.impl.duck.Duck_InheritableDataComponentMap; -import lgbt.greenhouse.cowponents.extensions.impl.event.CowponentsInheritanceEventsImpl; -import lgbt.greenhouse.cowponents.extensions.impl.mixin.Accessor_PatchedDataComponentMap; -import net.minecraft.core.HolderLookup; -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 net.minecraft.world.item.ItemStack; -import org.jspecify.annotations.NonNull; -import org.jspecify.annotations.Nullable; - -import java.util.Optional; - -import static lgbt.greenhouse.cowponents.impl.util.ComponentCastUtils.castComponentType; - -public class InheritedValueUtils { - public static void refreshInheritedValues(PatchedDataComponentMap components, - Object instance) { - HolderLookup.Provider lookupProvider = CowponentsInheritanceEventsImpl.invokeObtainHolderLookupProvider(instance); - if (instance != null && lookupProvider != null) { - ((Duck_InheritableDataComponentMap) (Object) components).cowponents_extensions$getFromInstanceInherited().clear(); - CowponentsInheritanceEventsImpl.invokeInstancedInherit( - instance, - lookupProvider, - getterSetterForInstanceInherited(components) - ); - } - refreshInheritedFromComponentValues(lookupProvider, components); - } - - public static void refreshInheritedFromItemStack(ItemStack stack, HolderLookup.Provider lookupProvider, DataComponentGetterSetter getterSetter) { - if (stack.isEmpty()) - return; - - for (var type : CowponentsInheritanceEventsImpl.INHERIT_FROM_ITEM_STACK_MAP.keySet()) { - inheritValueFromItemStack(lookupProvider, stack, type, getterSetter); - } - } - - @SuppressWarnings("DataFlowIssue") - public static void refreshInheritedFromComponentValues(HolderLookup.Provider lookupProvider, - PatchedDataComponentMap components) { - ReferenceSet> operatedComponents = new ReferenceArraySet<>(); - - Reference2ObjectMap, Object> fromComponent = ((Duck_InheritableDataComponentMap) (Object) components).cowponents_extensions$getFromComponentsInherited(); - Reference2ObjectMap, Object> fromInstance = ((Duck_InheritableDataComponentMap) (Object) components).cowponents_extensions$getFromInstanceInherited(); - DataComponentMap prototype = ((Accessor_PatchedDataComponentMap) (Object) components).cowponents_extensions$getPrototype(); - Reference2ObjectMap, Optional> patch = ((Accessor_PatchedDataComponentMap) (Object) components).cowponents_extensions$getPatch(); - - fromComponent.clear(); - - for (TypedDataComponent entry : prototype) { - if (patch.containsKey(entry.type()) || fromInstance.containsKey(entry.type())) - continue; - - handleComponentInheritance(lookupProvider, castComponentType(entry.type()), entry.value(), components, fromComponent, operatedComponents); - } - - for (Reference2ObjectMap.Entry, Object> entry : Reference2ObjectMaps.fastIterable(fromInstance)) { - if (patch.containsKey(entry.getKey())) - continue; - - handleComponentInheritance(lookupProvider, castComponentType(entry.getKey()), entry.getValue(), components, fromComponent, operatedComponents); - } - - for (Reference2ObjectMap.Entry, Optional> entry : Reference2ObjectMaps.fastIterable(patch)) { - handleComponentInheritance(lookupProvider, castComponentType(entry.getKey()), entry.getValue().orElse(null), components, fromComponent, operatedComponents); - } - - for (Reference2ObjectMap.Entry, Object> entry : Reference2ObjectMaps.fastIterable(fromComponent)) { - if (patch.containsKey(entry.getKey())) - continue; - - handleComponentInheritance(lookupProvider, castComponentType(entry.getKey()), entry.getValue(), components, fromComponent, operatedComponents); - } - } - - private static void handleComponentInheritance(HolderLookup.Provider lookupProvider, - DataComponentType type, - @Nullable T value, - PatchedDataComponentMap components, - Reference2ObjectMap, Object> inherited, - ReferenceSet> operatedComponents) { - if (value != null) { - for (var to : CowponentsInheritanceEventsImpl.INHERIT_FROM_OTHER_COMPONENT_MAP.keySet().stream() - .filter(types -> types.from() == type) - .map(CowponentsInheritanceEventsImpl.InheritanceComponents::to) - .toList()) { - inheritValueFromComponent(lookupProvider, type, value, to, inherited); - operatedComponents.add(type); - } - } - - for (var from : CowponentsInheritanceEventsImpl.INHERIT_FROM_OTHER_COMPONENT_MAP.keySet().stream() - .filter(types -> types.to() == type) - .map(CowponentsInheritanceEventsImpl.InheritanceComponents::from) - .toList()) { - if (!operatedComponents.contains(from)) { - handleComponentInheritance(lookupProvider, castComponentType(from), components.get(from), components, inherited, operatedComponents); - } - inheritValueFromComponent(lookupProvider, castComponentType(from), components.get(from), type, inherited); - } - } - - private static void inheritValueFromComponent(HolderLookup.Provider lookupProvider, - DataComponentType fromType, - @Nullable F fromValue, - DataComponentType toType, - Reference2ObjectMap, Object> inherited) { - if (fromValue == null) - return; - T toValue = CowponentsInheritanceEventsImpl.invokeInheritFromOtherComponentEvent(fromType, toType, fromValue, lookupProvider); - if (toValue != null) { - inherited.put(toType, toValue); - } - } - - private static void inheritValueFromItemStack(HolderLookup.Provider lookupProvider, - ItemStack stack, - DataComponentType type, - DataComponentGetterSetter getterSetter) { - if (stack == null) - return; - T toValue = CowponentsInheritanceEventsImpl.invokeInheritFromItemStackEvent(type, stack, lookupProvider); - if (toValue != null) { - getterSetter.set(type, toValue); - } - } - - private static DataComponentGetterSetter getterSetterForInstanceInherited(PatchedDataComponentMap components) { - return new DataComponentGetterSetter() { - @Override - public void set(DataComponentType type, T value) { - ((Duck_InheritableDataComponentMap) (Object) components).cowponents_extensions$getFromInstanceInherited().put(type, value); - } - - @Override - public @Nullable T get(@NonNull DataComponentType type) { - return components.get(type); - } - }; - } -} diff --git a/extensions/xplat/src/main/resources/cowponents_extensions.mixins.json b/extensions/xplat/src/main/resources/cowponents_extensions.mixins.json index 9506844..396012b 100644 --- a/extensions/xplat/src/main/resources/cowponents_extensions.mixins.json +++ b/extensions/xplat/src/main/resources/cowponents_extensions.mixins.json @@ -4,8 +4,8 @@ "package": "lgbt.greenhouse.cowponents.extensions.impl.mixin", "compatibilityLevel": "JAVA_25", "mixins": [ - "Accessor_PatchedDataComponentMap", "Mixin_DataComponentTypeBuilder", + "Mixin_DataComponentTypeBuilderSimpleType", "Mixin_ItemStack", "Mixin_PatchedDataComponentMap" ], @@ -13,5 +13,8 @@ ], "injectors": { "defaultRequire": 1 + }, + "mixinextras": { + "minVersion": "0.5.0" } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index f95261d..5695a2b 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -5,15 +5,15 @@ minecraft = "26.1.2" # Xplat neoform = "26.1.2-1" mixin = "0.17.1+mixin.0.8.7" -mixin-extras = "0.5.3" +mixin-extras = "0.5.4" # Fabric -fabric-loader = "0.19.0" -fabric-api = "0.145.4+26.1.2" +fabric-loader = "0.19.2" +fabric-api = "0.147.0+26.1.2" mod-menu = "18.0.0-alpha.8" # NeoForge -neoforge = "26.1.2.13-beta" +neoforge = "26.1.2.30-beta" # Dependencies yumi-mc-foundation = "1.0.0+26.1" diff --git a/test/fabric/src/test/java/lgbt/greenhouse/cowponents/test/fabric/CowponentsTestFabric.java b/test/fabric/src/test/java/lgbt/greenhouse/cowponents/test/fabric/CowponentsTestFabric.java index ab62da5..d32342f 100644 --- a/test/fabric/src/test/java/lgbt/greenhouse/cowponents/test/fabric/CowponentsTestFabric.java +++ b/test/fabric/src/test/java/lgbt/greenhouse/cowponents/test/fabric/CowponentsTestFabric.java @@ -10,8 +10,11 @@ public class CowponentsTestFabric implements ModInitializer { @Override public void onInitialize() { CowponentsTestDataComponents.DEFERRED.registerContents(); + + CowponentsTest.initValidationEvents(); CowponentsTest.initDefaultComponentEvents(); - CowponentsTest.initInheritanceEvents(); + CowponentsTest.initItemStackInheritanceEvents(); + CowponentsTest.initEntityInheritanceEvents(); CommandRegistrationCallback.EVENT.register((dispatcher, _, _) -> CowponentsTestCommands.registerCommands(dispatcher) diff --git a/test/neoforge/src/test/java/lgbt/greenhouse/cowponents/test/neoforge/CowponentsTestNeoForge.java b/test/neoforge/src/test/java/lgbt/greenhouse/cowponents/test/neoforge/CowponentsTestNeoForge.java index cade0ca..070c193 100644 --- a/test/neoforge/src/test/java/lgbt/greenhouse/cowponents/test/neoforge/CowponentsTestNeoForge.java +++ b/test/neoforge/src/test/java/lgbt/greenhouse/cowponents/test/neoforge/CowponentsTestNeoForge.java @@ -20,7 +20,10 @@ import net.neoforged.neoforge.registries.RegisterEvent; @EventBusSubscriber(modid = CowponentsTest.MOD_ID) public class CowponentsTestNeoForge { public CowponentsTestNeoForge(IEventBus eventBus) { + CowponentsTest.initValidationEvents(); CowponentsTest.initDefaultComponentEvents(); + CowponentsTest.initItemStackInheritanceEvents(); + CowponentsTest.initEntityInheritanceEvents(); } @SubscribeEvent @@ -38,7 +41,7 @@ public class CowponentsTestNeoForge { @SubscribeEvent public static void onFmlCommonSetup(FMLCommonSetupEvent event) { - event.enqueueWork(CowponentsTest::initInheritanceEvents); + event.enqueueWork(CowponentsTest::initItemStackInheritanceEvents); } @SubscribeEvent 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 dc1fdc5..77902cc 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 @@ -1,17 +1,21 @@ package lgbt.greenhouse.cowponents.test; -import lgbt.greenhouse.cowponents.extensions.api.v1.event.CowponentsComponentInheritanceEvents; -import lgbt.greenhouse.cowponents.extensions.api.v1.event.CowponentsItemStackInheritanceEvents; +import com.mojang.serialization.DataResult; +import lgbt.greenhouse.cowponents.api.v1.event.ComponentValidationEvents; +import lgbt.greenhouse.cowponents.api.v1.event.ItemStackComponentValidationEvents; +import lgbt.greenhouse.cowponents.entities.api.v1.event.EntityComponentValidationEvents; +import lgbt.greenhouse.cowponents.entities.api.v1.event.CowponentsEntityInheritanceEvents; +import lgbt.greenhouse.cowponents.extensions.api.v1.event.ItemStackInheritanceEvents; import lgbt.greenhouse.cowponents.entities.api.v1.event.CowponentsDefaultEntityComponentEvents; -import lgbt.greenhouse.cowponents.entities.api.v1.event.CowponentsEntityEvents; 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.enchantment.Enchantment; import net.minecraft.world.item.enchantment.Enchantments; import net.minecraft.world.item.enchantment.ItemEnchantments; import net.minecraft.world.level.levelgen.LegacyRandomSource; @@ -23,6 +27,25 @@ public class CowponentsTest { public static final String MOD_NAME = "Cowponents Test"; public static final Logger LOG = LoggerFactory.getLogger(MOD_NAME); + private static final Identifier DEFAULT_EVENT_PHASE = Identifier.fromNamespaceAndPath("yumi", "default"); + private static final Identifier CYCLING_ENCHANTMENTS_EVENT_PHASE = id("cycling_enchantments"); + + public static void initValidationEvents() { + ItemStackComponentValidationEvents.VALIDATE.register(components -> { + if (components.has(CowponentsTestDataComponents.ENTITY_TINT.get())) { + return DataResult.error(() -> "Entity tint can not be applied to item stacks"); + } + return DataResult.success(Unit.INSTANCE); + }); + + EntityComponentValidationEvents.VALIDATE.register(components -> { + if (!components.getOrDefault(DataComponents.STORED_ENCHANTMENTS, ItemEnchantments.EMPTY).isEmpty()) { + return DataResult.error(() -> "Stored enchantments can not be applied to entities"); + } + return DataResult.success(Unit.INSTANCE); + }); + } + public static void initDefaultComponentEvents() { CowponentsDefaultEntityComponentEvents.MODIFY.register(context -> { context.modify(EntityType.PIG, (builder, _, _) -> builder.set(CowponentsTestDataComponents.ENTITY_TINT.get(), 0x00FF00)); @@ -31,31 +54,45 @@ public class CowponentsTest { }); } - public static void initInheritanceEvents() { - CowponentsItemStackInheritanceEvents.registerForItems(DataComponents.ENCHANTMENTS, (_, registryAccess) -> { - ItemEnchantments.Mutable mutable = new ItemEnchantments.Mutable(ItemEnchantments.EMPTY); - mutable.set(registryAccess.getOrThrow(Enchantments.FIRE_ASPECT), 2); - return mutable.toImmutable(); - }, Items.NETHERITE_AXE); + public static void initItemStackInheritanceEvents() { + ItemStackInheritanceEvents.addPhaseOrdering(DEFAULT_EVENT_PHASE, CYCLING_ENCHANTMENTS_EVENT_PHASE, DataComponents.ENCHANTMENTS); + ItemStackInheritanceEvents.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(); + }, + Items.NETHERITE_AXE + ); - CowponentsEntityEvents.registerForTypes(CowponentsTestDataComponents.ENTITY_TINT.get(), (value, _) -> { - RandomSource random = new LegacyRandomSource(value.getUUID().hashCode()); - return random.nextIntBetweenInclusive(0, 0xFFFFFF); - }, EntityType.PLAYER); + ItemStackInheritanceEvents.register( + CYCLING_ENCHANTMENTS_EVENT_PHASE, + DataComponents.ENCHANTMENTS, + (value, _) -> value.get(CowponentsTestDataComponents.CYCLING_ENCHANTMENTS.get()) + ); - CowponentsComponentInheritanceEvents.register(DataComponents.ENCHANTMENTS, DataComponents.ITEM_MODEL, (value, _) -> { - if (value.keySet().stream().noneMatch(enchantmentHolder -> enchantmentHolder.is(Enchantments.BANE_OF_ARTHROPODS))) { - return null; - } - return CowponentsTest.id("smiley_face"); - }); + ItemStackInheritanceEvents.register( + DataComponents.ITEM_MODEL, + (value, lookupProvider) -> { + Holder baneOfArthropods = lookupProvider.getOrThrow(Enchantments.BANE_OF_ARTHROPODS); + if (value.getOrDefault(DataComponents.ENCHANTMENTS, ItemEnchantments.EMPTY).getLevel(baneOfArthropods) > 0) { + return id("smiley_face"); + } + return null; + } + ); + } - CowponentsComponentInheritanceEvents.register(DataComponents.ENCHANTMENTS, DataComponents.ITEM_NAME, (value, _) -> { - if (value.keySet().stream().noneMatch(enchantmentHolder -> enchantmentHolder.is(Enchantments.BANE_OF_ARTHROPODS))) { - return null; - } - return Component.literal("You are an idiot!"); - }); + public static void initEntityInheritanceEvents() { + CowponentsEntityInheritanceEvents.registerForTypes( + CowponentsTestDataComponents.ENTITY_TINT.get(), + (value, _) -> { + RandomSource random = new LegacyRandomSource(value.getUUID().hashCode()); + return random.nextIntBetweenInclusive(0, 0xFFFFFF); + }, + EntityType.PLAYER); } public static Identifier id(String path) { diff --git a/test/xplat/src/test/java/lgbt/greenhouse/cowponents/test/registry/CowponentsTestDataComponents.java b/test/xplat/src/test/java/lgbt/greenhouse/cowponents/test/registry/CowponentsTestDataComponents.java index bb1082b..74d7d2a 100644 --- a/test/xplat/src/test/java/lgbt/greenhouse/cowponents/test/registry/CowponentsTestDataComponents.java +++ b/test/xplat/src/test/java/lgbt/greenhouse/cowponents/test/registry/CowponentsTestDataComponents.java @@ -1,6 +1,5 @@ package lgbt.greenhouse.cowponents.test.registry; -import lgbt.greenhouse.cowponents.extensions.api.v1.CowponentsExtensionsDataComponentTypeBuilderWrapper; import lgbt.greenhouse.cowponents.impl.util.ComponentCodecUtils; import lgbt.greenhouse.cowponents.entities.api.v1.CowponentsEntitiesDataComponentTypeBuilderWrapper; import lgbt.greenhouse.cowponents.entities.api.v1.extension.CowponentsEntityDataComponentType; @@ -34,11 +33,9 @@ public class CowponentsTestDataComponents { * Tests component inherited addedComponents. */ public static final CowponentsTestDeferredValue, DataComponentType> CYCLING_ENCHANTMENTS = DEFERRED.register("cycling_enchantments", - () -> CowponentsExtensionsDataComponentTypeBuilderWrapper.wrap( - DataComponentType.builder() - .persistent(ItemEnchantments.CODEC) - .networkSynchronized(ItemEnchantments.STREAM_CODEC) - ).inheritTo(DataComponents.ENCHANTMENTS, (value, _) -> value) + () -> DataComponentType.builder() + .persistent(ItemEnchantments.CODEC) + .networkSynchronized(ItemEnchantments.STREAM_CODEC) .build()); /** diff --git a/test/xplat/src/test/resources/cowponents_test.mixins.json b/test/xplat/src/test/resources/cowponents_test.mixins.json index 15f18a4..6e26378 100644 --- a/test/xplat/src/test/resources/cowponents_test.mixins.json +++ b/test/xplat/src/test/resources/cowponents_test.mixins.json @@ -12,5 +12,8 @@ ], "injectors": { "defaultRequire": 1 + }, + "mixinextras": { + "minVersion": "0.5.0" } } -- 2.51.2