Jump to content

Find the Item that was used for an Attack?


Recommended Posts

So I'm creating a mod that includes a double-wieldable variation of the sworditem. The double wielding itsself works fine. 

But my mod also adds some enchantments to the game. Right now I have the problem that offhand weapons use the mainhand weapons' enchantment information. Here's my enchantment code:

    public static void OnHitEntity(LivingAttackEvent event) {
        Entity entity = event.getSource().getTrueSource();
        if(entity instanceof LivingEntity) {
            LivingEntity living = (LivingEntity) entity;
            LivingEntity target = event.getEntityLiving();
            Iterable<ItemStack> equipment = living.getEquipmentAndArmor();
            ItemStack stack = living.getHeldItem(living.getActiveHand());

            //Dark Contract
            level = EnchantmentHelper.getEnchantmentLevel(DARK_CONTRACT.get(), stack);
            if(level > 0) {
                target.attackEntityFrom(DamageSource.GENERIC, level*2);
                living.attackEntityFrom(DamageSource.causeIndirectMagicDamage(living, null), level);

As you see, I only apply the effects of the active hand. In my double sword code, I set the active hand to offhand on right click: 

public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        return ActionResult.resultSuccess(playerIn.getHeldItemOffhand());
public ActionResultType itemInteractionForEntity(ItemStack stack, PlayerEntity playerIn, LivingEntity target, Hand hand) {
        stack.getItem().hitEntity(stack, target, playerIn);
        return ActionResultType.SUCCESS;

So even though I set the active hand and use this information to apply the effects, it keeps using the wrong hand's enchantments. 

Since the hitEntity() method takes an ItemStack as an argument, I started asking myself if there was a way to get the stack that was used from the event. That would make it much easier. Any Ideas are appreciated. 

Link to comment
Share on other sites

So can you think of any other way to find out which hand was used for an attack?

The problem here is that I want these effects to also work on vanilla swords or even weapons from other mods. Could I create a mixin that fires an event when Item#hitEntity is called to know which stack was used? 

I haven't really worked with mixins yet so I don't know if they are a solution in this situation. 

Link to comment
Share on other sites

My question's if you're able to find the stack that was given as a parameter. Because right now I could also shoot with a bow and then switch to an enchanted sword while the arrow is flying. The effect would be applied since the item that I had in my hand on impact was enchanted. 

I'm trying to trace the itemstack that was used to initiate the attack. 

Link to comment
Share on other sites

Or deny the enchantment on bows.

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


So I followed @diesieben07's instructions and created a boolean capabbility for players that checks if their last hand used was the offhand. Enchantments that are hand-based will simply check the OffhandUsed capability and only apply the effect if they're in the correct hand when they're used by players. 

Sadly Vanilla enchants won't work on the left hand like this but it's the best I could do for now. 

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.

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.


  • Create New...

Important Information

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