From 432e3ab08a725a313fb573a630c15acee584c4b2 Mon Sep 17 00:00:00 2001 From: Bernhard Bonigl Date: Sun, 3 Jul 2016 15:19:31 +0200 Subject: [PATCH] Add an event that allows to modify the looting level based on damage source --- .../entity/EntityLivingBase.java.patch | 4 ++- .../net/minecraftforge/common/ForgeHooks.java | 7 +++++ .../entity/living/LootingLevelEvent.java | 29 +++++++++++++++++++ .../minecraftforge/debug/LootTablesDebug.java | 13 +++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/minecraftforge/event/entity/living/LootingLevelEvent.java diff --git a/patches/minecraft/net/minecraft/entity/EntityLivingBase.java.patch b/patches/minecraft/net/minecraft/entity/EntityLivingBase.java.patch index c842cddc9..b5b2dbe67 100644 --- a/patches/minecraft/net/minecraft/entity/EntityLivingBase.java.patch +++ b/patches/minecraft/net/minecraft/entity/EntityLivingBase.java.patch @@ -76,9 +76,11 @@ if (!this.field_70729_aU) { Entity entity = p_70645_1_.func_76346_g(); -@@ -1070,11 +1076,24 @@ +@@ -1069,12 +1075,26 @@ + { i = EnchantmentHelper.func_185283_h((EntityLivingBase)entity); } ++ i = net.minecraftforge.common.ForgeHooks.getLootingLevel(this, p_70645_1_, i); + captureDrops = true; + capturedDrops.clear(); diff --git a/src/main/java/net/minecraftforge/common/ForgeHooks.java b/src/main/java/net/minecraftforge/common/ForgeHooks.java index 9e41636a7..dae6dea94 100644 --- a/src/main/java/net/minecraftforge/common/ForgeHooks.java +++ b/src/main/java/net/minecraftforge/common/ForgeHooks.java @@ -109,6 +109,7 @@ import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent; import net.minecraftforge.event.entity.living.LivingFallEvent; import net.minecraftforge.event.entity.living.LivingHurtEvent; import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent; +import net.minecraftforge.event.entity.living.LootingLevelEvent; import net.minecraftforge.event.entity.player.AnvilRepairEvent; import net.minecraftforge.event.entity.player.AttackEntityEvent; import net.minecraftforge.event.entity.player.PlayerInteractEvent; @@ -515,6 +516,12 @@ 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) { + LootingLevelEvent event = new LootingLevelEvent(target, cause, level); + MinecraftForge.EVENT_BUS.post(event); + return event.getLootingLevel(); + } + public static boolean isLivingOnLadder(IBlockState state, World world, BlockPos pos, EntityLivingBase entity) { boolean isSpectator = (entity instanceof EntityPlayer && ((EntityPlayer)entity).isSpectator()); diff --git a/src/main/java/net/minecraftforge/event/entity/living/LootingLevelEvent.java b/src/main/java/net/minecraftforge/event/entity/living/LootingLevelEvent.java new file mode 100644 index 000000000..3d91472c3 --- /dev/null +++ b/src/main/java/net/minecraftforge/event/entity/living/LootingLevelEvent.java @@ -0,0 +1,29 @@ +package net.minecraftforge.event.entity.living; + +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.util.DamageSource; + +public class LootingLevelEvent extends LivingEvent { + + private final DamageSource damageSource; + + private int lootingLevel; + + public LootingLevelEvent(EntityLivingBase entity, DamageSource damageSource, int lootingLevel) { + super(entity); + this.damageSource = damageSource; + this.lootingLevel = lootingLevel; + } + + public DamageSource getDamageSource() { + return damageSource; + } + + public int getLootingLevel() { + return lootingLevel; + } + + public void setLootingLevel(int lootingLevel) { + this.lootingLevel = lootingLevel; + } +} diff --git a/src/test/java/net/minecraftforge/debug/LootTablesDebug.java b/src/test/java/net/minecraftforge/debug/LootTablesDebug.java index 7b4a89e94..4e3d943d7 100644 --- a/src/test/java/net/minecraftforge/debug/LootTablesDebug.java +++ b/src/test/java/net/minecraftforge/debug/LootTablesDebug.java @@ -1,6 +1,9 @@ package net.minecraftforge.debug; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.projectile.EntityArrow; import net.minecraft.init.Items; +import net.minecraft.util.DamageSource; import net.minecraft.util.ResourceLocation; import net.minecraft.world.storage.loot.LootEntryItem; import net.minecraft.world.storage.loot.LootPool; @@ -9,6 +12,7 @@ import net.minecraft.world.storage.loot.conditions.LootCondition; import net.minecraft.world.storage.loot.functions.LootFunction; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.LootTableLoadEvent; +import net.minecraftforge.event.entity.living.LootingLevelEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; @@ -39,4 +43,13 @@ public class LootTablesDebug { // Get rid of all building mats. Which is pool #3, index starts at 0, but 0 is named "main" event.getTable().removePool("pool3"); } + + @SubscribeEvent + public void lootingEvent(LootingLevelEvent event) { + // if the player shoots something with a projectile, use looting 3 + DamageSource damageSource = event.getDamageSource(); + if(damageSource.isProjectile() && damageSource.getEntity() instanceof EntityPlayer && damageSource.getSourceOfDamage() instanceof EntityArrow) { + event.setLootingLevel(3); + } + } }