DeferredRegister.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * This file is part of architectury.
  3. * Copyright (C) 2020, 2021 shedaniel
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 3 of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. package me.shedaniel.architectury.registry;
  20. import net.minecraft.resources.ResourceKey;
  21. import net.minecraft.resources.ResourceLocation;
  22. import net.minecraft.util.LazyLoadedValue;
  23. import org.jetbrains.annotations.NotNull;
  24. import org.jetbrains.annotations.Nullable;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import java.util.Objects;
  28. import java.util.function.Supplier;
  29. public class DeferredRegister<T> {
  30. @NotNull
  31. private final Supplier<Registries> registriesSupplier;
  32. @NotNull
  33. private final ResourceKey<net.minecraft.core.Registry<T>> key;
  34. private final List<Entry<T>> entries = new ArrayList<>();
  35. private boolean registered = false;
  36. @Nullable
  37. private String modId;
  38. private DeferredRegister(@NotNull Supplier<Registries> registriesSupplier, @NotNull ResourceKey<net.minecraft.core.Registry<T>> key, @Nullable String modId) {
  39. this.registriesSupplier = Objects.requireNonNull(registriesSupplier);
  40. this.key = Objects.requireNonNull(key);
  41. this.modId = modId;
  42. }
  43. @NotNull
  44. public static <T> DeferredRegister<T> create(@NotNull String modId, @NotNull ResourceKey<net.minecraft.core.Registry<T>> key) {
  45. LazyLoadedValue<Registries> value = new LazyLoadedValue<>(() -> Registries.get(modId));
  46. return new DeferredRegister<>(value::get, key, Objects.requireNonNull(modId));
  47. }
  48. @NotNull
  49. @Deprecated
  50. public static <T> DeferredRegister<T> create(@NotNull Registries registries, @NotNull ResourceKey<net.minecraft.core.Registry<T>> key) {
  51. return new DeferredRegister<>(() -> registries, key, null);
  52. }
  53. @NotNull
  54. @Deprecated
  55. public static <T> DeferredRegister<T> create(@NotNull Supplier<Registries> registries, @NotNull ResourceKey<net.minecraft.core.Registry<T>> key) {
  56. return new DeferredRegister<>(registries, key, null);
  57. }
  58. @NotNull
  59. @Deprecated
  60. public static <T> DeferredRegister<T> create(@NotNull LazyLoadedValue<Registries> registries, @NotNull ResourceKey<net.minecraft.core.Registry<T>> key) {
  61. return create(registries::get, key);
  62. }
  63. public <R extends T> RegistrySupplier<R> register(String id, Supplier<? extends R> supplier) {
  64. if (modId == null) {
  65. throw new NullPointerException("You must create the deferred register with a mod id to register entries without the namespace!");
  66. }
  67. return register(new ResourceLocation(modId, id), supplier);
  68. }
  69. public <R extends T> RegistrySupplier<R> register(ResourceLocation id, Supplier<? extends R> supplier) {
  70. Entry<T> entry = new Entry<>(id, (Supplier<T>) supplier);
  71. this.entries.add(entry);
  72. if (registered) {
  73. Registry<T> registry = registriesSupplier.get().get(key);
  74. entry.value = registry.registerSupplied(entry.id, entry.supplier);
  75. }
  76. return (RegistrySupplier<R>) entry;
  77. }
  78. public void register() {
  79. if (registered) {
  80. throw new IllegalStateException("Cannot register a deferred register twice!");
  81. }
  82. registered = true;
  83. Registry<T> registry = registriesSupplier.get().get(key);
  84. for (Entry<T> entry : entries) {
  85. entry.value = registry.registerSupplied(entry.id, entry.supplier);
  86. }
  87. }
  88. private class Entry<R> implements RegistrySupplier<R> {
  89. private final ResourceLocation id;
  90. private final Supplier<R> supplier;
  91. private RegistrySupplier<R> value;
  92. public Entry(ResourceLocation id, Supplier<R> supplier) {
  93. this.id = id;
  94. this.supplier = supplier;
  95. }
  96. @Override
  97. public @NotNull ResourceLocation getRegistryId() {
  98. return key.location();
  99. }
  100. @Override
  101. public @NotNull ResourceLocation getId() {
  102. return id;
  103. }
  104. @Override
  105. public boolean isPresent() {
  106. return value != null && value.isPresent();
  107. }
  108. @Override
  109. public R get() {
  110. if (isPresent()) {
  111. return value.get();
  112. }
  113. throw new NullPointerException("Registry Object not present: " + this.id);
  114. }
  115. @Override
  116. public int hashCode() {
  117. return com.google.common.base.Objects.hashCode(getRegistryId(), getId());
  118. }
  119. @Override
  120. public boolean equals(Object obj) {
  121. if (this == obj) return true;
  122. if (!(obj instanceof RegistrySupplier)) return false;
  123. RegistrySupplier<?> other = (RegistrySupplier<?>) obj;
  124. return other.getRegistryId().equals(getRegistryId()) && other.getId().equals(getId());
  125. }
  126. @Override
  127. public String toString() {
  128. return getRegistryId().toString() + "@" + id.toString();
  129. }
  130. }
  131. }