Search in sources :

Example 36 with DamageSource

use of net.minecraft.util.DamageSource in project ArsMagica2 by Mithion.

the class DamageSources method causeEntityMagicDamage.

public static DamageSource causeEntityMagicDamage(EntityLivingBase source) {
    DamageSource ds = new EntityDamageSource("magic", source);
    ds.setMagicDamage();
    setDamageSourceUnblockable(ds);
    return ds;
}
Also used : EntityDamageSource(net.minecraft.util.EntityDamageSource) DamageSource(net.minecraft.util.DamageSource) EntityDamageSource(net.minecraft.util.EntityDamageSource)

Example 37 with DamageSource

use of net.minecraft.util.DamageSource in project Overloaded by CJ-MC-Mods.

the class ArmorEventHandler method onLivingAttackedEvent.

@SubscribeEvent(priority = EventPriority.LOWEST)
public void onLivingAttackedEvent(LivingAttackEvent event) {
    Entity entity = event.getEntity();
    if (!(entity instanceof PlayerEntity))
        return;
    PlayerEntity player = ((PlayerEntity) entity);
    boolean setEquipped = isMultiArmorSetEquipped(player);
    if (setEquipped) {
        DamageSource damageSource = event.getSource();
        int energyCost = OverloadedConfig.INSTANCE.multiArmorConfig.baseCost;
        float damageAmount = (float) (event.getAmount() * OverloadedConfig.INSTANCE.multiArmorConfig.damageMultiplier);
        if (damageSource.isBypassMagic())
            damageAmount *= OverloadedConfig.INSTANCE.multiArmorConfig.absoluteDamageMultiplier;
        if (damageSource.isBypassArmor())
            damageAmount *= OverloadedConfig.INSTANCE.multiArmorConfig.unblockableMultiplier;
        if (damageAmount > Integer.MAX_VALUE)
            return;
        energyCost += damageAmount;
        // Overflow
        if (energyCost < 0)
            return;
        if (extractEnergy(player, energyCost, false)) {
            event.setCanceled(true);
        }
    }
}
Also used : Entity(net.minecraft.entity.Entity) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) DamageSource(net.minecraft.util.DamageSource) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 38 with DamageSource

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

the class DamageEventHandler method createArmorModifiers.

public static Optional<List<DamageFunction>> createArmorModifiers(EntityLivingBase entityLivingBase, DamageSource damageSource, double damage) {
    if (!damageSource.isUnblockable()) {
        damage *= 25;
        net.minecraft.item.ItemStack[] inventory = Iterables.toArray(entityLivingBase.getArmorInventoryList(), net.minecraft.item.ItemStack.class);
        List<DamageFunction> modifiers = new ArrayList<>();
        List<DamageObject> damageObjects = new ArrayList<>();
        for (int index = 0; index < inventory.length; index++) {
            net.minecraft.item.ItemStack itemStack = inventory[index];
            if (itemStack.isEmpty()) {
                continue;
            }
            Item item = itemStack.getItem();
            if (item instanceof ItemArmor) {
                ItemArmor armor = (ItemArmor) item;
                double reduction = armor.damageReduceAmount / 25D;
                DamageObject object = new DamageObject();
                object.slot = index;
                object.ratio = reduction;
                damageObjects.add(object);
            }
        }
        boolean first = true;
        double ratio = 0;
        for (DamageObject prop : damageObjects) {
            EquipmentType type = resolveEquipment(prop.slot);
            final DamageObject object = new DamageObject();
            object.ratio = ratio;
            if (first) {
                object.previousDamage = damage;
                object.augment = true;
            }
            DoubleUnaryOperator function = incomingDamage -> {
                incomingDamage *= 25;
                if (object.augment) {
                    // This is the damage that needs to be archived for the "first" armor modifier
                    // function since the armor modifiers work based on the initial damage and not as
                    // a chain one after another.
                    damageToHandle = incomingDamage;
                }
                double functionDamage = damageToHandle;
                object.previousDamage = functionDamage;
                object.ratio = prop.ratio;
                object.ratio += prop.ratio;
                return -((functionDamage * prop.ratio) / 25);
            };
            ratio += prop.ratio;
            // TODO: direct cause creation: bad bad bad
            DamageModifier modifier = DamageModifier.builder().cause(Cause.of(EventContext.empty(), ((org.spongepowered.api.item.inventory.ItemStack) inventory[prop.slot]).createSnapshot(), // We need this property to refer to the slot.
            prop, // We need this object later on.
            object)).type(DamageModifierTypes.ARMOR).build();
            modifiers.add(new DamageFunction(modifier, function));
            first = false;
        }
        if (!modifiers.isEmpty()) {
            return Optional.of(modifiers);
        }
    }
    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) ItemArmor(net.minecraft.item.ItemArmor) ArrayList(java.util.ArrayList) EquipmentType(org.spongepowered.api.item.inventory.equipment.EquipmentType) Item(net.minecraft.item.Item) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) ItemStack(org.spongepowered.api.item.inventory.ItemStack) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator)

