Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add component system #4115

Draft
wants to merge 5 commits into
base: 1.21.1
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions fabric-component-api-v1/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version = getSubprojectVersion(project)

moduleDependencies(project, [
'fabric-api-base',
'fabric-data-attachment-api-v1',
'fabric-lifecycle-events-v1'
])
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.api.component.v1.api;

import net.fabricmc.fabric.api.attachment.v1.AttachmentTarget;

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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.api.component.v1.api;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.world.ServerWorld;

import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents;

public final class ComponentEvents {
public static final TargetedEvent<Entity, ServerWorld> ENTITY_LOAD = TargetedEvent.create(ServerEntityEvents.ENTITY_LOAD,
event -> event::accept);

public static final TargetedEvent<Entity, ServerWorld> ENTITY_UNLOAD = TargetedEvent.create(ServerEntityEvents.ENTITY_UNLOAD,
event -> event::accept);

public static final TargetedEvent<LivingEntity, EquipmentChangeEvent> EQUIPMENT_CHANGE = TargetedEvent.create(ServerEntityEvents.EQUIPMENT_CHANGE,
event -> (livingEntity, equipmentSlot, previousStack, currentStack) ->
event.accept(livingEntity, new EquipmentChangeEvent(equipmentSlot, previousStack, currentStack)));

private ComponentEvents() {
}

public record EquipmentChangeEvent(EquipmentSlot equipmentSlot, ItemStack previousStack, ItemStack currentStack) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.api.component.v1.api;

import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import net.minecraft.util.Identifier;

import net.fabricmc.fabric.api.attachment.v1.AttachmentRegistry;
import net.fabricmc.fabric.api.attachment.v1.AttachmentTarget;
import net.fabricmc.fabric.api.attachment.v1.AttachmentType;
import net.fabricmc.fabric.api.component.v1.impl.ComponentTypeImpl;

public interface ComponentType<A extends AttachmentTarget, C extends Component<A>> extends AttachmentType<C> {
static <A extends AttachmentTarget, C extends Component<A>> ComponentType<A, C> create(Identifier identifier) {
return create(identifier, builder -> { });
}

static <A extends AttachmentTarget, C extends Component<A>> ComponentType<A, C> create(Identifier identifier, Consumer<Builder<A, C>> consumer) {
var builder = new ComponentTypeImpl.BuilderImpl<A, C>();

consumer.accept(builder);

return builder.buildAndRegister(identifier);
}

<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, 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, 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
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.api.component.v1.api;

import java.util.function.BiConsumer;
import java.util.function.Function;

import net.fabricmc.fabric.api.attachment.v1.AttachmentTarget;
import net.fabricmc.fabric.api.component.v1.impl.TargetedEventImpl;
import net.fabricmc.fabric.api.event.Event;

public interface TargetedEvent<A extends AttachmentTarget, E> {
static <T, A extends AttachmentTarget, E> TargetedEvent<A, E> create(Event<T> event, Function<BiConsumer<A, E>, T> invokerFactory) {
TargetedEventImpl<A, E> targetedEvent = TargetedEventImpl.create(event);
T invoker = invokerFactory.apply(targetedEvent::invoke);

event.register(invoker);

return targetedEvent;
}

void invoke(A attachmentTarget, E eventContext);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.api.component.v1.impl;

import java.util.ArrayList;
import java.util.Collections;
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;
import org.jetbrains.annotations.Nullable;

import net.minecraft.util.Identifier;

import net.fabricmc.fabric.api.attachment.v1.AttachmentRegistry;
import net.fabricmc.fabric.api.attachment.v1.AttachmentTarget;
import net.fabricmc.fabric.api.attachment.v1.AttachmentType;
import net.fabricmc.fabric.api.component.v1.api.Component;
import net.fabricmc.fabric.api.component.v1.api.ComponentType;
import net.fabricmc.fabric.api.component.v1.api.TargetedEvent;

public final class ComponentTypeImpl<A extends AttachmentTarget, C extends Component<A>> implements ComponentType<A, C> {
private final AttachmentType<C> attachmentType;
private final Map<TargetedEvent<? super A, ?>, List<Component.EventHandler<?, ? super A, ?>>> eventHandlers = new IdentityHashMap<>();

public ComponentTypeImpl(AttachmentType<C> attachmentType) {
this.attachmentType = attachmentType;
}

@Override
public <EA extends AttachmentTarget, E> List<Component.EventHandler<?, EA, E>> getEventHandlers(TargetedEvent<EA, E> event) {
//noinspection unchecked
return (List<Component.EventHandler<?, EA, E>>) (Object) this.eventHandlers.getOrDefault(event, Collections.emptyList());
}

@Override
public Identifier identifier() {
return this.attachmentType.identifier();
}

@Override
public @Nullable Codec<C> persistenceCodec() {
return this.attachmentType.persistenceCodec();
}

@Override
public boolean isPersistent() {
return this.attachmentType.isPersistent();
}

@Override
public @Nullable Supplier<C> initializer() {
return this.attachmentType.initializer();
}

@Override
public boolean copyOnDeath() {
return this.attachmentType.copyOnDeath();
}

public static final class BuilderImpl<A extends AttachmentTarget, C extends Component<A>> implements Builder<A, C>, AttachmentRegistry.Builder<C> {
private final AttachmentRegistry.Builder<C> attachmentBuilder = AttachmentRegistry.builder();
private final Map<TargetedEvent<? super A, ?>, List<Component.EventHandler<?, ? super A, ?>>> eventHandlers = new IdentityHashMap<>();

@Override
public <E> Builder<A, C> listen(TargetedEvent<? super A, E> event, Class<A> targetClass, Component.EventHandler<C, A, E> handler) {
List<Component.EventHandler<?, ? super A, ?>> handlers = this.eventHandlers.computeIfAbsent(event, e -> new ArrayList<>());

handlers.add(new Component.EventHandler<C, AttachmentTarget, E>() {
@Override
public void handle(C component, AttachmentTarget target, E eventPayload) {
if (targetClass.isAssignableFrom(target.getClass())) {
//noinspection unchecked
handler.handle(component, (A) target, eventPayload);
}
}
});

return this;
}

@Override
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.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)
);
}

@Override
public <E> Builder<A, C> listen(TargetedEvent<A, E> event, Component.EventHandler<C, A, E> handler) {
this.eventHandlers.computeIfAbsent(event, e -> new ArrayList<>()).add(handler);

return this;
}

@Override
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.accept(component)
);
}

public ComponentTypeImpl<A, C> buildAndRegister(Identifier id) {
AttachmentType<C> attachmentType = this.attachmentBuilder.buildAndRegister(id);
ComponentTypeImpl<A, C> type = new ComponentTypeImpl<>(attachmentType);

this.eventHandlers.forEach((event, list) -> {
type.eventHandlers.put(event, List.copyOf(list));
((TargetedEventImpl<? super A, ?>) event).addListener(type);
});

return type;
}

@Override
public AttachmentRegistry.Builder<C> persistent(Codec<C> codec) {
return attachmentBuilder.persistent(codec);
}

@Override
public AttachmentRegistry.Builder<C> copyOnDeath() {
return attachmentBuilder.copyOnDeath();
}

@Override
public AttachmentRegistry.Builder<C> initializer(Supplier<C> initializer) {
return attachmentBuilder.initializer(initializer);
}
}
}
Loading