Skip to content

Commit

Permalink
Fix datagen crashes when running game test server
Browse files Browse the repository at this point in the history
Removes all model gen helpers from `DeferredBlockBuilder` and `DeferredItemBuilder` apart from `withDefaultWhiteModel`

Helpers refactored into proper `addClientProvider` calls within each test
  • Loading branch information
ApexModder committed Dec 11, 2024
1 parent a81ae8d commit dced55d
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import com.mojang.serialization.MapCodec;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Stream;
import net.minecraft.client.color.item.ItemTintSource;
Expand All @@ -18,7 +17,6 @@
import net.minecraft.client.data.models.model.ModelTemplates;
import net.minecraft.client.data.models.model.TextureMapping;
import net.minecraft.client.data.models.model.TextureSlot;
import net.minecraft.client.data.models.model.TexturedModel;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
Expand Down Expand Up @@ -66,11 +64,34 @@ public DeferredBlockBuilder<T> withLang(String name) {
private boolean hasItem = false;
private boolean hasColor = false;

public DeferredBlockBuilder<T> withModel(BiConsumer<T, BlockModelGenerators> consumer) {
helper.addClientProvider(client -> new ModelProvider(client.getGenerator().getPackOutput(), helper.modId()) {
public DeferredBlockBuilder<T> withDefaultWhiteModel() {
if (!FMLLoader.getDist().isClient()) {
return this;
}

helper.addClientProvider(event -> event.addProvider(new ModelProvider(event.getGenerator().getPackOutput(), helper.modId()) {
@Override
protected void registerModels(BlockModelGenerators blockModels, ItemModelGenerators itemModels) {
consumer.accept(value(), blockModels);
ModelTemplate template;

if (hasColor) {
template = ExtendedModelTemplateBuilder.builder()
.element(element -> element
.from(0F, 0F, 0F)
.to(16F, 16F, 16F)
.allFaces((face, builder) -> builder
.uvs(0F, 0F, 16F, 16F)
.texture(TextureSlot.ALL)
.tintindex(0)
.cullface(face)))
.requiredTextureSlot(TextureSlot.ALL)
.build();
} else {
template = ModelTemplates.CUBE_ALL;
}

var modelPath = template.create(value(), TextureMapping.cube(ResourceLocation.fromNamespaceAndPath("testframework", "block/white")), blockModels.modelOutput);
blockModels.blockStateOutput.accept(BlockModelGenerators.createSimpleBlock(value(), modelPath));
}

@Override
Expand All @@ -85,46 +106,12 @@ protected Stream<? extends Holder<Block>> getKnownBlocks() {

@Override
public String getName() {
return key.location().getPath() + "-model-generator";
return key.location().toDebugFileName() + "-default-white-model-generator";
}
});
}));
return this;
}

public DeferredBlockBuilder<T> withModel(TexturedModel.Provider model) {
return withModel((block, blockModels) -> blockModels.createTrivialBlock(block, model));
}

public DeferredBlockBuilder<T> withModel(TextureMapping textures, Consumer<ExtendedModelTemplateBuilder> modelConsumer) {
return withModel((block, blockModels) -> {
var builder = ExtendedModelTemplateBuilder.builder();
modelConsumer.accept(builder);
var modelPath = builder.build().create(block, textures, blockModels.modelOutput);
blockModels.blockStateOutput.accept(BlockModelGenerators.createSimpleBlock(block, modelPath));
});
}

public DeferredBlockBuilder<T> withDefaultWhiteModel() {
return withModel((block, blockModels) -> {
ModelTemplate template;

if (hasColor) {
template = ExtendedModelTemplateBuilder.builder()
.element(element -> element
.from(0, 0, 0)
.to(16, 16, 16)
.allFaces((direction, faceBuilder) -> faceBuilder.uvs(0, 0, 16, 16).texture(TextureSlot.ALL).tintindex(0).cullface(direction)))
.requiredTextureSlot(TextureSlot.ALL)
.build();
} else {
template = ModelTemplates.CUBE_ALL;
}

var modelPath = template.create(block, TextureMapping.cube(ResourceLocation.fromNamespaceAndPath("testframework", "block/white")), blockModels.modelOutput);
blockModels.blockStateOutput.accept(BlockModelGenerators.createSimpleBlock(block, modelPath));
});
}

public DeferredBlockBuilder<T> withColor(int color) {
if (FMLLoader.getDist().isClient()) {
colorInternal(color);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,9 @@

package net.neoforged.testframework.registration;

import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Stream;
import net.minecraft.client.data.models.BlockModelGenerators;
import net.minecraft.client.data.models.ItemModelGenerators;
import net.minecraft.client.data.models.ModelProvider;
import net.minecraft.client.data.models.model.ItemModelUtils;
import net.minecraft.client.data.models.model.ModelLocationUtils;
import net.minecraft.client.data.models.model.ModelTemplate;
import net.minecraft.client.data.models.model.TextureMapping;
import net.minecraft.core.Holder;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.neoforged.neoforge.client.model.generators.template.ExtendedModelTemplateBuilder;
import net.neoforged.neoforge.common.data.LanguageProvider;
import net.neoforged.neoforge.event.BuildCreativeModeTabContentsEvent;
import net.neoforged.neoforge.registries.DeferredItem;
Expand All @@ -46,43 +33,4 @@ public DeferredItemBuilder<I> tab(ResourceKey<CreativeModeTab> tab) {
});
return this;
}

public DeferredItemBuilder<I> withModel(BiConsumer<I, ItemModelGenerators> consumer) {
registrationHelper.addClientProvider(client -> new ModelProvider(client.getGenerator().getPackOutput(), registrationHelper.modId()) {
@Override
protected void registerModels(BlockModelGenerators blockModels, ItemModelGenerators itemModels) {
consumer.accept(value(), itemModels);
}

@Override
protected Stream<? extends Holder<Item>> getKnownItems() {
return Stream.of(DeferredItemBuilder.this);
}

@Override
protected Stream<? extends Holder<Block>> getKnownBlocks() {
return Stream.empty();
}

@Override
public String getName() {
return key.location().getPath() + "-model-generator";
}
});
return this;
}

public DeferredItemBuilder<I> withModel(ModelTemplate template) {
return withModel((item, itemModels) -> itemModels.generateFlatItem(item, template));
}

public DeferredItemBuilder<I> withModel(TextureMapping textures, Consumer<ExtendedModelTemplateBuilder> modelConsumer) {
return withModel((item, itemModels) -> {
var modelPath = ModelLocationUtils.getModelLocation(item);
var builder = ExtendedModelTemplateBuilder.builder();
modelConsumer.accept(builder);
itemModels.itemModelOutput.accept(item, ItemModelUtils.plainModel(modelPath));
builder.build().create(item, textures, itemModels.modelOutput);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
import net.minecraft.world.item.equipment.Equippable;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.testframework.DynamicTest;
import net.neoforged.testframework.annotation.ForEachTest;
import net.neoforged.testframework.annotation.TestHolder;
import net.neoforged.testframework.registration.RegistrationHelper;

@ForEachTest(groups = "vanilla_data_gen")
@ForEachTest(groups = "vanilla_data_gen", side = Dist.CLIENT)
public interface VanillaDataGenTest {
@TestHolder(description = "Tests the the patched vanilla model generators work for modded usages")
static void testModelGenerators(DynamicTest test, RegistrationHelper reg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
package net.neoforged.neoforge.debug.block;

import java.util.Optional;
import java.util.stream.Stream;
import net.minecraft.client.data.models.BlockModelGenerators;
import net.minecraft.client.data.models.ItemModelGenerators;
import net.minecraft.client.data.models.ModelProvider;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
import net.minecraft.gametest.framework.GameTest;
import net.minecraft.network.protocol.game.ClientboundSoundPacket;
Expand All @@ -22,6 +27,7 @@
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.GameType;
Expand All @@ -36,6 +42,7 @@
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.neoforged.neoforge.common.enums.BubbleColumnDirection;
import net.neoforged.neoforge.registries.DeferredItem;
import net.neoforged.testframework.DynamicTest;
import net.neoforged.testframework.annotation.ForEachTest;
import net.neoforged.testframework.annotation.TestHolder;
Expand Down Expand Up @@ -79,8 +86,29 @@ static void decoratedPotBreaking(final DynamicTest test) {
static void woodlessFenceGate(final DynamicTest test, final RegistrationHelper reg) {
final var gate = reg.blocks().registerBlock("gate", props -> new FenceGateBlock(props, SoundEvents.BARREL_OPEN, SoundEvents.CHEST_CLOSE), BlockBehaviour.Properties.ofFullCopy(Blocks.ACACIA_FENCE_GATE))
.withLang("Woodless Fence Gate")
.withBlockItem()
.withModel((block, blockModels) -> blockModels.createFenceGate(block, Blocks.IRON_BLOCK));
.withBlockItem();

reg.addClientProvider(event -> event.addProvider(new ModelProvider(event.getGenerator().getPackOutput(), reg.modId()) {
@Override
protected void registerModels(BlockModelGenerators blockModels, ItemModelGenerators itemModels) {
blockModels.createFenceGate(gate.value(), Blocks.IRON_BLOCK);
}

@Override
protected Stream<? extends Holder<Item>> getKnownItems() {
return Stream.of(DeferredItem.createItem(gate.getId()));
}

@Override
protected Stream<? extends Holder<Block>> getKnownBlocks() {
return Stream.of(gate);
}

@Override
public String getName() {
return "test_woodless_fence_gate_model_generator";
}
}));

test.onGameTest(helper -> helper.startSequence(() -> helper.makeTickingMockServerPlayerInCorner(GameType.SURVIVAL))
.thenExecute(() -> helper.setBlock(1, 1, 1, gate.get().defaultBlockState().setValue(FenceGateBlock.OPEN, true)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@

package net.neoforged.neoforge.debug.fluid;

import java.util.stream.Stream;
import net.minecraft.client.data.models.BlockModelGenerators;
import net.minecraft.client.data.models.ItemModelGenerators;
import net.minecraft.client.data.models.ModelProvider;
import net.minecraft.client.data.models.model.TextureMapping;
import net.minecraft.client.data.models.model.TextureSlot;
import net.minecraft.client.data.models.model.TexturedModel;
import net.minecraft.client.renderer.block.LiquidBlockRenderer;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Holder;
import net.minecraft.gametest.framework.GameTest;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.TransparentBlock;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.neoforge.registries.DeferredItem;
import net.neoforged.testframework.DynamicTest;
import net.neoforged.testframework.annotation.ForEachTest;
import net.neoforged.testframework.annotation.TestHolder;
Expand Down Expand Up @@ -52,13 +60,34 @@ public boolean shouldHideAdjacentFluidFace(BlockState state, Direction selfFace,
static void testWaterGlassFaceRemoval(final DynamicTest test, final RegistrationHelper reg) {
final var glass = reg.blocks().registerBlock("water_glass", WaterGlassBlock::new, BlockBehaviour.Properties.ofFullCopy(Blocks.GLASS))
.withLang("Water Glass")
.withBlockItem()
.withModel((block, blockModels) -> blockModels
.createTrivialBlock(block, TexturedModel.CUBE
.updateTemplate(modelTemplate -> modelTemplate.extend()
.renderType("cutout")
.build())
.updateTexture(textures -> textures.put(TextureSlot.ALL, TextureMapping.getBlockTexture(Blocks.GLASS)))));
.withBlockItem();

reg.addClientProvider(event -> event.addProvider(new ModelProvider(event.getGenerator().getPackOutput(), reg.modId()) {
@Override
protected void registerModels(BlockModelGenerators blockModels, ItemModelGenerators itemModels) {
blockModels.createTrivialBlock(glass.value(), TexturedModel.CUBE
.updateTemplate(modelTemplate -> modelTemplate
.extend()
.renderType("cutout")
.build())
.updateTexture(textures -> textures.put(TextureSlot.ALL, TextureMapping.getBlockTexture(Blocks.GLASS))));
}

@Override
protected Stream<? extends Holder<Item>> getKnownItems() {
return Stream.of(DeferredItem.createItem(glass.getId()));
}

@Override
protected Stream<? extends Holder<Block>> getKnownBlocks() {
return Stream.of(glass);
}

@Override
public String getName() {
return "test_water_glass_face_removal_model_generator";
}
}));

final var waterPosition = new BlockPos(1, 1, 2);
final var glassDirection = WaterGlassBlock.HIDE_DIRECTION.getOpposite();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
import java.util.EnumMap;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;
import net.minecraft.ChatFormatting;
import net.minecraft.Util;
import net.minecraft.client.data.models.BlockModelGenerators;
import net.minecraft.client.data.models.ItemModelGenerators;
import net.minecraft.client.data.models.ModelProvider;
import net.minecraft.client.renderer.entity.PigRenderer;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Holder;
import net.minecraft.core.dispenser.BlockSource;
import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
import net.minecraft.gametest.framework.GameTest;
Expand Down Expand Up @@ -46,6 +51,7 @@
import net.minecraft.world.item.equipment.ArmorType;
import net.minecraft.world.item.equipment.EquipmentAssets;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.DispenserBlock;
import net.minecraft.world.level.block.entity.DispenserBlockEntity;
Expand Down Expand Up @@ -139,8 +145,29 @@ public InteractionResult use(Level level, Player player, InteractionHand hand) {
return sup;
}
})
.tab(CreativeModeTabs.SPAWN_EGGS)
.withModel((item, itemModels) -> itemModels.generateSpawnEgg(item, 0xFFFFFF, 0xFFFFFF));
.tab(CreativeModeTabs.SPAWN_EGGS);

reg.addClientProvider(event -> event.addProvider(new ModelProvider(event.getGenerator().getPackOutput(), reg.modId()) {
@Override
protected void registerModels(BlockModelGenerators blockModels, ItemModelGenerators itemModels) {
itemModels.generateSpawnEgg(egg.value(), 0xFFFFFF, 0xFFFFFF);
}

@Override
protected Stream<? extends Holder<Item>> getKnownItems() {
return Stream.of(egg);
}

@Override
protected Stream<? extends Holder<Block>> getKnownBlocks() {
return Stream.empty();
}

@Override
public String getName() {
return "forge_spawn_egg_test_model_generator";
}
}));

test.onGameTest(helper -> helper.startSequence()
.thenExecute(() -> helper.setBlock(1, 1, 1, Blocks.IRON_BLOCK))
Expand Down

0 comments on commit dced55d

Please sign in to comment.