Jump to content

[1.15.2] Structure Gen - Filling air/water blocks underneath structure + other questions


Recommended Posts

Posted (edited)

Hey all,

 

I'm currently playing with structure generation in MC 1.15.2 and have largely gotten everything working. My custom structures generate and I can control how they're placed which is cool. However, there are some quirks I'd like to fix but I'm struggling to find answers.

 

The big one is when the structure overhangs surface elevation changes or water (so there are air or water blocks below part of the structure. I've got a method that checks chunk elevation and will only spawn if the elevation is changes within certain bounds, but this still allows for air or water below parts of the structure (unless I tell it to only spawn on a completely flat chunk, which limits spawn possibilities on larger structures). What I would like to do is to fill any air or water blocks underneath the custom structure with a solid block, which I've found code for within some of the vanilla structures, however I cannot get these functions to work within my custom structure piece.

 

When I implement them, they appeared to do nothing, outputting to the console it appears all blocks in the vertical column are air and if I try to replace an air block with sandstone, the generator hangs.

 

public class TestStructurePieces
{
    private static final Logger LOGGER = LogManager.getLogger();

    private static String pathIn = "test_structure";
    private static String pieceLocation = "test/test_structure";
    private static IStructurePieceType pieceStructureIn = CeStructurePieces.TEST_STRUCTURE;
    private static IWorld iWorldIn;
    private static int size = 3;

    public static void register()
    {


        JigsawManager.REGISTRY.register(new JigsawPattern(
                new ResourceLocation(
                        CE.MODID,
                        pathIn
                ),
                new ResourceLocation("empty"),
                ImmutableList.of(
                                Pair.of(new SingleJigsawPiece(CE.MODID + ":" + pieceLocation), 1)
                                ),
                JigsawPattern.PlacementBehaviour.RIGID)
        );




    }

    public static void create(ChunkGenerator<?> generator, TemplateManager templateManager, BlockPos position, List<StructurePiece> pieces, SharedSeedRandom random) {

        // Set the world from chunk generator (not sure how else to get world from here)
        iWorldIn = ObfuscationReflectionHelper.getPrivateValue(ChunkGenerator.class, generator, "field_222540_a");

        LOGGER.info("Adding Pieces: " + position);
       
        JigsawManager.addPieces(new ResourceLocation(CE.MODID, pathIn), 7, Piece::new, generator, templateManager, position, pieces, random);
    }




    public static class Piece extends AbstractVillagePiece {
    
        public Piece(TemplateManager templateManager, JigsawPiece jigsawPiece, BlockPos position, int groundLevelDelta, Rotation rotation, MutableBoundingBox bounds) {

            super(pieceStructureIn, templateManager, jigsawPiece, position, groundLevelDelta, rotation, bounds);

                      
            // Fill underside
          	int yStart = 100;
          	int xPos = position.getX();            
            int zPos = position.getZ();            
          
          	int xPosMax = xPos + bounds.getXSize();
            int zPosMax = zPos + bounds.getZSize();
		
            // Tried various ways of getting yStart here, the bounding box returns 0, I fixed to 100 for testing
          
            for(int x = xPos; x < xPosMax; ++x) {
                for(int z = zPos; z < zPosMax; ++z) {
                    //int k = -5;
                    this.replaceAirAndLiquidDownwards(iWorldIn, Blocks.SANDSTONE.getDefaultState(), x,  yStart, z, bounds);
                }
            }


        }

        public Piece(TemplateManager templateManager, CompoundNBT compoundNBT) {
            super(templateManager, compoundNBT, pieceStructureIn);
        }


        /**
         * Replaces air and liquid from given position downwards. Stops when hitting anything else than air or liquid
         */
        protected void replaceAirAndLiquidDownwards(IWorld worldIn, BlockState blockstateIn, int x, int y, int z, MutableBoundingBox boundingboxIn)
        {            
           int i = this.getXWithOffset(x, z);
           int j = this.getYWithOffset(y);
      		int k = this.getZWithOffset(x, z);
          
          // If I output data to console here, I get 4 blocks inside the bounding box (the structure is 7 high), and everything is air. It appears like the blocks arent set yet???
      		if (boundingboxIn.isVecInside(new BlockPos(i, j, k))) {
         while((worldIn.isAirBlock(new BlockPos(i, j, k)) || worldIn.getBlockState(new BlockPos(i, j, k)).getMaterial().isLiquid()) && j > 1) {
            worldIn.setBlockState(new BlockPos(i, j, k), blockstateIn, 2);
            --j;
         }

      }
        }
     }
  }
}

 

 

