use of thpmc.engine.api.entity.EngineEntity in project THP-Engine by TheHollowPlanetMC.
the class TickRunner method run.
@Override
public void run() {
if (beforeTime + TIME - 20 > System.currentTimeMillis())
return;
if (isStopped)
return;
if (i % TPS == 0) {
long time = System.currentTimeMillis();
tps = ((double) Math.round((20.0 / (((double) (time - beforeTime)) / 1000.0)) * 10)) / 10;
beforeTime = time;
}
currentThread = Thread.currentThread();
this.lastTickMS = System.currentTimeMillis();
// Should remove check
entities.removeIf(TickBase::shouldRemove);
tickOnlyEntities.removeIf(TickBase::shouldRemove);
// Add entities
if (addEntities.size() != 0) {
try {
ADD_LOCK.lock();
for (TickBase entity : addEntities) {
if (entity instanceof EngineEntity) {
entities.add((EngineEntity) entity);
} else {
tickOnlyEntities.add(entity);
}
}
addEntities.clear();
} finally {
ADD_LOCK.unlock();
}
}
// tick
tickOnlyEntities.forEach(TickBase::tick);
entities.forEach(TickBase::tick);
// Tracker
for (EnginePlayer enginePlayer : EnginePlayer.getAllPlayers()) {
EntityTracker entityTracker = getEntityTracker(enginePlayer);
entityTracker.tick(entities);
}
if (i % 40 == 0) {
trackerMap.keySet().removeIf(enginePlayer -> !enginePlayer.getBukkitPlayer().isOnline());
}
entities.forEach(EngineEntity::setPreviousPosition);
if (i % 100 == 0)
System.out.printf("ID -> %d TPS: %.1f%n", ID, tps);
i++;
}
use of thpmc.engine.api.entity.EngineEntity in project THP-Engine by TheHollowPlanetMC.
the class EngineWorld method rayTraceEntitiesForShortRange.
@Nullable
default EngineRayTraceResult rayTraceEntitiesForShortRange(@NotNull Vector startPosition, @NotNull Vector direction, double distance, @NotNull CollideOption collideOption) {
Vector movement = direction.clone().normalize().multiply(distance);
Vector endPosition = startPosition.clone().add(movement);
// create region for get entities
EngineBoundingBox region = new EngineBoundingBox(startPosition.getX(), startPosition.getY(), startPosition.getZ(), endPosition.getX(), endPosition.getY(), endPosition.getZ()).expandForMovement(movement);
region.expand(collideOption.getBoundingBoxGrow());
region.expand(16);
// get entities
Collection<EngineEntity> entities = getEntitiesInRangeChunkSection(region);
// collect collisions
Set<EngineBoundingBox> boxList = new HashSet<>();
for (EngineEntity entity : entities) {
EngineEntityBoundingBox boundingBox = entity.getBoundingBox();
if (boundingBox != null) {
boxList.add(boundingBox);
}
}
// perform raytrace
return rayTraceForCollisionList(startPosition, direction, distance, boxList, collideOption);
}
use of thpmc.engine.api.entity.EngineEntity in project THP-Engine by TheHollowPlanetMC.
the class EntityTracker method tick.
public void tick(Set<EngineEntity> entities) {
tick++;
// Collect tracking entities
if (tick % TRACK_INTERVAL == 0) {
Location pl = enginePlayer.getBukkitPlayer().getLocation();
int playerChunkX = pl.getBlockX() >> 4;
int playerChunkZ = pl.getBlockZ() >> 4;
int drawDistance = enginePlayer.getEntityDrawDistance();
ThreadLocalEngineWorld world = tickRunner.getThreadLocalCache().getWorld(Objects.requireNonNull(pl.getWorld()).getName());
for (EngineEntity entity : entities) {
EngineLocation location = entity.getLocation();
if (location.getWorld() != world) {
continue;
}
int entityChunkX = location.getBlockX() >> 4;
int entityChunkZ = location.getBlockZ() >> 4;
if (Math.abs(entityChunkX - playerChunkX) + Math.abs(entityChunkZ - playerChunkZ) <= drawDistance) {
// in range
if (!trackedEntities.contains(entity) && trackedEntities.size() <= (enginePlayer.getEntityTrackLimit() / THPEngineAPI.getInstance().getTickRunnerPool().getPoolSize())) {
entity.show(enginePlayer);
trackedEntities.add(entity);
}
} else {
// out range
if (trackedEntities.contains(entity)) {
entity.hide(enginePlayer);
trackedEntities.remove(entity);
}
}
}
}
// Show tick result
boolean absolute = tick % ABSOLUTE_PACKET_INTERVAL == 0;
trackedEntities.forEach(engineEntity -> engineEntity.playTickResult(enginePlayer, absolute));
}
Aggregations