Add RegistrySupplier to ease registry delegation

This commit is contained in:
shedaniel
2020-12-28 17:17:31 +08:00
parent 575654f2bb
commit d605cd5028
4 changed files with 271 additions and 15 deletions

View File

@@ -21,6 +21,7 @@ package me.shedaniel.architectury.registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
@@ -29,9 +30,21 @@ import java.util.Set;
import java.util.function.Supplier;
public interface Registry<T> extends Iterable<T> {
Supplier<T> delegate(ResourceLocation id);
@NotNull
default Supplier<T> delegate(ResourceLocation id) {
return delegateSupplied(id);
}
Supplier<T> register(ResourceLocation id, Supplier<T> supplier);
@NotNull
RegistrySupplier<T> delegateSupplied(ResourceLocation id);
@NotNull
default Supplier<T> register(ResourceLocation id, Supplier<T> supplier) {
return registerSupplied(id, supplier);
}
@NotNull
RegistrySupplier<T> registerSupplied(ResourceLocation id, Supplier<T> supplier);
@Nullable
ResourceLocation getId(T obj);

View File

@@ -0,0 +1,83 @@
/*
* This file is part of architectury.
* Copyright (C) 2020 shedaniel
*
* 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 me.shedaniel.architectury.registry;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Stream;
public interface RegistrySupplier<T> extends Supplier<T> {
@NotNull
ResourceLocation getRegistryId();
@NotNull
ResourceLocation getId();
boolean isPresent();
@Nullable
default T getOrNull() {
if (isPresent()) {
return get();
}
return null;
}
@NotNull
default Optional<T> toOptional() {
return Optional.ofNullable(getOrNull());
}
default void ifPresent(Consumer<? super T> action) {
if (isPresent()) {
action.accept(get());
}
}
default void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) {
if (isPresent()) {
action.accept(get());
} else {
emptyAction.run();
}
}
@NotNull
default Stream<T> stream() {
if (!isPresent()) {
return Stream.empty();
} else {
return Stream.of(get());
}
}
default T orElse(T other) {
return isPresent() ? get() : other;
}
default T orElseGet(Supplier<? extends T> supplier) {
return isPresent() ? get() : supplier.get();
}
}

View File

@@ -19,15 +19,20 @@
package me.shedaniel.architectury.registry.fabric;
import com.google.common.base.Objects;
import me.shedaniel.architectury.registry.Registries;
import me.shedaniel.architectury.registry.Registry;
import me.shedaniel.architectury.registry.RegistrySupplier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.LazyLoadedValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
public class RegistriesImpl {
@@ -69,15 +74,53 @@ public class RegistriesImpl {
}
@Override
public Supplier<T> delegate(ResourceLocation id) {
public @NotNull RegistrySupplier<T> delegateSupplied(ResourceLocation id) {
LazyLoadedValue<T> value = new LazyLoadedValue<>(() -> get(id));
return value::get;
return new RegistrySupplier<T>() {
@Override
public @NotNull ResourceLocation getRegistryId() {
return delegate.key().location();
}
@Override
public @NotNull ResourceLocation getId() {
return id;
}
@Override
public boolean isPresent() {
return contains(id);
}
@Override
public T get() {
return value.get();
}
@Override
public int hashCode() {
return Objects.hashCode(getRegistryId(), getId());
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof RegistrySupplier)) return false;
RegistrySupplier<?> other = (RegistrySupplier<?>) obj;
return other.getRegistryId().equals(getRegistryId()) && other.getId().equals(getId());
}
@Override
public String toString() {
return getRegistryId().toString() + "@" + id.toString();
}
};
}
@Override
public Supplier<T> register(ResourceLocation id, Supplier<T> supplier) {
public @NotNull RegistrySupplier<T> registerSupplied(ResourceLocation id, Supplier<T> supplier) {
net.minecraft.core.Registry.register(delegate, id, supplier.get());
return delegate(id);
return delegateSupplied(id);
}
@Override

View File

@@ -19,11 +19,13 @@
package me.shedaniel.architectury.registry.forge;
import com.google.common.base.Objects;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import me.shedaniel.architectury.platform.forge.EventBuses;
import me.shedaniel.architectury.registry.Registries;
import me.shedaniel.architectury.registry.Registry;
import me.shedaniel.architectury.registry.RegistrySupplier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.LazyLoadedValue;
@@ -34,6 +36,7 @@ import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.registries.IForgeRegistry;
import net.minecraftforge.registries.IForgeRegistryEntry;
import net.minecraftforge.registries.RegistryManager;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
import java.lang.reflect.Type;
@@ -106,15 +109,53 @@ public class RegistriesImpl {
}
@Override
public Supplier<T> delegate(ResourceLocation id) {
public @NotNull RegistrySupplier<T> delegateSupplied(ResourceLocation id) {
LazyLoadedValue<T> value = new LazyLoadedValue<>(() -> get(id));
return value::get;
return new RegistrySupplier<T>() {
@Override
public @NotNull ResourceLocation getRegistryId() {
return delegate.key().location();
}
@Override
public @NotNull ResourceLocation getId() {
return id;
}
@Override
public boolean isPresent() {
return contains(id);
}
@Override
public T get() {
return value.get();
}
@Override
public int hashCode() {
return Objects.hashCode(getRegistryId(), getId());
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof RegistrySupplier)) return false;
RegistrySupplier<?> other = (RegistrySupplier<?>) obj;
return other.getRegistryId().equals(getRegistryId()) && other.getId().equals(getId());
}
@Override
public String toString() {
return getRegistryId().toString() + "@" + id.toString();
}
};
}
@Override
public Supplier<T> register(ResourceLocation id, Supplier<T> supplier) {
public @NotNull RegistrySupplier<T> registerSupplied(ResourceLocation id, Supplier<T> supplier) {
net.minecraft.core.Registry.register(delegate, id, supplier.get());
return delegate(id);
return delegateSupplied(id);
}
@Override
@@ -175,16 +216,92 @@ public class RegistriesImpl {
}
@Override
public Supplier<T> delegate(ResourceLocation id) {
public @NotNull RegistrySupplier<T> delegateSupplied(ResourceLocation id) {
LazyLoadedValue<T> value = new LazyLoadedValue<>(() -> get(id));
return value::get;
return new RegistrySupplier<T>() {
@Override
public @NotNull ResourceLocation getRegistryId() {
return delegate.getRegistryName();
}
@Override
public @NotNull ResourceLocation getId() {
return id;
}
@Override
public boolean isPresent() {
return contains(id);
}
@Override
public T get() {
return value.get();
}
@Override
public int hashCode() {
return Objects.hashCode(getRegistryId(), getId());
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof RegistrySupplier)) return false;
RegistrySupplier<?> other = (RegistrySupplier<?>) obj;
return other.getRegistryId().equals(getRegistryId()) && other.getId().equals(getId());
}
@Override
public String toString() {
return getRegistryId().toString() + "@" + id.toString();
}
};
}
@Override
public Supplier<T> register(ResourceLocation id, Supplier<T> supplier) {
public @NotNull RegistrySupplier<T> registerSupplied(ResourceLocation id, Supplier<T> supplier) {
RegistryObject registryObject = RegistryObject.of(id, delegate);
registry.put(delegate.getRegistrySuperType(), registryObject, () -> supplier.get().setRegistryName(id));
return registryObject;
return new RegistrySupplier<T>() {
@Override
public @NotNull ResourceLocation getRegistryId() {
return delegate.getRegistryName();
}
@Override
public @NotNull ResourceLocation getId() {
return registryObject.getId();
}
@Override
public boolean isPresent() {
return registryObject.isPresent();
}
@Override
public T get() {
return (T) registryObject.get();
}
@Override
public int hashCode() {
return Objects.hashCode(getRegistryId(), getId());
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof RegistrySupplier)) return false;
RegistrySupplier<?> other = (RegistrySupplier<?>) obj;
return other.getRegistryId().equals(getRegistryId()) && other.getId().equals(getId());
}
@Override
public String toString() {
return getRegistryId().toString() + "@" + id.toString();
}
};
}
@Override