From 0653b0ba9cfa6151debe9d05bd3fbc3f57bb6c0f Mon Sep 17 00:00:00 2001 From: forgetmenot <149003152+forgetmenot13579@users.noreply.github.com> Date: Mon, 23 Sep 2024 15:58:09 -0700 Subject: [PATCH] - Add additional listerner method that takes no arguments. --- .../api/components/v1/api/Component.java | 4 ---- .../api/components/v1/api/ComponentType.java | 9 +++++-- .../components/v1/impl/ComponentTypeImpl.java | 24 +++++++++++++++---- .../test/components/ComponentTestMod.java | 2 ++ .../fabric/test/components/CreeperColor.java | 5 ++++ 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/fabric-components-api-v1/src/main/java/net/fabricmc/fabric/api/components/v1/api/Component.java b/fabric-components-api-v1/src/main/java/net/fabricmc/fabric/api/components/v1/api/Component.java index 5775eec135..7cf52da831 100644 --- a/fabric-components-api-v1/src/main/java/net/fabricmc/fabric/api/components/v1/api/Component.java +++ b/fabric-components-api-v1/src/main/java/net/fabricmc/fabric/api/components/v1/api/Component.java @@ -22,8 +22,4 @@ public interface Component { interface EventHandler, A extends AttachmentTarget, E> { void handle(C component, A target, E eventPayload); } - - interface EventListener, A extends AttachmentTarget> { - void handle(C component, A target); - } } diff --git a/fabric-components-api-v1/src/main/java/net/fabricmc/fabric/api/components/v1/api/ComponentType.java b/fabric-components-api-v1/src/main/java/net/fabricmc/fabric/api/components/v1/api/ComponentType.java index 3b0daa10e3..80527a2531 100644 --- a/fabric-components-api-v1/src/main/java/net/fabricmc/fabric/api/components/v1/api/ComponentType.java +++ b/fabric-components-api-v1/src/main/java/net/fabricmc/fabric/api/components/v1/api/ComponentType.java @@ -17,6 +17,7 @@ package net.fabricmc.fabric.api.components.v1.api; import java.util.List; +import java.util.function.BiConsumer; import java.util.function.Consumer; import net.minecraft.util.Identifier; @@ -42,9 +43,13 @@ static > ComponentType List> getEventHandlers(TargetedEvent event); interface Builder> extends AttachmentRegistry.Builder { + // We probably don't absolutely need all three of Builder listen(TargetedEvent event, Class targetClass, Component.EventHandler handler); - Builder listen(TargetedEvent event, Class targetClass, Component.EventListener handler); + Builder listen(TargetedEvent event, Class targetClass, BiConsumer handler); + Builder listen(TargetedEvent event, Class targetClass, Consumer handler); + Builder listen(TargetedEvent event, Component.EventHandler handler); - Builder listen(TargetedEvent event, Component.EventListener handler); + Builder listen(TargetedEvent event, BiConsumer handler); + Builder listen(TargetedEvent event, Consumer handler); } } diff --git a/fabric-components-api-v1/src/main/java/net/fabricmc/fabric/api/components/v1/impl/ComponentTypeImpl.java b/fabric-components-api-v1/src/main/java/net/fabricmc/fabric/api/components/v1/impl/ComponentTypeImpl.java index a8cbb1a758..c5c0ff766a 100644 --- a/fabric-components-api-v1/src/main/java/net/fabricmc/fabric/api/components/v1/impl/ComponentTypeImpl.java +++ b/fabric-components-api-v1/src/main/java/net/fabricmc/fabric/api/components/v1/impl/ComponentTypeImpl.java @@ -21,6 +21,8 @@ import java.util.IdentityHashMap; import java.util.List; import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.Consumer; import java.util.function.Supplier; import com.mojang.serialization.Codec; @@ -96,9 +98,16 @@ public void handle(C component, AttachmentTarget target, E eventPayload) { } @Override - public Builder listen(TargetedEvent event, Class targetClass, Component.EventListener handler) { + public Builder listen(TargetedEvent event, Class targetClass, BiConsumer handler) { return this.listen(event, targetClass, (component, attachmentTarget, eventPayload) -> - handler.handle(component, attachmentTarget) + handler.accept(component, attachmentTarget) + ); + } + + @Override + public Builder listen(TargetedEvent event, Class targetClass, Consumer handler) { + return this.listen(event, targetClass, (component, attachmentTarget, eventPayload) -> + handler.accept(component) ); } @@ -110,9 +119,16 @@ public Builder listen(TargetedEvent event, Component.EventHandle } @Override - public Builder listen(TargetedEvent event, Component.EventListener handler) { + public Builder listen(TargetedEvent event, BiConsumer handler) { + return this.listen(event, (component, attachmentTarget, eventPayload) -> + handler.accept(component, attachmentTarget) + ); + } + + @Override + public Builder listen(TargetedEvent event, Consumer handler) { return this.listen(event, (component, attachmentTarget, eventPayload) -> - handler.handle(component, attachmentTarget) + handler.accept(component) ); } diff --git a/fabric-components-api-v1/src/testmod/java/net/fabricmc/fabric/test/components/ComponentTestMod.java b/fabric-components-api-v1/src/testmod/java/net/fabricmc/fabric/test/components/ComponentTestMod.java index e252ceed6a..3670702620 100644 --- a/fabric-components-api-v1/src/testmod/java/net/fabricmc/fabric/test/components/ComponentTestMod.java +++ b/fabric-components-api-v1/src/testmod/java/net/fabricmc/fabric/test/components/ComponentTestMod.java @@ -35,6 +35,8 @@ public class ComponentTestMod implements ModInitializer { builder.listen(ComponentEvents.ENTITY_LOAD, CreeperEntity.class, CreeperColor::onLoad); // Not using the context object builder.listen(ComponentEvents.ENTITY_UNLOAD, CreeperEntity.class, CreeperColor::onUnload); + // Not using the context object or the attachment target + builder.listen(ComponentEvents.ENTITY_UNLOAD, CreeperEntity.class, CreeperColor::onLoad2); builder.initializer(CreeperColor::new); }); diff --git a/fabric-components-api-v1/src/testmod/java/net/fabricmc/fabric/test/components/CreeperColor.java b/fabric-components-api-v1/src/testmod/java/net/fabricmc/fabric/test/components/CreeperColor.java index c7194be05c..b89c3ddcb5 100644 --- a/fabric-components-api-v1/src/testmod/java/net/fabricmc/fabric/test/components/CreeperColor.java +++ b/fabric-components-api-v1/src/testmod/java/net/fabricmc/fabric/test/components/CreeperColor.java @@ -30,7 +30,12 @@ void onLoad(CreeperEntity creeper, ServerWorld world) { ComponentTestMod.LOGGER.info("Creeper with color {} loaded at {}", this, creeper.getBlockPos()); } + void onUnload(CreeperEntity creeper) { ComponentTestMod.LOGGER.info("Creeper with color {} unloaded at {}", this, creeper.getBlockPos()); } + + void onLoad2() { + ComponentTestMod.LOGGER.info("Creeper with color {} loaded", this); + } }