Search in sources :

Example 1 with DamageSource

use of net.minecraft.util.DamageSource in project NetherEx by LogicTechCorp.

the class EventHandler method onLivingAttacked.

@SubscribeEvent
public static void onLivingAttacked(LivingAttackEvent event) {
    EntityLivingBase entity = (EntityLivingBase) event.getEntity();
    World world = entity.getEntityWorld();
    BlockPos pos = entity.getPosition();
    DamageSource source = event.getSource();
    if (source.isFireDamage()) {
        if (!entity.isImmuneToFire() && entity.isRiding() && entity.getRidingEntity() instanceof EntityObsidianBoat) {
            event.setCanceled(true);
        }
    }
    if (entity instanceof EntityPlayer) {
        EntityPlayer player = (EntityPlayer) entity;
        if (source.isFireDamage()) {
            if (ArmorUtil.isWearingFullArmorSet(player, NetherExMaterials.ARMOR_HIDE_SALAMANDER)) {
                event.setCanceled(true);
            }
        }
        if (player.dimension == -1) {
            if (source == DamageSource.LAVA && player.isPotionActive(MobEffects.FIRE_RESISTANCE)) {
                player.addStat(NetherExAchievements.STAYIN_FROSTY);
            }
        }
    }
}
Also used : DamageSource(net.minecraft.util.DamageSource) EntityObsidianBoat(nex.entity.item.EntityObsidianBoat) EntityLivingBase(net.minecraft.entity.EntityLivingBase) EntityPlayer(net.minecraft.entity.player.EntityPlayer) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 2 with DamageSource

use of net.minecraft.util.DamageSource in project NetherEx by LogicTechCorp.

the class EventHandler method onLivingDeath.

@SubscribeEvent
public static void onLivingDeath(LivingDeathEvent event) {
    Entity entity = event.getEntity();
    World world = entity.getEntityWorld();
    BlockPos pos = entity.getPosition();
    DamageSource source = event.getSource();
    if (entity instanceof AbstractSkeleton) {
        if (source.getSourceOfDamage() instanceof EntityPlayer) {
            EntityPlayer player = (EntityPlayer) source.getSourceOfDamage();
            if (ArmorUtil.isWearingFullArmorSet(player, NetherExMaterials.ARMOR_BONE_WITHERED)) {
                player.addStat(NetherExAchievements.FROM_WITHIN);
            }
        }
    }
}
Also used : Entity(net.minecraft.entity.Entity) AbstractSkeleton(net.minecraft.entity.monster.AbstractSkeleton) DamageSource(net.minecraft.util.DamageSource) EntityPlayer(net.minecraft.entity.player.EntityPlayer) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 3 with DamageSource

use of net.minecraft.util.DamageSource in project SpongeCommon by SpongePowered.

the class DamageSourceRegistryModule method registerDefaults.

