Search in sources :

Example 76 with LivingEntity

use of org.bukkit.entity.LivingEntity in project Citizens2 by CitizensDev.

the class NMSImpl method setHeadYaw.

@Override
public void setHeadYaw(org.bukkit.entity.Entity entity, float yaw) {
    if (!(entity instanceof LivingEntity))
        return;
    EntityLiving handle = (EntityLiving) getHandle(entity);
    yaw = Util.clampYaw(yaw);
    handle.aO = yaw;
    if (!(handle instanceof EntityHuman)) {
        handle.aN = yaw;
    }
    handle.aP = yaw;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) EntityHuman(net.minecraft.server.v1_12_R1.EntityHuman) EntityLiving(net.minecraft.server.v1_12_R1.EntityLiving)

Example 77 with LivingEntity

use of org.bukkit.entity.LivingEntity in project Citizens2 by CitizensDev.

the class CitizensNPC method update.

@Override
public void update() {
    try {
        super.update();
        if (!isSpawned())
            return;
        if (data().get(NPC.SWIMMING_METADATA, true)) {
            NMS.trySwim(getEntity());
        }
        navigator.run();
        try {
            getEntity().setGlowing(data().get(NPC.GLOWING_METADATA, false));
        } catch (NoSuchMethodError e) {
        }
        if (!getNavigator().isNavigating() && updateCounter++ > Setting.PACKET_UPDATE_DELAY.asInt()) {
            updateCounter = 0;
            if (getEntity() instanceof LivingEntity) {
                boolean nameVisibility = false;
                if (!getEntity().isCustomNameVisible() && !data().<Object>get(NPC.NAMEPLATE_VISIBLE_METADATA, true).toString().equals("hover")) {
                    getEntity().setCustomName("");
                } else {
                    nameVisibility = true;
                    getEntity().setCustomName(getFullName());
                }
                String teamName = data().get(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA, "");
                if (getEntity() instanceof Player && teamName.length() > 0 && Bukkit.getScoreboardManager().getMainScoreboard().getTeam(teamName) != null) {
                    Team team = Bukkit.getScoreboardManager().getMainScoreboard().getTeam(teamName);
                    if (!Setting.USE_SCOREBOARD_TEAMS.asBoolean()) {
                        team.unregister();
                        data().remove(NPC.SCOREBOARD_FAKE_TEAM_NAME_METADATA);
                    } else {
                        try {
                            team.setOption(Option.NAME_TAG_VISIBILITY, nameVisibility ? OptionStatus.NEVER : OptionStatus.ALWAYS);
                        } catch (NoSuchMethodError e) {
                        } catch (NoClassDefFoundError e) {
                        }
                        if (data().has(NPC.GLOWING_COLOR_METADATA)) {
                            if (team.getPrefix() == null || team.getPrefix().length() == 0 || (data().has("previous-glowing-color") && !team.getPrefix().equals(data().get("previous-glowing-color")))) {
                                team.setPrefix(ChatColor.valueOf(data().<String>get(NPC.GLOWING_COLOR_METADATA)).toString());
                                data().set("previous-glowing-color", team.getPrefix());
                            }
                        }
                    }
                }
            }
            Player player = getEntity() instanceof Player ? (Player) getEntity() : null;
            NMS.sendPositionUpdate(player, getEntity(), getStoredLocation());
        }
        if (getEntity() instanceof LivingEntity) {
            String nameplateVisible = data().<Object>get(NPC.NAMEPLATE_VISIBLE_METADATA, true).toString();
            if (nameplateVisible.equals("hover")) {
                ((LivingEntity) getEntity()).setCustomNameVisible(false);
            } else {
                ((LivingEntity) getEntity()).setCustomNameVisible(Boolean.parseBoolean(nameplateVisible));
            }
            if (data().get(NPC.DEFAULT_PROTECTED_METADATA, true)) {
                NMS.setKnockbackResistance((LivingEntity) getEntity(), 1D);
            } else {
                NMS.setKnockbackResistance((LivingEntity) getEntity(), 0D);
            }
        }
        if (data().has(NPC.SILENT_METADATA)) {
            try {
                getEntity().setSilent(Boolean.parseBoolean(data().get(NPC.SILENT_METADATA).toString()));
            } catch (NoSuchMethodError e) {
            }
        }
    } catch (Exception ex) {
        Throwable error = Throwables.getRootCause(ex);
        Messaging.logTr(Messages.EXCEPTION_UPDATING_NPC, getId(), error.getMessage());
        error.printStackTrace();
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) Team(org.bukkit.scoreboard.Team)

Example 78 with LivingEntity

use of org.bukkit.entity.LivingEntity in project Citizens2 by CitizensDev.

the class CitizensNPC method spawn.

@Override
public boolean spawn(Location at) {
    Preconditions.checkNotNull(at, "location cannot be null");
    if (isSpawned()) {
        Messaging.debug("Tried to spawn", getId(), "while already spawned.");
        return false;
    }
    data().get(NPC.DEFAULT_PROTECTED_METADATA, true);
    at = at.clone();
    getTrait(CurrentLocation.class).setLocation(at);
    entityController.spawn(at, this);
    getEntity().setMetadata(NPC_METADATA_MARKER, new FixedMetadataValue(CitizensAPI.getPlugin(), true));
    boolean couldSpawn = !Util.isLoaded(at) ? false : NMS.addEntityToWorld(getEntity(), SpawnReason.CUSTOM);
    // send skin packets, if applicable, before other NMS packets are sent
    if (couldSpawn) {
        SkinnableEntity skinnable = getEntity() instanceof SkinnableEntity ? ((SkinnableEntity) getEntity()) : null;
        if (skinnable != null) {
            skinnable.getSkinTracker().onSpawnNPC();
        }
    }
    getEntity().teleport(at);
    if (!couldSpawn) {
        Messaging.debug("Retrying spawn of", getId(), "later due to chunk being unloaded.", Util.isLoaded(at) ? "Util.isLoaded true" : "Util.isLoaded false");
        // we need to wait for a chunk load before trying to spawn
        entityController.remove();
        Bukkit.getPluginManager().callEvent(new NPCNeedsRespawnEvent(this, at));
        return false;
    }
    NMS.setHeadYaw(getEntity(), at.getYaw());
    // Set the spawned state
    getTrait(CurrentLocation.class).setLocation(at);
    getTrait(Spawned.class).setSpawned(true);
    NPCSpawnEvent spawnEvent = new NPCSpawnEvent(this, at);
    Bukkit.getPluginManager().callEvent(spawnEvent);
    if (spawnEvent.isCancelled()) {
        entityController.remove();
        Messaging.debug("Couldn't spawn", getId(), "due to event cancellation.");
        return false;
    }
    navigator.onSpawn();
    // Modify NPC using traits after the entity has been created
    Collection<Trait> onSpawn = traits.values();
    // work around traits modifying the map during this iteration.
    for (Trait trait : onSpawn.toArray(new Trait[onSpawn.size()])) {
        try {
            trait.onSpawn();
        } catch (Throwable ex) {
            Messaging.severeTr(Messages.TRAIT_ONSPAWN_FAILED, trait.getName(), getId());
            ex.printStackTrace();
        }
    }
    if (getEntity() instanceof LivingEntity) {
        LivingEntity entity = (LivingEntity) getEntity();
        entity.setRemoveWhenFarAway(false);
        if (NMS.getStepHeight(entity) < 1) {
            NMS.setStepHeight(entity, 1);
        }
        if (getEntity() instanceof Player) {
            NMS.replaceTrackerEntry((Player) getEntity());
        }
    }
    return true;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) SkinnableEntity(net.citizensnpcs.npc.skin.SkinnableEntity) NPCNeedsRespawnEvent(net.citizensnpcs.NPCNeedsRespawnEvent) Spawned(net.citizensnpcs.api.trait.trait.Spawned) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) NPCSpawnEvent(net.citizensnpcs.api.event.NPCSpawnEvent) Trait(net.citizensnpcs.api.trait.Trait) CurrentLocation(net.citizensnpcs.trait.CurrentLocation)

