Search in sources :

Example 1 with EntityTargetLivingEntityEvent

use of org.bukkit.event.entity.EntityTargetLivingEntityEvent in project IridiumSkyblock by Iridium-Development.

the class EntityTargetListener method onEntityTargetEntity.

@EventHandler
public void onEntityTargetEntity(EntityTargetLivingEntityEvent event) {
    if (IridiumSkyblock.getInstance().getConfiguration().pvpSettings.mobsVisitorTargeting)
        return;
    Optional<Island> island = IridiumSkyblock.getInstance().getIslandManager().getIslandViaLocation(event.getEntity().getLocation());
    if (!island.isPresent())
        return;
    if (event.getTarget() == null || !(event.getTarget() instanceof Player))
        return;
    Player targetPlayer = (Player) event.getTarget();
    User targetUser = IridiumSkyblock.getInstance().getUserManager().getUser(targetPlayer);
    Optional<IslandTrusted> targetTrusted = IridiumSkyblock.getInstance().getIslandManager().getIslandTrusted(island.get(), targetUser);
    if (island.get().equals(targetUser.getIsland().orElse(null)) || targetTrusted.isPresent())
        return;
    if (!canSearchTarget(event.getEntity())) {
        return;
    }
    List<Player> nextTargets = event.getEntity().getNearbyEntities(8, 8, 8).stream().filter(entity -> entity instanceof Player).filter(entity -> island.get().isInIsland(entity.getLocation())).map(entity -> (Player) entity).filter(player -> player != targetPlayer).filter(player -> island.get().equals(IridiumSkyblock.getInstance().getUserManager().getUser(player).getIsland().orElse(null))).filter(player -> IridiumSkyblock.getInstance().getIslandManager().getIslandTrusted(island.get(), IridiumSkyblock.getInstance().getUserManager().getUser(player)).isPresent()).filter(player -> player.hasLineOfSight(event.getEntity())).collect(Collectors.toList());
    if (nextTargets.isEmpty()) {
        event.setCancelled(true);
        return;
    }
    Player nextTarget = nextTargets.get(ThreadLocalRandom.current().nextInt(nextTargets.size()));
    event.setTarget(nextTarget);
}
Also used : EntityTargetLivingEntityEvent(org.bukkit.event.entity.EntityTargetLivingEntityEvent) Entity(org.bukkit.entity.Entity) User(com.iridium.iridiumskyblock.database.User) Island(com.iridium.iridiumskyblock.database.Island) Player(org.bukkit.entity.Player) Collectors(java.util.stream.Collectors) EventHandler(org.bukkit.event.EventHandler) List(java.util.List) CooldownProvider(com.iridium.iridiumskyblock.managers.CooldownProvider) Duration(java.time.Duration) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IslandTrusted(com.iridium.iridiumskyblock.database.IslandTrusted) Optional(java.util.Optional) IridiumSkyblock(com.iridium.iridiumskyblock.IridiumSkyblock) Listener(org.bukkit.event.Listener) Player(org.bukkit.entity.Player) User(com.iridium.iridiumskyblock.database.User) IslandTrusted(com.iridium.iridiumskyblock.database.IslandTrusted) Island(com.iridium.iridiumskyblock.database.Island) EventHandler(org.bukkit.event.EventHandler)

Example 2 with EntityTargetLivingEntityEvent

use of org.bukkit.event.entity.EntityTargetLivingEntityEvent in project WindSpigot by Wind-Development.

the class EntityInsentient method setGoalTarget.

public void setGoalTarget(EntityLiving entityliving, EntityTargetEvent.TargetReason reason, boolean fireEvent) {
    if (getGoalTarget() == entityliving) {
        return;
    }
    if (fireEvent) {
        if (reason == EntityTargetEvent.TargetReason.UNKNOWN && getGoalTarget() != null && entityliving == null) {
            reason = getGoalTarget().isAlive() ? EntityTargetEvent.TargetReason.FORGOT_TARGET : EntityTargetEvent.TargetReason.TARGET_DIED;
        }
        if (reason == EntityTargetEvent.TargetReason.UNKNOWN) {
            world.getServer().getLogger().log(java.util.logging.Level.WARNING, "Unknown target reason, please report on the issue tracker", new Exception());
        }
        CraftLivingEntity ctarget = null;
        if (entityliving != null) {
            ctarget = (CraftLivingEntity) entityliving.getBukkitEntity();
        }
        EntityTargetLivingEntityEvent event = new EntityTargetLivingEntityEvent(this.getBukkitEntity(), ctarget, reason);
        world.getServer().getPluginManager().callEvent(event);
        if (event.isCancelled()) {
            return;
        }
        if (event.getTarget() != null) {
            entityliving = ((CraftLivingEntity) event.getTarget()).getHandle();
        } else {
            entityliving = null;
        }
    }
    // KigPaper - WeakReference
    this.goalTarget = new WeakReference<>(entityliving);
// CraftBukkit end
}
Also used : EntityTargetLivingEntityEvent(org.bukkit.event.entity.EntityTargetLivingEntityEvent) CraftLivingEntity(org.bukkit.craftbukkit.entity.CraftLivingEntity)

