Search in sources :

Example 1 with DamageSource

use of net.minecraft.world.damagesource.DamageSource in project Denizen-For-Bukkit by DenizenScript.

the class EntityHelperImpl method getDamageTo.

@Override
public double getDamageTo(LivingEntity attacker, Entity target) {
    MobType monsterType;
    if (target instanceof LivingEntity) {
        monsterType = ((CraftLivingEntity) target).getHandle().getMobType();
    } else {
        monsterType = MobType.UNDEFINED;
    }
    double damage = 0;
    AttributeInstance attrib = attacker.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE);
    if (attrib != null) {
        damage = attrib.getValue();
    }
    if (attacker.getEquipment() != null && attacker.getEquipment().getItemInMainHand() != null) {
        damage += EnchantmentHelper.getDamageBonus(CraftItemStack.asNMSCopy(attacker.getEquipment().getItemInMainHand()), monsterType);
    }
    if (damage <= 0) {
        return 0;
    }
    if (target != null) {
        DamageSource source;
        if (attacker instanceof Player) {
            source = DamageSource.playerAttack(((CraftPlayer) attacker).getHandle());
        } else {
            source = DamageSource.mobAttack(((CraftLivingEntity) attacker).getHandle());
        }
        net.minecraft.world.entity.Entity nmsTarget = ((CraftEntity) target).getHandle();
        if (nmsTarget.isInvulnerableTo(source)) {
            return 0;
        }
        if (!(nmsTarget instanceof net.minecraft.world.entity.LivingEntity)) {
            return damage;
        }
        net.minecraft.world.entity.LivingEntity livingTarget = (net.minecraft.world.entity.LivingEntity) nmsTarget;
        damage = CombatRules.getDamageAfterAbsorb((float) damage, (float) livingTarget.getArmorValue(), (float) livingTarget.getAttributeValue(Attributes.ARMOR_TOUGHNESS));
        int enchantDamageModifier = EnchantmentHelper.getDamageProtection(livingTarget.getArmorSlots(), source);
        if (enchantDamageModifier > 0) {
            damage = CombatRules.getDamageAfterMagicAbsorb((float) damage, (float) enchantDamageModifier);
        }
    }
    return damage;
}
Also used : ServerPlayer(net.minecraft.server.level.ServerPlayer) DamageSource(net.minecraft.world.damagesource.DamageSource) LivingEntity(org.bukkit.entity.LivingEntity) AttributeInstance(org.bukkit.attribute.AttributeInstance) org.bukkit.entity(org.bukkit.entity) org.bukkit.craftbukkit.v1_18_R1.entity(org.bukkit.craftbukkit.v1_18_R1.entity) net.minecraft.world.entity(net.minecraft.world.entity)

Example 2 with DamageSource

use of net.minecraft.world.damagesource.DamageSource in project Denizen-For-Bukkit by DenizenScript.

the class EntityHelperImpl method damage.

@Override
public void damage(LivingEntity target, float amount, Entity source, EntityDamageEvent.DamageCause cause) {
    if (target == null) {
        return;
    }
    net.minecraft.world.entity.LivingEntity nmsTarget = ((CraftLivingEntity) target).getHandle();
    net.minecraft.world.entity.Entity nmsSource = source == null ? null : ((CraftEntity) source).getHandle();
    CraftEventFactory.entityDamage = nmsSource;
    try {
        DamageSource src = getSourceFor(nmsSource, cause);
        if (src instanceof FakeDamageSrc) {
            src = ((FakeDamageSrc) src).real;
            EntityDamageEvent ede = fireFakeDamageEvent(target, source, cause, amount);
            if (ede.isCancelled()) {
                return;
            }
        }
        nmsTarget.hurt(src, amount);
    } finally {
        CraftEventFactory.entityDamage = null;
    }
}
Also used : DamageSource(net.minecraft.world.damagesource.DamageSource) org.bukkit.craftbukkit.v1_17_R1.entity(org.bukkit.craftbukkit.v1_17_R1.entity) org.bukkit.entity(org.bukkit.entity) net.minecraft.world.entity(net.minecraft.world.entity) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Example 3 with DamageSource

use of net.minecraft.world.damagesource.DamageSource in project Denizen-For-Bukkit by DenizenScript.

the class EntityHelperImpl method getDamageTo.

