public float distanceSquared(double x, double y, double z) { return distanceSquared((float) x, (float) y, (float) z); }
public float distanceSquared(Vector3f v) { return distanceSquared(v.x, v.y, v.z); }
public float distanceSquared(double x, double y, double z) { return distanceSquared((float) x, (float) y, (float) z); }
public float distanceSquared(Vector3f v) { return distanceSquared(v.x, v.y, v.z); }
public float distance(float x, float y, float z) { return (float) Math.sqrt(distanceSquared(x, y, z)); }
public float distance(float x, float y, float z) { return (float) Math.sqrt(distanceSquared(x, y, z)); }
public void onTick() { List<PlayerSession> playersInWorld = level.getEntityManager().getPlayers(); NetworkPackage np; while ((np = broadcastQueue.poll()) != null) { for (PlayerSession session : playersInWorld) { if (!session.isRemoved()) { session.getMcpeSession().addToSendQueue(np); } } } synchronized (specificEntityViewerQueue) { specificEntityViewerQueue.forEachEntry((eid, queue) -> { Optional<BaseEntity> entityById = level.getEntityManager().findEntityById(eid); if (entityById.isPresent()) { Entity entity = entityById.get(); for (PlayerSession session : playersInWorld) { if (session == entity) continue; // Don't move ourselves if (session.getPosition().distanceSquared(entity.getPosition()) <= ENTITY_VIEW_DISTANCE_SQ && !session.isRemoved()) { for (NetworkPackage aPackage : queue) { session.getMcpeSession().addToSendQueue(aPackage); } } } } return true; }); specificEntityViewerQueue.clear(); } }
@Override public void handle(McpeMovePlayer packet) { Health health = ensureAndGet(Health.class); if (!spawned || health.isDead()) { return; } // TODO: We may do well to perform basic anti-cheat Vector3f originalPosition = getPosition(); Vector3f newPosition = packet.getPosition().sub(0, 1.62, 0); // Reject moves that are obviously too fast. (>=100 blocks) if (newPosition.distanceSquared(newPosition) >= 10000) { setPosition(originalPosition); setRotation(packet.getRotation()); return; } setPosition(newPosition); setRotation(packet.getRotation()); // If we haven't moved in the X or Z axis, don't update viewable entities or try updating chunks - they haven't changed. if (hasSubstantiallyMoved(originalPosition, newPosition)) { updateViewableEntities(); sendNewChunks().exceptionally(throwable -> { LOGGER.error("Unable to send chunks", throwable); disconnect("Internal server error"); return null; }); } }