Search in sources :

Example 11 with DamageFunction

use of org.spongepowered.api.event.cause.entity.damage.DamageFunction in project SpongeForge by SpongePowered.

the class StaticMixinForgeHelper method createArmorModifiers.

private static Optional<List<DamageFunction>> createArmorModifiers(List<ISpecialArmor.ArmorProperties> dmgVals, List<ItemStack> inventory, double damage) {
    if (dmgVals.size() > 0) {
        final List<DamageFunction> list = new ArrayList<>();
        ISpecialArmor.ArmorProperties[] props = dmgVals.toArray(new ISpecialArmor.ArmorProperties[dmgVals.size()]);
        sortProperties(props, damage);
        boolean first = true;
        int level = props[0].Priority;
        double ratio = 0;
        for (ISpecialArmor.ArmorProperties prop : props) {
            EquipmentType type = DamageEventHandler.resolveEquipment(prop.Slot);
            final DamageEventObject object = new DamageEventObject();
            object.previousLevel = prop.Priority;
            object.previousRatio = ratio;
            if (first) {
                object.previousDamage = damage;
                object.augment = true;
            }
            DoubleUnaryOperator function = incomingDamage -> {
                incomingDamage *= 25;
                if (object.augment) {
                    damageToHandle = incomingDamage;
                }
                double functionDamage = damageToHandle;
                object.previousDamage = functionDamage;
                object.level = prop.Priority;
                object.ratio = prop.AbsorbRatio;
                if (object.previousLevel != prop.Priority) {
                    functionDamage -= (functionDamage * object.previousRatio);
                    damageToHandle = functionDamage;
                    object.ratio = 0;
                    object.level = prop.Priority;
                }
                object.ratio += prop.AbsorbRatio;
                return -((functionDamage * prop.AbsorbRatio) / 25);
            };
            // We still need to "simulate" the original function so that the ratios are handled
            if (level != prop.Priority) {
                damage -= (damage * ratio);
                ratio = 0;
                level = prop.Priority;
            }
            ratio += prop.AbsorbRatio;
            EventContextKey<ItemStackSnapshot> contextKey = ARMOR_KEYS.get("armor:" + type.getId());
            if (contextKey == null) {
                contextKey = new EventContextKey<ItemStackSnapshot>() {

                    @Override
                    public Class<ItemStackSnapshot> getAllowedType() {
                        return ItemStackSnapshot.class;
                    }

                    @Override
                    public String getId() {
                        return "armor:" + type.getId();
                    }

                    @Override
                    public String getName() {
                        return type.getName();
                    }
                };
                ARMOR_KEYS.put("armor: " + type.getId(), contextKey);
            }
            final ItemStack itemStack = inventory.get(prop.Slot);
            DamageModifier modifier = DamageModifier.builder().cause(Cause.of(EventContext.builder().add(contextKey, ItemStackUtil.snapshotOf(itemStack)).add(ARMOR_PROPERTY, prop).add(DAMAGE_MODIFIER_OBJECT, object).build(), itemStack)).type(DamageModifierTypes.ARMOR).build();
            list.add(DamageFunction.of(modifier, function));
            first = false;
        }
        return Optional.of(list);
    }
    return Optional.empty();
}
Also used : SpongeEntityType(org.spongepowered.common.entity.SpongeEntityType) Arrays(java.util.Arrays) Loader(net.minecraftforge.fml.common.Loader) EntityEntry(net.minecraftforge.fml.common.registry.EntityEntry) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) DamageModifierTypes(org.spongepowered.api.event.cause.entity.damage.DamageModifierTypes) ItemStackUtil(org.spongepowered.common.item.inventory.util.ItemStackUtil) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) Lists(com.google.common.collect.Lists) Map(java.util.Map) NonNullList(net.minecraft.util.NonNullList) EventContextKey(org.spongepowered.api.event.cause.EventContextKey) ItemArmor(net.minecraft.item.ItemArmor) Nullable(javax.annotation.Nullable) Entity(net.minecraft.entity.Entity) EquipmentType(org.spongepowered.api.item.inventory.equipment.EquipmentType) EntityList(net.minecraft.entity.EntityList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SpongeMod(org.spongepowered.mod.SpongeMod) EntityTypeRegistryModule(org.spongepowered.common.registry.type.entity.EntityTypeRegistryModule) DamageSource(net.minecraft.util.DamageSource) ISpecialArmor(net.minecraftforge.common.ISpecialArmor) IBlockState(net.minecraft.block.state.IBlockState) DamageEventHandler(org.spongepowered.common.event.damage.DamageEventHandler) Cause(org.spongepowered.api.event.cause.Cause) List(java.util.List) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) EntityLivingBase(net.minecraft.entity.EntityLivingBase) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Optional(java.util.Optional) ModContainer(net.minecraftforge.fml.common.ModContainer) EventContext(org.spongepowered.api.event.cause.EventContext) ArrayList(java.util.ArrayList) EquipmentType(org.spongepowered.api.item.inventory.equipment.EquipmentType) ISpecialArmor(net.minecraftforge.common.ISpecialArmor) 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) ItemStack(net.minecraft.item.ItemStack) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator)