Example 3 with EntityTargetLivingEntityEvent

use of org.bukkit.event.entity.EntityTargetLivingEntityEvent in project BentoBox by BentoBoxWorld.

the class ExperiencePickupListenerTest method setUp.

/**
 * @throws java.lang.Exception
 */
@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    when(island.isAllowed(any(), any())).thenReturn(true);
    // Set up as valid
    entity = mock(ExperienceOrb.class);
    when(entity.getLocation()).thenReturn(location);
    TargetReason reason = TargetReason.CLOSEST_PLAYER;
    e = new EntityTargetLivingEntityEvent(entity, player, reason);
    epl = new ExperiencePickupListener();
}
Also used : EntityTargetLivingEntityEvent(org.bukkit.event.entity.EntityTargetLivingEntityEvent) TargetReason(org.bukkit.event.entity.EntityTargetEvent.TargetReason) ExperienceOrb(org.bukkit.entity.ExperienceOrb) Before(org.junit.Before)

Example 4 with EntityTargetLivingEntityEvent

use of org.bukkit.event.entity.EntityTargetLivingEntityEvent in project NachoSpigot by CobbleSword.

the class EntityExperienceOrb method t_.

public void t_() {
    super.t_();
    // CraftBukkit - store old target
    EntityHuman prevTarget = this.targetPlayer;
    if (this.c > 0) {
        --this.c;
    }
    this.lastX = this.locX;
    this.lastY = this.locY;
    this.lastZ = this.locZ;
    this.motY -= 0.029999999329447746D;
    if (this.world.getType(new BlockPosition(this)).getBlock().getMaterial() == Material.LAVA) {
        this.motY = 0.20000000298023224D;
        this.motX = (double) ((this.random.nextFloat() - this.random.nextFloat()) * 0.2F);
        this.motZ = (double) ((this.random.nextFloat() - this.random.nextFloat()) * 0.2F);
        this.makeSound("random.fizz", 0.4F, 2.0F + this.random.nextFloat() * 0.4F);
    }
    this.j(this.locX, (this.getBoundingBox().b + this.getBoundingBox().e) / 2.0D, this.locZ);
    double d0 = 8.0D;
    if (this.targetTime < this.a - 20 + this.getId() % 100) {
        if (this.targetPlayer == null || this.targetPlayer.h(this) > d0 * d0) {
            this.targetPlayer = this.world.findNearbyPlayer(this, d0);
        }
        this.targetTime = this.a;
    }
    if (this.targetPlayer != null && this.targetPlayer.isSpectator()) {
        this.targetPlayer = null;
    }
    if (this.targetPlayer != null) {
        // CraftBukkit start
        boolean cancelled = false;
        if (this.targetPlayer != prevTarget) {
            EntityTargetLivingEntityEvent event = CraftEventFactory.callEntityTargetLivingEvent(this, targetPlayer, EntityTargetEvent.TargetReason.CLOSEST_PLAYER);
            EntityLiving target = event.getTarget() == null ? null : ((org.bukkit.craftbukkit.entity.CraftLivingEntity) event.getTarget()).getHandle();
            targetPlayer = target instanceof EntityHuman ? (EntityHuman) target : null;
            cancelled = event.isCancelled();
        }
        if (!cancelled && targetPlayer != null) {
            double d1 = (this.targetPlayer.locX - this.locX) / d0;
            double d2 = (this.targetPlayer.locY + (double) this.targetPlayer.getHeadHeight() - this.locY) / d0;
            double d3 = (this.targetPlayer.locZ - this.locZ) / d0;
            double d4 = Math.sqrt(d1 * d1 + d2 * d2 + d3 * d3);
            double d5 = 1.0D - d4;
            if (d5 > 0.0D) {
                d5 *= d5;
                this.motX += d1 / d4 * d5 * 0.1D;
                this.motY += d2 / d4 * d5 * 0.1D;
                this.motZ += d3 / d4 * d5 * 0.1D;
            }
        }
    // CraftBukkit end
    }
    this.move(this.motX, this.motY, this.motZ);
    float f = 0.98F;
    if (this.onGround) {
        f = this.world.getType(new BlockPosition(MathHelper.floor(this.locX), MathHelper.floor(this.getBoundingBox().b) - 1, MathHelper.floor(this.locZ))).getBlock().frictionFactor * 0.98F;
    }
    this.motX *= (double) f;
    this.motY *= 0.9800000190734863D;
    this.motZ *= (double) f;
    if (this.onGround) {
        this.motY *= -0.8999999761581421D;
    }
    ++this.a;
    ++this.b;
    if (this.b >= 6000) {
        this.die();
    }
}
Also used : EntityTargetLivingEntityEvent(org.bukkit.event.entity.EntityTargetLivingEntityEvent)