Example 39 with DamageSource

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

the class DamageEventHandler method createEnchantmentModifiers.

public static Optional<List<DamageFunction>> createEnchantmentModifiers(EntityLivingBase entityLivingBase, DamageSource damageSource) {
    if (!damageSource.isDamageAbsolute()) {
        Iterable<net.minecraft.item.ItemStack> inventory = entityLivingBase.getArmorInventoryList();
        if (EnchantmentHelper.getEnchantmentModifierDamage(Lists.newArrayList(entityLivingBase.getArmorInventoryList()), damageSource) == 0) {
            return Optional.empty();
        }
        List<DamageFunction> modifiers = new ArrayList<>();
        boolean first = true;
        int totalModifier = 0;
        for (net.minecraft.item.ItemStack itemStack : inventory) {
            if (itemStack.isEmpty()) {
                continue;
            }
            final Multimap<Enchantment, Short> enchantments = LinkedHashMultimap.create();
            NBTTagList enchantmentList = itemStack.getEnchantmentTagList();
            if (enchantmentList == null) {
                continue;
            }
            for (int i = 0; i < enchantmentList.tagCount(); ++i) {
                final short enchantmentId = enchantmentList.getCompoundTagAt(i).getShort(NbtDataUtil.ITEM_ENCHANTMENT_ID);
                final short level = enchantmentList.getCompoundTagAt(i).getShort(NbtDataUtil.ITEM_ENCHANTMENT_LEVEL);
                if (Enchantment.getEnchantmentByID(enchantmentId) != null) {
                    // Ok, we have an enchantment!
                    final Enchantment enchantment = Enchantment.getEnchantmentByID(enchantmentId);
                    final int temp = enchantment.calcModifierDamage(level, damageSource);
                    if (temp != 0) {
                        enchantments.put(enchantment, level);
                    }
                }
            }
            ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(itemStack);
            for (Map.Entry<Enchantment, Collection<Short>> enchantment : enchantments.asMap().entrySet()) {
                final DamageObject object = new DamageObject();
                int modifierTemp = 0;
                for (short level : enchantment.getValue()) {
                    modifierTemp += enchantment.getKey().calcModifierDamage(level, damageSource);
                }
                final int modifier = modifierTemp;
                object.previousDamage = totalModifier;
                if (object.previousDamage > 25) {
                    object.previousDamage = 25;
                }
                totalModifier += modifier;
                object.augment = first;
                object.ratio = modifier;
                DoubleUnaryOperator enchantmentFunction = damageIn -> {
                    if (object.augment) {
                        enchantmentDamageTracked = damageIn;
                    }
                    if (damageIn <= 0) {
                        return 0D;
                    }
                    double actualDamage = enchantmentDamageTracked;
                    if (object.previousDamage > 25) {
                        return 0D;
                    }
                    double modifierDamage = actualDamage;
                    double magicModifier;
                    if (modifier > 0 && modifier <= 20) {
                        int j = 25 - modifier;
                        magicModifier = modifierDamage * j;
                        modifierDamage = magicModifier / 25.0F;
                    }
                    return -Math.max(actualDamage - modifierDamage, 0.0D);
                };
                if (first) {
                    first = false;
                }
                // TODO: direct cause creation: bad bad bad
                DamageModifier enchantmentModifier = DamageModifier.builder().cause(Cause.of(EventContext.empty(), enchantment, snapshot, entityLivingBase)).type(DamageModifierTypes.ARMOR_ENCHANTMENT).build();
                modifiers.add(new DamageFunction(enchantmentModifier, enchantmentFunction));
            }
            if (!modifiers.isEmpty()) {
                return Optional.of(modifiers);
            }
        }
    }
    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) ArrayList(java.util.ArrayList) NBTTagList(net.minecraft.nbt.NBTTagList) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Collection(java.util.Collection) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Enchantment(net.minecraft.enchantment.Enchantment) Map(java.util.Map) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator)

Example 40 with DamageSource

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

the class MixinEntityLivingBase method onDeath.

