Jump to content

[1.7.10] Modifying the drop(s) of an existing block


winnetrie

Recommended Posts

I'm looking for something i can remove/add drop(s) of an existing block. I found some information on forums but nothing like that was working, so i tried something myself:

 

I made a new class for this and i do not even know if this is right.

If it is right, how do i register this?

public class TemDrops {

@SubscribeEvent
 public void Temloot(BlockEvent.HarvestDropsEvent event)

 {
	List<ItemStack> loot = event.drops;
    	Iterator<ItemStack> Leash = loot.iterator();
    	
    	while (Leash.hasNext()) {
	ItemStack is = Leash.next();

	if (is != null && is.getItem() == Item.getItemFromBlock(Blocks.leaves))
		{
		((List<ItemStack>) Leash).add(new ItemStack(Items.potato));
		}
    	}
}
}

Link to comment
Share on other sites

MinecraftForge.EVENT_BUS.register(new YourClass());

 

In pre-1.8 there is also FML event bus, but that is for different events. (google).

Yeah that was the first thing i tried, but it didn't do anything

i registered it in my CommonProxy class like this:

 

public void init(FMLInitializationEvent event) {
    	TemRecipes.init();
    	
    	if(Loader.isModLoaded("BiomesOPlenty")){
		BOPAddonRecipes.init();
		}
    	MinecraftForge.EVENT_BUS.register(new TemDrops());
    	 //
    	 GameRegistry.registerWorldGenerator(new TemChunkGenerator(), 1);
    	 GameRegistry.registerWorldGenerator(new ChalkStoneGenerator(), 10);
    	 

    }

So i guess there is something wrong with the TemDrops.class itself

Link to comment
Share on other sites

Are you saying method is not called or logic is not working like it is supposed to?

 

Make Syso there and check.

 

Also - very important - Harvesting is NOT called in creative mode (notice there is no drops when you pawn block in creative). Ppl often forget that.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

For 1.7.10, the event is fired on the

MinecraftForge.EVENT_BUS

, which you are doing.

 

Show your current TemDrops class with the "TEST TEST TEST" line.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

I have it working now. I tried something else:

public class TemDrops {


@SubscribeEvent
public void Temloot(BlockEvent.BreakEvent e)

{
	Block block =e.block;
	if ( block instanceof BlockLeaves)
	  {
	   Random r = new Random();
	  
	   if ( r.nextFloat() < 0.2F){
	   EntityItem i = new EntityItem(e.world, e.x, e.y, e.z, new ItemStack(Items.stick));
	   System.out.println("TEST TEST TESTertje");
	   e.world.spawnEntityInWorld(i);
	   }
	  
	  }

}

}

I have seen a page "jabelar's minecraft forge tutorials"(lot's of information there!!  ;D). There the autor says on his page to use breakevent instead of harvestevent to get a drop from blocks like leaves.

So the registering was done but my method wasn't right. I now have made a better 1 with some help ofc.

While this method adds a drop, i was wondering how i would remove  a drop.

I also made it check an instance of BlockLeaves.

 

EDIT:

Oh btw leaves DO drop the sticks in creative mode, but only the sticks.

Maybe i need to make a check if the player is in creative mode or not. Does it matter anyway?

Link to comment
Share on other sites

Leaves still triggers

HarvestDropsEvent

.  It overrides

harvestBlock

, but all it does is call super().

 

The block default method calls

dropBlockAsItem

which passes off to

dropBlockAsItemWithChance

which calls

getDrops

just prior to firing the event, which BlockLeaves overrides again (to possibly drop a sapling).

 

Adding a stick to that is just as easy as

event.drops.add(new ItemStack(...))

 

Removing is a bit trickier, as you should loop through the array to locate the item you want to remove, and for every other item add it to a new array (you don't want to just delete everything: some other mod may have added an item that should still drop!).  Then call

event.drops.clear()

and then copy your saved list back to event.drops.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Because I forget things.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

I have this now:

public class TemDrops {

@SubscribeEvent
public void Temloot(HarvestDropsEvent event) 
{
	System.out.println("TEST TEST TESTertje");
   	Iterator<ItemStack> Leash = event.drops.iterator();
   	while (Leash.hasNext()) {
   		Block block= event.block;
		ItemStack is = Leash.next();
		if (block != Blocks.leaves)
			{
			System.out.println("nothing"); 
			}
		else{ 
			System.out.println("leaves!!"); 
			event.drops.add(new ItemStack(Items.potato));
		}
   	}
}
}

But this isn't working at all and it makes minecraft crash if the 'else' statement triggers.

 

Link to comment
Share on other sites

You are trying to add to a List while iterating through it, this is not allowed.

AAAH yes now i understand it.

So i can only remove from that list while iterating but not adding.

To add something to the drop i now have this:

public class TemDrops {

@SubscribeEvent
public void Temloot(HarvestDropsEvent event) 
{
	Block block = event.block;
	if (block instanceof BlockLeaves){
		event.drops.add(new ItemStack(Items.potato));
	}
}
}

I didn't had a need to remove something but i was wondering how to.

It is ennoying not to know. And yes i really need to study more java, i know!

Link to comment
Share on other sites

Yes.  There's no point in adding inside the loop (even if it didn't crash, you'd then have to check the item you just added as well and that could be bad!).

 

Also, your loop in your prior post makes no sense.

 

//for each item in the list
//  save a reference to the item
//  if the block harvested is Leaves
//    add potato.

 

You never do anything with the ItemStack

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.