Example 5 with EntityTargetLivingEntityEvent

use of org.bukkit.event.entity.EntityTargetLivingEntityEvent in project NachoSpigot by CobbleSword.

the class EntityInsentient method setGoalTarget.

public void setGoalTarget(EntityLiving entityliving, EntityTargetEvent.TargetReason reason, boolean fireEvent) {
    if (getGoalTarget() == entityliving)
        return;
    if (fireEvent) {
        if (reason == EntityTargetEvent.TargetReason.UNKNOWN && getGoalTarget() != null && entityliving == null) {
            reason = getGoalTarget().isAlive() ? EntityTargetEvent.TargetReason.FORGOT_TARGET : EntityTargetEvent.TargetReason.TARGET_DIED;
        }
        if (reason == EntityTargetEvent.TargetReason.UNKNOWN) {
            world.getServer().getLogger().log(java.util.logging.Level.WARNING, "Unknown target reason, please report on the issue tracker", new Exception());
        }
        CraftLivingEntity ctarget = null;
        if (entityliving != null) {
            ctarget = (CraftLivingEntity) entityliving.getBukkitEntity();
        }
        EntityTargetLivingEntityEvent event = new EntityTargetLivingEntityEvent(this.getBukkitEntity(), ctarget, reason);
        world.getServer().getPluginManager().callEvent(event);
        if (event.isCancelled()) {
            return;
        }
        if (event.getTarget() != null) {
            entityliving = ((CraftLivingEntity) event.getTarget()).getHandle();
        } else {
            entityliving = null;
        }
    }
    // KigPaper - WeakReference
    this.goalTarget = new WeakReference<>(entityliving);
// CraftBukkit end
}
Also used : EntityTargetLivingEntityEvent(org.bukkit.event.entity.EntityTargetLivingEntityEvent) CraftLivingEntity(org.bukkit.craftbukkit.entity.CraftLivingEntity)

Aggregations

EntityTargetLivingEntityEvent (org.bukkit.event.entity.EntityTargetLivingEntityEvent)16 CraftLivingEntity (org.bukkit.craftbukkit.entity.CraftLivingEntity)4 CraftLivingEntity (org.bukkit.craftbukkit.v.entity.CraftLivingEntity)2 LivingEntity (org.bukkit.entity.LivingEntity)2 IridiumSkyblock (com.iridium.iridiumskyblock.IridiumSkyblock)1 Island (com.iridium.iridiumskyblock.database.Island)1 IslandTrusted (com.iridium.iridiumskyblock.database.IslandTrusted)1 User (com.iridium.iridiumskyblock.database.User)1 CooldownProvider (com.iridium.iridiumskyblock.managers.CooldownProvider)1 Duration (java.time.Duration)1 List (java.util.List)1 Optional (java.util.Optional)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 Collectors (java.util.stream.Collectors)1 EntityLivingBase (net.minecraft.entity.EntityLivingBase)1 LivingEntity (net.minecraft.entity.LivingEntity)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 PlayerEntity (net.minecraft.entity.player.PlayerEntity)1 BlockPos (net.minecraft.util.math.BlockPos)1 LivingEntity (net.minecraft.world.entity.LivingEntity)1