Skip to content

Commit

Permalink
move Registrar to Registries
Browse files Browse the repository at this point in the history
  • Loading branch information
ToCraft committed Nov 14, 2024
1 parent f6acbac commit 8f24c9d
Show file tree
Hide file tree
Showing 19 changed files with 110 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ public void renderWidget(@NotNull GuiGraphics context, int mouseX, int mouseY, f
updateZoomedHeight();

// cut widget
context.flush();
final double scaleFactor = minecraft.getWindow().getGuiScale();
RenderSystem.enableScissor((int) (getX() * scaleFactor), (int) (getY() * scaleFactor), (int) (width * scaleFactor), (int) (height * scaleFactor));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.tocraft.ctgen.platform;

import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import org.jetbrains.annotations.ApiStatus;

import java.util.ServiceLoader;

@ApiStatus.Internal
public interface PlatformService {
PlatformService INSTANCE = ServiceLoader.load(PlatformService.class).findFirst().orElseThrow(() -> new RuntimeException("Couldn't initialize CTGen Platform Service!"));

<T> Registry<T> createSimpleRegistry(ResourceKey<Registry<T>> key);
}
2 changes: 1 addition & 1 deletion common/src/main/java/dev/tocraft/ctgen/util/Codecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public final class Codecs {
Codec.INT.fieldOf("blue").forGetter(i -> i & 0xFF)
).apply(instance, instance.stable((r, g, b) -> (0xFF << 24) |
((r & 0xFF) << 16) |
((g & 0xFF) << 8) |
((g & 0xFF) << 8) |
((b & 0xFF)))));

