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 71 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,341 @@
/*
* 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.base.api.util;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
* Represents a value that is only initialized once it's needed.<br/>
* Similar to {@link com.google.common.base.Suppliers#memoize}.
*
* @param <T> The type of the value this {@link Lazy} represents.
*/
public abstract sealed class Lazy<T> implements Supplier<T> permits Lazy.Filled, Lazy.OfSupplier {
/**
* Wraps the provided {@link Supplier} into a {@link OfSupplier} instance.
*
* @param supplier The {@link Supplier} to a value.
* @param <T> The type of the wrapped value.
* @return A new {@link OfSupplier} instance.
*/
public static <T> Lazy<T> of(Supplier<? extends T> supplier) {
return new OfSupplier<>(supplier);
}

/**
* Returns an {@link Filled} instance wrapping the provided {@link T} value.
*
* @param value The value to wrap.
* @param <T> The type of the wrapped value.
* @return A new {@link Filled} instance.
*/
public static <T> Lazy<T> filled(T value) {
return new Filled<>(value);
}

/**
* Creates a new {@link Lazy} containing the current value mapped using the provided {@link Function}.<br/>
* <i><b>This does not initialize the contained value!</b></i>
*
* @param mapper The function to apply to the contained value.
* @param <U> The new contained type.
* @return A new {@link Lazy} instance, that may or may not be initialized that contains the mapped value.
*/
public abstract <U> Lazy<U> map(Function<T, ? extends U> mapper);

/**
* If we contain a value, we instantly return the transformed version, using the {@link Function} provided.<br/>
* Otherwise, we return a new {@link Lazy} containing the value of the result of the mapper {@link Function}.<br/>
* <i><b>This does not initialize the contained value!</b></i>
*
* @param mapper The {@link Function} to use.
* @param <U> The new contained type.
* @return The mapped {@link Lazy} value.
*/
public abstract <U> Lazy<U> flatMap(Function<T, ? extends Lazy<U>> mapper);

/**
* Initializes the current {@link Lazy} and returns the result of applying the provided {@link Function} to the value.<br/>
* <i><b>This initializes the contained value.</b></i>
*
* @param function The {@link Function} to use.
* @param <M> The return type.
* @return The {@link M} value produced from the application of the provided {@link Function}}.
*/
public <M> M mapUnwrap(Function<T, ? extends M> function) {
return function.apply(this.unwrap());
}

/**
* Whether the current {@link Lazy} has initialized its value.
*
* @return Whether the current {@link Lazy} contains an initialized value.
*/
public abstract boolean isFilled();

/**
* Applies the provided {@link Consumer} on the current value, if it is initialized.
*
* @param action The {@link Consumer} to apply the value to.
* @return This instance(for chaining).
*/
public abstract Lazy<T> ifFilled(Consumer<? super T> action);

/**
* Runs the provided {@link Runnable}, if our value is not initialized.<br/>
*
* @param action The {@link Runnable} to run.
* @return This instance(for chaining).
*/
public abstract Lazy<T> ifEmpty(Runnable action);

/**
* Whether the current instance has an initialized value.
*
* @return Whether there is a value in this {@link Lazy} that is initialized.
*/
public abstract boolean isEmpty();

/**
* Initializes the current value and applies it to the provided {@link Consumer}.<br/>
* <i><b>This initializes the contained value.</b></i>
*
* @param action The {@link Consumer} to accept the current value.
* @return This instance(for chaining).
*/
public Lazy<T> compute(Consumer<? super T> action) {
action.accept(this.get());
return this;
}

/**
* @see Lazy#get
*/
public T unwrap() {
return this.get();
}

/**
* Returns the contained value if it is initialized, otherwise the provided {@link T} instance.
* <i><b>This does not initialize the contained value.</b></i>
*
* @param defaultValue The default {@link T} instance to return.
* @return The contained value or the provided {@link T}.
*/
public abstract T unwrapOr(T defaultValue);

/**
* Works like {@link Lazy#unwrapOr} except the default {@link T} value is provided lazily.
*
* @param defaultSupplier A provider of a {@link T} instance.
* @return Either the contained value or the result of {@link Supplier#get} when called on the provided {@link Supplier}.
*/
public abstract T unwrapOrGet(Supplier<? extends T> defaultSupplier);

/**
* A {@link Lazy} with an already initialized value.<br/>
*
* <p>
* This class can either be created using the {@link Lazy#filled} method or by mapping a {@link Lazy} with an initialized value.<br/>
* A {@link Lazy} cannot be converted by itself into a {@link Filled}.
*
* @param <T> The contained type.
*/
public static final class Filled<T> extends Lazy<T> {
private final T value;

/**
* @param value The contained value.
*/
private Filled(T value) {
this.value = value;
}

/**
* @see Lazy#get
*/
@Override
public T get() {
return this.value;
}

/**
* @see Lazy#map
*/
@Override
public <U> Lazy<U> map(Function<T, ? extends U> mapper) {
return filled(mapper.apply(this.value));
}

/**
* @see Lazy#flatMap
*/
@Override
public <U> Lazy<U> flatMap(Function<T, ? extends Lazy<U>> mapper) {
return mapper.apply(this.get());
}

/**
* @see Lazy#isFilled
*/
@Override
public boolean isFilled() {
return true;
}

/**
* @see Lazy#ifFilled
*/
@Override
public Lazy<T> ifFilled(Consumer<? super T> action) {
action.accept(this.get());
return this;
}

/**
* @see Lazy#ifEmpty
*/
@Override
public Lazy<T> ifEmpty(Runnable action) {
return this;
}

/**
* @see Lazy#isEmpty
*/
@Override
public boolean isEmpty() {
return false;
}

/**
* @see Lazy#unwrapOr
*/
@Override
public T unwrapOr(T defaultValue) {
return this.value;
}

/**
* @see Lazy#unwrapOrGet
*/
@Override
public T unwrapOrGet(Supplier<? extends T> defaultSupplier) {
return this.value;
}
}

/**
* A {@link Lazy} with either an initialized value or not.
*
* @param <T>
*/
public static final class OfSupplier<T> extends Lazy<T> {
private Supplier<? extends T> supplier;
// There is no overhead to using a Maybe here since Maybe has statically evaluated returns for Nothing instances..
private Maybe<T> value;

/**
* @param supplier The {@link Supplier} used to initialize the value.
*/
private OfSupplier(Supplier<? extends T> supplier) {
this.supplier = supplier;
this.value = Maybe.nothing();
}

/**
* @see Lazy#get
*/
@Override
public T get() {
if (this.value.isNothing()) {
this.value = Maybe.just(this.supplier.get());
this.supplier = null;
}

return this.value.unwrap();
}

/**
* @see Lazy#map
*/
@Override
public <U> Lazy<U> map(Function<T, ? extends U> mapper) {
return this.value.isJust() ? filled(mapper.apply(this.get())) : of(() -> mapper.apply(this.supplier.get()));
}

/**
* @see Lazy#flatMap
*/
@Override
public <U> Lazy<U> flatMap(Function<T, ? extends Lazy<U>> mapper) {
return this.value.isJust() ? mapper.apply(this.get()) : of(() -> mapper.apply(this.get()).get());
}

/**
* @see Lazy#isFilled
*/
@Override
public boolean isFilled() {
return this.value.isJust();
}

/**
* @see Lazy#ifFilled
*/
@Override
public Lazy<T> ifFilled(Consumer<? super T> action) {
this.value.ifJust(action);
return this;
}

/**
* @see Lazy#ifEmpty
*/
@Override
public Lazy<T> ifEmpty(Runnable action) {
this.value.ifNothing(action);
return this;
}

/**
* @see Lazy#isEmpty
*/
@Override
public boolean isEmpty() {
return this.value.isNothing();
}

/**
* @see Lazy#unwrapOr
*/
@Override
public T unwrapOr(T defaultValue) {
return this.value.unwrapOr(defaultValue);
}

/**
* @see Lazy#unwrapOrGet
*/
@Override
public T unwrapOrGet(Supplier<? extends T> defaultSupplier) {
return this.value.unwrapOrGet(defaultSupplier);
}
}
}
Loading