Anyone have any idea whats going on and how to get this working?

 

 

My other question is, is it possible to have a structure generate partially underground, for example a house with a basement?

 

 

 

Edited by FizixRichard
Posted

I thought I'd add the full source.

 

Registries:

 

SetupWorldGen

public class setupWorldGen
{
    private static final Logger LOGGER = LogManager.getLogger();


    public static void setupWorldGen()
    {
        for (Biome biome : ForgeRegistries.BIOMES)
        {
            // Add structures ignoring Oceans
            if (!biome.getCategory() == Biome.Category.OCEAN)
            {
                addSurfaceStructure(biome, CeStructures.TEST_STRUCTURE);
            }
        }
    }




    private static void addSurfaceStructure(Biome biome, Structure<NoFeatureConfig> structure) {
        biome.addStructure(structure.withConfiguration(IFeatureConfig.NO_FEATURE_CONFIG));
        biome.addFeature(GenerationStage.Decoration.SURFACE_STRUCTURES, structure.withConfiguration(IFeatureConfig.NO_FEATURE_CONFIG).withPlacement(Placement.NOPE.configure(IPlacementConfig.NO_PLACEMENT_CONFIG)));
    }


}

 

 

CEStructures

public class CeStructures
{
    private static final Logger LOGGER = LogManager.getLogger();


    // Declare structures
    public static final Structure<NoFeatureConfig> TEST_STRUCTURE = create(TestStructure.SHORT_NAME, new TestStructure(NoFeatureConfig::deserialize));


    @SubscribeEvent
    public static void createStructures(RegistryEvent.Register<Feature<?>> event)
    {
        // Register Structure Pieces
        CeStructurePieces.registerPieces();


        // Register Structures
        IForgeRegistry<Feature<?>> registry = event.getRegistry();

        registry.register(TEST_STRUCTURE);

    }


    private static @Nonnull
    <T extends Feature<?>> T create(String name, T feature) {
        feature.setRegistryName(CE.MODID, name.toLowerCase(Locale.ROOT));
        return feature;
    }


}

 

 

CeStructurePieces

public class CeStructurePieces
{

    private static final Logger LOGGER = LogManager.getLogger();


    // Declare Structure Pieces
    public static final IStructurePieceType TEST_STRUCTURE = TestStructurePieces.Piece::new;



    public static void registerPieces()
    {
        // Register The Jigsaw Pieces
        TestStructurePieces.register();



        // Register Structure Pieces
        register(TestStructure.SHORT_NAME, TEST_STRUCTURE);

    }


    @SuppressWarnings("UnusedReturnValue")
    private static @Nonnull
    IStructurePieceType register(String name, IStructurePieceType type) {
        return Registry.register(Registry.STRUCTURE_PIECE, new ResourceLocation(CE.MODID, name.toLowerCase(Locale.ROOT)), type);
    }


}

 

 

Structure and Structure Piece Classes:

 

TestStructure

public class TestStructure extends AbstractStructure
{
    private static final Logger LOGGER = LogManager.getLogger();

    // Properties
    public static final String SHORT_NAME = "test_structure";
    public static final String LONG_NAME = CE.MODID + ":" + SHORT_NAME;
    private static final int SEED_MODIFIER = 165745296;
    private static final int STRUCTURE_SIZE = 3;
    private static final int STRUCTURE_DISTANCE = 10;
    private static final int STRUCTURE_SEPARATION = 5;
    private static final int HEIGHT_DELTA = 3;
    private static final double SPAWN_CHANCE = 0.7;



    public TestStructure(Function<Dynamic<?>, ? extends NoFeatureConfig> configFactoryIn) {
        super(configFactoryIn, SHORT_NAME, STRUCTURE_SIZE, HEIGHT_DELTA);        
    }


    @Override
    protected int getSeedModifier() {
        return SEED_MODIFIER;
    }

