Jump to content

[1.8] Quirky event handler behavior


Anon10W1z

Recommended Posts

In 1.8 I use an event handler to add functionality for an automatic smelting enchantment.

 

 


if (player != null && !player.capabilities.isCreativeMode && EnchantmentHelper.getEnchantmentLevel(this.effectId, player.getHeldItem()) > 0) {
HarvestDropsEvent event = (HarvestDropsEvent) baseEvent;
ItemStack drop = event.drops.size() == 0 ? null : event.drops.get(0);
if (drop != null) {
	ItemStack smeltStack = new ItemStack(drop.getItem(), 1, drop.getMetadata());
	ItemStack smeltResult = FurnaceRecipes.instance().getSmeltingResult(smeltStack);
	if (smeltResult != null) {
		smeltResult.stackSize *= drop.stackSize;
		event.drops.clear();
		event.drops.add(smeltResult);
	}
}
}

This code works only the first time breaking a certain block. After that, nothing drops. For example, the first time breaking an iron ore yielded one iron ingot. Tries after that all yielded nothing. This is done using the HarvestDropsEvent.

Maker of the Craft++ mod.

Link to comment
Share on other sites

Always use itemStack.copy() when editing it. (stacks are mutable)

 

When pulling ItemStack from any kind of registry or something - 1st do something like ItemStack tempStack = stack.copy();

Also:

HarvestDropsEvent event = (HarvestDropsEvent) baseEvent;

Wtf? I really hope you are actually doing it as event, not like this.

And:

ItemStack drop = event.drops.size() == 0 ? null : event.drops.get(0);

drops can be bigger than just size==1 and then you do event.drops.clear(); which clears them all, kinda bad design in my opinion.

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

Link to comment
Share on other sites

Always use itemStack.copy() when editing it. (stacks are mutable)

 

When pulling ItemStack from any kind of registry or something - 1st do something like ItemStack tempStack = stack.copy();

Also:

HarvestDropsEvent event = (HarvestDropsEvent) baseEvent;

Wtf? I really hope you are actually doing it as event, not like this.

And:

ItemStack drop = event.drops.size() == 0 ? null : event.drops.get(0);

drops can be bigger than just size==1 and then you do event.drops.clear(); which clears them all, kinda bad design in my opinion.

This is part of a method called from my event handler. I'll try the .copy() method out.

UPDATE: Worked like a charm! Thanks.

Maker of the Craft++ mod.

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.