Skip to content
Open
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
@@ -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();
}
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);
}
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);
}
}
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<>();
Copy link
Contributor

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


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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationConnectionEvents;
import net.fabricmc.fabric.api.networking.v1.ServerConfigurationNetworking;
import net.fabricmc.fabric.impl.registry.sync.entrylists.defaults.DefaultCustomRegistryEntryListsImpl;
import net.fabricmc.fabric.impl.registry.sync.entrylists.util.ChannelUtil;
import net.fabricmc.fabric.impl.registry.sync.packet.DirectRegistryPacketHandler;

public class FabricRegistryInit implements ModInitializer {
Expand Down Expand Up @@ -211,5 +213,8 @@ public void onInitialize() {
// Synced via PacketCodecs.registryValue
RegistryAttributeHolder.get(Registries.RECIPE_BOOK_CATEGORY)
.addAttribute(RegistryAttribute.SYNCED);

ChannelUtil.init();
DefaultCustomRegistryEntryListsImpl.register();
}
}
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);
}
}
Loading