-
Notifications
You must be signed in to change notification settings - Fork 504
Initial Commit: CustomRegistryEntryLists. #4680
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
Open
cputnam-a11y
wants to merge
8
commits into
FabricMC:1.21.6
Choose a base branch
from
cputnam-a11y:1.21.6
base: 1.21.6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
48c9e05
Initial Commit: CustomRegistryEntryLists for every occasion
cputnam-a11y cf40a6f
Comments from Discord per pepper
cputnam-a11y 21e6ebf
Changes
cputnam-a11y 70a8be7
rename
cputnam-a11y 28ba2dd
Unit Testing & Proper Weakness
cputnam-a11y 78b7e52
Fix Mistake (This is why we can't have nice things)
cputnam-a11y 08744e5
address review comments
cputnam-a11y 79e9ca6
javadocs :tada:
cputnam-a11y File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
.../main/java/net/fabricmc/fabric/api/event/registry/entrylists/CustomRegistryEntryList.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * 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.event.registry.entrylists; | ||
|
|
||
| import net.minecraft.registry.entry.RegistryEntryList; | ||
|
|
||
| /** | ||
| * | ||
| * @param <T> the type of elements contained by this list | ||
| */ | ||
| public interface CustomRegistryEntryList<T> extends RegistryEntryList<T> { | ||
| /** | ||
| * | ||
| * @return a registered serializer capable of serializing this list | ||
| * @see CustomRegistryEntryListSerializer#registerSerializer | ||
| */ | ||
| CustomRegistryEntryListSerializer getSerializer(); | ||
| } |
60 changes: 60 additions & 0 deletions
60
.../net/fabricmc/fabric/api/event/registry/entrylists/CustomRegistryEntryListSerializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * 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.event.registry.entrylists; | ||
|
|
||
| import com.mojang.serialization.Codec; | ||
| import com.mojang.serialization.MapCodec; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| import net.minecraft.network.RegistryByteBuf; | ||
| import net.minecraft.network.codec.PacketCodec; | ||
| import net.minecraft.registry.Registry; | ||
| import net.minecraft.registry.RegistryKey; | ||
| import net.minecraft.registry.entry.RegistryEntry; | ||
| import net.minecraft.util.Identifier; | ||
|
|
||
| import net.fabricmc.fabric.impl.registry.sync.entrylists.CustomRegistryEntryListSerializerImpl; | ||
|
|
||
| public interface CustomRegistryEntryListSerializer { | ||
| /** | ||
| * registers a {@link CustomRegistryEntryListSerializer}. this must be done before the list is used | ||
| * @param serializer the serializer to register | ||
| */ | ||
| static void registerSerializer(@NotNull CustomRegistryEntryListSerializer serializer) { | ||
| CustomRegistryEntryListSerializerImpl.registerSerializer(serializer); | ||
| } | ||
|
|
||
| /** | ||
| * gets a registered serializer. | ||
| * @param id the identifier of the serializer | ||
| * @return the serializer, or {@code null} if no serializer is registered with the given id | ||
| */ | ||
| static @Nullable CustomRegistryEntryListSerializer getSerializer(Identifier id) { | ||
| return CustomRegistryEntryListSerializerImpl.getSerializer(id); | ||
| } | ||
|
|
||
| /** | ||
| * used to encode the entry list over the network, or in json. | ||
| * @return the registry id of this serializer | ||
| */ | ||
| Identifier getIdentifier(); | ||
|
|
||
| <T1> MapCodec<? extends CustomRegistryEntryList<T1>> createCodec(RegistryKey<? extends Registry<T1>> registryKey, Codec<RegistryEntry<T1>> entryCodec, boolean forceList); | ||
|
|
||
| <T1> PacketCodec<RegistryByteBuf, ? extends CustomRegistryEntryList<T1>> createPacketCodec(RegistryKey<? extends Registry<T1>> registryKey); | ||
| } |
89 changes: 89 additions & 0 deletions
89
...va/net/fabricmc/fabric/api/event/registry/entrylists/DefaultCustomRegistryEntryLists.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* | ||
| * 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.event.registry.entrylists; | ||
|
|
||
| import net.minecraft.registry.RegistryEntryLookup; | ||
| import net.minecraft.registry.entry.RegistryEntryList; | ||
|
|
||
| import net.fabricmc.fabric.impl.registry.sync.entrylists.defaults.DefaultCustomRegistryEntryListsImpl; | ||
|
|
||
| /** | ||
| * convenience implementations for {@link CustomRegistryEntryList}. | ||
| */ | ||
| public class DefaultCustomRegistryEntryLists { | ||
| /** | ||
| * creates a {@link RegistryEntryList} that contains anything contained by any element of {@code parts}. | ||
| * | ||
| * @param parts the component parts to OR | ||
| * @param <T> the type of elements contained in all the lists | ||
| * @return the custom list | ||
| */ | ||
| @SafeVarargs | ||
| public static <T> RegistryEntryList<T> union(RegistryEntryList<T>... parts) { | ||
| return DefaultCustomRegistryEntryListsImpl.union(parts); | ||
| } | ||
|
|
||
| /** | ||
| * creates a {@link RegistryEntryList} that contains anything contained by all element of {@code parts}. | ||
| * | ||
| * @param parts the component parts to AND | ||
| * @param <T> the type of elements contained in all the lists | ||
| * @return the custom list | ||
| */ | ||
| @SafeVarargs | ||
| public static <T> RegistryEntryList<T> intersection(RegistryEntryList<T>... parts) { | ||
| return DefaultCustomRegistryEntryListsImpl.intersection(parts); | ||
| } | ||
|
|
||
| /** | ||
| * creates a {@link RegistryEntryList} that contains anything not contained by its opposite. | ||
| * | ||
| * @param lookup the source registry for this list | ||
| * @param opposite the list that contains everything the returned list will not | ||
| * @param <T> the type of elements contained in all the lists | ||
| * @return the custom list | ||
| * @implNote {@link net.minecraft.item.Items#AIR} and {@link net.minecraft.block.Blocks#AIR} are included in the returned list, provided that they are not in opposite. for certain use cases, such as {@link net.minecraft.recipe.Ingredient}s, the caller must ensure that air is not in the end result, or else the ingredient will fail to serialize | ||
| */ | ||
| public static <T> RegistryEntryList<T> inverse(RegistryEntryLookup<T> lookup, RegistryEntryList<T> opposite) { | ||
| return DefaultCustomRegistryEntryListsImpl.inverse(lookup, opposite); | ||
| } | ||
|
|
||
| /** | ||
| * creates a {@link RegistryEntryList} that contains everything in the source lookup. | ||
| * | ||
| * @param lookup the source registry for this list | ||
| * @param <T> the type of elements contained in all the lists | ||
| * @return the custom list | ||
| * @implNote {@link net.minecraft.item.Items#AIR} and {@link net.minecraft.block.Blocks#AIR} are included in the returned list. for certain use cases, such as {@link net.minecraft.recipe.Ingredient}s, the caller must ensure that air is not in the end result, or else the ingredient will fail to serialize | ||
| */ | ||
| public static <T> RegistryEntryList<T> universal(RegistryEntryLookup<T> lookup) { | ||
| return DefaultCustomRegistryEntryListsImpl.universal(lookup); | ||
| } | ||
|
|
||
| /** | ||
| * creates a {@link RegistryEntryList} that contains everything in {@code initial} that is not also in {@code subtracted}. | ||
| * | ||
| * @param lookup the source registry for this list | ||
| * @param initial the beginning list | ||
| * @param subtracted the list to subtract from initial | ||
| * @param <T> the type of elements contained in all the lists | ||
| * @return the custom list | ||
| */ | ||
| public static <T> RegistryEntryList<T> subtraction(RegistryEntryLookup<T> lookup, RegistryEntryList<T> initial, RegistryEntryList<T> subtracted) { | ||
| return DefaultCustomRegistryEntryListsImpl.subtraction(lookup, initial, subtracted); | ||
| } | ||
| } |
91 changes: 91 additions & 0 deletions
91
.../main/java/net/fabricmc/fabric/api/event/registry/entrylists/FabricRegistryEntryList.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * 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.event.registry.entrylists; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.WeakHashMap; | ||
|
|
||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.MustBeInvokedByOverriders; | ||
|
|
||
| import net.minecraft.registry.entry.RegistryEntryList; | ||
|
|
||
| // Injected to RegistryEntryList<T> | ||
| @ApiStatus.NonExtendable | ||
| public interface FabricRegistryEntryList<T> { | ||
| // creating a cycle in the graph will lead to stack overflow, do with this knowledge what you will | ||
| Map<RegistryEntryList<?>, Set<RegistryEntryList<?>>> DEPENDENCIES = new WeakHashMap<>(); | ||
|
|
||
| default Set<RegistryEntryList<T>> getDependencies() { | ||
| return Collections.unmodifiableSet(castToT(DEPENDENCIES.getOrDefault(this.asSelf(), Set.of()))); | ||
| } | ||
|
|
||
| /** | ||
| * Unregisters a dependency on this. | ||
| * Does nothing is the dependency is not registered. | ||
| * | ||
| * @param dependency the dependency to unregister | ||
| */ | ||
| default void unregisterDependency(RegistryEntryList<T> dependency) { | ||
| Set<RegistryEntryList<T>> dependencies = castToT(DEPENDENCIES.get(this.asSelf())); | ||
|
|
||
| if (dependencies != null) { | ||
| dependencies.remove(dependency); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * registers a dependency on this. | ||
| * | ||
| * @param dependency the dependency to register | ||
| */ | ||
| default void registerDependency(RegistryEntryList<T> dependency) { | ||
| DEPENDENCIES.computeIfAbsent( | ||
| this.asSelf(), | ||
| k -> Collections.newSetFromMap(new WeakHashMap<>()) | ||
| ) | ||
| .add(dependency); | ||
| } | ||
|
|
||
| /** | ||
| * Invalidate dependents is called by this list when it is invalidated. | ||
| */ | ||
| private void invalidateDependents() { | ||
| DEPENDENCIES.getOrDefault(this.asSelf(), Set.of()).forEach(FabricRegistryEntryList::invalidate); | ||
| } | ||
|
|
||
| /** | ||
| * Invalidate is called by any list that this depends on when the parent list is invalidated. | ||
| */ | ||
| @MustBeInvokedByOverriders | ||
| default void invalidate() { | ||
| this.invalidateDependents(); | ||
| } | ||
|
|
||
| // this interface should only be implemented on RegistryEntryList, so the case **should** be safe... | ||
| private RegistryEntryList<T> asSelf() { | ||
| return (RegistryEntryList<T>) this; | ||
| } | ||
|
|
||
| // why can't java just have field generics or something? | ||
| @SuppressWarnings("unchecked") | ||
| private static <T> Set<RegistryEntryList<T>> castToT(Set<RegistryEntryList<?>> entryList) { | ||
| return (Set<RegistryEntryList<T>>) (Object) entryList; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
.../fabricmc/fabric/impl/registry/sync/entrylists/CustomRegistryEntryListSerializerImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /* | ||
| * 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.impl.registry.sync.entrylists; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
|
|
||
| import com.mojang.serialization.Codec; | ||
| import com.mojang.serialization.DataResult; | ||
| import com.mojang.serialization.DynamicOps; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import net.minecraft.util.Identifier; | ||
|
|
||
| import net.fabricmc.fabric.api.event.registry.entrylists.CustomRegistryEntryListSerializer; | ||
|
|
||
| public final class CustomRegistryEntryListSerializerImpl { | ||
| private CustomRegistryEntryListSerializerImpl() { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(CustomRegistryEntryListSerializerImpl.class); | ||
| private static final Map<Identifier, CustomRegistryEntryListSerializer> SERIALIZERS = new HashMap<>(); | ||
| public static final Codec<CustomRegistryEntryListSerializer> SERIALIZER_CODEC = Identifier.CODEC.flatXmap( | ||
| id -> Optional.ofNullable(SERIALIZERS.get(id)) | ||
| .map(DataResult::success) | ||
| .orElseGet( | ||
| () -> DataResult.error( | ||
| () -> "Unknown CustomRegistryEntryListSerializer: " + id | ||
| ) | ||
| ), | ||
| serializer -> Optional.of(serializer.getIdentifier()) | ||
| .filter(SERIALIZERS::containsKey) | ||
| .map(DataResult::success) | ||
| .orElseGet( | ||
| () -> DataResult.error( | ||
| () -> "Unknown CustomRegistryEntryListSerializer: " + serializer | ||
| ) | ||
| ) | ||
| ); | ||
|
|
||
| public static <T> boolean isSerializedCustomRegistryEntryList(DynamicOps<T> ops, T entryList) { | ||
| return ops.getMap(entryList) | ||
| .map(it -> it.get("fabric:type")) | ||
| .map(Objects::nonNull) | ||
| .result() | ||
| .orElse(false); | ||
| } | ||
|
|
||
| public static void registerSerializer(CustomRegistryEntryListSerializer serializer) { | ||
| CustomRegistryEntryListSerializer previous = SERIALIZERS.put(serializer.getIdentifier(), serializer); | ||
|
|
||
| if (previous != null) { | ||
| LOGGER.warn("Overwriting CustomRegistryEntryListSerializer {} with {}", previous, serializer); | ||
| } | ||
| } | ||
|
|
||
| public static CustomRegistryEntryListSerializer getSerializer(Identifier id) { | ||
| return SERIALIZERS.get(id); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should not be public, i.e. it should be moved to a separate impl class