@Override
public double getDamageTo(LivingEntity attacker, Entity target) {
    MobType monsterType;
    if (target instanceof LivingEntity) {
        monsterType = ((CraftLivingEntity) target).getHandle().getMobType();
    } else {
        monsterType = MobType.UNDEFINED;
    }
    double damage = 0;
    AttributeInstance attrib = attacker.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE);
    if (attrib != null) {
        damage = attrib.getValue();
    }
    if (attacker.getEquipment() != null && attacker.getEquipment().getItemInMainHand() != null) {
        damage += EnchantmentHelper.getDamageBonus(CraftItemStack.asNMSCopy(attacker.getEquipment().getItemInMainHand()), monsterType);
    }
    if (damage <= 0) {
        return 0;
    }
    if (target != null) {
        DamageSource source;
        if (attacker instanceof Player) {
            source = DamageSource.playerAttack(((CraftPlayer) attacker).getHandle());
        } else {
            source = DamageSource.mobAttack(((CraftLivingEntity) attacker).getHandle());
        }
        net.minecraft.world.entity.Entity nmsTarget = ((CraftEntity) target).getHandle();
        if (nmsTarget.isInvulnerableTo(source)) {
            return 0;
        }
        if (!(nmsTarget instanceof net.minecraft.world.entity.LivingEntity)) {
            return damage;
        }
        net.minecraft.world.entity.LivingEntity livingTarget = (net.minecraft.world.entity.LivingEntity) nmsTarget;
        damage = CombatRules.getDamageAfterAbsorb((float) damage, (float) livingTarget.getArmorValue(), (float) livingTarget.getAttributeValue(Attributes.ARMOR_TOUGHNESS));
        int enchantDamageModifier = EnchantmentHelper.getDamageProtection(livingTarget.getArmorSlots(), source);
        if (enchantDamageModifier > 0) {
            damage = CombatRules.getDamageAfterMagicAbsorb((float) damage, (float) enchantDamageModifier);
        }
    }
    return damage;
}
Also used : ServerPlayer(net.minecraft.server.level.ServerPlayer) DamageSource(net.minecraft.world.damagesource.DamageSource) LivingEntity(org.bukkit.entity.LivingEntity) AttributeInstance(org.bukkit.attribute.AttributeInstance) org.bukkit.craftbukkit.v1_17_R1.entity(org.bukkit.craftbukkit.v1_17_R1.entity) org.bukkit.entity(org.bukkit.entity) net.minecraft.world.entity(net.minecraft.world.entity)

Example 4 with DamageSource

use of net.minecraft.world.damagesource.DamageSource in project Denizen-For-Bukkit by DenizenScript.

the class EnchantmentHelperImpl method registerFakeEnchantment.

@Override
public org.bukkit.enchantments.Enchantment registerFakeEnchantment(EnchantmentScriptContainer.EnchantmentReference script) {
    try {
        EquipmentSlot[] slots = new EquipmentSlot[script.script.slots.size()];
        for (int i = 0; i < slots.length; i++) {
            slots[i] = EquipmentSlot.valueOf(script.script.slots.get(i).toUpperCase());
        }
        net.minecraft.world.item.enchantment.Enchantment nmsEnchant = new net.minecraft.world.item.enchantment.Enchantment(net.minecraft.world.item.enchantment.Enchantment.Rarity.valueOf(script.script.rarity), EnchantmentCategory.valueOf(script.script.category), slots) {

            @Override
            public int getMinLevel() {
                return script.script.minLevel;
            }

            @Override
            public int getMaxLevel() {
                return script.script.maxLevel;
            }

            @Override
            public int getMinCost(int level) {
                return script.script.getMinCost(level);
            }

            @Override
            public int getMaxCost(int level) {
                return script.script.getMaxCost(level);
            }

            @Override
            public int getDamageProtection(int level, DamageSource src) {
                return script.script.getDamageProtection(level, src.msgId, src.getEntity() == null ? null : src.getEntity().getBukkitEntity());
            }

            @Override
            public float getDamageBonus(int level, MobType type) {
                String typeName = "UNDEFINED";
                if (type == MobType.ARTHROPOD) {
                    typeName = "ARTHROPOD";
                } else if (type == MobType.ILLAGER) {
                    typeName = "ILLAGER";
                } else if (type == MobType.UNDEAD) {
                    typeName = "UNDEAD";
                } else if (type == MobType.WATER) {
                    typeName = "WATER";
                }
                return script.script.getDamageBonus(level, typeName);
            }

            @Override
            protected boolean checkCompatibility(net.minecraft.world.item.enchantment.Enchantment nmsEnchantment) {
                ResourceLocation nmsKey = Registry.ENCHANTMENT.getKey(nmsEnchantment);
                NamespacedKey bukkitKey = CraftNamespacedKey.fromMinecraft(nmsKey);
                org.bukkit.enchantments.Enchantment bukkitEnchant = CraftEnchantment.getByKey(bukkitKey);
                return script.script.isCompatible(bukkitEnchant);
            }

            @Override
            protected String getOrCreateDescriptionId() {
                return script.script.descriptionId;
            }

            @Override
            public String getDescriptionId() {
                return script.script.descriptionId;
            }

            @Override
            public Component getFullname(int level) {
                return Handler.componentToNMS(script.script.getFullName(level));
            }

            @Override
            public boolean canEnchant(net.minecraft.world.item.ItemStack var0) {
                return super.canEnchant(var0) && script.script.canEnchant(CraftItemStack.asBukkitCopy(var0));
            }

            @Override
            public void doPostAttack(LivingEntity attacker, Entity victim, int level) {
                script.script.doPostAttack(attacker.getBukkitEntity(), victim.getBukkitEntity(), level);
            }

            @Override
            public void doPostHurt(LivingEntity victim, Entity attacker, int level) {
                script.script.doPostHurt(victim.getBukkitEntity(), attacker.getBukkitEntity(), level);
            }

            @Override
            public boolean isTreasureOnly() {
                return script.script.isTreasureOnly;
            }

            @Override
            public boolean isCurse() {
                return script.script.isCurse;
            }

            @Override
            public boolean isTradeable() {
                return script.script.isTradable;
            }

            @Override
            public boolean isDiscoverable() {
                return script.script.isDiscoverable;
            }
        };
        String enchName = script.script.id.toUpperCase();
        Registry.register(Registry.ENCHANTMENT, "denizen:" + script.script.id, nmsEnchant);
        CraftEnchantment ench = new CraftEnchantment(nmsEnchant) {

            @Override
            public String getName() {
                return enchName;
            }
        };
        ENCHANTMENTS_BY_KEY.put(ench.getKey(), ench);
        ENCHANTMENTS_BY_NAME.put(enchName, ench);
        return ench;
    } catch (Throwable ex) {
        Debug.echoError("Failed to register enchantment " + script.script.id);
        Debug.echoError(ex);
        return null;
    }
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) CraftEntity(org.bukkit.craftbukkit.v1_17_R1.entity.CraftEntity) Entity(net.minecraft.world.entity.Entity) DamageSource(net.minecraft.world.damagesource.DamageSource) Enchantment(org.bukkit.enchantments.Enchantment) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) MobType(net.minecraft.world.entity.MobType) LivingEntity(net.minecraft.world.entity.LivingEntity) NamespacedKey(org.bukkit.NamespacedKey) CraftNamespacedKey(org.bukkit.craftbukkit.v1_17_R1.util.CraftNamespacedKey) ResourceLocation(net.minecraft.resources.ResourceLocation) Enchantment(org.bukkit.enchantments.Enchantment) CraftEnchantment(org.bukkit.craftbukkit.v1_17_R1.enchantments.CraftEnchantment) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack) CraftEnchantment(org.bukkit.craftbukkit.v1_17_R1.enchantments.CraftEnchantment)