    @Override
    public @Nonnull
    String getStructureName() {
        return LONG_NAME;
    }

    @Override
    public int getSize() {
        return STRUCTURE_SIZE;
    }

    @Override
    public int getHeightDelta() {
        return HEIGHT_DELTA;
    }

    @Override
    protected int getFeatureDistance(ChunkGenerator<?> generator) { return STRUCTURE_DISTANCE; }

    @Override
    protected int getFeatureSeparation(ChunkGenerator<?> generator) { return STRUCTURE_SEPARATION; }

    @Override
    protected double getSpawnChance() { return SPAWN_CHANCE; }



    @Override
    public @Nonnull
    IStartFactory getStartFactory() {
        return Start::new;
    }


    public static class Start extends MarginedStructureStart {

        public Start(Structure<?> structure, int chunkX, int chunkZ, MutableBoundingBox bounds, int reference, long seed) {
            super(structure, chunkX, chunkZ, bounds, reference, seed);
        }

        @Override
        public void init(@Nonnull ChunkGenerator<?> chunkGenerator, @Nonnull TemplateManager templateManager, int chunkX, int chunkZ, @Nonnull Biome biome) {
            Rotation rotation = Rotation.values()[this.rand.nextInt(Rotation.values().length)];
            BlockPos position = getSurfaceStructurePosition(chunkGenerator, 3, rotation, chunkX, chunkZ);

            
            TestStructurePieces.create(chunkGenerator, templateManager, position, this.components, this.rand);

            this.recalculateStructureSize();
        }
      
      
      public BlockPos getSurfaceStructurePosition(@Nonnull ChunkGenerator generator, int size, Rotation rotation, int chunkX, int chunkZ) {
            int xOffset = size * 16;
            int zOffset = size * 16;

            int x = (chunkX << 4);
            int z = (chunkZ << 4);

           
            BlockPos adjustedPos = new BlockPos(x, 0, z);

            return adjustedPos;            
        }
    }

}

 

AbstractStruture

public abstract class AbstractStructure extends ScatteredStructure<NoFeatureConfig> {
    private static final Logger LOGGER = LogManager.getLogger();
  
    private final String name;
    private final int size;
    private final int heightdelta;


    public AbstractStructure(Function<Dynamic<?>, ? extends NoFeatureConfig> configFactoryIn, String name, int size, int heightdelta) {
        super(configFactoryIn);
        this.name = name;
        this.size = size;
        this.heightdelta = heightdelta;
    }

    protected abstract double getSpawnChance();

    @Override
    public int getSize() {
        return this.size;
    }


    public int getHeightDelta() {
        return this.heightdelta;
    }

    @Override
    protected int getBiomeFeatureDistance(ChunkGenerator<?> chunkGenerator) {
        return getFeatureDistance(chunkGenerator);
    }

    protected abstract int getFeatureDistance(ChunkGenerator<?> chunkGenerator);

    @Override
    protected int getBiomeFeatureSeparation(ChunkGenerator<?> chunkGenerator) {
        return getFeatureSeparation(chunkGenerator);
    }

    protected abstract int getFeatureSeparation(ChunkGenerator<?> chunkGenerator);


    @Override
    @Nonnull
    public String getStructureName() {
        return new ResourceLocation(CE.MODID, name).toString();
    }


