Jump to content

Recommended Posts

Posted

Hello, I have a crop that is supposed to harvest when you punch or right click it. I have these two methods in my block class.

    /* Left-click harvest */
    @Override
    public void onBlockClicked (World world, int x, int y, int z, EntityPlayer player)
    {
        if (!world.isRemote)
        {
            int meta = world.getBlockMetadata(x, y, z);
            if (this == this.grownBlock)
            {
            	world.setBlockToAir(x, y, z);
                world.setBlock(x, y, z, this.unGrownBlock, meta, 3);
                RandomUtils.spawnItemAtPlayer(player, new ItemStack(this.produceItem, 1, 0));
            }
        }
    }

    /* Right-click harvest */
    @Override
    public boolean onBlockActivated (World world, int x, int y, int z, EntityPlayer player, 
    		int par6, float par7, float par8, float par9)
    {

        int meta = world.getBlockMetadata(x, y, z);
        if (this == this.grownBlock)
        {
            if (world.isRemote)
                return true;

            world.setBlockToAir(x, y, z);
            world.setBlock(x, y, z, this, meta, 3);
            RandomUtils.spawnItemAtPlayer(player, new ItemStack(this.produceItem, 1, 0));
            return true;
        }

        return false;
    }

and the spawnItemAtPlayer method is

public static void spawnItemAtPlayer (EntityPlayer player, ItemStack stack)
    {
        EntityItem entityitem = new EntityItem(player.worldObj, player.posX + 0.5D, player.posY + 0.5D, player.posZ + 0.5D, stack);
        World world = player.worldObj;
        world.spawnEntityInWorld(entityitem);
        if (!(player instanceof FakePlayer))
            entityitem.onCollideWithPlayer(player);

    }

. It's doing nothing when I right click it. What am I doing wrong?

Posted

Sorry, didn't see the scroll bar.

 

I would suggest creating some debugging outputs. Just add

System.out.println("Method XYZ has been called");

to your methods at the very beginning and outside of the if - Blocks!

You also should output the values of the variables that you check in the if Blocks.

 

If you have registered the Block correctly, the methods should be called.

Posted

Might my problem lye in here? i've tried a lot of things.

 

package com.jmanpenilla.picklecraft.blocks;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;

import com.jmanpenilla.picklecraft.PickleCraft;
import com.jmanpenilla.picklecraft.items.ModItems;

import cpw.mods.fml.common.registry.GameRegistry;

public class ModBlocks {

public static Block cucumberVine;
public static String cucumberVine_unloc_name = "cucumberVine";

public static Block cucumberVineGrown;
public static String cucumberVineGrown_unloc_name = "cucumberVineGrown";

public static void initializeBlocks()
{

	cucumberVine = new ModBlockVine(Material.vine, cucumberVine, cucumberVineGrown, ModItems.cucumber)
	.setHardness(0.3F)
	.setStepSound(Block.soundTypeWood)
	.setBlockName(cucumberVine_unloc_name)
	.setBlockTextureName(PickleCraft.modid + ":" + cucumberVine_unloc_name)
	.setCreativeTab(PickleCraft.tabPickle);

	GameRegistry.registerBlock(cucumberVine, cucumberVine_unloc_name);

	cucumberVineGrown = new ModBlockVine(Material.vine, cucumberVine, cucumberVineGrown, ModItems.cucumber)
	.setHardness(0.8F)
	.setStepSound(Block.soundTypeWood)
	.setBlockName(cucumberVineGrown_unloc_name)
	.setBlockTextureName(PickleCraft.modid + ":" + cucumberVineGrown_unloc_name)
	.setCreativeTab(PickleCraft.tabPickle);

	GameRegistry.registerBlock(cucumberVineGrown, cucumberVineGrown_unloc_name);

}

}

Posted

You should check if that is working as you expect.

this == this.grownBlock

This is most likely the problem.

cucumberVine = new ModBlockVine(Material.vine, cucumberVine, cucumberVineGrown, ModItems.cucumber)

Look, you initialize cucumberVine, passing it a reference to itself and the currently uninitialized cucumberVineGrown, then you point cucumberVineGrown at a new instance of block. Why don't you just try using your blocks directly, rather than storing a Block within a Block?

if (this == ModBlocks.cucumberVineGrown)

Posted

Yeah, I knew my initialization order was sort of screwed up. But I need to have access to the ungrown and grown blocks in the block class. Again, it's on github, and more updated. I'm gonna try a few things but any other ideas are appreciated.

 

Thanks

Posted

What I'm saying is that you don't need to store any Block in your crop class; just access them directly instead. Meaning your constructor should only have one or two arguments, none of which are Blocks.

cucumberVine = new ModBlockVine(Material.vine, ModItems.cucumber)

There you go. Also, are your Items initialized at this point? This just looks like a recipe for disaster.

Posted

Yeah, items are initialized before blocks. I need access to the grown block and ingrown block to be able to preform my right/left click actions and my update tick method. So how am I supposed to do that without having blocks in the constructor? If you look on the github you can see what else I have tried (check ModBlocks.java).

Posted

@Alix_The_Alicorn

 

coolAlias is correct in that you are using uninitialized blocks in the constructor of the ModBlockVine.

 

The problem you are creating is that you store the value of what is in cucumberVine and cucumberVineGrown (which are both null until the constructor returns) into unGrownBlock and grownBlock in the ModBlockVine constuctor.  Then you use these null values to check if your block matches later in the onBlockClicked and onBlockActivated (also updateTick, I bet your pickles dont grow either).

 

You should take coolAlias's advice and not store the values in unGrownBlock and grownBlock.  Remove those two and their getter/setter methods.  Then just use ModBlocks.cucumberVine and ModBlocks.cucumberVineGrown in the ModBlockVine code where you currently have unGrownBlock and grownBlock.

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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