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

Immutable Components #190

Draft
wants to merge 5 commits into
base: 1.21
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,19 @@
*/
package org.ladysnake.cca.api.v3.component;

import com.mojang.serialization.MapCodec;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.ladysnake.cca.api.v3.component.immutable.ImmutableComponent;
import org.ladysnake.cca.api.v3.component.immutable.ImmutableComponentKey;
import org.ladysnake.cca.internal.base.ComponentRegistryImpl;

import javax.annotation.Nullable;
import java.util.Optional;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -76,6 +83,22 @@ public static <C extends Component> ComponentKey<C> getOrCreate(Identifier compo
return ComponentRegistryV3.INSTANCE.getOrCreate(componentId, componentClass);
}

public static <C extends ImmutableComponent> ImmutableComponentKey<C> getOrCreateTransient(Identifier componentId, Class<C> componentClass) {
return ComponentRegistryV3.INSTANCE.getOrCreateImmutable(componentId, componentClass, null, null);
}

public static <C extends ImmutableComponent> ImmutableComponentKey<C> getOrCreateTransient(Identifier componentId, Class<C> componentClass, PacketCodec<RegistryByteBuf, C> packetCodec) {
return ComponentRegistryV3.INSTANCE.getOrCreateImmutable(componentId, componentClass, null, packetCodec);
}

public static <C extends ImmutableComponent> ImmutableComponentKey<C> getOrCreate(Identifier componentId, Class<C> componentClass, MapCodec<C> mapCodec, PacketCodec<RegistryByteBuf, C> packetCodec) {
return ComponentRegistryV3.INSTANCE.getOrCreateImmutable(componentId, componentClass, mapCodec, packetCodec);
}

public static <C extends ImmutableComponent> ImmutableComponentKey<C> getOrCreate(Identifier componentId, Class<C> componentClass, MapCodec<C> mapCodec) {
return ComponentRegistryV3.INSTANCE.getOrCreateImmutable(componentId, componentClass, mapCodec, null);
}

/**
* Directly retrieves a ComponentKey using its id.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@
*/
package org.ladysnake.cca.api.v3.component;

import com.mojang.serialization.MapCodec;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.ladysnake.cca.api.v3.component.immutable.ImmutableComponent;
import org.ladysnake.cca.api.v3.component.immutable.ImmutableComponentKey;
import org.ladysnake.cca.internal.base.ComponentRegistryImpl;

