Jump to content

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


Recommended Posts

Posted

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

Posted

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!

Posted

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

Posted

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!

Posted

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!

Posted

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!

Posted

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!

Posted

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

Posted

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.

Posted

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.

Posted

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

Posted

I replaced it with a & (don't know if this is the AND operator, but I assume it is) and it doesn't work. Is there a TileEntity or Renderer for plants?

 

Note sure if this helps but it has to be double &, for example:

if(Bacon == "yum" && Cat == "Mao"){}

Posted

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

Posted

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

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

    • Hello, I have this same problem. Did you manage to find a solution? Any help would be appreciated. Thanks.
    • log: https://mclo.gs/QJg3wYX as stated in the title, my game freezes upon loading into the server after i used a far-away waystone in it. The modpack i'm using is better minecraft V18. Issue only comes up in this specific server, singleplayer and other servers are A-okay. i've already experimented with removing possible culprits like modernfix and various others to no effect. i've also attempted a full reinstall of the modpack profile. Issue occurs shortly after the 'cancel' button dissapears on the 'loading world' section of the loading screen.   thanks in advance.
    • You would have better results asking a more specific question. What have you done? What exactly do you need help with? Please also read the FAQ regarding posting logs.
    • Hi, this is my second post with the same content as no one answered this and it's been a long time since I made the last post, I want to make a client-only mod, everything is ok, but when I use shaders, none of the textures rendered in RenderLevelStageEvent nor the crow entity model are rendered, I want them to be visible, because it's a horror themed mod I've already tried it with different shaders, but it didn't work with any of them and I really want to add support for shaders Here is how i render the crow model in the CrowEntityRenderer<CrowEntity>, by the time i use this method, i know is not the right method but i don't think this is the cause of the problem, the renderType i'm using is entityCutout @Override public void render(CrowEntity p_entity, float entityYaw, float partialTick, PoseStack poseStack, MultiBufferSource bufferSource, int packedLight) { super.render(p_entity, entityYaw, partialTick, poseStack, bufferSource, packedLight); ClientEventHandler.getClient().crow.renderToBuffer(poseStack, bufferSource.getBuffer(ClientEventHandler.getClient().crow .renderType(TEXTURE)), packedLight, OverlayTexture.NO_OVERLAY, Utils.rgb(255, 255, 255)); } Here renderLevelStage @Override public void renderWorld(RenderLevelStageEvent e) { horrorEvents.draw(e); } Here is how i render every event public void draw(RenderLevelStageEvent e) { for (HorrorEvent event : currentHorrorEvents) { event.tick(e.getPartialTick()); event.draw(e); } } Here is how i render the crow model on the event @Override public void draw(RenderLevelStageEvent e) { if(e.getStage() == RenderLevelStageEvent.Stage.AFTER_ENTITIES) { float arcProgress = getArcProgress(0.25f); int alpha = (int) Mth.lerp(arcProgress, 0, 255); int packedLight = LevelRenderer.getLightColor(Minecraft.getInstance().level, blockPos); VertexConsumer builder = ClientEventHandler.bufferSource.getBuffer(crow); Crow<CreepyBirdHorrorEvent> model = ClientEventHandler .getClient().crow; model.setupAnim(this); RenderHelper.renderModelInWorld(model, position, offset, e.getCamera(), e.getPoseStack(), builder, packedLight, OverlayTexture.NO_OVERLAY, alpha); builder = ClientEventHandler.bufferSource.getBuffer(eyes); RenderHelper.renderModelInWorld(model, position, offset, e.getCamera(), e.getPoseStack(), builder, 15728880, OverlayTexture.NO_OVERLAY, alpha); } } How i render the model public static void renderModelInWorld(Model model, Vector3f pos, Vector3f offset, Camera camera, PoseStack matrix, VertexConsumer builder, int light, int overlay, int alpha) { matrix.pushPose(); Vec3 cameraPos = camera.getPosition(); double finalX = pos.x - cameraPos.x + offset.x; double finalY = pos.y - cameraPos.y + offset.y; double finalZ = pos.z - cameraPos.z + offset.z; matrix.pushPose(); matrix.translate(finalX, finalY, finalZ); matrix.mulPose(Axis.XP.rotationDegrees(180f)); model.renderToBuffer(matrix, builder, light, overlay, Utils .rgba(255, 255, 255, alpha)); matrix.popPose(); matrix.popPose(); } Thanks in advance
  • Topics

×
×
  • Create New...

Important Information

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