Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.latvian.mods.rhino.util.HideFromJS;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
Expand All @@ -36,7 +37,7 @@ public GTLayerPattern(List<Layer> layers) {

public @Nullable Layer rollNext(@Nullable Layer previous, RandomSource random) {
if (layers.isEmpty()) return null;
if (layers.size() == 1) return layers.get(0);
if (layers.size() == 1) return layers.getFirst();

int totalWeight = 0;
for (Layer layer : layers) {
Expand All @@ -56,6 +57,11 @@ public GTLayerPattern(List<Layer> layers) {
return null;
}

public static Builder builder(IWorldGenLayer layer) {
return builder(layer.getTarget());
}

@HideFromJS
public static Builder builder(RuleTest... rules) {
return new Builder(rules);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.gregtechceu.gtceu.api.worldgen.generator.VeinGenerator;
import com.gregtechceu.gtceu.api.worldgen.generator.indicators.SurfaceIndicatorGenerator;
import com.gregtechceu.gtceu.api.worldgen.generator.veins.*;
import com.gregtechceu.gtceu.utils.codec.CodecUtils;

import net.minecraft.core.Holder;
import net.minecraft.core.HolderGetter;
Expand All @@ -32,6 +33,7 @@
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.experimental.ExtensionMethod;
import lombok.experimental.Tolerate;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
Expand All @@ -41,6 +43,7 @@
import java.util.function.Supplier;

@SuppressWarnings("UnusedReturnValue")
@ExtensionMethod(CodecUtils.class)
@Accessors(chain = true, fluent = true)
public class OreVeinDefinition {

Expand All @@ -53,8 +56,8 @@ public class OreVeinDefinition {
ResourceKey.codec(Registries.DIMENSION).listOf().fieldOf("dimension_filter").forGetter(ft -> new ArrayList<>(ft.dimensionFilter)),
HeightRangePlacement.CODEC.fieldOf("height_range").forGetter(OreVeinDefinition::heightRange),
Codec.floatRange(0.0F, 1.0F).fieldOf("discard_chance_on_air_exposure").forGetter(OreVeinDefinition::discardChanceOnAirExposure),
RegistryCodecs.homogeneousList(Registries.BIOME).lenientOptionalFieldOf("biomes", HolderSet.empty()).forGetter(OreVeinDefinition::biomes),
BiomeWeightModifier.CODEC.optionalFieldOf("weight_modifier", BiomeWeightModifier.EMPTY).forGetter(ext -> ext.biomeWeightModifier),
RegistryCodecs.homogeneousList(Registries.BIOME).lenientNullableOptionalFieldOf("biomes", HolderSet.empty()).forGetter(OreVeinDefinition::biomes),
BiomeWeightModifier.CODEC.lenientNullableOptionalFieldOf("weight_modifier", BiomeWeightModifier.EMPTY).forGetter(ext -> ext.biomeWeightModifier),
VeinGenerator.DIRECT_CODEC.fieldOf("generator").forGetter(ft -> ft.veinGenerator),
Codec.list(IndicatorGenerator.DIRECT_CODEC).fieldOf("indicators").forGetter(ft -> ft.indicatorGenerators)
).apply(instance, OreVeinDefinition::new));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.gregtechceu.gtceu.api.material.material.Material;
import com.gregtechceu.gtceu.api.registry.GTRegistries;
import com.gregtechceu.gtceu.api.worldgen.BiomeWeightModifier;
import com.gregtechceu.gtceu.utils.codec.CodecUtils;

import net.minecraft.core.Holder;
import net.minecraft.core.HolderGetter;
Expand All @@ -24,10 +25,12 @@
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.experimental.ExtensionMethod;
import lombok.experimental.Tolerate;

import java.util.*;

@ExtensionMethod(CodecUtils.class)
@Accessors(fluent = true, chain = true)
public class BedrockOreDefinition {

Expand All @@ -40,9 +43,9 @@ public class BedrockOreDefinition {
ExtraCodecs.intRange(0, 100).fieldOf("depletion_chance").forGetter(BedrockOreDefinition::depletionChance),
Codec.INT.fieldOf("depleted_yield").forGetter(BedrockOreDefinition::depletedYield),
WeightedMaterial.CODEC.listOf().fieldOf("materials").forGetter(BedrockOreDefinition::materials),
BiomeWeightModifier.CODEC.optionalFieldOf("weight_modifier", BiomeWeightModifier.EMPTY).forGetter(BedrockOreDefinition::biomeWeightModifier),
BiomeWeightModifier.CODEC.lenientNullableOptionalFieldOf("weight_modifier", BiomeWeightModifier.EMPTY).forGetter(BedrockOreDefinition::biomeWeightModifier),
ResourceKey.codec(Registries.DIMENSION).listOf().fieldOf("dimension_filter").forGetter(ft -> new ArrayList<>(ft.dimensionFilter))
).apply(instance, BedrockOreDefinition::new));
).apply(instance, BedrockOreDefinition::new));
public static final Codec<Holder<BedrockOreDefinition>> CODEC = RegistryFixedCodec.create(GTRegistries.BEDROCK_ORE_REGISTRY);
// spotless:on
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ public class VeinGenerators {
public static final MapCodec<CuboidVeinGenerator> CUBOID = register(GTCEu.id("cuboid"), CuboidVeinGenerator.CODEC,
CuboidVeinGenerator::new);

public static <
T extends VeinGenerator> MapCodec<T> register(ResourceLocation id, MapCodec<T> codec,
Supplier<? extends VeinGenerator> function) {
public static <T extends VeinGenerator> MapCodec<T> register(ResourceLocation id, MapCodec<T> codec,
Supplier<T> function) {
WorldGeneratorUtils.VEIN_GENERATORS.put(id, codec);
WorldGeneratorUtils.VEIN_GENERATOR_FUNCTIONS.put(id, function);
return codec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.gregtechceu.gtceu.api.material.ChemicalHelper;
import com.gregtechceu.gtceu.api.material.material.Material;
import com.gregtechceu.gtceu.api.worldgen.GTLayerPattern;
import com.gregtechceu.gtceu.api.worldgen.IWorldGenLayer;
import com.gregtechceu.gtceu.api.worldgen.OreVeinDefinition;
import com.gregtechceu.gtceu.api.worldgen.generator.VeinGenerator;
import com.gregtechceu.gtceu.api.worldgen.ores.OreBlockPlacer;
Expand All @@ -19,20 +20,23 @@
import net.minecraft.world.level.chunk.LevelChunkSection;
import net.minecraft.world.level.levelgen.XoroshiroRandomSource;
import net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration;
import net.minecraft.world.level.levelgen.structure.templatesystem.RuleTest;

import com.mojang.datafixers.util.Either;
import com.mojang.serialization.MapCodec;
import com.tterrag.registrate.util.nullness.NonNullSupplier;
import dev.latvian.mods.rhino.util.HideFromJS;
import it.unimi.dsi.fastutil.floats.FloatArrayList;
import it.unimi.dsi.fastutil.floats.FloatList;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

@SuppressWarnings("UnusedReturnValue")
@NoArgsConstructor
Expand All @@ -42,16 +46,8 @@ public class LayeredVeinGenerator extends VeinGenerator {
public static final MapCodec<LayeredVeinGenerator> CODEC = GTLayerPattern.CODEC.listOf().fieldOf("layer_patterns")
.xmap(LayeredVeinGenerator::new, LayeredVeinGenerator::getLayerPatterns);
// spotless:on
private final List<NonNullSupplier<GTLayerPattern>> bakingLayerPatterns = new ArrayList<>();

public List<GTLayerPattern> layerPatterns;

public List<GTLayerPattern> getLayerPatterns() {
if (layerPatterns == null || this.layerPatterns.isEmpty()) {
layerPatterns = bakingLayerPatterns.stream().map(Supplier::get).collect(Collectors.toList());
}
return layerPatterns;
}
@Getter
public List<GTLayerPattern> layerPatterns = new ArrayList<>();

@Override
public List<VeinEntry> getAllEntries() {
Expand Down Expand Up @@ -204,21 +200,37 @@ private static void placeBlock(BulkSectionAccess access, LevelChunkSection secti
}
}

public LayeredVeinGenerator(List<GTLayerPattern> layerPatterns) {
super();
public LayeredVeinGenerator(@NotNull List<GTLayerPattern> layerPatterns) {
this.layerPatterns = layerPatterns;
}

public LayeredVeinGenerator withLayerPattern(NonNullSupplier<GTLayerPattern> pattern) {
this.bakingLayerPatterns.add(pattern);
public LayeredVeinGenerator buildLayerPattern(IWorldGenLayer layer, Consumer<GTLayerPattern.Builder> config) {
return buildLayerPattern(layer.getTarget(), config);
}

@HideFromJS
public LayeredVeinGenerator buildLayerPattern(RuleTest rule, Consumer<GTLayerPattern.Builder> config) {
var builder = GTLayerPattern.builder(rule);
config.accept(builder);

return withLayerPattern(builder);
}

public LayeredVeinGenerator withLayerPattern(Supplier<GTLayerPattern> pattern) {
return this.withLayerPattern(pattern.get());
}

public LayeredVeinGenerator withLayerPattern(GTLayerPattern pattern) {
this.layerPatterns.add(pattern);
return this;
}

public LayeredVeinGenerator withLayerPattern(GTLayerPattern.Builder builder) {
return this.withLayerPattern(builder.build());
}

@Override
public VeinGenerator build() {
if (this.layerPatterns != null && !this.layerPatterns.isEmpty()) return this;
this.layerPatterns = this.bakingLayerPatterns.stream()
.map(NonNullSupplier::get)
.toList();
return this;
}

Expand Down
56 changes: 38 additions & 18 deletions src/main/java/com/gregtechceu/gtceu/core/MixinHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import com.gregtechceu.gtceu.api.registry.GTRegistries;
import com.gregtechceu.gtceu.api.registry.registrate.forge.GTClientFluidTypeExtensions;
import com.gregtechceu.gtceu.api.tag.TagPrefix;
import com.gregtechceu.gtceu.api.worldgen.OreVeinDefinition;
import com.gregtechceu.gtceu.api.worldgen.bedrockfluid.BedrockFluidDefinition;
import com.gregtechceu.gtceu.api.worldgen.bedrockore.BedrockOreDefinition;
import com.gregtechceu.gtceu.config.ConfigHolder;
import com.gregtechceu.gtceu.core.mixins.BlockBehaviourAccessor;
import com.gregtechceu.gtceu.data.block.GTMaterialBlocks;
Expand All @@ -33,6 +36,7 @@
import net.minecraft.core.registries.Registries;
import net.minecraft.data.loot.packs.VanillaBlockLoot;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.RegistryDataLoader;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.*;
Expand Down Expand Up @@ -391,18 +395,24 @@ public static void addMaterialBlockLootTables(TriConsumer<ResourceLocation, Loot
});
}

public static void postKJSVeinEvents(WritableRegistry<?> registry) {
public static void updateCachedRegistryAndPostKJSVeinEvents(RegistryAccess staticRegistries,
List<RegistryDataLoader.Loader<?>> loaders) {
// create a temporary registry access instance from static registries + new dynamic registries
RegistryAccess registries = createRegistryContext(staticRegistries, loaders);
GTRegistries.updateFrozenRegistry(registries);

if (!GTCEu.Mods.isKubeJSLoaded()) {
return;
}
KJSCallWrapper.postOreEvents(loaders, registries);
}

if (registry.key() == GTRegistries.ORE_VEIN_REGISTRY) {
KJSCallWrapper.postOreVeinEvent();
} else if (registry.key() == GTRegistries.BEDROCK_FLUID_REGISTRY) {
KJSCallWrapper.postBedrockFluidEvent();
} else if (registry.key() == GTRegistries.BEDROCK_ORE_REGISTRY) {
KJSCallWrapper.postBedrockOreEvent();
}
private static RegistryAccess createRegistryContext(RegistryAccess staticRegistries,
List<RegistryDataLoader.Loader<?>> registryLoaders) {
final Map<ResourceKey<? extends Registry<?>>, Registry<?>> map = new HashMap<>();
staticRegistries.registries().forEach(entry -> map.put(entry.key(), entry.value()));
registryLoaders.forEach(loader -> map.put(loader.registry().key(), loader.registry()));
return new RegistryAccess.ImmutableRegistryAccess(map);
}

public static void addFluidTexture(Material material, FluidStorage.FluidEntry value) {
Expand All @@ -417,16 +427,26 @@ public static void addFluidTexture(Material material, FluidStorage.FluidEntry va

private static final class KJSCallWrapper {

private static void postOreVeinEvent() {
GTCEuServerEvents.ORE_VEIN_MODIFICATION.post(new GTOreVeinKubeEvent());
}

private static void postBedrockFluidEvent() {
GTCEuServerEvents.FLUID_VEIN_MODIFICATION.post(new GTBedrockFluidVeinKubeEvent());
}

private static void postBedrockOreEvent() {
GTCEuServerEvents.BEDROCK_ORE_VEIN_MODIFICATION.post(new GTBedrockOreVeinKubeEvent());
@SuppressWarnings("unchecked")
private static void postOreEvents(List<RegistryDataLoader.Loader<?>> dynamicRegistryLoaders,
RegistryAccess registries) {
for (RegistryDataLoader.Loader<?> loader : dynamicRegistryLoaders) {
switch (loader.registry().key()) {
case ResourceKey<?> key when key ==
GTRegistries.ORE_VEIN_REGISTRY -> GTCEuServerEvents.ORE_VEIN_MODIFICATION.post(
new GTOreVeinKubeEvent((WritableRegistry<OreVeinDefinition>) loader.registry(),
registries));
case ResourceKey<?> key when key ==
GTRegistries.BEDROCK_FLUID_REGISTRY -> GTCEuServerEvents.ORE_VEIN_MODIFICATION
.post(new GTBedrockFluidVeinKubeEvent(
(WritableRegistry<BedrockFluidDefinition>) loader.registry(), registries));
case ResourceKey<?> key when key ==
GTRegistries.BEDROCK_ORE_REGISTRY -> GTCEuServerEvents.ORE_VEIN_MODIFICATION
.post(new GTBedrockOreVeinKubeEvent(
(WritableRegistry<BedrockOreDefinition>) loader.registry(), registries));
default -> {}
}
}
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.gregtechceu.gtceu.core.mixins;

import com.gregtechceu.gtceu.core.MixinHelpers;

import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.RegistryDataLoader;

import com.llamalad7.mixinextras.sugar.Local;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.List;

@Mixin(RegistryDataLoader.class)
public class RegistryDataLoaderMixin {

/// this is called after data load and before registry freeze
@Inject(method = "load(Lnet/minecraft/resources/RegistryDataLoader$LoadingFunction;Lnet/minecraft/core/RegistryAccess;Ljava/util/List;)Lnet/minecraft/core/RegistryAccess$Frozen;",
at = @At(value = "INVOKE",
target = "Ljava/util/List;forEach(Ljava/util/function/Consumer;)V",
ordinal = 1,
remap = false))
private static void gtceu$postKJSVeinEvents(RegistryDataLoader.LoadingFunction loadingFunction,
RegistryAccess registryAccess,
List<RegistryDataLoader.RegistryData<?>> registryData,
CallbackInfoReturnable<RegistryAccess.Frozen> cir,
@Local(ordinal = 1) List<RegistryDataLoader.Loader<?>> loaders) {
MixinHelpers.updateCachedRegistryAndPostKJSVeinEvents(registryAccess, loaders);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,15 @@
import com.gregtechceu.gtceu.integration.kjs.builders.worldgen.BedrockOreBuilder;
import com.gregtechceu.gtceu.integration.kjs.builders.worldgen.DimensionMarkerBuilder;
import com.gregtechceu.gtceu.integration.kjs.builders.worldgen.OreVeinDefinitionBuilder;
import com.gregtechceu.gtceu.integration.kjs.helpers.GTResourceLocation;
import com.gregtechceu.gtceu.integration.kjs.helpers.MachineConstructors;
import com.gregtechceu.gtceu.integration.kjs.helpers.MachineModifiers;
import com.gregtechceu.gtceu.integration.kjs.helpers.MaterialStackWrapper;
import com.gregtechceu.gtceu.integration.kjs.helpers.*;
import com.gregtechceu.gtceu.integration.kjs.recipe.GTRecipeSchema;
import com.gregtechceu.gtceu.integration.kjs.recipe.GTShapedRecipeSchema;
import com.gregtechceu.gtceu.integration.kjs.recipe.KJSHelpers;
import com.gregtechceu.gtceu.integration.kjs.recipe.components.CapabilityMapComponent;
import com.gregtechceu.gtceu.integration.kjs.recipe.components.GTRecipeComponents;
import com.gregtechceu.gtceu.utils.data.RuntimeBlockStateProvider;

import net.minecraft.core.HolderSet;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
Expand Down Expand Up @@ -366,7 +364,9 @@ public void registerBindings(BindingRegistry event) {
event.add("GTOreVein", OreVeinDefinition.class);
event.add("OreVeinDefinition", OreVeinDefinition.class);
event.add("GTLayerPattern", GTLayerPattern.class);
event.add("LayerPattern", GTLayerPattern.class);
event.add("GTDikeBlockDefinition", DikeVeinGenerator.DikeBlockDefinition.class);
event.add("DikeBlockDefinition", DikeVeinGenerator.DikeBlockDefinition.class);
event.add("GTOres", GTOreVeins.class);
event.add("GTOreVeins", GTOreVeins.class);
event.add("GTWorldGenLayers", WorldGenLayers.class);
Expand Down Expand Up @@ -476,6 +476,8 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) {
if (o instanceof IWorldGenLayer.RuleTestSupplier supplier) return supplier;
return () -> BlockStatePredicate.wrapRuleTest(cx, o);
});
// register a conditional type wrapper for early parsing of holder sets
registry.register(HolderSet.class, SpecialHolderWrapper::canWrapHolderSet, SpecialHolderWrapper::wrapHolderSet);
registry.register(CraftingComponent.class, o -> {
if (o instanceof CraftingComponent comp) return comp;
if (o instanceof CharSequence str) return CraftingComponent.ALL_COMPONENTS.get(str.toString());
Expand Down
Loading
Loading