    @Override
    public boolean canBeGenerated(BiomeManager biomeManager, @Nonnull ChunkGenerator<?> chunkGenerator, @Nonnull Random randIn, int chunkX, int chunkZ, @Nonnull Biome biomeIn)
    {

        /* original
        ChunkPos chunkpos = this.getStartPositionForPosition(generatorIn, randIn, chunkX, chunkZ, 0, 0);
      return chunkX == chunkpos.x && chunkZ == chunkpos.z && generatorIn.hasStructure(biomeIn, this);
         */
        

        ChunkPos chunkpos = getStartPositionForPosition(chunkGenerator, randIn, chunkX, chunkZ, 0, 0);

        
        if (chunkX == chunkpos.x && chunkZ == chunkpos.z)
        {
            // Is the surface where we want to generate flat enough
            if (surfaceCheck(chunkGenerator, chunkX, chunkZ))
            {

                
                for (int x = chunkX - getSize(); x <= chunkX + getSize(); x++) {
                    for (int z = chunkZ - getSize(); z <= chunkZ + getSize(); z++)
                    {
                        if (!chunkGenerator.hasStructure(biomeIn, this)) {

                            return false;
                        }
                    }
                }
              

                // Check how far from villages we are            
                for (int x2 = chunkX - 5; x2 <= chunkX + 5; ++x2) {
                    for (int z2 = chunkZ - 5; z2 <= chunkZ + 5; ++z2) {
                        BlockPos position = new BlockPos((x2 << 4) + 9, 0, (z2 << 4) + 9);
                        if (Feature.VILLAGE.canBeGenerated(biomeManager, chunkGenerator, randIn, x2, z2, biomeManager.getBiome(position))) {
                            
                            return false;
                        }
                    }
                }

                // Apply spawn chance               
                int x3 = chunkX >> 4;
                int z3 = chunkZ >> 4;
                randIn.setSeed((long) (x3 ^ z3 << 4) ^ chunkGenerator.getSeed());
                randIn.nextInt();

                Double randVal = randIn.nextDouble();
                boolean chanceCheck = randVal < getSpawnChance();               

                return chanceCheck;

            }
        }

        return false;
    }



    @Override
    @Nonnull
    protected ChunkPos getStartPositionForPosition(ChunkGenerator<?> chunkGenerator, Random random, int x, int z, int spacingOffsetsX, int spacingOffsetsZ)
    {
        
        int featureDistance = this.getBiomeFeatureDistance(chunkGenerator);
        int featureSeparation = this.getBiomeFeatureSeparation(chunkGenerator);
        int xPos = x + featureDistance * spacingOffsetsX;
        int zPos = z + featureDistance * spacingOffsetsZ;
        int i1 = xPos < 0 ? xPos - featureDistance + 1 : xPos;
        int j1 = zPos < 0 ? zPos - featureDistance + 1 : zPos;
        int validChunkX = i1 / featureDistance;
        int validChunkZ = j1 / featureDistance;

        ((SharedSeedRandom) random).setLargeFeatureSeedWithSalt(chunkGenerator.getSeed(), validChunkX, validChunkZ, getSeedModifier());

        validChunkX = validChunkX * featureDistance;
        validChunkZ = validChunkZ * featureDistance;
        validChunkX = validChunkX + random.nextInt(featureDistance - featureSeparation);
        validChunkZ = validChunkZ + random.nextInt(featureDistance - featureSeparation);
        return new ChunkPos(validChunkX, validChunkZ);
    }




    protected boolean surfaceCheck(@Nonnull ChunkGenerator<?> chunkGenerator, int chunkX, int chunkZ)
    {      

        int offset = getSize() * 16;
        int xStart = (chunkX << 4);
        int zStart = (chunkZ << 4);

        
        int c1 = chunkGenerator.func_222531_c(xStart, zStart, Heightmap.Type.WORLD_SURFACE_WG);
        int c2 = chunkGenerator.func_222531_c(xStart, zStart + offset, Heightmap.Type.WORLD_SURFACE_WG);
        int c3 = chunkGenerator.func_222531_c(xStart + offset, zStart, Heightmap.Type.WORLD_SURFACE_WG);
        int c4 = chunkGenerator.func_222531_c(xStart + offset, zStart + offset, Heightmap.Type.WORLD_SURFACE_WG);


        int lowerHeight = Math.min(Math.min(c1, c2), Math.min(c3, c4));
        int upperHeight = Math.max(Math.max(c1, c2), Math.max(c3, c4));

        //LOGGER.info("Area Height Range: " + lowerHeight + " to " + upperHeight);

        boolean isFlat = Math.abs(upperHeight - lowerHeight) <= getHeightDelta();      

        return isFlat;
    }
}

 

 

TestStructurePiece

public class TestStructurePieces
{
    private static final Logger LOGGER = LogManager.getLogger();

    private static String pathIn = "test_structure";
    private static String pieceLocation = "test/test_structure";
    private static IStructurePieceType pieceStructureIn = CeStructurePieces.TEST_STRUCTURE;
    private static IWorld iWorldIn;
    private static int size = 3;