Example 79 with LivingEntity

use of org.bukkit.entity.LivingEntity in project NoCheatPlus by NoCheatPlus.

the class Direction method check.

/**
 * "Classic" check.
 *
 * @param player
 *            the player
 * @param damaged
 *            the damaged
 * @return true, if successful
 */
public boolean check(final Player player, final Location loc, final Entity damaged, final boolean damagedIsFake, final Location dLoc, final FightData data, final FightConfig cc) {
    boolean cancel = false;
    final MCAccess mcAccess = this.mcAccess.getHandle();
    // if (damaged instanceof EntityComplex || damaged instanceof EntityComplexPart)
    if (!damagedIsFake && mcAccess.isComplexPart(damaged)) {
        return false;
    }
    // Find out how wide the entity is.
    final double width = damagedIsFake ? 0.6 : mcAccess.getWidth(damaged);
    // entity.height is broken and will always be 0, therefore. Calculate height instead based on boundingBox.
    final double height = damagedIsFake ? (damaged instanceof LivingEntity ? ((LivingEntity) damaged).getEyeHeight() : 1.75) : mcAccess.getHeight(damaged);
    // TODO: allow any hit on the y axis (might just adapt interface to use foot position + height)!
    // How far "off" is the player with their aim. We calculate from the players eye location and view direction to
    // the center of the target entity. If the line of sight is more too far off, "off" will be bigger than 0.
    final Vector direction = loc.getDirection();
    final double off;
    if (cc.directionStrict) {
        off = CollisionUtil.combinedDirectionCheck(loc, player.getEyeHeight(), direction, dLoc.getX(), dLoc.getY() + height / 2D, dLoc.getZ(), width, height, TrigUtil.DIRECTION_PRECISION, 80.0);
    } else {
        // Also take into account the angle.
        off = CollisionUtil.directionCheck(loc, player.getEyeHeight(), direction, dLoc.getX(), dLoc.getY() + height / 2D, dLoc.getZ(), width, height, TrigUtil.DIRECTION_PRECISION);
    }
    if (off > 0.1) {
        // Player failed the check. Let's try to guess how far they were from looking directly to the entity...
        final Vector blockEyes = new Vector(dLoc.getX() - loc.getX(), dLoc.getY() + height / 2D - loc.getY() - player.getEyeHeight(), dLoc.getZ() - loc.getZ());
        final double distance = blockEyes.crossProduct(direction).length() / direction.length();
        // Add the overall violation level of the check.
        data.directionVL += distance;
        // Execute whatever actions are associated with this check and the violation level and find out if we should
        // cancel the event.
        cancel = executeActions(player, data.directionVL, distance, cc.directionActions).willCancel();
        if (cancel) {
            // Deal an attack penalty time.
            data.attackPenalty.applyPenalty(cc.directionPenalty);
        }
    } else {
        // Reward the player by lowering their violation level.
        data.directionVL *= 0.8D;
    }
    return cancel;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) MCAccess(fr.neatmonster.nocheatplus.compat.MCAccess) Vector(org.bukkit.util.Vector)

