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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hi, i'm really having problems trying to set the texture to my custom item. I thought i'm doing everything correctly, but all i see is the missing texture block for my item. I am trying this for over a week now and getting really frustrated. The only time i could make the texture work, was when i used an older Forge version (52.0.1) for Minecraft (1.21.4). Was there a fundamental change for textures and models somewhere between versions that i'm missing? I started with Forge 54.1.0 and had this problem, so in my frustration i tried many things: Upgrading to Forge 54.1.1, created multiple new projects, workspaces, redownloaded everything and setting things up multiple times, as it was suggested in an older thread. Therea are no errors in the console logs, but maybe i'm blind, so i pasted the console logs to pastebin anyway: https://pastebin.com/zAM8RiUN The only time i see an error is when i change the models JSON file to an incorrect JSON which makes sense and that suggests to me it is actually reading the JSON file.   I set the github repository to public, i would be so thankful if anyone could take a look and tell me what i did wrong: https://github.com/xLorkin/teleport_pug_forge   As a note: i'm pretty new to modding, this is my first mod ever. But i'm used to programming. I had some up and downs, but through reading the documentation, using google and experimenting, i could solve all other problems. I only started modding for Minecraft because my son is such a big fan and wanted this mod.
    • Please read the FAQ (link in orange bar at top of page), and post logs as described there.
    • Hello fellow Minecrafters! I recently returned to Minecraft and realized I needed a wiki that displays basic information easily and had great user navigation. That’s why I decided to build: MinecraftSearch — a site by a Minecraft fan, for Minecraft fans. Key Features So Far Straight-to-the-Point Info: No extra fluff; just the essentials on items, mobs, recipes, loot and more. Clean & Intuitive Layout: Easy navigation so you spend less time scrolling and more time playing. Optimized Search: Search for anything—items, mobs, blocks—and get results instantly. What I’m Thinking of Adding More data/information: Catch chances for fishing rod, traveling villager trades, biomes info and a lot more. The website is still under development and need a lot more data added. Community Contributions: Potential for user-uploaded tips for items/mobs/blocks in the future. Feature Requests Welcome: Your ideas could shape how the wiki evolves! You can see my roadmap at the About page https://minecraftsearch.com/about I’d love for you to check out MinecraftSearch and see if it helps you find the info you need faster. Feedback is crucial—I want to develop this further based on what the community needs most, so please let me know what you think. Thanks, and happy crafting!
    • Instructions on how to install newer Java can be found in the FAQ
    • That's just plain wrong... newer versions are much better optimised and start a lot faster than 1.8.9, both Forge and Minecraft itself. Comparing Fabric 1.21 with Forge 1.8 is like comparing apples and oranges... one's brand new and the other's over a decade old.
  • Topics

×
×
  • Create New...

Important Information

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