public static final Codec<Integer> COLOR_HEX = Codec.STRING.xmap(s -> ((0xFF) << 24) |
Expand Down
49 changes: 0 additions & 49 deletions common/src/main/java/dev/tocraft/ctgen/util/Registrar.java

This file was deleted.

15 changes: 10 additions & 5 deletions common/src/main/java/dev/tocraft/ctgen/xtend/CTRegistries.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package dev.tocraft.ctgen.xtend;

import com.mojang.serialization.MapCodec;
import dev.tocraft.ctgen.util.Registrar;
import dev.tocraft.ctgen.CTerrainGeneration;
import dev.tocraft.ctgen.platform.PlatformService;
import dev.tocraft.ctgen.xtend.carver.Carver;
import dev.tocraft.ctgen.xtend.height.TerrainHeight;
import dev.tocraft.ctgen.xtend.layer.BlockLayer;
Expand All @@ -13,9 +14,13 @@

public final class CTRegistries {
public static final ResourceKey<Registry<Zone>> ZONES_KEY = ResourceKey.createRegistryKey(ResourceLocation.parse("worldgen/map_based/zones"));
public static final ResourceKey<Registry<MapCodec<? extends BlockPlacer>>> BLOCK_PLACER_KEY = ResourceKey.createRegistryKey(CTerrainGeneration.id("block_placer"));
public static final ResourceKey<Registry<MapCodec<? extends BlockLayer>>> BLOCK_LAYER_KEY = ResourceKey.createRegistryKey(CTerrainGeneration.id("block_layer"));
public static final ResourceKey<Registry<MapCodec<? extends TerrainHeight>>> TERRAIN_KEY = ResourceKey.createRegistryKey(CTerrainGeneration.id("terrain"));
public static final ResourceKey<Registry<MapCodec<? extends Carver>>> CARVER_KEY = ResourceKey.createRegistryKey(CTerrainGeneration.id("carver"));

public static final Registrar<MapCodec<? extends BlockPlacer>> BLOCK_PLACER = new Registrar<>();
public static final Registrar<MapCodec<? extends BlockLayer>> BLOCK_LAYER = new Registrar<>();
public static final Registrar<MapCodec<? extends TerrainHeight>> TERRAIN = new Registrar<>();
public static final Registrar<MapCodec<? extends Carver>> CARVER = new Registrar<>();
public static final Registry<MapCodec<? extends BlockPlacer>> BLOCK_PLACER = PlatformService.INSTANCE.createSimpleRegistry(BLOCK_PLACER_KEY);
public static final Registry<MapCodec<? extends BlockLayer>> BLOCK_LAYER = PlatformService.INSTANCE.createSimpleRegistry(BLOCK_LAYER_KEY);
public static final Registry<MapCodec<? extends TerrainHeight>> TERRAIN = PlatformService.INSTANCE.createSimpleRegistry(TERRAIN_KEY);
public static final Registry<MapCodec<? extends Carver>> CARVER = PlatformService.INSTANCE.createSimpleRegistry(CARVER_KEY);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
import com.mojang.serialization.MapCodec;
import dev.tocraft.ctgen.xtend.CTRegistries;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import java.util.function.BiConsumer;
import java.util.function.Function;

public abstract class Carver {
public static final Codec<Carver> CODEC = CTRegistries.CARVER.byNameCodec().dispatchStable(Carver::codec, Function.identity());

public static void register() {
CTRegistries.CARVER.register(NoiseCarver.ID, NoiseCarver.CODEC);
@ApiStatus.Internal
public static void register(@NotNull BiConsumer<ResourceLocation, MapCodec<? extends Carver>> registerFunc) {
registerFunc.accept(NoiseCarver.ID, NoiseCarver.CODEC);
}

public abstract boolean canSetBlock(SimplexNoise noise, @NotNull BlockPos pos, double surfaceHeight, int minHeight, double carverModifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
import com.mojang.serialization.MapCodec;
import dev.tocraft.ctgen.worldgen.MapSettings;
import dev.tocraft.ctgen.xtend.CTRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import java.util.function.BiConsumer;
import java.util.function.Function;

public abstract class TerrainHeight {
public static final Codec<TerrainHeight> CODEC = CTRegistries.TERRAIN.byNameCodec().dispatchStable(TerrainHeight::codec, Function.identity());

public static void register() {
CTRegistries.TERRAIN.register(BasicHeight.ID, BasicHeight.CODEC);
CTRegistries.TERRAIN.register(NoiseHeight.ID, NoiseHeight.CODEC);
@ApiStatus.Internal
public static void register(@NotNull BiConsumer<ResourceLocation, MapCodec<? extends TerrainHeight>> registerFunc) {
registerFunc.accept(BasicHeight.ID, BasicHeight.CODEC);
registerFunc.accept(NoiseHeight.ID, NoiseHeight.CODEC);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
import dev.tocraft.ctgen.xtend.placer.BasicPlacer;
import dev.tocraft.ctgen.xtend.placer.BlockPlacer;
import dev.tocraft.ctgen.zone.Zone;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;

import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;

public abstract class BlockLayer {
Expand All @@ -26,10 +29,11 @@ public BlockLayer(String name, boolean hasCaves, BlockPlacer fallback) {
this.fallback = fallback;
}

public static void register() {
CTRegistries.BLOCK_LAYER.register(SeaLayer.ID, SeaLayer.CODEC);
CTRegistries.BLOCK_LAYER.register(HeightLayer.ID, HeightLayer.CODEC);
CTRegistries.BLOCK_LAYER.register(WeightLayer.ID, WeightLayer.CODEC);
@ApiStatus.Internal
public static void register(@NotNull BiConsumer<ResourceLocation, MapCodec<? extends BlockLayer>> registerFunc) {
registerFunc.accept(SeaLayer.ID, SeaLayer.CODEC);
registerFunc.accept(HeightLayer.ID, HeightLayer.CODEC);
registerFunc.accept(WeightLayer.ID, WeightLayer.CODEC);
}

@Contract("_ -> new")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import com.mojang.serialization.MapCodec;
import dev.tocraft.ctgen.util.Codecs;
import dev.tocraft.ctgen.xtend.CTRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Function;

public abstract class BlockPlacer {
Expand All @@ -29,9 +32,10 @@ public abstract class BlockPlacer {
}
});

public static void register() {
CTRegistries.BLOCK_PLACER.register(BasicPlacer.ID, BasicPlacer.CODEC);
CTRegistries.BLOCK_PLACER.register(NoisePlacer.ID, NoisePlacer.CODEC);
@ApiStatus.Internal
public static void register(@NotNull BiConsumer<ResourceLocation, MapCodec<? extends BlockPlacer>> registerFunc) {
registerFunc.accept(BasicPlacer.ID, BasicPlacer.CODEC);
registerFunc.accept(NoisePlacer.ID, NoisePlacer.CODEC);
}

@NotNull
Expand Down
9 changes: 5 additions & 4 deletions fabric/src/main/java/dev/tocraft/ctgen/fabric/CTGFabric.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.tocraft.ctgen.impl.network.SyncMapPacket;
import dev.tocraft.ctgen.worldgen.MapBasedBiomeSource;
import dev.tocraft.ctgen.worldgen.MapBasedChunkGenerator;
import dev.tocraft.ctgen.xtend.CTRegistries;
import dev.tocraft.ctgen.xtend.carver.Carver;
import dev.tocraft.ctgen.xtend.height.TerrainHeight;
import dev.tocraft.ctgen.xtend.layer.BlockLayer;
Expand Down Expand Up @@ -58,10 +59,10 @@ public ResourceLocation getFabricId() {
CommandRegistrationCallback.EVENT.register((dispatcher, context, environment) -> CTGCommand.register(dispatcher, context));

// register built-in registry entries
BlockPlacer.register();
BlockLayer.register();
TerrainHeight.register();
Carver.register();
BlockPlacer.register((id, codec) -> Registry.register(CTRegistries.BLOCK_PLACER, id, codec));
BlockLayer.register((id, codec) -> Registry.register(CTRegistries.BLOCK_LAYER, id, codec));
TerrainHeight.register((id, codec) -> Registry.register(CTRegistries.TERRAIN, id, codec));
Carver.register((id, codec) -> Registry.register(CTRegistries.CARVER, id, codec));

// register network packet type
PayloadTypeRegistry.playS2C().register(SyncMapPacket.TYPE, SyncMapPacket.streamCodec());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.tocraft.ctgen.fabric.service;

import dev.tocraft.ctgen.platform.PlatformService;
import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;

public class FabricPlatformService implements PlatformService {
@Override
public <T> Registry<T> createSimpleRegistry(ResourceKey<Registry<T>> key) {
return FabricRegistryBuilder.createSimple(key).buildAndRegister();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dev.tocraft.ctgen.fabric.service.FabricPlatformService
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ org.gradle.jvmargs=-Xmx4G
# Base Versions
archives_base_name=ctgen
artifact_type=release
mod_version=0.26
mod_version=0.28
maven_group=dev.tocraft
main_class=dev.tocraft.ctgen.runtime.Main
# Loader Versions
Expand Down
10 changes: 0 additions & 10 deletions neoforge/src/main/java/dev/tocraft/ctgen/neoforge/CTGNeoForge.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package dev.tocraft.ctgen.neoforge;

import dev.tocraft.ctgen.CTerrainGeneration;
import dev.tocraft.ctgen.xtend.carver.Carver;
import dev.tocraft.ctgen.xtend.height.TerrainHeight;
import dev.tocraft.ctgen.xtend.layer.BlockLayer;
import dev.tocraft.ctgen.xtend.placer.BlockPlacer;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.ModList;
Expand All @@ -26,11 +22,5 @@ public CTGNeoForge() {
if (FMLEnvironment.dist == Dist.CLIENT) {
CTGNeoForgeClientEventListener.initialize(modEventBus);
}

// values for the built-in registries
BlockPlacer.register();
BlockLayer.register();
TerrainHeight.register();
Carver.register();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
import dev.tocraft.ctgen.impl.network.SyncMapPacket;
import dev.tocraft.ctgen.worldgen.MapBasedBiomeSource;
import dev.tocraft.ctgen.worldgen.MapBasedChunkGenerator;
import dev.tocraft.ctgen.xtend.CTRegistries;
import dev.tocraft.ctgen.xtend.carver.Carver;
import dev.tocraft.ctgen.xtend.height.TerrainHeight;
import dev.tocraft.ctgen.xtend.layer.BlockLayer;
import dev.tocraft.ctgen.xtend.placer.BlockPlacer;
import net.minecraft.core.registries.Registries;
import net.minecraft.server.packs.resources.PreparableReloadListener;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.AddReloadListenerEvent;
import net.neoforged.neoforge.event.RegisterCommandsEvent;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent;
import net.neoforged.neoforge.registries.NewRegistryEvent;
import net.neoforged.neoforge.registries.RegisterEvent;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
Expand All @@ -26,6 +32,7 @@ public final class CTGNeoForgeEventListener {
public static void initialize(@NotNull IEventBus modEventBus) {
NeoForge.EVENT_BUS.addListener(CTGNeoForgeEventListener::addReloadListenerEvent);
NeoForge.EVENT_BUS.addListener(CTGNeoForgeEventListener::registerCommands);
modEventBus.addListener(CTGNeoForgeEventListener::registerRegistries);
modEventBus.addListener(CTGNeoForgeEventListener::register);
modEventBus.addListener(CTGNeoForgeEventListener::registerPayload);
}
Expand All @@ -46,10 +53,23 @@ private static void registerCommands(@NotNull RegisterCommandsEvent event) {
CTGCommand.register(event.getDispatcher(), event.getBuildContext());
}

private static void registerRegistries(@NotNull NewRegistryEvent event) {
event.register(CTRegistries.BLOCK_PLACER);
event.register(CTRegistries.BLOCK_LAYER);
event.register(CTRegistries.TERRAIN);
event.register(CTRegistries.CARVER);
}

private static void register(@NotNull RegisterEvent event) {
// generic stuff
event.register(Registries.BIOME_SOURCE, helper -> helper.register(MapBasedBiomeSource.ID, MapBasedBiomeSource.CODEC));
event.register(Registries.CHUNK_GENERATOR, helper -> helper.register(MapBasedChunkGenerator.ID, MapBasedChunkGenerator.CODEC));

// custom stuff
event.register(CTRegistries.BLOCK_PLACER_KEY, helper -> BlockPlacer.register(helper::register));
event.register(CTRegistries.BLOCK_LAYER_KEY, helper -> BlockLayer.register(helper::register));
event.register(CTRegistries.TERRAIN_KEY, helper -> TerrainHeight.register(helper::register));
event.register(CTRegistries.CARVER_KEY, helper -> Carver.register(helper::register));
}

private static void registerPayload(@NotNull RegisterPayloadHandlersEvent event) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.tocraft.ctgen.neoforge.service;

import dev.tocraft.ctgen.platform.PlatformService;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.neoforged.neoforge.registries.RegistryBuilder;

public class NeoForgePlatformService implements PlatformService {
@Override
public <T> Registry<T> createSimpleRegistry(ResourceKey<Registry<T>> key) {
return new RegistryBuilder<>(key).create();
}
}
2 changes: 1 addition & 1 deletion neoforge/src/main/resources/META-INF/neoforge.mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ logoFile = "pack.png"

[[dependencies.ctgen]]
modId = "minecraft"
type="required"
type = "required"
versionRange = "[${minecraft},)"
ordering = "NONE"
side = "BOTH"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dev.tocraft.ctgen.neoforge.service.NeoForgePlatformService
3 changes: 0 additions & 3 deletions props/1.21.3.properties
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
# PreProcessor
mc_id=1213

# JDK
java=21

# Mappings
#mappings=2024.07.28

# Loader
fabric=0.107.0
neoforge=21.3.3-beta

0 comments on commit 8f24c9d

Please sign in to comment.