    public static void register()
    {


        JigsawManager.REGISTRY.register(new JigsawPattern(
                new ResourceLocation(
                        CE.MODID,
                        pathIn
                ),
                new ResourceLocation("empty"),
                ImmutableList.of(
                                Pair.of(new SingleJigsawPiece(CE.MODID + ":" + pieceLocation), 1)
                                ),
                JigsawPattern.PlacementBehaviour.RIGID)
        );




    }

    public static void create(ChunkGenerator<?> generator, TemplateManager templateManager, BlockPos position, List<StructurePiece> pieces, SharedSeedRandom random) {

        // Set the world from chunk generator (not sure how else to get world from here)
        iWorldIn = ObfuscationReflectionHelper.getPrivateValue(ChunkGenerator.class, generator, "field_222540_a");

        LOGGER.info("Adding Pieces: " + position);
       
        JigsawManager.addPieces(new ResourceLocation(CE.MODID, pathIn), 7, Piece::new, generator, templateManager, position, pieces, random);
    }




    public static class Piece extends AbstractVillagePiece {
    
        public Piece(TemplateManager templateManager, JigsawPiece jigsawPiece, BlockPos position, int groundLevelDelta, Rotation rotation, MutableBoundingBox bounds) {

            super(pieceStructureIn, templateManager, jigsawPiece, position, groundLevelDelta, rotation, bounds);

                      
            // Fill underside
          	int yStart = 100;
          	int xPos = position.getX();            
            int zPos = position.getZ();            
          
          	int xPosMax = xPos + bounds.getXSize();
            int zPosMax = zPos + bounds.getZSize();
		
            // Tried various ways of getting yStart here, the bounding box returns 0, I fixed to 100 for testing
          
            for(int x = xPos; x < xPosMax; ++x) {
                for(int z = zPos; z < zPosMax; ++z) {
                    //int k = -5;
                    this.replaceAirAndLiquidDownwards(iWorldIn, Blocks.SANDSTONE.getDefaultState(), x,  yStart, z, bounds);
                }
            }


        }

        public Piece(TemplateManager templateManager, CompoundNBT compoundNBT) {
            super(templateManager, compoundNBT, pieceStructureIn);
        }


        /**
         * Replaces air and liquid from given position downwards. Stops when hitting anything else than air or liquid
         */
        protected void replaceAirAndLiquidDownwards(IWorld worldIn, BlockState blockstateIn, int x, int y, int z, MutableBoundingBox boundingboxIn)
        {            
           int i = this.getXWithOffset(x, z);
           int j = this.getYWithOffset(y);
      		int k = this.getZWithOffset(x, z);
          
          // If I output data to console here, I get 4 blocks inside the bounding box (the structure is 7 high), and everything is air. It appears like the blocks arent set yet???
      		if (boundingboxIn.isVecInside(new BlockPos(i, j, k))) {
         while((worldIn.isAirBlock(new BlockPos(i, j, k)) || worldIn.getBlockState(new BlockPos(i, j, k)).getMaterial().isLiquid()) && j > 1) {
            worldIn.setBlockState(new BlockPos(i, j, k), blockstateIn, 2);
            --j;
         }

      }
        }
     }
  }
}

 

 

 

 

 

Posted

Figured this out and I thought I'd update this in case anyone else is trying to figure structure generation stuff out.

 

 

public class TestStructurePieces
{
    private static final Logger LOGGER = LogManager.getLogger();

    private static String pathIn = "test_structure";
    private static String pieceLocation = "test/test_structure";
    private static IStructurePieceType pieceStructureIn = CeStructurePieces.TEST_STRUCTURE;


    public static void register()
    {
        JigsawManager.REGISTRY.register(new JigsawPattern(
                new ResourceLocation(
                        Ce.MODID,
                        pathIn
                ),
                new ResourceLocation("empty"),
                ImmutableList.of(
                                Pair.of(new SingleJigsawPiece(Ce.MODID + ":" + pieceLocation), 1)                         
                                ),
                JigsawPattern.PlacementBehaviour.RIGID)
        );
    }

    public static void create(ChunkGenerator<?> generator, TemplateManager templateManager, BlockPos position, List<StructurePiece> pieces, SharedSeedRandom random) {

        JigsawManager.addPieces(new ResourceLocation(Ce.MODID, pathIn), 7, Piece::new, generator, templateManager, position, pieces, random);
    }




