Jump to content

[SOLVED]Custom flowers - How to only be placed on custom blocks?


bl4ckscor3

Recommended Posts

Hello! I just added a new flower to my mod, and it works perfectly except for the thing, that it can only be played on dirt and grass. How can I change it, so that it can only be placed on 2 custom blocks added by the same mod?

 

Thanks in advance

~Minecrafter1912(My MC-Forums username - derp)

↑↑↑↑↑↑↑↑↑↑ Click it please? :3 ↑↑↑↑↑↑↑↑↑↑

Link to comment
Share on other sites

If you rewrite your flower like a custom rendered block, you can use ... I think ...

This function you can use:

public void onBlockPlacedBy(World par1World, int x, int y, int z, EntityLiving par5EntityLiving, ItemStack par6ItemStack) 

And in, you can check for the block with the x, y-1, z coordinates...

world.getBlockId(x, y-1, z) == your_block.blockID

Like:

if(world.getBlockId(x, y-1, z) == Mod.CustomDirt.blockID){//DO NOTHING}
else if(world.getBlockId(x, y-1, z) == Mod.CustomGrass.blockID){//DO NOTHING}
else{//DESTROY BLOCK AND INSERT HIM BACK INTO PLAYERS INVENTORY}

Check out my m2cAPI: http://pastebin.com/SJmjgdgK [WIP! If something doesnt work or you have a better resolution, write me a PM]

If you want to use my API please give me a Karma/Thank you

Sorry for some bad words ´cause I am not a walkin´ library!

Link to comment
Share on other sites

How can I rewrite it as a custom rendered block? I tried what you said and it did nothing. My code:

package MoreDimensions.blocks;

import static net.minecraftforge.common.EnumPlantType.Plains;

import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;
import net.minecraftforge.common.EnumPlantType;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.common.IPlantable;
import MoreDimensions.MoreDimensions;

public class SlimeFlower extends Block implements IPlantable
{
    public SlimeFlower(int par1, Material par2Material)
    {
        super(par1, par2Material);
        this.setTickRandomly(true);
        float f = 0.2F;
        this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, f * 3.0F, 0.5F + f);
    }

    public SlimeFlower(int id)
    {
        this(id, Material.plants);
    }

    public boolean canPlaceBlockAt(World par1World, int par2, int par3, int par4)
    {
        return super.canPlaceBlockAt(par1World, par2, par3, par4) && canBlockStay(par1World, par2, par3, par4);
    }

    public boolean canThisPlantGrowOnThisBlockID(int par1)
    {
        return par1 == MoreDimensions.slimeGrass.blockID || par1 == MoreDimensions.slimeDirt.blockID;
    }

    public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5)
    {
        super.onNeighborBlockChange(par1World, par2, par3, par4, par5);
        this.checkFlowerChange(par1World, par2, par3, par4);
    }

    public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
    {
        this.checkFlowerChange(par1World, par2, par3, par4);
    }

    public final void checkFlowerChange(World par1World, int par2, int par3, int par4)
    {
        if (!this.canBlockStay(par1World, par2, par3, par4))
        {
            this.dropBlockAsItem(par1World, par2, par3, par4, par1World.getBlockMetadata(par2, par3, par4), 0);
            par1World.setBlockToAir(par2, par3, par4);
        }
    }

    public boolean canBlockStay(World par1World, int par2, int par3, int par4)
    {
        Block soil = blocksList[par1World.getBlockId(par2, par3 - 1, par4)];
        return (par1World.getFullBlockLightValue(par2, par3, par4) >= 8 || par1World.canBlockSeeTheSky(par2, par3, par4)) && 
                (soil != null && soil.canSustainPlant(par1World, par2, par3 - 1, par4, ForgeDirection.UP, this));
    }

    public AxisAlignedBB getCollisionBoundingBoxFromPool(World par1World, int par2, int par3, int par4)
    {
        return null;
    }

    public boolean isOpaqueCube()
    {
        return false;
    }

    public boolean renderAsNormalBlock()
    {
        return false;
    }

    public int getRenderType()
    {
        return 1;
    }

    @Override
    public EnumPlantType getPlantType(World world, int x, int y, int z)
    {
        if (blockID == MoreDimensions.slimeFlower.blockID) return Plains;
        return Plains;
    }

    @Override
    public int getPlantID(World world, int x, int y, int z)
    {
        return blockID;
    }

    @Override
    public int getPlantMetadata(World world, int x, int y, int z)
    {
        return world.getBlockMetadata(x, y, z);
    }
    
    @Override
    public void registerIcons(IconRegister reg)
    {
    	this.blockIcon = reg.registerIcon("MoreDimensions:slimeFlower");
    }
    
    public void onBlockPlacedBy(World world, int x, int y, int z, EntityLiving entityliving, ItemStack itemstack, EntityPlayer player) 
    {
    	if(world.getBlockId(x, y - 1, z) == MoreDimensions.slimeGrass.blockID)
    	{
    	}
    	else if(world.getBlockId(x, y - 1, z) == MoreDimensions.slimeDirt.blockID)
    	{
    	}
    	else
    	{
    		world.setBlockToAir(x, y, z);
    		player.inventory.addItemStackToInventory(itemstack);
    	}
    }
}

↑↑↑↑↑↑↑↑↑↑ Click it please? :3 ↑↑↑↑↑↑↑↑↑↑

Link to comment
Share on other sites

Please, forget what I set!  :)