@Override
public void registerDefaults() {
    try {
        // These need to be instantiated after the DamageTypeRegistryModule has had a chance to register
        // the damage types, otherwise it will fail and have invalid types.
        DAMAGESOURCE_POISON = (new DamageSource("poison")).setDamageBypassesArmor().setMagicDamage();
        DAMAGESOURCE_MELTING = (new DamageSource("melting")).setDamageBypassesArmor().setFireDamage();
        IGNORED_DAMAGE_SOURCE = new DamageSource("spongespecific").setDamageBypassesArmor().setDamageAllowedInCreativeMode();
        DamageSources.class.getDeclaredField("DROWNING").set(null, DamageSource.DROWN);
        DamageSources.class.getDeclaredField("FALLING").set(null, DamageSource.FALL);
        DamageSources.class.getDeclaredField("FIRE_TICK").set(null, DamageSource.ON_FIRE);
        DamageSources.class.getDeclaredField("GENERIC").set(null, DamageSource.GENERIC);
        DamageSources.class.getDeclaredField("MAGIC").set(null, DamageSource.MAGIC);
        DamageSources.class.getDeclaredField("MELTING").set(null, DAMAGESOURCE_MELTING);
        DamageSources.class.getDeclaredField("POISON").set(null, DAMAGESOURCE_POISON);
        DamageSources.class.getDeclaredField("STARVATION").set(null, DamageSource.STARVE);
        DamageSources.class.getDeclaredField("WITHER").set(null, DamageSource.WITHER);
        DamageSources.class.getDeclaredField("VOID").set(null, DamageSource.OUT_OF_WORLD);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : DamageSource(net.minecraft.util.DamageSource) DamageSources(org.spongepowered.api.event.cause.entity.damage.source.DamageSources)

Example 4 with DamageSource

use of net.minecraft.util.DamageSource in project SpongeCommon by SpongePowered.

the class DamageEventHandler method createAbsorptionModifier.

public static Optional<DamageFunction> createAbsorptionModifier(EntityLivingBase entityLivingBase, DamageSource damageSource) {
    final float absorptionAmount = entityLivingBase.getAbsorptionAmount();
    if (absorptionAmount > 0) {
        DoubleUnaryOperator function = damage -> -(Math.max(damage - Math.max(damage - absorptionAmount, 0.0F), 0.0F));
        // TODO: direct cause creation: bad bad bad
        DamageModifier modifier = DamageModifier.builder().cause(Cause.of(EventContext.empty(), entityLivingBase)).type(DamageModifierTypes.ABSORPTION).build();
        return Optional.of(new DamageFunction(modifier, function));
    }
    return Optional.empty();
}
Also used : EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Item(net.minecraft.item.Item) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) ItemStackUtil(org.spongepowered.common.item.inventory.util.ItemStackUtil) NBTTagList(net.minecraft.nbt.NBTTagList) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Map(java.util.Map) EntityDamageSourceIndirect(net.minecraft.util.EntityDamageSourceIndirect) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) ItemArmor(net.minecraft.item.ItemArmor) LinkedHashMultimap(com.google.common.collect.LinkedHashMultimap) NbtDataUtil(org.spongepowered.common.data.util.NbtDataUtil) Location(org.spongepowered.api.world.Location) IMixinLocation(org.spongepowered.common.interfaces.world.IMixinLocation) Predicate(java.util.function.Predicate) EquipmentType(org.spongepowered.api.item.inventory.equipment.EquipmentType) Collection(java.util.Collection) Sponge(org.spongepowered.api.Sponge) EnchantmentHelper(net.minecraft.enchantment.EnchantmentHelper) EntityUtil(org.spongepowered.common.entity.EntityUtil) Cause(org.spongepowered.api.event.cause.Cause) List(java.util.List) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) EntityPlayer(net.minecraft.entity.player.EntityPlayer) World(org.spongepowered.api.world.World) Optional(java.util.Optional) MobEffects(net.minecraft.init.MobEffects) EventContext(org.spongepowered.api.event.cause.EventContext) IMixinChunkProviderServer(org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer) Iterables(com.google.common.collect.Iterables) Enchantment(net.minecraft.enchantment.Enchantment) AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) EntityEquipmentSlot(net.minecraft.inventory.EntityEquipmentSlot) DamageModifierTypes(org.spongepowered.api.event.cause.entity.damage.DamageModifierTypes) Multimap(com.google.common.collect.Multimap) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Chunk(net.minecraft.world.chunk.Chunk) Entity(net.minecraft.entity.Entity) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) BlockPos(net.minecraft.util.math.BlockPos) DamageSource(net.minecraft.util.DamageSource) IBlockState(net.minecraft.block.state.IBlockState) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) EntityLivingBase(net.minecraft.entity.EntityLivingBase) MathHelper(net.minecraft.util.math.MathHelper) EntityDamageSource(net.minecraft.util.EntityDamageSource) EnumCreatureAttribute(net.minecraft.entity.EnumCreatureAttribute) EquipmentTypes(org.spongepowered.api.item.inventory.equipment.EquipmentTypes) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator)

Example 5 with DamageSource

use of net.minecraft.util.DamageSource in project SpongeCommon by SpongePowered.

the class DamageEventHandler method generateCauseFor.