Example 5 with DamageSource

use of net.minecraft.world.damagesource.DamageSource in project Denizen-For-Bukkit by DenizenScript.

the class EntityHelperImpl method damage.

@Override
public void damage(LivingEntity target, float amount, Entity source, EntityDamageEvent.DamageCause cause) {
    if (target == null) {
        return;
    }
    net.minecraft.world.entity.LivingEntity nmsTarget = ((CraftLivingEntity) target).getHandle();
    net.minecraft.world.entity.Entity nmsSource = source == null ? null : ((CraftEntity) source).getHandle();
    CraftEventFactory.entityDamage = nmsSource;
    try {
        DamageSource src = getSourceFor(nmsSource, cause);
        if (src instanceof FakeDamageSrc) {
            src = ((FakeDamageSrc) src).real;
            EntityDamageEvent ede = fireFakeDamageEvent(target, source, cause, amount);
            if (ede.isCancelled()) {
                return;
            }
        }
        nmsTarget.hurt(src, amount);
    } finally {
        CraftEventFactory.entityDamage = null;
    }
}
Also used : DamageSource(net.minecraft.world.damagesource.DamageSource) org.bukkit.entity(org.bukkit.entity) org.bukkit.craftbukkit.v1_18_R1.entity(org.bukkit.craftbukkit.v1_18_R1.entity) net.minecraft.world.entity(net.minecraft.world.entity) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent)

Aggregations

DamageSource (net.minecraft.world.damagesource.DamageSource)6 net.minecraft.world.entity (net.minecraft.world.entity)4 org.bukkit.entity (org.bukkit.entity)4 ResourceLocation (net.minecraft.resources.ResourceLocation)2 ServerPlayer (net.minecraft.server.level.ServerPlayer)2 Entity (net.minecraft.world.entity.Entity)2 EquipmentSlot (net.minecraft.world.entity.EquipmentSlot)2 LivingEntity (net.minecraft.world.entity.LivingEntity)2 MobType (net.minecraft.world.entity.MobType)2 NamespacedKey (org.bukkit.NamespacedKey)2 AttributeInstance (org.bukkit.attribute.AttributeInstance)2 org.bukkit.craftbukkit.v1_17_R1.entity (org.bukkit.craftbukkit.v1_17_R1.entity)2 org.bukkit.craftbukkit.v1_18_R1.entity (org.bukkit.craftbukkit.v1_18_R1.entity)2 Enchantment (org.bukkit.enchantments.Enchantment)2 LivingEntity (org.bukkit.entity.LivingEntity)2 EntityDamageEvent (org.bukkit.event.entity.EntityDamageEvent)2 CraftEnchantment (org.bukkit.craftbukkit.v1_17_R1.enchantments.CraftEnchantment)1 CraftEntity (org.bukkit.craftbukkit.v1_17_R1.entity.CraftEntity)1 CraftItemStack (org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack)1 CraftNamespacedKey (org.bukkit.craftbukkit.v1_17_R1.util.CraftNamespacedKey)1