Skip to content

Commit

Permalink
support biome registration
Browse files Browse the repository at this point in the history
  • Loading branch information
JieningYu committed Oct 12, 2022
1 parent 8b1b0be commit cb23588
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx3G
loader_version=0.14.9

# Mod Properties
mod_version = 0.1.2
mod_version = 0.1.3
maven_group = com.dm.earth
archives_base_name = deferred_registries

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/dm/earth/deferred_registries/DeferredObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import java.util.function.Supplier;

import com.dm.earth.deferred_registries.helper.BiomeRegistryHelper;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.biome.Biome;

public class DeferredObject<T> {
private final Identifier id;
Expand Down Expand Up @@ -31,7 +34,15 @@ public void register(Registry<? super T> registry) {
Registry.register(registry, this.id, this.entry);
}

public void register(RegistryKey<Registry<? super T>> registryKey) {
if (BiomeRegistryHelper.isBiomeKey(registryKey)) BiomeRegistryHelper.registerBiome((Biome) this.entry, this.id);
}

public static <T> DeferredObject<T> of(Identifier id, T entry) {
return new DeferredObject<T>(id, entry);
}

public static <T> DeferredObject<T> of(Identifier id, Supplier<T> entry) {
return new DeferredObject<T>(id, entry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,38 @@

import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import org.jetbrains.annotations.Nullable;

public class DeferredRegistries<T> {
private final Registry<? super T> registry;
private Registry<? super T> registry;
private RegistryKey<Registry<? super T>> registryKey;
private final String modId;
private final List<DeferredObject<T>> entries;

private DeferredRegistries(Registry<? super T> registry, String modId) {
this.registry = registry;
public DeferredRegistries(String modId) {
this.modId = modId;
this.entries = new ArrayList<>();
this.registryKey = null;
this.registry = null;
}

public DeferredRegistries<T> registry(Registry<? super T> registry) {
this.registry = registry;
return this;
}

public DeferredRegistries<T> registryKey(RegistryKey<Registry<? super T>> registryKey) {
this.registryKey = registryKey;
return this;
}

public static <T> DeferredRegistries<T> create(Registry<? super T> registry, String modId) {
return new DeferredRegistries<T>(registry, modId);
return new DeferredRegistries<T>(modId).registry(registry);
}

public static <T> DeferredRegistries<T> create(RegistryKey<Registry<? super T>> registryKey, String modId) {
return new DeferredRegistries<T>(modId).registryKey(registryKey);
}

public DeferredObject<T> register(String name, T entry) {
Expand All @@ -37,7 +54,11 @@ public DeferredObject<T> register(String name, Supplier<T> entry) {

public void register() {
for (DeferredObject<T> entry : entries) {
entry.register(this.registry);
if (this.registry != null) {
entry.register(this.registry);
} else if (this.registryKey != null) {
entry.register(this.registryKey);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.dm.earth.deferred_registries.helper;

import net.minecraft.util.Identifier;
import net.minecraft.util.registry.BuiltinRegistries;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.biome.Biome;

public class BiomeRegistryHelper {
public static <T> void registerBiome(Biome biome, Identifier id) {
RegistryKey<Biome> biomeKey = RegistryKey.of(Registry.BIOME_KEY, id);
BuiltinRegistries.add(BuiltinRegistries.BIOME, biomeKey, biome);
}

public static boolean isBiomeKey(RegistryKey<?> registryKey) {
return registryKey.equals(Registry.BIOME_KEY);
}
}

0 comments on commit cb23588

Please sign in to comment.