Jump to content

[1.8] Plant example?


ulfgur

Recommended Posts

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.

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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