Jump to content

how to reduce the damage you receive if you have an item in inventory


Recommended Posts

Posted

You can subscribe to the LivingHurtEvent and check the players inventory.

 

Something like this should work:

    @SubscribeEvent
    public static void onPLayerHurtEvent(LivingHurtEvent event) {
        Entity entity = event.getEntity();
        if (entity instanceof EntityPlayer) {
            EntityPlayer player = (EntityPlayer) entity;
            float damage = event.getAmount();

            for (ItemStack stack : player.inventory.mainInventory)
            {
                if(stack.getItem() instanceof ItemAppleGold){
                    damage = damage/2;
                }
            }
        }
    }

 

This should reduce the damage by half if you got a golden apple in your inventory.

Posted

but I wanted it to be like armor, so the item would add armor points. like this

aaaa.png.076e7773dc65f29c01015a8b61fa770b.png

but without using ItemArmor and ArmorMaterial, since it is a normal item

Posted
1 hour ago, Ragnar said:

but I wanted it to be like armor, so the item would add armor points. like this

aaaa.png.076e7773dc65f29c01015a8b61fa770b.png

but without using ItemArmor and ArmorMaterial, since it is a normal item

If I'm not mistaken the armor of the player is calculated on demand so you cannot do this without making it an instance of ItemArmor.

 

However if it isnt then it is stored within a SharedMonsterAttribute and you can add your own modifier in a player tick event if the item is found and remove the modifier if it isnt.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

I believe this is the code that the item armor uses to do thisI believe this is the code that the item armor uses to do this

 

    public Multimap<String, AttributeModifier> getItemAttributeModifiers(EntityEquipmentSlot equipmentSlot)
    {
        Multimap<String, AttributeModifier> multimap = super.getItemAttributeModifiers(equipmentSlot);

        if (equipmentSlot == this.armorType)
        {
            multimap.put(SharedMonsterAttributes.ARMOR.getName(), new AttributeModifier(ARMOR_MODIFIERS[equipmentSlot.getIndex()], "Armor modifier", (double)this.damageReduceAmount, 0));
            multimap.put(SharedMonsterAttributes.ARMOR_TOUGHNESS.getName(), new AttributeModifier(ARMOR_MODIFIERS[equipmentSlot.getIndex()], "Armor toughness", (double)this.toughness, 0));
        }

        return multimap;
    }

Posted
5 hours ago, Animefan8888 said:

you can add your own modifier in a player tick event if the item is found and remove the modifier if it isnt.

 

4 hours ago, Elyon13 said:

multimap.put(SharedMonsterAttributes.ARMOR.getName(), new AttributeModifier(ARMOR_MODIFIERS[equipmentSlot.getIndex()], "Armor modifier", (double)this.damageReduceAmount, 0));
            multimap.put(SharedMonsterAttributes.ARMOR_TOUGHNESS.getName(), new AttributeModifier(ARMOR_MODIFIERS[equipmentSlot.getIndex()], "Armor toughness", (double)this.toughness, 0));

 

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Posted
On 10/10/2018 at 3:03 PM, Keitaro said:

You can subscribe to the LivingHurtEvent and check the players inventory.

 

Something like this should work:


    @SubscribeEvent
    public static void onPLayerHurtEvent(LivingHurtEvent event) {
        Entity entity = event.getEntity();
        if (entity instanceof EntityPlayer) {
            EntityPlayer player = (EntityPlayer) entity;
            float damage = event.getAmount();

            for (ItemStack stack : player.inventory.mainInventory)
            {
                if(stack.getItem() instanceof ItemAppleGold){
                    damage = damage/2;
                }
            }
        }
    }

 

This should reduce the damage by half if you got a golden apple in your inventory.

No it doesn't, because you never applied the value back to the event. float damage = event.getAmount(); copies the value because float is a primitive type, not a reference type.

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.

Posted
23 minutes ago, Draco18s said:

copies the value because float is a primitive type, not a reference type.

Well technically in java this doesn't matter. Consider this example:

class A
{
	public String foo()
    {
    	return "1";
    }
}

class B extends A
{
	@Override
    public String foo()
    {
    	return "5";
    }
}

class Main
{
  void bar()
  {
    A a = new A();
    this.baz(a);
    System.out.println(a.foo()); 
  }
  
  void baz(A a)
  {
  	a = new B();
  }
}

This will output 1, not 5 because changing the variable a in method baz changes the local copy of that variable that the method operates with. Even though A is not a primitive type. If you wanted the change to apply in a scope outside of baz you would need to pass A as a reference. Which java can't do. However changing a field inside of the passed A would change it for bar too.

 

I am sure you know this already, I am just pointing this out to avoid confusion for members who may not be so familiar with java.

Posted (edited)
16 minutes ago, V0idWa1k3r said:

class A

That's a reference type.

 

Also, you changed the reference inside the method, rather than changing its properties.

 

Try this:

 

class Main
{
  void bar()
  {
    float a = 1f;
    this.baz(a);
    System.out.println(a); 
  }
  
  void baz(float a)
  {
  	a = 
  }

 

It'll print 1, not 5.

 

Which is why the code I quoted doesn't work.

You need to change the event's properties.

Edited by Draco18s

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.

Posted
4 minutes ago, Draco18s said:

That's a reference type. Good job, you completely failed.

 

 

18 minutes ago, V0idWa1k3r said:

though A is not a primitive type.

Yes, I've acknowledge that. It's just that you've explicitly mentioned that this doesn't work for the OP because they are using a primitive type. I just clarified that it doesn't matter whether the type is primitive or a reference type, it won't work regardless.

Posted

I edited my post because I was a little too hasty. The important bit is the bit I was trying to call out:

4 minutes ago, Draco18s said:

Which is why the code I quoted doesn't work.

You need to change the event's properties.

 

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.

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.