Is not problem there?

    public boolean canThisPlantGrowOnThisBlockID(int par1)
    {
        return par1 == MoreDimensions.slimeGrass.blockID || par1 == MoreDimensions.slimeDirt.blockID;
    }

The || (OR) operator ?

Check out my m2cAPI: http://pastebin.com/SJmjgdgK [WIP! If something doesnt work or you have a better resolution, write me a PM]

If you want to use my API please give me a Karma/Thank you

Sorry for some bad words ´cause I am not a walkin´ library!

Link to comment
Share on other sites

I think you can create for plant the tile entity, and the renderer is from default game I think for roses/dandelions/etc

Check out my m2cAPI: http://pastebin.com/SJmjgdgK [WIP! If something doesnt work or you have a better resolution, write me a PM]

If you want to use my API please give me a Karma/Thank you

Sorry for some bad words ´cause I am not a walkin´ library!

Link to comment
Share on other sites

Heh, this is the right question :D

Try in the FlowerClass:

    public TileEntity createNewTileEntity(World par1World)
    {
        return new TE_CustomFlower();
    }

And create the TE_CustomFlower class. Like this:

package yourpackage;
//make there all required imports (Shift+Ctrl+O)
public class TE_CustomFlower extends TileEntity{
//YOUR STUFF
}

 

I hope  :-\

Check out my m2cAPI: http://pastebin.com/SJmjgdgK [WIP! If something doesnt work or you have a better resolution, write me a PM]

If you want to use my API please give me a Karma/Thank you

Sorry for some bad words ´cause I am not a walkin´ library!

Link to comment
Share on other sites

Try replace this:

    public boolean canThisPlantGrowOnThisBlockID(int par1)
    {
        return par1 == MoreDimensions.slimeGrass.blockID || par1 == MoreDimensions.slimeDirt.blockID;
    }

With this:

    
@Override
public boolean canThisPlantGrowOnThisBlockID(int par1)
    {
        return par1 == MoreDimensions.slimeGrass.blockID;
    }

And try place the flower on the slimeGrass

 

if it dont work try:

    
@Override
protected boolean canThisPlantGrowOnThisBlockID(int par1)
    {
        return par1 == MoreDimensions.slimeGrass.blockID;
    }

Check out my m2cAPI: http://pastebin.com/SJmjgdgK [WIP! If something doesnt work or you have a better resolution, write me a PM]

If you want to use my API please give me a Karma/Thank you

Sorry for some bad words ´cause I am not a walkin´ library!

Link to comment
Share on other sites

Ok now I rewrote the class, it now extends BlockFlower. I know have all unnecessary methods removed, and I added the

	@Override
    protected boolean canThisPlantGrowOnThisBlockID(int par1)
    {
        return par1 == MoreDimensions.slimeGrass.blockID || par1 == MoreDimensions.slimeDirt.blockID;
    }

and it worked. However, I can still place the flower on normal grass and dirt... Any workaround?

 

Whole class:

package MoreDimensions.blocks;

import static net.minecraftforge.common.EnumPlantType.Cave;
import static net.minecraftforge.common.EnumPlantType.Crop;
import static net.minecraftforge.common.EnumPlantType.Desert;
import static net.minecraftforge.common.EnumPlantType.Nether;
import static net.minecraftforge.common.EnumPlantType.Plains;
import static net.minecraftforge.common.EnumPlantType.Water;
import net.minecraft.block.Block;
import net.minecraft.block.BlockFlower;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.world.World;
import net.minecraftforge.common.EnumPlantType;
import MoreDimensions.MoreDimensions;