Example 80 with LivingEntity

use of org.bukkit.entity.LivingEntity in project NoCheatPlus by NoCheatPlus.

the class MCAccessSpigotCB1_9_R2 method getHeight.

@Override
public double getHeight(final Entity entity) {
    final net.minecraft.server.v1_9_R2.Entity mcEntity = ((CraftEntity) entity).getHandle();
    AxisAlignedBB boundingBox = mcEntity.getBoundingBox();
    final double entityHeight = Math.max(mcEntity.length, Math.max(mcEntity.getHeadHeight(), boundingBox.e - boundingBox.b));
    if (entity instanceof LivingEntity) {
        return Math.max(((LivingEntity) entity).getEyeHeight(), entityHeight);
    } else {
        return entityHeight;
    }
}
Also used : AxisAlignedBB(net.minecraft.server.v1_9_R2.AxisAlignedBB) LivingEntity(org.bukkit.entity.LivingEntity) CraftEntity(org.bukkit.craftbukkit.v1_9_R2.entity.CraftEntity)

Aggregations

LivingEntity (org.bukkit.entity.LivingEntity)322 Entity (org.bukkit.entity.Entity)169 Player (org.bukkit.entity.Player)121 Location (org.bukkit.Location)71 EventHandler (org.bukkit.event.EventHandler)63 Vector (org.bukkit.util.Vector)60 ItemStack (org.bukkit.inventory.ItemStack)47 ArrayList (java.util.ArrayList)41 ISoliniaLivingEntity (com.solinia.solinia.Interfaces.ISoliniaLivingEntity)37 PotionEffect (org.bukkit.potion.PotionEffect)34 CoreStateInitException (com.solinia.solinia.Exceptions.CoreStateInitException)30 Block (org.bukkit.block.Block)24 Mage (com.elmakers.mine.bukkit.api.magic.Mage)22 Projectile (org.bukkit.entity.Projectile)20 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)18 FixedMetadataValue (org.bukkit.metadata.FixedMetadataValue)17 ISoliniaPlayer (com.solinia.solinia.Interfaces.ISoliniaPlayer)16 Target (com.elmakers.mine.bukkit.utility.Target)15 World (org.bukkit.World)14 Creature (org.bukkit.entity.Creature)14