Skip to content

Commit

Permalink
add tag entity.weapon_damage, fixes #2047
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Oct 2, 2019
1 parent d112ff9 commit ec16cdc
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 1 deletion.
Expand Up @@ -22,6 +22,8 @@

public abstract class EntityHelper {

public abstract double getDamageTo(LivingEntity attacker, Entity target);

public abstract String getRawHoverText(Entity entity);

public List<String> getDiscoveredRecipes(Player player) {
Expand Down
Expand Up @@ -14,7 +14,6 @@

public abstract class ItemHelper {


public abstract void removeRecipe(NamespacedKey key);

public abstract void clearDenizenRecipes();
Expand Down
Expand Up @@ -2754,6 +2754,25 @@ public ObjectTag run(Attribute attribute, EntityTag object) {
}
});

// <--[tag]
// @attribute <EntityTag.weapon_damage[(<entity>)]>
// @returns ElementTag(Number)
// @group properties
// @description
// Returns the amount of damage the entity will do based on its held item.
// Optionally, specify a target entity to test how much damage will be done to that specific target (modified based on enchantments).
// -->
registerSpawnedOnlyTag("weapon_damage", new TagRunnable.ObjectForm<EntityTag>() {
@Override
public ObjectTag run(Attribute attribute, EntityTag object) {
Entity target = null;
if (attribute.hasContext(1)) {
target = valueOf(attribute.getContext(1)).getBukkitEntity();
}
return new ElementTag(NMSHandler.getEntityHelper().getDamageTo(object.getLivingEntity(), target));
}
});

// <--[tag]
// @attribute <EntityTag.describe>
// @returns ElementTag(Boolean)
Expand Down
Expand Up @@ -10,13 +10,15 @@
import net.minecraft.server.v1_12_R1.*;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.attribute.Attribute;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_12_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftAnimals;
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftCreature;
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftLivingEntity;
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack;
import org.bukkit.entity.*;
import org.bukkit.entity.Entity;
import org.bukkit.event.entity.EntityTargetEvent;
Expand All @@ -33,6 +35,22 @@

public class EntityHelperImpl extends EntityHelper {

@Override
public double getDamageTo(LivingEntity attacker, Entity target) {
EnumMonsterType monsterType;
if (target instanceof LivingEntity) {
monsterType = ((CraftLivingEntity) target).getHandle().getMonsterType();
}
else {
monsterType = EnumMonsterType.UNDEFINED;
}
double damage = attacker.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE).getValue();
if (attacker.getEquipment() != null && attacker.getEquipment().getItemInMainHand() != null) {
damage += EnchantmentManager.a(CraftItemStack.asNMSCopy(attacker.getEquipment().getItemInMainHand()), monsterType);
}
return damage;
}

public static final MethodHandle ENTITY_HOVER_TEXT_GETTER = ReflectionHelper.getMethodHandle(net.minecraft.server.v1_12_R1.Entity.class, "bv");

@Override
Expand Down
Expand Up @@ -13,6 +13,7 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.attribute.Attribute;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_13_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_13_R2.block.CraftBlock;
Expand All @@ -34,6 +35,22 @@

public class EntityHelperImpl extends EntityHelper {

@Override
public double getDamageTo(LivingEntity attacker, Entity target) {
EnumMonsterType monsterType;
if (target instanceof LivingEntity) {
monsterType = ((CraftLivingEntity) target).getHandle().getMonsterType();
}
else {
monsterType = EnumMonsterType.UNDEFINED;
}
double damage = attacker.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE).getValue();
if (attacker.getEquipment() != null && attacker.getEquipment().getItemInMainHand() != null) {
damage += EnchantmentManager.a(CraftItemStack.asNMSCopy(attacker.getEquipment().getItemInMainHand()), monsterType);
}
return damage;
}

public static final MethodHandle ENTITY_HOVER_TEXT_GETTER = ReflectionHelper.getMethodHandle(net.minecraft.server.v1_13_R2.Entity.class, "bC");

@Override
Expand Down
Expand Up @@ -14,6 +14,7 @@
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.attribute.Attribute;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_14_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_14_R1.block.CraftBlock;
Expand All @@ -34,6 +35,22 @@

public class EntityHelperImpl extends EntityHelper {

@Override
public double getDamageTo(LivingEntity attacker, Entity target) {
EnumMonsterType monsterType;
if (target instanceof LivingEntity) {
monsterType = ((CraftLivingEntity) target).getHandle().getMonsterType();
}
else {
monsterType = EnumMonsterType.UNDEFINED;
}
double damage = attacker.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE).getValue();
if (attacker.getEquipment() != null && attacker.getEquipment().getItemInMainHand() != null) {
damage += EnchantmentManager.a(CraftItemStack.asNMSCopy(attacker.getEquipment().getItemInMainHand()), monsterType);
}
return damage;
}

public static final Field RECIPE_BOOK_DISCOVERED_SET = ReflectionHelper.getFields(RecipeBook.class).get("a");

public static final MethodHandle ENTITY_HOVER_TEXT_GETTER = ReflectionHelper.getMethodHandle(net.minecraft.server.v1_14_R1.Entity.class, "bK");
Expand Down

0 comments on commit ec16cdc

Please sign in to comment.