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

Component API Implementation #146

Closed
wants to merge 77 commits into from
Closed
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
4e6504d
Implemented the core Component API
0xJoeMama Jun 13, 2022
a400da6
Modified creeper testing a bit
0xJoeMama Jun 13, 2022
d3ddc7b
Implemented caching for injection targets.
0xJoeMama Jun 14, 2022
f2f6c51
Reformat and better tests.
0xJoeMama Jun 14, 2022
20a75cb
Implemented ComponentProvider for Block Entities.
0xJoeMama Jun 14, 2022
febe5a8
Added Chunks are Component Providers(serialization does not work yet).
0xJoeMama Jun 20, 2022
7c30217
Fully implemented chunk component providers and some itemstack
0xJoeMama Jun 20, 2022
4ad2213
Added marking dirty to other providers that require it
0xJoeMama Jun 21, 2022
dd6837d
Fixed a minor issue with chunks not saving qsl data when generated.
0xJoeMama Jun 21, 2022
dc72de3
Made LevelProperties a ComponentProvider allowing you to attach
0xJoeMama Jun 21, 2022
f70624e
Reformatted code.
0xJoeMama Jun 21, 2022
a1aa7c9
Merge branch 'QuiltMC:1.19' into 1.19
0xJoeMama Jun 21, 2022
25bc9ce
Added some todos.
0xJoeMama Jun 21, 2022
60dfd4a
Added container(WIP)
0xJoeMama Jun 21, 2022
cc36b93
Implemented containers fully and switched to using them
0xJoeMama Jun 22, 2022
127f91d
Added SimpleComponent for custom ComponentProvider implementations.
0xJoeMama Jun 22, 2022
e53f5e7
Fixed testmod issue.
0xJoeMama Jun 23, 2022
737371b
Added SimpleComponentContainer
0xJoeMama Jun 23, 2022
b0bf069
Turned ComponentProvider into an injected interface.
0xJoeMama Jun 23, 2022
789c339
Reformat.
0xJoeMama Jun 23, 2022
e487767
Run applyLicenses
0xJoeMama Jun 23, 2022
32851c4
Added Component.Factory and restructured code.
0xJoeMama Jun 24, 2022
35b62d1
Switched to using vanilla registries to handle our registration process.
0xJoeMama Jun 24, 2022
b8bba11
Woops that went unnoticed.
0xJoeMama Jun 24, 2022
0bd3c48
ItemStacks are now ComponentProviders.
0xJoeMama Jun 24, 2022
62fef93
Removed unneeded imports.
0xJoeMama Jun 24, 2022
c126c25
Merged implementation of itemstacks.
0xJoeMama Jun 24, 2022
8c11524
Added component events.
0xJoeMama Jun 24, 2022
21eed51
Hooked the event for injections properly.
0xJoeMama Jun 25, 2022
dd8b4b3
Improved API structure.
0xJoeMama Jun 27, 2022
3902f9b
More structure improvements.
0xJoeMama Jun 27, 2022
a03dac8
SimpleComponentContainer builder.
0xJoeMama Jun 27, 2022
7df1c73
Implemented the FunctionComponent library.
0xJoeMama Jun 27, 2022
aee0845
Finalized the FunctionComponent system.
0xJoeMama Jun 27, 2022
0c9fc00
Code reformat
0xJoeMama Jun 27, 2022
5ccb0de
Almost the whole implementation of network sync.
0xJoeMama Jun 28, 2022
25994bb
Finished network sync(still unstable).
0xJoeMama Jun 29, 2022
16230e8
Apply licenses before PR
0xJoeMama Jun 29, 2022
0636caa
Applied changes requested by Leo40Git.
0xJoeMama Jun 29, 2022
af64a98
Exposed as in part of the public API.
0xJoeMama Jun 29, 2022
33e5d4a
Fixed runtime crash with FilteredInheritedInjectionPredicate
0xJoeMama Jun 29, 2022
c058bbc
Applied LambdaAurora's requested changes.
0xJoeMama Jun 29, 2022
e94f338
Merge branch 'QuiltMC:1.19' into 1.19
0xJoeMama Jun 29, 2022
74658de
Added the block_entity module as a dependency of the testmod and removed
0xJoeMama Jun 29, 2022
1289e47
Made the LOGGER fields public and removed the getLogger method.
0xJoeMama Jun 30, 2022
fb8741a
Switched to IdentityHashMap and Component type keys.
0xJoeMama Jun 30, 2022
1907f44
Added GenericComponent
0xJoeMama Jun 30, 2022
71a3679
Removed FunctionComponents after discussion.
0xJoeMama Jul 2, 2022
6372ac5
Minor changes here and there.
0xJoeMama Jul 2, 2022
8c4f500
Added OnAccessComponentContainer and restructured a bit.
0xJoeMama Jul 2, 2022
315d2de
Applied changed requested from Leo40Git
0xJoeMama Jul 3, 2022
07e3b21
Added the Maybe monad class to the base QSL module.
0xJoeMama Jul 11, 2022
5f72bd8
Major rewrite of injections and removed notnulls.
0xJoeMama Jul 12, 2022
4b72357
Removed ItemStacks from ComponentProviders.
0xJoeMama Jul 12, 2022
aefd5bf
Cleaned up networking code.
0xJoeMama Jul 12, 2022
cf29d50
Reverted small mistake.
0xJoeMama Jul 12, 2022
ee09df5
Applied changes requested by EnnuiL
0xJoeMama Jul 14, 2022
b390401
Applied suggestions Leo40Git
0xJoeMama Jul 15, 2022
436f55d
Applied Leo40Git's suggestions + other minor changes.
0xJoeMama Jul 16, 2022
2144439
Major Rewrite + Docs(doesn't function right now)
0xJoeMama Jul 18, 2022
3b0b356
Merge branch 'QuiltMC:1.19' into 1.19
0xJoeMama Jul 18, 2022
664c0d1
Cleared up some code.
0xJoeMama Jul 18, 2022
93c72f0
Rewrite of syncing:
0xJoeMama Jul 21, 2022
07802ca
Reformat using Checkstyle
0xJoeMama Jul 21, 2022
8f566e2
Run applyLicenses
0xJoeMama Jul 21, 2022
41af965
Removed the Component interface.
0xJoeMama Jul 24, 2022
a39fb1a
Fixed docs.
0xJoeMama Jul 24, 2022
4b0e39f
Revamped the way to perform injections.
0xJoeMama Jul 24, 2022
6665cd1
Merge branch 'QuiltMC:1.19' into 1.19
0xJoeMama Jul 24, 2022
c51ed9b
Removed invalid mixin
0xJoeMama Jul 25, 2022
b3ac896
Fixed various issues and resolved some todoes
0xJoeMama Jul 25, 2022
374134b
Component injections are now the responsibility of ComponentContainer…
0xJoeMama Aug 1, 2022
23b240e
Added the default component container factory that will be used.
0xJoeMama Aug 1, 2022
d82ea97
Merge branch 'QuiltMC:1.19' into 1.19
0xJoeMama Aug 1, 2022
c90b2fc
Merge branch 'QuiltMC:1.19' into 1.19
0xJoeMama Aug 9, 2022
6437549
Nullificating Part 01
0xJoeMama Aug 9, 2022
541ef90
Nullification Part 02
0xJoeMama Aug 10, 2022
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
60 changes: 60 additions & 0 deletions library/data/component/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
plugins {
id("qsl.module")
}

qslModule {
name = "Quilt Component Attachment API"
0xJoeMama marked this conversation as resolved.
Show resolved Hide resolved
moduleName = "component"
id = "quilt_component"
description = "API allowing attachment and querying of an ECS-like interface for entities, block-entities, chunks, etc."
0xJoeMama marked this conversation as resolved.
Show resolved Hide resolved
library = "data"
moduleDependencies {
core {
api("qsl_base")
api("lifecycle_events")
api("networking")
}
}

entrypoints {
events {
values = [
"org.quiltmc.qsl.component.impl.event.ServerTickEventListener"
]
}

init {
values = ["org.quiltmc.qsl.component.impl.CommonInitializer"]
}

client_init {
values = ["org.quiltmc.qsl.component.impl.client.ClientInitializer"]
}
}

accessWidener()

injectedInterface("net/minecraft/class_2586") {
values = ["org/quiltmc/qsl/component/api/ComponentProvider"]
}

injectedInterface("net/minecraft/class_1297") {
values = ["org/quiltmc/qsl/component/api/ComponentProvider"]
}

injectedInterface("net/minecraft/class_2791") {
values = ["org/quiltmc/qsl/component/api/ComponentProvider"]
}

injectedInterface("net/minecraft/class_31") {
values = ["org/quiltmc/qsl/component/api/ComponentProvider"]
}

injectedInterface("net/minecraft/class_1799") {
values = ["org/quiltmc/qsl/component/api/ComponentProvider"]
}

injectedInterface("net/minecraft/class_310") {
values = ["org/quiltmc/qsl/component/api/ComponentProvider"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2022 QuiltMC
*
* 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 org.quiltmc.qsl.component.api;

import org.jetbrains.annotations.NotNull;

public interface Component {
@FunctionalInterface
interface Factory<T extends Component> {
@NotNull
T create();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2022 QuiltMC
*
* 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 org.quiltmc.qsl.component.api;

import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;

public interface ComponentContainer { // TODO: Maybe split up responsibilities?
Optional<Component> expose(Identifier id);

void writeNbt(@NotNull NbtCompound providerRootNbt);

void readNbt(@NotNull NbtCompound providerRootNbt);

void tick(@NotNull ComponentProvider provider);

void receiveSyncPacket(@NotNull Identifier id, @NotNull PacketByteBuf buf);

void sync(@NotNull ComponentProvider provider);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2022 QuiltMC
*
* 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 org.quiltmc.qsl.component.api;

public interface ComponentInjectionPredicate {
boolean canInject(ComponentProvider provider);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2022 QuiltMC
*
* 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 org.quiltmc.qsl.component.api;

import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.level.LevelProperties;
import org.jetbrains.annotations.NotNull;
import org.quiltmc.qsl.base.api.util.InjectedInterface;
import org.quiltmc.qsl.component.api.components.FunctionComponent;

import java.util.Optional;

@InjectedInterface({
Entity.class,
BlockEntity.class,
Chunk.class,
LevelProperties.class,
ItemStack.class,
MinecraftClient.class
})
public interface ComponentProvider {
@NotNull
ComponentContainer getContainer();
0xJoeMama marked this conversation as resolved.
Show resolved Hide resolved

default <T, U, C extends FunctionComponent<T, U>> Optional<U> call(ComponentType<C> type, T t) {
return this.expose(type).map(func -> func.call(this, t));
}

default <C extends Component> Optional<C> expose(ComponentType<C> id) {
return Components.expose(id, this);
}

default void sync() {
0xJoeMama marked this conversation as resolved.
Show resolved Hide resolved
this.getContainer().sync(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2022 QuiltMC
*
* 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 org.quiltmc.qsl.component.api;

import net.minecraft.util.Identifier;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public record ComponentType<T extends Component>(Identifier id, Component.Factory<T> factory,
boolean isStatic) implements Component.Factory<T> {
public static final Static STATIC_CACHE = new Static();

@SuppressWarnings("unchecked")
public Optional<T> cast(Component component) {
try {
return Optional.of((T) component);
} catch (ClassCastException ignored) {
return Optional.empty();
}
}

@Override
public @NotNull T create() {
if (this.isStatic) {
return STATIC_CACHE.getOrCreate(this);
}
return this.factory.create();
}

public static class Static {
private final Map<Identifier, Component> staticInstances = new HashMap<>();

private Static() {
0xJoeMama marked this conversation as resolved.
Show resolved Hide resolved

}

@SuppressWarnings("unchecked")
@NotNull <C extends Component> C getOrCreate(ComponentType<C> type) {
if (this.staticInstances.containsKey(type.id())) {
return (C) this.staticInstances.get(type.id());
} else {
C singleton = type.factory.create();
this.staticInstances.put(type.id(), singleton);
return singleton;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2022 QuiltMC
*
* 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 org.quiltmc.qsl.component.api;

import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import org.quiltmc.qsl.component.api.components.TickingComponent;
import org.quiltmc.qsl.component.impl.ComponentsImpl;
import org.quiltmc.qsl.component.impl.predicates.ClassInjectionPredicate;
import org.quiltmc.qsl.component.impl.predicates.FilteredInheritedInjectionPredicate;
import org.quiltmc.qsl.component.impl.predicates.InheritedInjectionPredicate;
import org.quiltmc.qsl.component.impl.predicates.RedirectedInjectionPredicate;

import java.util.Optional;
import java.util.Set;

public final class Components {
public static final RegistryKey<Registry<ComponentType<?>>> REGISTRY_KEY = ComponentsImpl.REGISTRY_KEY;

public static final Registry<ComponentType<?>> REGISTRY = ComponentsImpl.REGISTRY;

public static <C extends Component> void inject(ComponentInjectionPredicate predicate, ComponentType<C> component) {
ComponentsImpl.inject(predicate, component);
}

public static <C extends Component> void inject(Class<?> clazz, ComponentType<C> component) {
ComponentsImpl.inject(new ClassInjectionPredicate(clazz), component);
}

public static <C extends Component> void injectInheritage(Class<?> clazz, ComponentType<C> component) {
ComponentsImpl.inject(new InheritedInjectionPredicate(clazz), component);
}

public static <C extends Component> void injectInheritanceExcept(Class<?> clazz, ComponentType<C> component, Class<?>... exceptions) {
ComponentsImpl.inject(new FilteredInheritedInjectionPredicate(clazz, exceptions), component);
}

public static <C extends Component> void injectRedirected(Class<?> mainClass, ComponentType<C> type, Class<?> others) {
ComponentsImpl.inject(new RedirectedInjectionPredicate(mainClass, Set.of(others)), type);
}

public static <C extends Component, S> Optional<C> expose(ComponentType<C> id, S obj) {
if (obj instanceof ComponentProvider provider) {
return provider.getContainer().expose(id.id())
.map(id::cast)
.map(Optional::orElseThrow); // If the casting fails something is wrong with the provided ComponentType. In that case we just throw.
}

return Optional.empty();
}

public static <C extends Component> ComponentType<C> register(Identifier id, Component.Factory<C> factory) {
return ComponentsImpl.register(id, factory);
}

public static <C extends Component> ComponentType<C> registerStatic(Identifier id, Component.Factory<C> factory) {
return ComponentsImpl.registerStatic(id, factory);
}

public static <C extends TickingComponent> ComponentType<C> registerTicking(Identifier id, C component) {
return ComponentsImpl.registerStatic(id, () -> component);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2022 QuiltMC
*
* 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 org.quiltmc.qsl.component.api.components;

import org.quiltmc.qsl.component.api.Component;

0xJoeMama marked this conversation as resolved.
Show resolved Hide resolved

public interface FloatComponent extends Component {
float get();

void set(float value);
}
Loading