Update to 20.2.59-beta & Adapt to Registry Changes

This commit is contained in:
shedaniel
2023-11-20 21:00:36 +08:00
parent 426ca41331
commit 9aba3cf1dc
13 changed files with 755 additions and 20 deletions

View File

@@ -0,0 +1,98 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021, 2022 architectury
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package dev.architectury.impl;
import com.mojang.datafixers.util.Either;
import dev.architectury.registry.registries.RegistrySupplier;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderOwner;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;
@ApiStatus.Internal
public interface RegistrySupplierImpl<T> extends RegistrySupplier<T> {
@Nullable
Holder<T> getHolder();
@Override
default T value() {
return get();
}
@Override
default boolean isBound() {
return isPresent();
}
@Override
default boolean is(ResourceLocation resourceLocation) {
return getId().equals(resourceLocation);
}
@Override
default boolean is(ResourceKey<T> resourceKey) {
return getKey().equals(resourceKey);
}
@Override
default boolean is(Predicate<ResourceKey<T>> predicate) {
return predicate.test(getKey());
}
@Override
default boolean is(TagKey<T> tagKey) {
Holder<T> holder = getHolder();
return holder != null && holder.is(tagKey);
}
@Override
default Stream<TagKey<T>> tags() {
Holder<T> holder = getHolder();
return holder != null ? holder.tags() : Stream.empty();
}
@Override
default Either<ResourceKey<T>, T> unwrap() {
return Either.left(getKey());
}
@Override
default Optional<ResourceKey<T>> unwrapKey() {
return Optional.of(getKey());
}
@Override
default Kind kind() {
return Kind.REFERENCE;
}
@Override
default boolean canSerializeIn(HolderOwner<T> holderOwner) {
Holder<T> holder = getHolder();
return holder != null && holder.canSerializeIn(holderOwner);
}
}

View File

@@ -20,6 +20,8 @@
package dev.architectury.registry.registries;
import com.google.common.base.Suppliers;
import dev.architectury.impl.RegistrySupplierImpl;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
@@ -90,16 +92,25 @@ public class DeferredRegister<T> implements Iterable<RegistrySupplier<T>> {
return registriesSupplier.get().get(key);
}
private class Entry<R> implements RegistrySupplier<R> {
private class Entry<R> implements RegistrySupplierImpl<R> {
private final ResourceLocation id;
private final Supplier<R> supplier;
private RegistrySupplier<R> value;
@Nullable
private Holder<R> holder = null;
public Entry(ResourceLocation id, Supplier<R> supplier) {
this.id = id;
this.supplier = supplier;
}
@Nullable
@Override
public Holder<R> getHolder() {
if (holder != null) return holder;
return holder = getRegistrar().getHolder(getId());
}
@Override
public RegistrarManager getRegistrarManager() {
return DeferredRegister.this.getRegistrarManager();

View File

@@ -19,6 +19,7 @@
package dev.architectury.registry.registries;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
@@ -68,6 +69,14 @@ public interface Registrar<T> extends Iterable<T> {
ResourceKey<? extends Registry<T>> key();
@Nullable
Holder<T> getHolder(ResourceKey<T> key);
@Nullable
default Holder<T> getHolder(ResourceLocation id) {
return getHolder(ResourceKey.create(key(), id));
}
/**
* Listens to when the registry entry is registered, and calls the given action.
* Evaluates immediately if the entry is already registered.

View File

@@ -19,12 +19,13 @@
package dev.architectury.registry.registries;
import net.minecraft.core.Holder;
import org.jetbrains.annotations.ApiStatus;
import java.util.function.Consumer;
@ApiStatus.NonExtendable
public interface RegistrySupplier<T> extends DeferredSupplier<T> {
public interface RegistrySupplier<T> extends DeferredSupplier<T>, Holder<T> {
RegistrarManager getRegistrarManager();
Registrar<T> getRegistrar();