Skip to content

Commit

Permalink
- Add additional listerner method that takes no arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
forgetmenot13579 committed Sep 23, 2024
1 parent a3a90d4 commit 0653b0b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,4 @@ public interface Component<A extends AttachmentTarget> {
interface EventHandler<C extends Component<? extends A>, A extends AttachmentTarget, E> {
void handle(C component, A target, E eventPayload);
}

interface EventListener<C extends Component<? extends A>, A extends AttachmentTarget> {
void handle(C component, A target);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,9 +43,13 @@ static <A extends AttachmentTarget, C extends Component<A>> ComponentType<A, C>
<EA extends AttachmentTarget, E> List<Component.EventHandler<?, EA, E>> getEventHandlers(TargetedEvent<EA, E> event);

interface Builder<A extends AttachmentTarget, C extends Component<A>> extends AttachmentRegistry.Builder<C> {
// We probably don't absolutely need all three of
<E> Builder<A, C> listen(TargetedEvent<? super A, E> event, Class<A> targetClass, Component.EventHandler<C, A, E> handler);
<E> Builder<A, C> listen(TargetedEvent<? super A, E> event, Class<A> targetClass, Component.EventListener<C, A> handler);
<E> Builder<A, C> listen(TargetedEvent<? super A, E> event, Class<A> targetClass, BiConsumer<C, A> handler);
<E> Builder<A, C> listen(TargetedEvent<? super A, E> event, Class<A> targetClass, Consumer<C> handler);

<E> Builder<A, C> listen(TargetedEvent<A, E> event, Component.EventHandler<C, A, E> handler);
<E> Builder<A, C> listen(TargetedEvent<A, E> event, Component.EventListener<C, A> handler);
<E> Builder<A, C> listen(TargetedEvent<A, E> event, BiConsumer<C, A> handler);
<E> Builder<A, C> listen(TargetedEvent<A, E> event, Consumer<C> handler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -96,9 +98,16 @@ public void handle(C component, AttachmentTarget target, E eventPayload) {
}

@Override
public <E> Builder<A, C> listen(TargetedEvent<? super A, E> event, Class<A> targetClass, Component.EventListener<C, A> handler) {
public <E> Builder<A, C> listen(TargetedEvent<? super A, E> event, Class<A> targetClass, BiConsumer<C, A> handler) {
return this.listen(event, targetClass, (component, attachmentTarget, eventPayload) ->
handler.handle(component, attachmentTarget)
handler.accept(component, attachmentTarget)
);
}

@Override
public <E> Builder<A, C> listen(TargetedEvent<? super A, E> event, Class<A> targetClass, Consumer<C> handler) {
return this.listen(event, targetClass, (component, attachmentTarget, eventPayload) ->
handler.accept(component)
);
}

Expand All @@ -110,9 +119,16 @@ public <E> Builder<A, C> listen(TargetedEvent<A, E> event, Component.EventHandle
}

@Override
public <E> Builder<A, C> listen(TargetedEvent<A, E> event, Component.EventListener<C, A> handler) {
public <E> Builder<A, C> listen(TargetedEvent<A, E> event, BiConsumer<C, A> handler) {
return this.listen(event, (component, attachmentTarget, eventPayload) ->
handler.accept(component, attachmentTarget)
);
}

@Override
public <E> Builder<A, C> listen(TargetedEvent<A, E> event, Consumer<C> handler) {
return this.listen(event, (component, attachmentTarget, eventPayload) ->
handler.handle(component, attachmentTarget)
handler.accept(component)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 0653b0b

Please sign in to comment.