Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I'm playing with plants, and can't seem to get the rendering working. Does someone have a working example I can look at?

 

Thanks.

  • Author

If you can't figure it out from that, you have to post what you have tried, otherwise it's very difficult to guess what you are doing wrong.

Lol... Didn't expect you to. :) I find that if I figure it out on my own, I remember how to do things for longer. Vanilla is so big that, while I took a look at things inside it, I was unable to extrapolate (all of) what to do.

 

 

...So, here's some of my code: (I don't know what's failing, so I'm putting all of it here. Sorry. :P )

 

I'm creating a strawberry. I'm not working from seeds atm, and don't have growth implemented yet. That comes after I have something both placeable and visible.

 

public class BlockStrawberry extends BlockBush implements IGrowable
{
public static final PropertyInteger AGE = PropertyInteger.create("age", 0, 4);

  public BlockStrawberry()
  {
    super(Material.plants);
    //This line will cause another crash
    //this.setDefaultState(this.blockState.getBaseState().withProperty(AGE, Integer.valueOf(0)));
    this.setStepSound(soundTypeGrass);
    this.setCreativeTab(CreativeTabs.tabFood);
    float sizeConst = 0.5F;
    this.setBlockBounds(0.5F - sizeConst, 0.0F, 0.5F - sizeConst, 0.5F + sizeConst, 0.25F, 0.5F + sizeConst);
    this.setHardness(0.0F);
  }

  @Override
  public boolean isOpaqueCube() {
    return false;
  }
  @Override
  public int getRenderType() {
    return 6;
  }

@Override
public boolean canGrow(World worldIn, BlockPos pos, IBlockState state, boolean isClient) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, IBlockState state) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public void grow(World worldIn, Random rand, BlockPos pos, IBlockState state) {
	// TODO Auto-generated method stub

}

protected BlockState createBlockState()
    {
        return new BlockState(this, new IProperty[] {AGE});
    }
}

 

ATM, this code will crash the game:

java.lang.IllegalArgumentException: Don't know how to convert minecraftbyexample:strawberries[age=0] back into data...

at net.minecraft.block.Block.getMetaFromState(Block.java:225) ~[block.class:?]

 

I "solved" (appeased) it by commenting out the stuff about propertyInteger temporarily to get a placeable, visible block.

 

The code in StartupClientOnly:

final int DEFAULT_ITEM_SUBTYPE = 0;
Item itemStrawberries = GameRegistry.findItem("minecraftbyexample", "strawberries");
    ModelResourceLocation strawberriesModelResourceLocation = new ModelResourceLocation("minecraftbyexample:strawberries", "inventory");
    Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(itemStrawberries, DEFAULT_ITEM_SUBTYPE, strawberriesModelResourceLocation);

 

the code in StartupCommon

blockStrawberry = (BlockStrawberry)(new BlockStrawberry());
	GameRegistry.registerBlock(blockStrawberry, "strawberries");

 

 

...and lastly, the JSONs

 

(in models/item)

{

  "parent": "minecraftbyexample:block/strawberries_0",

  "display": {

    "thirdperson": {

      "rotation": [ 10, -45, 170 ],

      "translation": [ 0, 1.5, -2.75 ],

      "scale": [ 0.375, 0.375, 0.375 ]

    }

  }

}

 

(in models/block)

(name: strawberries)

{

    "parent": "block/crop",

    "textures": {

        "crop": "minecraftbyexample:blocks/strawberries-0"

    }

}

 

(name strawberries_0, strawberries_1, etc, up to 4)

{

    "parent": "block/crop",

    "textures": {

        "crop": "minecraftbyexample:blocks/strawberries-0"

    }

}

 

 

(in blockstates)

{

    "variants": {

        "age=0": { "model": "strawberries_0" },

        "age=1": { "model": "strawberries_1" },

        "age=2": { "model": "strawberries_2" },

        "age=3": { "model": "strawberries_3" },

        "age=4": { "model": "strawberries_4" }

    }

}

 

 

The current behavior: my plant renders in my inventory, and can be placed. I can see the little highlight box around it. The problem: it doesn't render in the world.

 

Additional questions: I really don't understand how the "switching models/textures by using metadata" thing works... So, once the rendering for the base plant is fixed, you can bet that'll be the next question...

 

 

...yes, I did start my mod by extending off of minecraftbyexample. It's wonderfully convenient, even if it does make me some sort of horrible parasite thing.

As CoolAlias says, your crash isn't really about plants but rather about how blocks in 1.8 work. You need to have a block property that represents growth stage and you need to convert that to and from metadata by overriding the appropriate methods.

 

In terms of plants generally, I have a tutorial but it is currently for 1.7.10 so you have to convert the block stuff to 1.8. I'll try to update the tutorial sometime, but it might at least give you some concepts that help you understand how to fully implement a plant: http://jabelarminecraft.blogspot.com/p/minecraft-forge-172-creating-custom.html

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

  • Author

Thanks for the help!

 

After some fiddling, working with the advice, and looking at those resources, I have the plant & it's seeds fully functional and spawning in swamps and snazzy.

 

(for those of you searching who find this in three months, there were stupid problems with my JSONs that I fixed, some readability problems with my PNGs, and the problems alias and jabelar mentioned.)

  • Author

The following code-dump comes with a warning: this works great in SP, but it may be causing crashes in MP. I haven't tested enough yet.

 

 

 

 

 

public class PlantGenerator extends WorldGenerator
{

@Override
public boolean generate(World worldIn, Random rand, BlockPos pos)
{
	BlockPos tempPos;
	BlockPos topPos;

	/*
	 * generate strawberries
	 */
	if(worldIn.getBiomeGenForCoords(pos) instanceof BiomeGenSwamp)
	{
		for(int i = 0; i < 1; i++)
		{
			if(rand.nextFloat() < .4f)
			{
				tempPos = pos;
				int x = rand.nextInt(16) - 8;
				int z = rand.nextInt(16) - 8;
				tempPos = tempPos.add(x, 0, z);
				int y = worldIn.getHorizon(tempPos).getY();
				topPos = tempPos.add(0, y, 0);

				if(StartupCommon.blockStrawberry.canPlaceBlockAt(worldIn, topPos));
				{
					worldIn.setBlockState(topPos, StartupCommon.blockStrawberry.getDefaultState());
					for(int ix = -1; ix <= 1; ix++){
						for(int iy = -1; iy <= 1; iy++){
							for(int iz = -1; iz <= 1; iz++){
								if(rand.nextFloat() < .4f)
								{
									tempPos = topPos.add(ix, iy, iz);
									if (Blocks.double_plant.canPlaceBlockAt(worldIn, tempPos))
									{
										worldIn.setBlockState(tempPos, StartupCommon.blockStrawberry.getDefaultState());
									}
								}
							}
						}
					}
				}
			}
		}
	}
	return false;
}

 

 

public class WldGen implements IWorldGenerator
{

@Override
public void generate(Random random, int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator, IChunkProvider chunkProvider)
{
	if(!world.isRemote)
	{
		BlockPos pos = new BlockPos(chunkX * 16, 0, chunkZ * 16);
                        //for my structures, not related to this mod
		//(new StructureGen()).generate(world, random, pos);
		(new PlantGenerator()).generate(world, random, pos);
	}

}

}

 

...and then in the mod's startup:

GameRegistry.registerWorldGenerator(new WldGen(), 1);

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.