    public static class Piece extends AbstractVillagePiece
    {
        private TemplateManager templateManager;

        public Piece(TemplateManager templateManager, JigsawPiece jigsawPiece, BlockPos position, int groundLevelDelta, Rotation rotation, MutableBoundingBox bounds) {

            super(pieceStructureIn, templateManager, jigsawPiece, position, groundLevelDelta, rotation, bounds);
            this.templateManager = templateManager;
        }

        public Piece(TemplateManager templateManager, CompoundNBT compoundNBT) {
            super(templateManager, compoundNBT, pieceStructureIn);

            this.templateManager = templateManager;
        }


        @Override
        public boolean create(IWorld worldIn, ChunkGenerator<?> chunkGeneratorIn, Random randomIn, MutableBoundingBox mutableBoundingBoxIn, ChunkPos chunkPosIn) {

            BlockPos position = this.getPos();

            

            // Foundation Fill Process
            for(int x = this.boundingBox.minX; x < this.boundingBox.maxX; ++x) {
                for(int z = this.boundingBox.minZ; z < this.boundingBox.maxZ; ++z) {
                    this.replaceAirAndLiquidDownwards(worldIn, Blocks.SANDSTONE.getDefaultState(), x, position.getY() - 1, z, mutableBoundingBoxIn);
                }
            }

            return this.jigsawPiece.place(this.templateManager, worldIn, chunkGeneratorIn, this.pos, this.rotation, mutableBoundingBoxIn, randomIn);
        }


