Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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,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();
}
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);
}
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);
}
}
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);
}
}
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> {
// 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;
}
}
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);
}
}
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;
}
}
Loading