Search in sources :

Example 1 with EdenPortable

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);
                }
            }
        }
    });
}
Also used : EdenPortable(paulevs.edenring.interfaces.EdenPortable) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) Entity(net.minecraft.world.entity.Entity) ServerLevel(net.minecraft.server.level.ServerLevel) ServerPlayer(net.minecraft.server.level.ServerPlayer) FeaturePlaceContext(net.minecraft.world.level.levelgen.feature.FeaturePlaceContext) MutableBlockPos(net.minecraft.core.BlockPos.MutableBlockPos) AABB(net.minecraft.world.phys.AABB) MinecraftServer(net.minecraft.server.MinecraftServer) ResourceKey(net.minecraft.resources.ResourceKey)

Aggregations

MutableBlockPos (net.minecraft.core.BlockPos.MutableBlockPos)1 ResourceKey (net.minecraft.resources.ResourceKey)1 MinecraftServer (net.minecraft.server.MinecraftServer)1 ServerLevel (net.minecraft.server.level.ServerLevel)1 ServerPlayer (net.minecraft.server.level.ServerPlayer)1 Entity (net.minecraft.world.entity.Entity)1 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)1 FeaturePlaceContext (net.minecraft.world.level.levelgen.feature.FeaturePlaceContext)1 AABB (net.minecraft.world.phys.AABB)1 EdenPortable (paulevs.edenring.interfaces.EdenPortable)1