Example 12 with DamageFunction

use of org.spongepowered.api.event.cause.entity.damage.DamageFunction in project SpongeCommon by SpongePowered.

the class DamageEventHandler method provideCriticalAttackTuple.

public static DamageFunction provideCriticalAttackTuple(EntityPlayer player) {
    // TODO: direct cause creation: bad bad bad
    final DamageModifier modifier = DamageModifier.builder().cause(Cause.of(EventContext.empty(), player)).type(DamageModifierTypes.CRITICAL_HIT).build();
    DoubleUnaryOperator function = (damage) -> damage * 1.5F;
    return new DamageFunction(modifier, function);
}
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 13 with DamageFunction

use of org.spongepowered.api.event.cause.entity.damage.DamageFunction 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 14 with DamageFunction

use of org.spongepowered.api.event.cause.entity.damage.DamageFunction 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 15 with DamageFunction

use of org.spongepowered.api.event.cause.entity.damage.DamageFunction in project SpongeCommon by SpongePowered.

the class DamageEventHandler method provideCooldownAttackStrengthFunction.

public static DamageFunction provideCooldownAttackStrengthFunction(EntityPlayer player, float attackStrength) {
    // TODO: direct cause creation: bad bad bad
    final DamageModifier modifier = DamageModifier.builder().cause(Cause.of(EventContext.empty(), player)).type(DamageModifierTypes.ATTACK_COOLDOWN).build();
    // The formula is as follows:
    // Since damage needs to be "multiplied", this needs to basically add negative damage but re-add the "reduced" damage.
    DoubleUnaryOperator function = (damage) -> -damage + (damage * (0.2F + attackStrength * attackStrength * 0.8F));
    return new DamageFunction(modifier, function);
}
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)

Aggregations

DamageFunction (org.spongepowered.api.event.cause.entity.damage.DamageFunction)25 DamageModifier (org.spongepowered.api.event.cause.entity.damage.DamageModifier)20 List (java.util.List)14 Cause (org.spongepowered.api.event.cause.Cause)13 EventContext (org.spongepowered.api.event.cause.EventContext)13 DoubleUnaryOperator (java.util.function.DoubleUnaryOperator)12 Lists (com.google.common.collect.Lists)11 ArrayList (java.util.ArrayList)11 Map (java.util.Map)11 EntityLivingBase (net.minecraft.entity.EntityLivingBase)10 EntityPlayer (net.minecraft.entity.player.EntityPlayer)10 DamageSource (net.minecraft.util.DamageSource)10 DamageEntityEvent (org.spongepowered.api.event.entity.DamageEntityEvent)10 Optional (java.util.Optional)9 Entity (net.minecraft.entity.Entity)9 Sponge (org.spongepowered.api.Sponge)9 DamageModifierTypes (org.spongepowered.api.event.cause.entity.damage.DamageModifierTypes)9 IBlockState (net.minecraft.block.state.IBlockState)8 EnchantmentHelper (net.minecraft.enchantment.EnchantmentHelper)8 MobEffects (net.minecraft.init.MobEffects)8