Search in sources :

Example 11 with TamableAnimal

use of net.minecraft.world.entity.TamableAnimal in project MyPet by xXKeyleXx.

the class ControlTarget method shouldStart.

@Override
public boolean shouldStart() {
    if (controlPathfinderGoal == null) {
        if (petEntity.getPathfinder().hasGoal("Control")) {
            controlPathfinderGoal = (Control) petEntity.getPathfinder().getGoal("Control");
        }
    }
    if (controlPathfinderGoal == null) {
        return false;
    }
    if (myPet.getDamage() <= 0 && myPet.getRangedDamage() <= 0) {
        return false;
    }
    if (controlPathfinderGoal.moveTo != null && petEntity.canMove()) {
        Behavior behaviorSkill = myPet.getSkills().get(Behavior.class);
        if (behaviorSkill.isActive()) {
            if (behaviorSkill.getBehavior() == BehaviorMode.Friendly) {
                return false;
            }
        }
        for (net.minecraft.world.entity.LivingEntity entityLiving : this.petEntity.level.getEntitiesOfClass(net.minecraft.world.entity.LivingEntity.class, this.petEntity.getBoundingBox().inflate(this.range, 4.0D, this.range))) {
            if (entityLiving != petEntity && !(entityLiving instanceof ArmorStand)) {
                if (entityLiving instanceof ServerPlayer) {
                    Player targetPlayer = (Player) entityLiving.getBukkitEntity();
                    if (myPet.getOwner().equals(targetPlayer)) {
                        continue;
                    } else if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), targetPlayer, true)) {
                        continue;
                    }
                } else if (entityLiving instanceof TamableAnimal) {
                    // Method: getOwner -> mojang mapping maps that to fx() even tho it still is getOwner.
                    Method getOwnerReflect = ReflectionUtil.getMethod(TamableAnimal.class, "getOwner");
                    TamableAnimal tameable = (TamableAnimal) entityLiving;
                    try {
                        if (tameable.isTame() && getOwnerReflect.invoke(tameable, null) != null) {
                            Player tameableOwner = (Player) ((net.minecraft.world.entity.player.Player) getOwnerReflect.invoke(tameable, null)).getBukkitEntity();
                            if (myPet.getOwner().equals(tameableOwner)) {
                                continue;
                            } else if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), tameableOwner, true)) {
                                continue;
                            }
                        }
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        e.printStackTrace();
                    }
                } else if (entityLiving instanceof EntityMyPet) {
                    MyPet targetMyPet = ((EntityMyPet) entityLiving).getMyPet();
                    if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), targetMyPet.getOwner().getPlayer(), true)) {
                        continue;
                    }
                }
                if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), entityLiving.getBukkitEntity())) {
                    continue;
                }
                if (behaviorSkill != null) {
                    if (behaviorSkill.getBehavior() == BehaviorMode.Raid) {
                        if (entityLiving instanceof TamableAnimal) {
                            continue;
                        } else if (entityLiving instanceof EntityMyPet) {
                            continue;
                        } else if (entityLiving instanceof ServerPlayer) {
                            continue;
                        }
                    }
                }
                controlPathfinderGoal.stopControl();
                this.target = entityLiving;
                return true;
            }
        }
    }
    return false;
}
Also used : CraftPlayer(org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer) Player(org.bukkit.entity.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) EntityMyPet(de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet) ArmorStand(net.minecraft.world.entity.decoration.ArmorStand) TamableAnimal(net.minecraft.world.entity.TamableAnimal) MyPet(de.Keyle.MyPet.api.entity.MyPet) EntityMyPet(de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet) ServerPlayer(net.minecraft.server.level.ServerPlayer) Behavior(de.Keyle.MyPet.api.skill.skills.Behavior) Method(java.lang.reflect.Method)

Example 12 with TamableAnimal

use of net.minecraft.world.entity.TamableAnimal in project MyPet by xXKeyleXx.

the class BehaviorAggressiveTarget method shouldStart.

