-
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 4 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
23 changes: 23 additions & 0 deletions
23
.../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,23 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| public interface CustomRegistryEntryList<T> extends RegistryEntryList<T> { | ||
| CustomRegistryEntryListSerializer getSerializer(); | ||
| } |
35 changes: 35 additions & 0 deletions
35
.../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,35 @@ | ||
| /* | ||
| * 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 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; | ||
|
|
||
| public interface CustomRegistryEntryListSerializer { | ||
| 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); | ||
| } |
34 changes: 34 additions & 0 deletions
34
...ricmc/fabric/api/event/registry/entrylists/CustomRegistryEntryListSerializerRegistry.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,34 @@ | ||
| /* | ||
| * 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 org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| import net.minecraft.util.Identifier; | ||
|
|
||
| import net.fabricmc.fabric.impl.registry.entrylists.CustomRegistryEntryListSerializerRegistryImpl; | ||
|
|
||
| public interface CustomRegistryEntryListSerializerRegistry { | ||
| static void registerSerializer(@NotNull CustomRegistryEntryListSerializer serializer) { | ||
| CustomRegistryEntryListSerializerRegistryImpl.registerSerializer(serializer); | ||
| } | ||
|
|
||
| static @Nullable CustomRegistryEntryListSerializer getSerializer(Identifier id) { | ||
| return CustomRegistryEntryListSerializerRegistryImpl.getSerializer(id); | ||
| } | ||
| } | ||
46 changes: 46 additions & 0 deletions
46
...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,46 @@ | ||
| /* | ||
| * 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.entrylists.defaults.DefaultCustomRegistryEntryListsImpl; | ||
|
|
||
| public class DefaultCustomRegistryEntryLists { | ||
| @SafeVarargs | ||
| public static <T> RegistryEntryList<T> union(RegistryEntryList<T>... parts) { | ||
| return DefaultCustomRegistryEntryListsImpl.union(parts); | ||
| } | ||
|
|
||
| @SafeVarargs | ||
| public static <T> RegistryEntryList<T> intersection(RegistryEntryList<T>... parts) { | ||
| return DefaultCustomRegistryEntryListsImpl.intersection(parts); | ||
| } | ||
|
|
||
| public static <T> RegistryEntryList<T> inverse(RegistryEntryLookup<T> lookup, RegistryEntryList<T> opposite) { | ||
| return DefaultCustomRegistryEntryListsImpl.inverse(lookup, opposite); | ||
| } | ||
|
|
||
| public static <T> RegistryEntryList<T> universal(RegistryEntryLookup<T> lookup) { | ||
| return DefaultCustomRegistryEntryListsImpl.universal(lookup); | ||
| } | ||
|
|
||
| public static <T> RegistryEntryList<T> subtraction(RegistryEntryLookup<T> lookup, RegistryEntryList<T> initial, RegistryEntryList<T> subtracted) { | ||
| return DefaultCustomRegistryEntryListsImpl.subtraction(lookup, initial, subtracted); | ||
| } | ||
| } |
92 changes: 92 additions & 0 deletions
92
...in/java/net/fabricmc/fabric/api/event/registry/entrylists/DependentRegistryEntryList.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,92 @@ | ||
| /* | ||
| * 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 com.google.common.collect.MapMaker; | ||
| import org.jetbrains.annotations.ApiStatus; | ||
| import org.jetbrains.annotations.MustBeInvokedByOverriders; | ||
|
|
||
| import net.minecraft.registry.entry.RegistryEntryList; | ||
|
|
||
| // Injected to RegistryEntryList<T> | ||
| @ApiStatus.NonExtendable | ||
| public interface DependentRegistryEntryList<T> { | ||
cputnam-a11y marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // creating a cycle in the graph will lead to stack overflow, do with this knowledge what you will | ||
| Map<RegistryEntryList<?>, Set<RegistryEntryList<?>>> DEPENDENCIES = new MapMaker().weakKeys().makeMap(); | ||
|
|
||
| default Set<RegistryEntryList<T>> getDependencies() { | ||
| return Collections.unmodifiableSet(castToT(DEPENDENCIES.get(this.asSelf()))); | ||
| } | ||
|
|
||
| /** | ||
| * 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(DependentRegistryEntryList::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; | ||
| } | ||
| } | ||
78 changes: 78 additions & 0 deletions
78
...bricmc/fabric/impl/registry/entrylists/CustomRegistryEntryListSerializerRegistryImpl.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.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 class CustomRegistryEntryListSerializerRegistryImpl { | ||
| private CustomRegistryEntryListSerializerRegistryImpl() { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(CustomRegistryEntryListSerializerRegistryImpl.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); | ||
| } | ||
| } |
78 changes: 78 additions & 0 deletions
78
...abricmc/fabric/impl/registry/entrylists/defaults/DefaultCustomRegistryEntryListsImpl.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.entrylists.defaults; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| import net.minecraft.registry.RegistryEntryLookup; | ||
| import net.minecraft.registry.entry.RegistryEntryList; | ||
|
|
||
| import net.fabricmc.fabric.impl.registry.entrylists.CustomRegistryEntryListSerializerRegistryImpl; | ||
| import net.fabricmc.fabric.impl.registry.entrylists.util.RegistryWrapperUtils; | ||
|
|
||
| public class DefaultCustomRegistryEntryListsImpl { | ||
| private static boolean initialized = false; | ||
|
|
||
| public static void register() { | ||
| if (initialized) { | ||
| return; | ||
| } | ||
|
|
||
| initialized = true; | ||
| CustomRegistryEntryListSerializerRegistryImpl.registerSerializer(UnionRegistryEntryList.SERIALIZER); | ||
| CustomRegistryEntryListSerializerRegistryImpl.registerSerializer(IntersectionRegistryEntryList.SERIALIZER); | ||
| CustomRegistryEntryListSerializerRegistryImpl.registerSerializer(InverseRegistryEntryList.SERIALIZER); | ||
| CustomRegistryEntryListSerializerRegistryImpl.registerSerializer(UniversalRegistryEntryList.SERIALIZER); | ||
| } | ||
|
|
||
| public static <T> RegistryEntryList<T> union(RegistryEntryList<T>[] parts) { | ||
| return new UnionRegistryEntryList<>(List.of(validateNotNull(parts))); | ||
| } | ||
|
|
||
| public static <T> RegistryEntryList<T> intersection(RegistryEntryList<T>[] parts) { | ||
| return new IntersectionRegistryEntryList<>(List.of(validateNotNull(parts))); | ||
| } | ||
|
|
||
| public static <T> RegistryEntryList<T> inverse(RegistryEntryLookup<T> lookup, RegistryEntryList<T> opposite) { | ||
| Objects.requireNonNull(lookup, "lookup"); | ||
| Objects.requireNonNull(opposite, "opposite"); | ||
| return new InverseRegistryEntryList<>(RegistryWrapperUtils.castOrCreateFromEntryLookup(lookup), opposite); | ||
| } | ||
|
|
||
| public static <T> RegistryEntryList<T> universal(RegistryEntryLookup<T> lookup) { | ||
| Objects.requireNonNull(lookup, "lookup"); | ||
| return new UniversalRegistryEntryList<>(RegistryWrapperUtils.castOrCreateFromEntryLookup(lookup)); | ||
| } | ||
|
|
||
| public static <T> RegistryEntryList<T> subtraction(RegistryEntryLookup<T> lookup, RegistryEntryList<T> initial, RegistryEntryList<T> subtracted) { | ||
| Objects.requireNonNull(lookup, "lookup"); | ||
| Objects.requireNonNull(initial, "initial"); | ||
| Objects.requireNonNull(subtracted, "subtracted"); | ||
| return new IntersectionRegistryEntryList<>(List.of(initial, inverse(lookup, subtracted))); | ||
| } | ||
|
|
||
| private static <T> RegistryEntryList<T>[] validateNotNull(RegistryEntryList<T>[] lists) { | ||
| Objects.requireNonNull(lists, "list array"); | ||
|
|
||
| for (RegistryEntryList<T> list : lists) { | ||
| Objects.requireNonNull(list, "list"); | ||
| } | ||
|
|
||
| return lists; | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.