import javax.annotation.Nullable;
import java.util.Optional;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -80,6 +86,8 @@ public interface ComponentRegistryV3 {
*/
<C extends Component> ComponentKey<C> getOrCreate(Identifier componentId, Class<C> componentClass);

<C extends ImmutableComponent> ImmutableComponentKey<C> getOrCreateImmutable(Identifier componentId, Class<C> componentClass, @org.jetbrains.annotations.Nullable MapCodec<C> mapCodec, @org.jetbrains.annotations.Nullable PacketCodec<RegistryByteBuf, C> packetCodec);

/**
* Directly retrieves a ComponentKey using its id.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ default Collection<Identifier> getSupportedComponentKeys() {
return Collections.emptySet();
}

default Collection<Identifier> getSupportedImmutableComponentKeys() {
return Collections.emptySet();
}

/**
* Called when static component bootstrap is finished.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.ladysnake.cca.api.v3.component.immutable;

public interface ImmutableComponent {
interface Modifier<C extends ImmutableComponent, O> {
C modify(C component, O attachedTo);
}
interface Listener<C extends ImmutableComponent, O> extends Modifier<C, O> {
void listen(C component, O attachedTo);
@Override
default C modify(C component, O attachedTo) {
this.listen(component, attachedTo);
return component;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.ladysnake.cca.api.v3.component.immutable;

import org.ladysnake.cca.api.v3.component.load.ClientLoadAwareComponent;
import org.ladysnake.cca.api.v3.component.load.ClientUnloadAwareComponent;
import org.ladysnake.cca.api.v3.component.load.ServerLoadAwareComponent;
import org.ladysnake.cca.api.v3.component.load.ServerUnloadAwareComponent;
import org.ladysnake.cca.api.v3.component.tick.ClientTickingComponent;
import org.ladysnake.cca.api.v3.component.tick.ServerTickingComponent;

import java.lang.invoke.MethodType;
import java.lang.reflect.Modifier;
import java.util.Arrays;

public record ImmutableComponentCallbackType<I>(Class<I> itf,
String methodName,
MethodType exposedType,
MethodType implType) {
public ImmutableComponentCallbackType {
if (!itf.isInterface()
|| Arrays.stream(itf.getDeclaredMethods())
.filter(m -> Modifier.isAbstract(m.getModifiers()))
.count() != 1) {
throw new IllegalArgumentException("ImmutableComponentCallbackType accepts only functional interfaces");
}
}

public static <I> ImmutableComponentCallbackType<I> fromFunctionalInterface(Class<I> itf) {
var method = Arrays.stream(itf.getDeclaredMethods())
.filter(m -> Modifier.isAbstract(m.getModifiers()))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("ImmutableComponentCallbackType accepts only functional interfaces"));
String name = method.getName();
var type = MethodType.methodType(method.getReturnType(), method.getParameterTypes());
var implType = type.insertParameterTypes(0, ImmutableComponentWrapper.class);
return new ImmutableComponentCallbackType<>(itf, name, type, implType);
}

public static final ImmutableComponentCallbackType<ServerTickingComponent> SERVER_TICK = fromFunctionalInterface(ServerTickingComponent.class);
public static final ImmutableComponentCallbackType<ClientTickingComponent> CLIENT_TICK = fromFunctionalInterface(ClientTickingComponent.class);
public static final ImmutableComponentCallbackType<ServerLoadAwareComponent> SERVER_LOAD = fromFunctionalInterface(ServerLoadAwareComponent.class);
public static final ImmutableComponentCallbackType<ClientLoadAwareComponent> CLIENT_LOAD = fromFunctionalInterface(ClientLoadAwareComponent.class);
public static final ImmutableComponentCallbackType<ServerUnloadAwareComponent> SERVER_UNLOAD = fromFunctionalInterface(ServerUnloadAwareComponent.class);
public static final ImmutableComponentCallbackType<ClientUnloadAwareComponent> CLIENT_UNLOAD = fromFunctionalInterface(ClientUnloadAwareComponent.class);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Cardinal-Components-API
* Copyright (C) 2019-2024 Ladysnake
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.ladysnake.cca.api.v3.component.immutable;

import org.jetbrains.annotations.Contract;
import org.ladysnake.cca.api.v3.component.Component;

/**
* A single-arg component factory.
*
* <p>When invoked, the factory must return a {@link Component} of the right type.
*
* @since 3.0.0
*/
@FunctionalInterface
public interface ImmutableComponentFactory<T, C extends ImmutableComponent> {
/**
* Instantiates a {@link Component} for the given provider.
*
* <p>The component returned by this method will be available
* on the provider as soon as all component factories have been invoked.
*
* @param t the factory argument
* @return a new {@link Component}
*/
@Contract(value = "_ -> new", pure = true)
C createComponent(T t);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.ladysnake.cca.api.v3.component.immutable;

import com.mojang.serialization.MapCodec;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
import org.ladysnake.cca.api.v3.component.ComponentKey;

public abstract class ImmutableComponentKey<C extends ImmutableComponent> extends ComponentKey<ImmutableComponentWrapper<C, ?>> {
private final @Nullable MapCodec<C> mapCodec;
private final @Nullable PacketCodec<RegistryByteBuf, C> packetCodec;
private final Class<C> immutableComponentClass;

protected ImmutableComponentKey(Identifier id, Class<C> immutableComponentClass, Class<ImmutableComponentWrapper<C, ?>> wrapperClass, @Nullable MapCodec<C> mapCodec, @Nullable PacketCodec<RegistryByteBuf, C> packetCodec) {
super(id, wrapperClass);
this.immutableComponentClass = immutableComponentClass;
this.mapCodec = mapCodec;
this.packetCodec = packetCodec;
}

public Class<C> getImmutableComponentClass() {
return this.immutableComponentClass;
}

public MapCodec<C> getMapCodec() {
return this.mapCodec;
}

public PacketCodec<RegistryByteBuf, C> getPacketCodec() {
return this.packetCodec;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.ladysnake.cca.api.v3.component.immutable;

import net.minecraft.nbt.NbtCompound;
import net.minecraft.registry.RegistryWrapper;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.ladysnake.cca.api.v3.component.Component;
import org.ladysnake.cca.api.v3.component.CopyableComponent;

@ApiStatus.NonExtendable
public abstract class ImmutableComponentWrapper<C extends ImmutableComponent, O> implements
Component,
CopyableComponent<ImmutableComponentWrapper<C, O>> {
private final ImmutableComponentKey<C> key;
private final O owner;
private @NotNull C data;

protected ImmutableComponentWrapper(ImmutableComponentKey<C> key, O owner, C data) {
this.key = key;
this.owner = owner;
this.data = data;
}

@Override
public void writeToNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) {
// overridden if key.mapCodec != null
}

@Override
public void readFromNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) {
// overridden if key.mapCodec != null
}

@Override
public void copyFrom(ImmutableComponentWrapper<C, O> other, RegistryWrapper.WrapperLookup registryLookup) {
this.data = other.data;
}

public ImmutableComponentKey<C> getKey() {
return key;
}

public O getOwner() {
return owner;
}

public C getData() {
return this.data;
}

//TODO more methods for this. syncing, unary operators, etc.
public void setData(C data) {
this.data = data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,24 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.mojang.serialization.MapCodec;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.util.Identifier;
import org.ladysnake.cca.api.v3.component.Component;
import org.ladysnake.cca.api.v3.component.ComponentKey;
import org.ladysnake.cca.api.v3.component.ComponentRegistryV3;
import org.ladysnake.cca.api.v3.component.immutable.ImmutableComponent;
import org.ladysnake.cca.api.v3.component.immutable.ImmutableComponentKey;
import org.ladysnake.cca.api.v3.component.immutable.ImmutableComponentWrapper;
import org.ladysnake.cca.internal.base.asm.CcaBootstrap;

import javax.annotation.Nullable;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

public final class ComponentRegistryImpl implements ComponentRegistryV3 {
Expand Down Expand Up @@ -68,6 +75,40 @@ public synchronized <T extends Component> ComponentKey<T> getOrCreate(Identifier
}
}

@Override
public synchronized <T extends ImmutableComponent> ImmutableComponentKey<T> getOrCreateImmutable(Identifier componentId, Class<T> immutableComponentClass, @org.jetbrains.annotations.Nullable MapCodec<T> cMapCodec, @org.jetbrains.annotations.Nullable PacketCodec<RegistryByteBuf, T> registryByteBufCPacketCodec) {
Preconditions.checkArgument(ImmutableComponent.class.isAssignableFrom(immutableComponentClass), "Component interface must extend " + ImmutableComponent.class.getCanonicalName());
// make sure 2+ components cannot get registered at the same time
@SuppressWarnings("unchecked")
ImmutableComponentKey<T> existing = (ImmutableComponentKey<T>) this.get(componentId);

if (existing != null) {
if (existing.getImmutableComponentClass() != immutableComponentClass) {
throw new IllegalStateException("Registered component " + componentId + " twice with 2 different classes: " + existing.getComponentClass() + ", " + immutableComponentClass);
}
return existing;
} else {
Class<? extends ComponentKey<?>> generated = CcaBootstrap.INSTANCE.getGeneratedComponentTypeClass(componentId);

if (generated == null) {
throw new IllegalStateException(componentId + " was not registered through mod metadata or plugin");
}

if (!ImmutableComponentKey.class.isAssignableFrom(generated)) {
throw new IllegalStateException(componentId + " was registered as a classic component, not an immutable one");
}

ImmutableComponentKey<T> registered = this.instantiateStaticImmutableType(
(Class<? extends ImmutableComponentKey<?>>) generated,
componentId,
immutableComponentClass,
cMapCodec,
registryByteBufCPacketCodec);
this.keys.put(componentId, registered);
return registered;
}
}

private <T extends Component> ComponentKey<T> instantiateStaticType(Class<? extends ComponentKey<?>> generated, Identifier componentId, Class<T> componentClass) {
try {
@SuppressWarnings("unchecked") ComponentKey<T> ret = (ComponentKey<T>) generated.getConstructor(Identifier.class, Class.class).newInstance(componentId, componentClass);
Expand All @@ -77,6 +118,16 @@ private <T extends Component> ComponentKey<T> instantiateStaticType(Class<? exte
}
}

private <T extends ImmutableComponent> ImmutableComponentKey<T> instantiateStaticImmutableType(Class<? extends ImmutableComponentKey<?>> generated, Identifier componentId, Class<T> componentClass, @org.jetbrains.annotations.Nullable MapCodec<T> mapCodec, @org.jetbrains.annotations.Nullable PacketCodec<RegistryByteBuf, T> packetCodec) {
try {
@SuppressWarnings("unchecked") ImmutableComponentKey<T> ret = (ImmutableComponentKey<T>) generated.getConstructor(Identifier.class, Class.class, Class.class, MapCodec.class, PacketCodec.class)
.newInstance(componentId, componentClass, ImmutableComponentWrapper.class, mapCodec, packetCodec);
return ret;
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new IllegalStateException("Failed to create statically declared component type", e);
}
}

@Nullable
@Override
public ComponentKey<?> get(Identifier id) {
Expand Down
Loading