From 5f9034d95b623918a8105a2847894bfb7a02f90a Mon Sep 17 00:00:00 2001 From: boni Date: Sun, 30 Oct 2016 16:53:50 +0100 Subject: [PATCH] Call Looting Event on player death and loot table drops (#3275) * Call Looting Event on player death and loot table drops * Simplify patches * More code cleanup: Replace old usage with compact forgehooks implementation --- .../entity/EntityLivingBase.java.patch | 17 +++++++++++------ .../world/storage/loot/LootContext.java.patch | 7 ++++++- .../RandomChanceWithLooting.java.patch | 17 +++++++++++++++++ .../functions/LootingEnchantBonus.java.patch | 11 +++++++++++ .../net/minecraftforge/common/ForgeHooks.java | 18 +++++++++++++++++- .../event/entity/player/PlayerDropsEvent.java | 7 ++----- 6 files changed, 64 insertions(+), 13 deletions(-) create mode 100644 patches/minecraft/net/minecraft/world/storage/loot/conditions/RandomChanceWithLooting.java.patch create mode 100644 patches/minecraft/net/minecraft/world/storage/loot/functions/LootingEnchantBonus.java.patch diff --git a/patches/minecraft/net/minecraft/entity/EntityLivingBase.java.patch b/patches/minecraft/net/minecraft/entity/EntityLivingBase.java.patch index 77601b5d93..a2846fd35c 100644 --- a/patches/minecraft/net/minecraft/entity/EntityLivingBase.java.patch +++ b/patches/minecraft/net/minecraft/entity/EntityLivingBase.java.patch @@ -76,15 +76,20 @@ if (!this.field_70729_aU) { Entity entity = p_70645_1_.func_76346_g(); -@@ -1069,12 +1075,26 @@ - { - i = EnchantmentHelper.func_185283_h((EntityLivingBase)entity); - } -+ i = net.minecraftforge.common.ForgeHooks.getLootingLevel(this, p_70645_1_, i); +@@ -1063,18 +1069,26 @@ + if (!this.field_70170_p.field_72995_K) + { +- int i = 0; ++ int i = net.minecraftforge.common.ForgeHooks.getLootingLevel(this, entity, p_70645_1_); + +- if (entity instanceof EntityPlayer) +- { +- i = EnchantmentHelper.func_185283_h((EntityLivingBase)entity); +- } + captureDrops = true; + capturedDrops.clear(); -+ + if (this.func_146066_aG() && this.field_70170_p.func_82736_K().func_82766_b("doMobLoot")) { boolean flag = this.field_70718_bc > 0; diff --git a/patches/minecraft/net/minecraft/world/storage/loot/LootContext.java.patch b/patches/minecraft/net/minecraft/world/storage/loot/LootContext.java.patch index 448ddce8a0..779ac13c81 100644 --- a/patches/minecraft/net/minecraft/world/storage/loot/LootContext.java.patch +++ b/patches/minecraft/net/minecraft/world/storage/loot/LootContext.java.patch @@ -1,6 +1,6 @@ --- ../src-base/minecraft/net/minecraft/world/storage/loot/LootContext.java +++ ../src-work/minecraft/net/minecraft/world/storage/loot/LootContext.java -@@ -89,6 +89,11 @@ +@@ -89,6 +89,16 @@ } } @@ -8,6 +8,11 @@ + { + return field_186499_b; + } ++ ++ public int getLootingModifier() ++ { ++ return net.minecraftforge.common.ForgeHooks.getLootingLevel(func_186493_a(), func_186492_c(), field_186503_f); ++ } + public static class Builder { diff --git a/patches/minecraft/net/minecraft/world/storage/loot/conditions/RandomChanceWithLooting.java.patch b/patches/minecraft/net/minecraft/world/storage/loot/conditions/RandomChanceWithLooting.java.patch new file mode 100644 index 0000000000..22ab3c87ab --- /dev/null +++ b/patches/minecraft/net/minecraft/world/storage/loot/conditions/RandomChanceWithLooting.java.patch @@ -0,0 +1,17 @@ +--- ../src-base/minecraft/net/minecraft/world/storage/loot/conditions/RandomChanceWithLooting.java ++++ ../src-work/minecraft/net/minecraft/world/storage/loot/conditions/RandomChanceWithLooting.java +@@ -23,13 +23,8 @@ + + public boolean func_186618_a(Random p_186618_1_, LootContext p_186618_2_) + { +- int i = 0; ++ int i = p_186618_2_.getLootingModifier(); + +- if (p_186618_2_.func_186492_c() instanceof EntityLivingBase) +- { +- i = EnchantmentHelper.func_185283_h((EntityLivingBase)p_186618_2_.func_186492_c()); +- } +- + return p_186618_1_.nextFloat() < this.field_186627_a + (float)i * this.field_186628_b; + } + diff --git a/patches/minecraft/net/minecraft/world/storage/loot/functions/LootingEnchantBonus.java.patch b/patches/minecraft/net/minecraft/world/storage/loot/functions/LootingEnchantBonus.java.patch new file mode 100644 index 0000000000..c6bf5438a6 --- /dev/null +++ b/patches/minecraft/net/minecraft/world/storage/loot/functions/LootingEnchantBonus.java.patch @@ -0,0 +1,11 @@ +--- ../src-base/minecraft/net/minecraft/world/storage/loot/functions/LootingEnchantBonus.java ++++ ../src-work/minecraft/net/minecraft/world/storage/loot/functions/LootingEnchantBonus.java +@@ -32,7 +32,7 @@ + + if (entity instanceof EntityLivingBase) + { +- int i = EnchantmentHelper.func_185283_h((EntityLivingBase)entity); ++ int i = p_186553_3_.getLootingModifier(); + + if (i == 0) + { diff --git a/src/main/java/net/minecraftforge/common/ForgeHooks.java b/src/main/java/net/minecraftforge/common/ForgeHooks.java index 11c336f41a..290cf88ec5 100644 --- a/src/main/java/net/minecraftforge/common/ForgeHooks.java +++ b/src/main/java/net/minecraftforge/common/ForgeHooks.java @@ -43,6 +43,7 @@ import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiScreen; +import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityList; import net.minecraft.entity.EntityLivingBase; @@ -518,7 +519,22 @@ public class ForgeHooks return (MinecraftForge.EVENT_BUS.post(event) ? null : new float[]{event.getDistance(), event.getDamageMultiplier()}); } - public static int getLootingLevel(EntityLivingBase target, DamageSource cause, int level) { + public static int getLootingLevel(Entity target, Entity killer, DamageSource cause) + { + int looting = 0; + if (killer instanceof EntityLivingBase) + { + looting = EnchantmentHelper.getLootingModifier((EntityLivingBase)killer); + } + if (target instanceof EntityLivingBase) + { + looting = getLootingLevel((EntityLivingBase)target, cause, looting); + } + return looting; + } + + public static int getLootingLevel(EntityLivingBase target, DamageSource cause, int level) + { LootingLevelEvent event = new LootingLevelEvent(target, cause, level); MinecraftForge.EVENT_BUS.post(event); return event.getLootingLevel(); diff --git a/src/main/java/net/minecraftforge/event/entity/player/PlayerDropsEvent.java b/src/main/java/net/minecraftforge/event/entity/player/PlayerDropsEvent.java index f68ea05bce..6aa3454576 100644 --- a/src/main/java/net/minecraftforge/event/entity/player/PlayerDropsEvent.java +++ b/src/main/java/net/minecraftforge/event/entity/player/PlayerDropsEvent.java @@ -21,9 +21,9 @@ package net.minecraftforge.event.entity.player; import java.util.List; +import net.minecraftforge.common.ForgeHooks; import net.minecraftforge.fml.common.eventhandler.Cancelable; -import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.DamageSource; @@ -48,10 +48,7 @@ public class PlayerDropsEvent extends LivingDropsEvent */ public PlayerDropsEvent(EntityPlayer entity, DamageSource source, List drops, boolean recentlyHit) { - super(entity, source, drops, - (source.getEntity() instanceof EntityPlayer) ? - EnchantmentHelper.getLootingModifier(((EntityPlayer)source.getEntity())) : 0, - recentlyHit); + super(entity, source, drops, ForgeHooks.getLootingLevel(entity, source.getEntity(), source), recentlyHit); this.entityPlayer = entity; }