@Override
public boolean shouldStart() {
    Behavior behaviorSkill = myPet.getSkills().get(Behavior.class);
    if (!behaviorSkill.isActive() || behaviorSkill.getBehavior() != BehaviorMode.Aggressive) {
        return false;
    }
    if (myPet.getDamage() <= 0 && myPet.getRangedDamage() <= 0) {
        return false;
    }
    if (!petEntity.canMove()) {
        return false;
    }
    if (petEntity.hasTarget()) {
        return false;
    }
    for (net.minecraft.world.entity.LivingEntity entityLiving : this.petEntity.level.getEntitiesOfClass(net.minecraft.world.entity.LivingEntity.class, this.petOwnerEntity.getBoundingBox().inflate(range, range, range))) {
        if (entityLiving != petEntity && !(entityLiving instanceof ArmorStand) && entityLiving.isAlive() && petEntity.distanceToSqr(entityLiving) <= 91) {
            if (entityLiving instanceof ServerPlayer) {
                Player targetPlayer = (Player) entityLiving.getBukkitEntity();
                if (myPet.getOwner().equals(targetPlayer)) {
                    continue;
                }
                if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), targetPlayer, true)) {
                    continue;
                }
            } else if (entityLiving instanceof EntityMyPet) {
                MyPet targetMyPet = ((EntityMyPet) entityLiving).getMyPet();
                if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), targetMyPet.getOwner().getPlayer(), true)) {
                    continue;
                }
            } else if (entityLiving instanceof TamableAnimal) {
                // Method: getOwner -> mojang mapping maps that to fx() even tho it still is getOwner.
                Method getOwnerReflect = ReflectionUtil.getMethod(TamableAnimal.class, "getOwner");
                TamableAnimal tameable = (TamableAnimal) entityLiving;
                try {
                    if (tameable.isTame() && getOwnerReflect.invoke(tameable, null) != null) {
                        Player tameableOwner = (Player) ((net.minecraft.world.entity.player.Player) getOwnerReflect.invoke(tameable, null)).getBukkitEntity();
                        if (myPet.getOwner().equals(tameableOwner)) {
                            continue;
                        } else if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), tameableOwner, true)) {
                            continue;
                        }
                    }
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            } else if (!MyPetApi.getHookHelper().canHurt(myPet.getOwner().getPlayer(), entityLiving.getBukkitEntity())) {
                continue;
            }
            this.target = entityLiving;
            return true;
        }
    }
    return false;
}
Also used : CraftPlayer(org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer) Player(org.bukkit.entity.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) EntityMyPet(de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet) TamableAnimal(net.minecraft.world.entity.TamableAnimal) MyPet(de.Keyle.MyPet.api.entity.MyPet) EntityMyPet(de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArmorStand(net.minecraft.world.entity.decoration.ArmorStand) ServerPlayer(net.minecraft.server.level.ServerPlayer) Behavior(de.Keyle.MyPet.api.skill.skills.Behavior)

Aggregations

ServerPlayer (net.minecraft.server.level.ServerPlayer)12 TamableAnimal (net.minecraft.world.entity.TamableAnimal)12 Behavior (de.Keyle.MyPet.api.skill.skills.Behavior)11 ArmorStand (net.minecraft.world.entity.decoration.ArmorStand)10 EntityMyPet (de.Keyle.MyPet.compat.v1_17_R1.entity.EntityMyPet)7 MyPet (de.Keyle.MyPet.api.entity.MyPet)6 Player (org.bukkit.entity.Player)6 EntityMyPet (de.Keyle.MyPet.compat.v1_18_R1.entity.EntityMyPet)5 Ranged (de.Keyle.MyPet.api.skill.skills.Ranged)4 Method (java.lang.reflect.Method)4 CraftPlayer (org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 LivingEntity (net.minecraft.world.entity.LivingEntity)2 CraftLivingEntity (org.bukkit.craftbukkit.v1_17_R1.entity.CraftLivingEntity)2 CraftLivingEntity (org.bukkit.craftbukkit.v1_18_R1.entity.CraftLivingEntity)2 CraftPlayer (org.bukkit.craftbukkit.v1_18_R1.entity.CraftPlayer)2