Add an event that allows to modify the looting level based on damage source

This commit is contained in:
Bernhard Bonigl 2016-07-03 15:19:31 +02:00
parent f58d1cae1a
commit 432e3ab08a
4 changed files with 52 additions and 1 deletions

View file

@ -76,9 +76,11 @@
if (!this.field_70729_aU) if (!this.field_70729_aU)
{ {
Entity entity = p_70645_1_.func_76346_g(); Entity entity = p_70645_1_.func_76346_g();
@@ -1070,11 +1076,24 @@ @@ -1069,12 +1075,26 @@
{
i = EnchantmentHelper.func_185283_h((EntityLivingBase)entity); i = EnchantmentHelper.func_185283_h((EntityLivingBase)entity);
} }
+ i = net.minecraftforge.common.ForgeHooks.getLootingLevel(this, p_70645_1_, i);
+ captureDrops = true; + captureDrops = true;
+ capturedDrops.clear(); + capturedDrops.clear();

View file

@ -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.LivingFallEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent; import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent; 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.AnvilRepairEvent;
import net.minecraftforge.event.entity.player.AttackEntityEvent; import net.minecraftforge.event.entity.player.AttackEntityEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent; 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()}); 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) public static boolean isLivingOnLadder(IBlockState state, World world, BlockPos pos, EntityLivingBase entity)
{ {
boolean isSpectator = (entity instanceof EntityPlayer && ((EntityPlayer)entity).isSpectator()); boolean isSpectator = (entity instanceof EntityPlayer && ((EntityPlayer)entity).isSpectator());

View file

@ -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;
}
}

View file

@ -1,6 +1,9 @@
package net.minecraftforge.debug; package net.minecraftforge.debug;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.init.Items; import net.minecraft.init.Items;
import net.minecraft.util.DamageSource;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraft.world.storage.loot.LootEntryItem; import net.minecraft.world.storage.loot.LootEntryItem;
import net.minecraft.world.storage.loot.LootPool; 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.minecraft.world.storage.loot.functions.LootFunction;
import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.LootTableLoadEvent; import net.minecraftforge.event.LootTableLoadEvent;
import net.minecraftforge.event.entity.living.LootingLevelEvent;
import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; 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" // Get rid of all building mats. Which is pool #3, index starts at 0, but 0 is named "main"
event.getTable().removePool("pool3"); 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);
}
}
} }