public class SlimeFlower extends BlockFlower
{

public SlimeFlower(int id)
{
	super(id);
	this.setCreativeTab(MoreDimensions.moreDimensions);
}

@Override
    protected boolean canThisPlantGrowOnThisBlockID(int par1)
    {
        return par1 == MoreDimensions.slimeGrass.blockID || par1 == MoreDimensions.slimeDirt.blockID;
    }

    @Override
    public EnumPlantType getPlantType(World world, int x, int y, int z)
    {
        if (blockID == MoreDimensions.slimeFlower.blockID) return Plains;
        return Plains;
    }
    
    @Override
    public void registerIcons(IconRegister reg)
    {
    	this.blockIcon = reg.registerIcon("MoreDimensions:slimeFlower");
    }
}

Removing the @Override annotion @ canThisPlantGrowOnThisBlockID doesn't do anything.

↑↑↑↑↑↑↑↑↑↑ Click it please? :3 ↑↑↑↑↑↑↑↑↑↑

Link to comment
Share on other sites

I'm using a block that extends BlockFlower, and I was curious about this, so I tried this code:

	@Override
protected boolean canThisPlantGrowOnThisBlockID(int par1)
    {
        return false;
    }

 

In my understanding, that should mean that it can't be place on any blocks, but even with this code it can still be placed on dirt and grass.

Link to comment
Share on other sites

Anyone got a solution on how the flower can only be placed on my custom grass/dirt? At the moment it can be placed on them, but it can be placed on normal grass/dirt, too.

 

You need to override the method from SeaBass and also this one:

public boolean canBlockStay(World par1World, int x, int y, int z)

Get the block ID from the block below (y-1) and check if it's equal to your custom block(s).

 

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

My code (still doesn't work):

package MoreDimensions.blocks;

import static net.minecraftforge.common.EnumPlantType.Plains;
import net.minecraft.block.BlockFlower;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.world.World;
import net.minecraftforge.common.EnumPlantType;
import MoreDimensions.MoreDimensions;

public class SlimeFlower extends BlockFlower
{

public SlimeFlower(int id)
{
	super(id);
	this.setCreativeTab(MoreDimensions.moreDimensions);
}

@Override
    protected boolean canThisPlantGrowOnThisBlockID(int par1)
    {
        return false;
    }

    @Override
    public EnumPlantType getPlantType(World world, int x, int y, int z)
    {
        if (blockID == MoreDimensions.slimeFlower.blockID) return Plains;
        return Plains;
    }
    
    @Override
    public void registerIcons(IconRegister reg)
    {
    	this.blockIcon = reg.registerIcon("MoreDimensions:slimeFlower");
    }
    
    @Override
    public boolean canPlaceBlockAt(World world, int x, int y, int z)
    {	
    	if(world.getBlockId(x, y-1, z) == MoreDimensions.slimeGrass.blockID)
    	{
    	}
    	else if(world.getBlockId(x, y-1, z) == MoreDimensions.slimeDirt.blockID)
    	{
    	}
    	else
    	{
         world.setBlockToAir(x, y, z);{return true;}
    	}
	return true;
    }
}

↑↑↑↑↑↑↑↑↑↑ Click it please? :3 ↑↑↑↑↑↑↑↑↑↑

Link to comment
Share on other sites

 @Override
    public boolean canPlaceBlockAt(World world, int x, int y, int z)
    {	
    	if(world.getBlockId(x, y-1, z) == MoreDimensions.slimeGrass.blockID)
    	{
    	}
    	else if(world.getBlockId(x, y-1, z) == MoreDimensions.slimeDirt.blockID)
    	{
    	}
    	else
    	{
         world.setBlockToAir(x, y, z);{return true;}
    	}
	return true;
    }

 

This appears to return true in every case. You want code that only returns true for your blocks, and returns false for everything else.

Link to comment
Share on other sites

I changed the code to this:

    @Override
    public boolean canPlaceBlockAt(World world, int x, int y, int z)
    {	
    	if(world.getBlockId(x, y-1, z) == MoreDimensions.slimeGrass.blockID)
    	{
    		return true;
    	}
    	else if(world.getBlockId(x, y-1, z) == MoreDimensions.slimeDirt.blockID)
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }

and it works! Hell yeah! Thanks!

↑↑↑↑↑↑↑↑↑↑ Click it please? :3 ↑↑↑↑↑↑↑↑↑↑

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



×
×
  • Create New...

Important Information

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