        protected void replaceAirAndLiquidDownwards(IWorld worldIn, BlockState blockstateIn, int x, int y, int z, MutableBoundingBox boundingboxIn) {

            int j = y;

            if (boundingboxIn.isVecInside(new BlockPos(x, j, z)))
            {
                while((worldIn.isAirBlock(new BlockPos(x, j, z)) || worldIn.getBlockState(new BlockPos(x, j, z)).getMaterial().isLiquid()) && j > 1) {
                    worldIn.setBlockState(new BlockPos(x, j, z), blockstateIn, 2);
                    --j;
                }
           }
        }

    }
}

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • You probably used jd-gui to open it, didn't you? Nothing wrong with that, I also made that mistake, except that Notch was a smart guy and he obfuscated the code. That's why you only see files called "a", "b", "c" and then a file that combines them all. As I said, use RetroMCP to deobfuscate the code so that you will 100% understand it and be able to navigate it.
    • Decompiling minecraft indev, infdev, alpha, beta or whichever legacy version is really easy. I'm not a plug, I just also got interested in modding legacy versions (Infdev to be specific). Use https://github.com/MCPHackers/RetroMCP-Java Once you install their client and the Zulu Architecture that they say they recommend (or use your own Java). I encountered some problems, so I run it with: "java -jar RetroMCP-Java-CLI.jar". You should run it in a seperate folder (not in downloads), otherwise the files and folders will go all over the place. How to use RetroMCP: Type setup (every time you want change version), copy-paste the version number from their list (they support indev), write "decompile" and done! The code will now be deobfuscated and filenames will be normal, instead of "a", "b" and "c"! Hope I helped you, but I don't expect you to reply, as this discussion is 9 years old! What a piece of history!  
    • I know that this may be a basic question, but I am very new to modding. I am trying to have it so that I can create modified Vanilla loot tables that use a custom enchantment as a condition (i.e. enchantment present = item). However, I am having trouble trying to implement this; the LootItemRandomChanceWithEnchantedBonusCondition constructor needs a Holder<Enchantment> and I am unable to use the getOrThrow() method on the custom enchantment declared in my mod's enchantments class. Here is what I have so far in the GLM:   protected void start(HolderLookup.Provider registries) { HolderLookup.RegistryLookup<Enchantment> registrylookup = registries.lookupOrThrow(Registries.ENCHANTMENT); LootItemRandomChanceWithEnchantedBonusCondition lootItemRandomChanceWithEnchantedBonusCondition = new LootItemRandomChanceWithEnchantedBonusCondition(0.0f, LevelBasedValue.perLevel(0.07f), registrylookup.getOrThrow(*enchantment here*)); this.add("nebu_from_deepslate", new AddItemModifier(new LootItemCondition[]{ LootItemBlockStatePropertyCondition.hasBlockStateProperties(Blocks.DEEPSLATE).build(), LootItemRandomChanceCondition.randomChance(0.25f).build(), lootItemRandomChanceWithEnchantedBonusCondition }, OrichalcumItems.NEBU.get())); }   Inserting Enchantments.[vanilla enchantment here] actually works but trying to declare an enchantment from my custom enchantments class as [mod enchantment class].[custom enchantment] does not work even though they are both a ResourceKey and are registered in Registries.ENCHANTMENT. Basically, how would I go about making it so that a custom enchantment declared as a ResourceKey<Enchantment> of value ResourceKey.create(Registries.ENCHANTMENT, ResourceLocation.fromNamespaceAndPath([modid], [name])), declared in a seperate enchantments class, can be used in the LootItemRandomChanceWithEnchantedBonusCondition constructor as a Holder? I can't use getOrThrow() because there is no level or block entity/entity in the start() method and it is running as datagen. It's driving me nuts.
    • Hi here is an update. I was able to fix the code so my mod does not crash Minecraft. Please understand that I am new to modding but I honestly am having a hard time understanding how anyone can get this to work without having extensive programming and debugging experience as well as searching across the Internet, multiple gen AI bots (claude, grok, openai), and examining source code hidden in the gradle directory and in various github repositories. I guess I am wrong because clearly there are thousands of mods so maybe I am just a newbie. Ok, rant over, here is a step by step summary so others can save the 3 days it took me to figure this out.   1. First, I am using forge 54.1.0 and Minecraft 1.21.4 2. I am creating a mod to add a shotgun to Minecraft 3. After creating the mod and compiling it, I installed the .jar file to the proper directory in Minecraft and used 1.21.4-forge-54.1.0 4. The mod immediately crashed with the error: Caused by: java.lang.NullPointerException: Item id not set 5. Using the stack trace, I determined that the Exception was being thrown from the net.minecraft.world.item.Item.Properties class 6. It seems that there are no javadocs for this class, so I used IntelliJ which was able to provide a decompiled version of the class, which I then examined to see the source of the error. Side question: Are there javadocs? 7. This method, specifically, was the culprit: protected String effectiveDescriptionId() {      return this.descriptionId.get(Objects.requireNonNull(this.id, "Item id not set"));  } 8. Now my quest was to determine how to set this.id. Looking at the same source file, I determined there was another method:  public Item.Properties setId(ResourceKey<Item> pId) {             this.id = pId;             return this;   } 9. So now, I need to figure out how to call setId(). This required working backwards a bit. Starting from the constructor, I stubbed out the variable p which is of type Item.Properties public static final RegistryObject<Item> SHOTGUN = ITEMS.register("shotgun", () -> new ShotgunItem(p)); Rather than putting this all on one line, I split it up for readability like this: private static final Item.Properties p = new Item.Properties().useItemDescriptionPrefix().setId(rk); Here is was the missing function, setId(), which takes a type of ResourceKey<Item>. My next problem is that due to the apparent lack of documentation (I tried searching the docs on this site) I could not determine the full import path to ResourceKey. I did some random searching on the Internet and stumbled across a Github repository which gave two clues: import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; Then I created the rk variable like this: private static ResourceKey<Item> rk = ResourceKey.create(Registries.ITEM, ResourceLocation.parse("modid:shotgunmod")); And now putting it all together in order: private static ResourceKey<Item> rk = ResourceKey.create(Registries.ITEM, ResourceLocation.parse("modid:shotgunmod")); private static final Item.Properties p = new Item.Properties().useItemDescriptionPrefix().setId(rk); public static final RegistryObject<Item> SHOTGUN = ITEMS.register("shotgun", () -> new ShotgunItem(p)); This compiled and the mod no longer crashes. I still have more to do on it, but hopefully this will save someone hours. I welcome any feedback and if I missed some obvious modding resource or tutorial that has this information. If not, I might suggest we add it somewhere for people trying to write a mod that doesn't crash. Thank you !!!  
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.