|
@@ -22,10 +22,14 @@ 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.core.RegistryEntry;
|
|
|
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 me.shedaniel.architectury.registry.registries.RegistryBuilder;
|
|
|
+import me.shedaniel.architectury.registry.registries.RegistryOption;
|
|
|
+import me.shedaniel.architectury.registry.registries.StandardRegistryOption;
|
|
|
import net.minecraft.resources.ResourceKey;
|
|
|
import net.minecraft.resources.ResourceLocation;
|
|
|
import net.minecraft.util.LazyLoadedValue;
|
|
@@ -76,7 +80,11 @@ public class RegistriesImpl {
|
|
|
|
|
|
@Override
|
|
|
public <T> Registry<T> get(ResourceKey<net.minecraft.core.Registry<T>> registryKey) {
|
|
|
- return new ForgeBackedRegistryImpl<>(registry, (IForgeRegistry) RegistryManager.ACTIVE.getRegistry(registryKey.location()));
|
|
|
+ return get(RegistryManager.ACTIVE.getRegistry(registryKey.location()));
|
|
|
+ }
|
|
|
+
|
|
|
+ public <T> Registry<T> get(IForgeRegistry registry) {
|
|
|
+ return new ForgeBackedRegistryImpl<>(this.registry, registry);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -84,6 +92,13 @@ public class RegistriesImpl {
|
|
|
return new VanillaBackedRegistryImpl<>(registry);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public <T extends RegistryEntry<T>> RegistryBuilder<T> builder(Class<T> type, ResourceLocation registryId) {
|
|
|
+ return new RegistryBuilderWrapper<>(this, new net.minecraftforge.registries.RegistryBuilder<>()
|
|
|
+ .setName(registryId)
|
|
|
+ .setType((Class) type));
|
|
|
+ }
|
|
|
+
|
|
|
public class EventListener {
|
|
|
@SubscribeEvent
|
|
|
public void handleEvent(RegistryEvent.Register event) {
|
|
@@ -101,6 +116,37 @@ public class RegistriesImpl {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public static class RegistryBuilderWrapper<T extends RegistryEntry<T>> implements RegistryBuilder<T> {
|
|
|
+ @NotNull
|
|
|
+ private final RegistryProviderImpl provider;
|
|
|
+ @NotNull
|
|
|
+ private final net.minecraftforge.registries.RegistryBuilder<?> builder;
|
|
|
+ private boolean saveToDisk = false;
|
|
|
+ private boolean syncToClients = false;
|
|
|
+
|
|
|
+ public RegistryBuilderWrapper(@NotNull RegistryProviderImpl provider, @NotNull net.minecraftforge.registries.RegistryBuilder<?> builder) {
|
|
|
+ this.provider = provider;
|
|
|
+ this.builder = builder;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public @NotNull Registry<T> build() {
|
|
|
+ if (!syncToClients) builder.disableSync();
|
|
|
+ if (!saveToDisk) builder.disableSaving();
|
|
|
+ return provider.get(builder.create());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public @NotNull RegistryBuilder<T> option(@NotNull RegistryOption option) {
|
|
|
+ if (option == StandardRegistryOption.SAVE_TO_DISC) {
|
|
|
+ this.saveToDisk = true;
|
|
|
+ } else if (option == StandardRegistryOption.SYNC_TO_CLIENTS) {
|
|
|
+ this.syncToClients = true;
|
|
|
+ }
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public static class VanillaBackedRegistryImpl<T> implements Registry<T> {
|
|
|
private net.minecraft.core.Registry<T> delegate;
|
|
|
|
|
@@ -121,22 +167,22 @@ public class RegistriesImpl {
|
|
|
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;
|
|
@@ -144,7 +190,7 @@ public class RegistriesImpl {
|
|
|
RegistrySupplier<?> other = (RegistrySupplier<?>) obj;
|
|
|
return other.getRegistryId().equals(getRegistryId()) && other.getId().equals(getId());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public String toString() {
|
|
|
return getRegistryId().toString() + "@" + id.toString();
|
|
@@ -223,27 +269,27 @@ public class RegistriesImpl {
|
|
|
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;
|
|
@@ -251,7 +297,7 @@ public class RegistriesImpl {
|
|
|
RegistrySupplier<?> other = (RegistrySupplier<?>) obj;
|
|
|
return other.getRegistryId().equals(getRegistryId()) && other.getId().equals(getId());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public String toString() {
|
|
|
return getRegistryId().toString() + "@" + id.toString();
|
|
@@ -268,27 +314,27 @@ public class RegistriesImpl {
|
|
|
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;
|
|
@@ -296,7 +342,7 @@ public class RegistriesImpl {
|
|
|
RegistrySupplier<?> other = (RegistrySupplier<?>) obj;
|
|
|
return other.getRegistryId().equals(getRegistryId()) && other.getId().equals(getId());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public String toString() {
|
|
|
return getRegistryId().toString() + "@" + id.toString();
|