/**
 * @author blood - May 12th, 2016
 * @author gabizou - June 4th, 2016 - Update for 1.9.4 and Cause Tracker changes
 *
 * @reason SpongeForge requires an overwrite so we do it here instead. This handles all living entity death events
 * (except players). Note should be taken that normally, there are lists for drops captured, however, the drops
 * are retroactively captured by the PhaseTracker and associated through the different phases, depending on
 * how they are handled, so no lists and flags on the entities themselves are needed as they are tracked through
 * the {@link PhaseContext} of the current {@link IPhaseState} at which this method is called. The compatibility
 * for Forge's events to throw are handled specially in SpongeForge.
 */
@Overwrite
public void onDeath(DamageSource cause) {
    // Sponge Start - Call our event, and forge's event
    // This will transitively call the forge event
    SpongeCommonEventFactory.callDestructEntityEventDeath((EntityLivingBase) (Object) this, cause);
    // Double check that the PhaseTracker is already capturing the Death phase
    final PhaseTracker phaseTracker = PhaseTracker.getInstance();
    final boolean isMainThread = !this.world.isRemote || Sponge.isServerAvailable() && Sponge.getServer().isMainThread();
    try (final StackFrame frame = isMainThread ? Sponge.getCauseStackManager().pushCauseFrame() : null) {
        if (!this.world.isRemote) {
            final PhaseData peek = phaseTracker.getCurrentPhaseData();
            final IPhaseState state = peek.state;
            this.tracksEntityDeaths = !phaseTracker.getCurrentState().tracksEntityDeaths() && state != EntityPhase.State.DEATH;
            if (this.tracksEntityDeaths) {
                Sponge.getCauseStackManager().pushCause(this);
                final PhaseContext<?> context = EntityPhase.State.DEATH.createPhaseContext().setDamageSource((org.spongepowered.api.event.cause.entity.damage.source.DamageSource) cause).source(this);
                this.getNotifierUser().ifPresent(context::notifier);
                this.getCreatorUser().ifPresent(context::owner);
                context.buildAndSwitch();
            }
        } else {
            this.tracksEntityDeaths = false;
        }
        // Sponge End
        if (this.dead) {
            // Sponge Start - ensure that we finish the tracker if necessary
            if (this.tracksEntityDeaths && !properlyOverridesOnDeathForCauseTrackerCompletion()) {
                phaseTracker.completePhase(EntityPhase.State.DEATH);
            }
            // Sponge End
            return;
        }
        Entity entity = cause.getTrueSource();
        EntityLivingBase entitylivingbase = this.getAttackingEntity();
        if (this.scoreValue >= 0 && entitylivingbase != null) {
            entitylivingbase.awardKillScore((EntityLivingBase) (Object) this, this.scoreValue, cause);
        }
        if (entity != null) {
            entity.onKillEntity((EntityLivingBase) (Object) this);
        }
        this.dead = true;
        this.getCombatTracker().reset();
        if (!this.world.isRemote) {
            int i = 0;
            if (entity instanceof EntityPlayer) {
                i = EnchantmentHelper.getLootingModifier((EntityLivingBase) entity);
            }
            if (this.canDropLoot() && this.world.getGameRules().getBoolean("doMobLoot")) {
                boolean flag = this.recentlyHit > 0;
                this.dropLoot(flag, i, cause);
            }
        }
        // Sponge Start - Don't send the state if this is a human. Fixes ghost items on client.
        if (!((EntityLivingBase) (Object) this instanceof EntityHuman)) {
            this.world.setEntityState((EntityLivingBase) (Object) this, (byte) 3);
        }
        if (phaseTracker != null && this.tracksEntityDeaths && !properlyOverridesOnDeathForCauseTrackerCompletion()) {
            this.tracksEntityDeaths = false;
            phaseTracker.completePhase(EntityPhase.State.DEATH);
        }
    }
// Sponge End
}
Also used : Entity(net.minecraft.entity.Entity) EntityHuman(org.spongepowered.common.entity.living.human.EntityHuman) PhaseData(org.spongepowered.common.event.tracking.PhaseData) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) DamageSource(net.minecraft.util.DamageSource) IPhaseState(org.spongepowered.common.event.tracking.IPhaseState) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) IMixinEntityLivingBase(org.spongepowered.common.interfaces.entity.IMixinEntityLivingBase) EntityLivingBase(net.minecraft.entity.EntityLivingBase) EntityPlayer(net.minecraft.entity.player.EntityPlayer) DamageObject(org.spongepowered.common.event.damage.DamageObject) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Aggregations

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