use of paulevs.edenring.interfaces.EdenPortable in project EdenRing by paulevsGitch.
the class EdenPortalBlockEntity method serverTick.
public static <T extends BlockEntity> void serverTick(Level level, BlockPos blockPos, BlockState state, T entity) {
if (entity == null || !(entity instanceof EdenPortalBlockEntity)) {
return;
}
EdenPortalBlockEntity portal = (EdenPortalBlockEntity) entity;
if (portal.checkTicks > 10) {
portal.checkTicks = 0;
if (!EdenPortal.checkOldPortal(level, blockPos)) {
EdenPortal.destroyPortal(level, blockPos);
level.setBlockAndUpdate(blockPos, Blocks.AIR.defaultBlockState());
portal.setRemoved();
return;
}
}
portal.checkTicks++;
if (portal.box == null) {
portal.box = new AABB(blockPos);
portal.box = portal.box.inflate(0.5, 0, 0.5).setMaxY(blockPos.getY() + 3);
}
List<Entity> entities = level.getEntities(null, portal.box);
if (entities.isEmpty()) {
return;
}
MinecraftServer server = level.getServer();
ResourceKey key = level.dimension().equals(EdenRing.EDEN_RING_KEY) ? Level.OVERWORLD : EdenRing.EDEN_RING_KEY;
ServerLevel destination = server.getLevel(key);
if (destination == null) {
return;
}
MutableBlockPos preExit = getExit(destination, blockPos);
if (preExit == null) {
preExit = blockPos.mutable();
getLand(destination, preExit);
if (preExit.getY() == 130 && destination.getBlockState(preExit.below(2)).isAir()) {
EdenFeatures.SMALL_ISLAND.getFeature().place(new FeaturePlaceContext(Optional.empty(), destination, destination.getChunkSource().getGenerator(), destination.random, preExit.below(2), null));
}
EdenPortal.buildPortal(destination, preExit);
}
final MutableBlockPos exit = preExit;
entities.forEach(e -> {
if (e.isAlive()) {
EdenPortable portable = (EdenPortable) e;
if (portable.getPortalTimeout() > 0) {
portable.setPortalTimeout(20);
} else {
if (e instanceof ServerPlayer) {
ServerPlayer player = (ServerPlayer) e;
player.teleportTo(destination, exit.getX() + 0.5, exit.getY(), exit.getZ() + 0.5, player.getYRot(), player.getXRot());
((EdenPortable) player).setPortalTimeout(20);
} else {
e.ejectPassengers();
Entity newEntity = e.getType().create(destination);
newEntity.restoreFrom(e);
e.remove(RemovalReason.CHANGED_DIMENSION);
destination.addDuringTeleport(newEntity);
((EdenPortable) newEntity).setPortalTimeout(100);
newEntity.teleportTo(exit.getX() + 0.5, exit.getY(), exit.getZ() + 0.5);
}
}
}
});
}
Aggregations