public static void generateCauseFor(DamageSource damageSource) {
    if (damageSource instanceof EntityDamageSourceIndirect) {
        net.minecraft.entity.Entity source = damageSource.getTrueSource();
        if (!(source instanceof EntityPlayer) && source != null) {
            final IMixinEntity mixinEntity = EntityUtil.toMixin(source);
            mixinEntity.getNotifierUser().ifPresent(notifier -> Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier));
            mixinEntity.getCreatorUser().ifPresent(owner -> Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, owner));
        }
    } else if (damageSource instanceof EntityDamageSource) {
        net.minecraft.entity.Entity source = damageSource.getTrueSource();
        if (!(source instanceof EntityPlayer) && source != null) {
            final IMixinEntity mixinEntity = EntityUtil.toMixin(source);
            // TODO only have a UUID, want a user
            mixinEntity.getNotifierUser().ifPresent(notifier -> Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier));
            mixinEntity.getCreatorUser().ifPresent(creator -> Sponge.getCauseStackManager().addContext(EventContextKeys.CREATOR, creator));
        }
    } else if (damageSource instanceof BlockDamageSource) {
        Location<org.spongepowered.api.world.World> location = ((BlockDamageSource) damageSource).getLocation();
        BlockPos blockPos = ((IMixinLocation) (Object) location).getBlockPos();
        final IMixinChunk mixinChunk = (IMixinChunk) ((net.minecraft.world.World) location.getExtent()).getChunkFromBlockCoords(blockPos);
        mixinChunk.getBlockNotifier(blockPos).ifPresent(notifier -> Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier));
        mixinChunk.getBlockOwner(blockPos).ifPresent(owner -> Sponge.getCauseStackManager().addContext(EventContextKeys.CREATOR, owner));
    }
    Sponge.getCauseStackManager().pushCause(damageSource);
}
Also used : EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Item(net.minecraft.item.Item) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) ItemStackUtil(org.spongepowered.common.item.inventory.util.ItemStackUtil) NBTTagList(net.minecraft.nbt.NBTTagList) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Map(java.util.Map) EntityDamageSourceIndirect(net.minecraft.util.EntityDamageSourceIndirect) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) ItemArmor(net.minecraft.item.ItemArmor) LinkedHashMultimap(com.google.common.collect.LinkedHashMultimap) NbtDataUtil(org.spongepowered.common.data.util.NbtDataUtil) Location(org.spongepowered.api.world.Location) IMixinLocation(org.spongepowered.common.interfaces.world.IMixinLocation) Predicate(java.util.function.Predicate) EquipmentType(org.spongepowered.api.item.inventory.equipment.EquipmentType) Collection(java.util.Collection) Sponge(org.spongepowered.api.Sponge) EnchantmentHelper(net.minecraft.enchantment.EnchantmentHelper) EntityUtil(org.spongepowered.common.entity.EntityUtil) Cause(org.spongepowered.api.event.cause.Cause) List(java.util.List) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) EntityPlayer(net.minecraft.entity.player.EntityPlayer) World(org.spongepowered.api.world.World) Optional(java.util.Optional) MobEffects(net.minecraft.init.MobEffects) EventContext(org.spongepowered.api.event.cause.EventContext) IMixinChunkProviderServer(org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer) Iterables(com.google.common.collect.Iterables) Enchantment(net.minecraft.enchantment.Enchantment) AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) EntityEquipmentSlot(net.minecraft.inventory.EntityEquipmentSlot) DamageModifierTypes(org.spongepowered.api.event.cause.entity.damage.DamageModifierTypes) Multimap(com.google.common.collect.Multimap) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Chunk(net.minecraft.world.chunk.Chunk) Entity(net.minecraft.entity.Entity) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) BlockPos(net.minecraft.util.math.BlockPos) DamageSource(net.minecraft.util.DamageSource) IBlockState(net.minecraft.block.state.IBlockState) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) EntityLivingBase(net.minecraft.entity.EntityLivingBase) MathHelper(net.minecraft.util.math.MathHelper) EntityDamageSource(net.minecraft.util.EntityDamageSource) EnumCreatureAttribute(net.minecraft.entity.EnumCreatureAttribute) EquipmentTypes(org.spongepowered.api.item.inventory.equipment.EquipmentTypes) Entity(net.minecraft.entity.Entity) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Entity(net.minecraft.entity.Entity) EntityDamageSourceIndirect(net.minecraft.util.EntityDamageSourceIndirect) IMixinLocation(org.spongepowered.common.interfaces.world.IMixinLocation) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) World(org.spongepowered.api.world.World) EntityDamageSource(net.minecraft.util.EntityDamageSource) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) EntityPlayer(net.minecraft.entity.player.EntityPlayer) BlockPos(net.minecraft.util.math.BlockPos)

Aggregations

DamageSource (net.minecraft.util.DamageSource)54 Entity (net.minecraft.entity.Entity)26 EntityLivingBase (net.minecraft.entity.EntityLivingBase)26 EntityPlayer (net.minecraft.entity.player.EntityPlayer)26 ItemStack (net.minecraft.item.ItemStack)15 BlockPos (net.minecraft.util.math.BlockPos)10 World (net.minecraft.world.World)10 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 IBlockState (net.minecraft.block.state.IBlockState)8 Item (net.minecraft.item.Item)8 EnchantmentHelper (net.minecraft.enchantment.EnchantmentHelper)7 AxisAlignedBB (net.minecraft.util.math.AxisAlignedBB)7 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)6 MobEffects (net.minecraft.init.MobEffects)6 EntityEquipmentSlot (net.minecraft.inventory.EntityEquipmentSlot)6 MathHelper (net.minecraft.util.math.MathHelper)6 Collection (java.util.Collection)5 Map (java.util.Map)5