Jump to content

Special drops for special tools


pro-mole

Recommended Posts

So I started modding Minecraft just out of fun and to learn something new, but quickly I started structuring it into a coherent idea and before I consider making this official I still want to experiment a little. This is just to explain why I haven't requested mod status yet, if that's needed. Apologies, if that's the case.

 

To the point: I've created a tool(an ItemSpade) and an item, and I'd like this item to be dropped if the player breaks dirt using this item. I had a real hard time trying to figure out how to manage that.

 

At first I tried changing the BlockDirt class directly, but that would only work when testing from Eclipse. After recompiling and reobfuscating the code and packing it up any changes to Dirt would be ignored.

 

I finally found a solution that involves creating a new class that will behave like BlockDirt, unregister the dirt block at initialization and register mine on top. It works, but sounds to me as incredibly shifty(not to mention possibly non-compatible with other mods). Is there another proper and better way to tamper with existing blocks with Forge?

Link to comment
Share on other sites

ASM might not be your best option if you're just starting out. Also, it greatly decreases performance. I believe it is similar to java reflection. An alternate solution is to take your fake dirt block and replace all real dirt in the world. then give it all the real dirt functions. then have it drop real dirt and your item when broken. not the best idea in my opinion I would recommend every other alternative, but if you absolutely have to get something to drop from dirt, then feel free to use ASM/java reflection, just note that the game will be laggier.

I like collars. XP

Link to comment
Share on other sites

Okay, midway through this thread I just decided to create a new block to do this instead, so it's a new block crafted from dirt that can only be crafted with a special tool now. A lot easier, I guess.

 

Still, thanks very much, maybe later on I'll look into ASM if I really need that.

Link to comment
Share on other sites

Aye, I think this should all be able to be handled in the blockbreak/blockharvest event, or even in the itemtool itself (overriding one of the methods).

 

You should basically be able to tell the game to spit out your item whenever a dirt block is broken with your tool.  I'll try and take a closer look and see if I can find the specific methods/etc.

 

Ahh, yes..in Item.java:

 

  public boolean onBlockDestroyed(ItemStack par1ItemStack, World par2World, int par3, int par4, int par5, int par6, EntityLiving par7EntityLiving)
    {
        return false;
    }

 

implement an override here, and make either the player drop the item, or merely spawn it in the world.  You might even be able to call the block-drop method to have the block spit it out.  You will have to check that the block destroyed==Block.dirt or ==Block.grass (by calling world.getblockID(x,y,z) and comparing that to dirt/grass blockIDs).

 

Either way..no base-edits.  No block overrides.  No compatibility issues.

 

Please let me know if it helps.

 

 

 

Link to comment
Share on other sites

Hey shadowmage! Thanks much, I actually found that out before I was coming back here to comment that, while I managed to use that to do what I want(now an ax/shear hybrid), it's not working because I can't get the blocks dropped.

 

I'll post the code later when I'm back home, but for now any idea of what might be going on? I basically used a code with an if for block == Block.leaves and spawning a new entity, and the leaves blocks just stand there on the ground. =/

 

Here's the code:

 

@Override
public boolean onBlockStartBreak(ItemStack itemstack, int x, int y, int z, EntityPlayer player)
{	
	//System.out.println("Cutting "+player.worldObj.getBlockId(x,y,z));
	if (player.worldObj.getBlockId(x,y,z) == Block.leaves.blockID)
	{
		EntityItem sheared = new EntityItem(player.worldObj, x, y, z, new ItemStack(Block.leaves, 1, player.worldObj.getBlockMetadata(x, y, z)));
		//sheared.delayBeforeCanPickup = 10;
		player.worldObj.spawnEntityInWorld(sheared);
		return true;
	}

	return false;
}

Link to comment
Share on other sites

ERP DERP, found it! Here's code for EntityItem.collideWithPlayer:

 

public void onCollideWithPlayer(EntityPlayer par1EntityPlayer)
    {
        if (!this.worldObj.isRemote)
        {

 

Apparently in Minecraft 1.3 isRemote is always true. This means any entityItem will never collide with the player.

 

With that in mind, how do I even create items in Minecraft 1.3? I'm confused.

 


But the leaves block does disappear, the block stack pops out and fall to the floor and... stays there. I can't take it.

 

 

Not sure if it helps, but here's a screenshot:

 

 

4dfrL.png

 

 

The leaves blocks are the ones I can't get. Yes, the beetle is the tool.

Link to comment
Share on other sites

Okay, this was completely crazy, but worked. Here's what I figured out:

 

When testing from Eclipse, the "isRemote" flag is always true. Since the usual "onBlockStartBreak" code is only executed on the Client side, this leads to the code never working. Except it will work normally when the mod is compiled.

 

And then I had to return a false value to make sure the leaves block was destroyed. I actually did try it returning true, and the block was never destroying, generating an infinite source of leaves! Woot!

 

Anyway, the code is working now. Awesome! :D

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.