Jump to content

[1.19.4] How can I add cooldown to my portal block?


Feroov

Recommended Posts

Since there's no cooldown the player will go on a constant loop on changing dimensions since the player is inside the block. Here's what I have:


My portal block:
 

    @Override
    public void entityInside(BlockState blockState, Level level, BlockPos blockPos, Entity entity)
    {
        if(entity.isOnPortalCooldown()) {
            entity.setPortalCooldown();
        }

        if(!entity.isOnPortalCooldown() && entity instanceof LivingEntity) {
            if (!level.isClientSide())
            {
                if (!entity.isCrouching())
                {
                    MinecraftServer server = level.getServer();

                    if (server != null)
                    {
                        if (level.dimension() == DimensionsSTLCON.XENOSPHERE_KEY)
                        {
                            ServerLevel overWorld = server.getLevel(Level.OVERWORLD);
                            if (overWorld != null)
                            {
                                entity.changeDimension(overWorld, new XenosphereTeleporter(blockPos, false));
                            }
                        }
                        else
                        {
                            ServerLevel serverLevel = server.getLevel(DimensionsSTLCON.XENOSPHERE_KEY);
                            if (serverLevel != null)
                            {
                                entity.changeDimension(serverLevel, new XenosphereTeleporter(blockPos, true));
                            }
                        }
                    }
                }
            }
        }
    }

 

And my teleporter:
 

public class XenosphereTeleporter implements ITeleporter
{
    public static BlockPos thisPos = BlockPos.ZERO;
    public static boolean insideDimension = true;

    public XenosphereTeleporter(BlockPos pos, boolean insideDim)
    {
        thisPos = pos;
        insideDimension = insideDim;
    }

    @Override
    public Entity placeEntity(Entity entity, ServerLevel currentWorld, ServerLevel destinationWorld,
                              float yaw, Function<Boolean, Entity> repositionEntity)
    {
        entity = repositionEntity.apply(false);
        int y = 69;

        if (!insideDimension)
        {
            y = thisPos.getY();
        }

        BlockPos destinationPos = new BlockPos(thisPos.getX(), y, thisPos.getZ());

        int tries = 0;
        while ((destinationWorld.getBlockState(destinationPos).getMaterial() != Material.AIR) &&
                !destinationWorld.getBlockState(destinationPos).canBeReplaced(Fluids.WATER) &&
                destinationWorld.getBlockState(destinationPos.above()).getMaterial() != Material.AIR &&
                !destinationWorld.getBlockState(destinationPos.above()).canBeReplaced(Fluids.WATER) && tries < 25)
        {
            destinationPos = destinationPos.above(2);
            tries++;
        }

        entity.teleportTo(destinationPos.getX(), destinationPos.getY(), destinationPos.getZ());

        if (insideDimension)
        {
            boolean doSetBlock = true;
            for (BlockPos checkPos : BlockPos.betweenClosed(destinationPos.below(10).west(10).south(10), destinationPos.above(10).east(10).north(10)))
            {
                if (destinationWorld.getBlockState(checkPos).getBlock() instanceof XenosphereTeleporterBlock)
                {
                    doSetBlock = false;
                    break;
                }
            }
            if (doSetBlock)
            {
                destinationWorld.setBlock(destinationPos, BlocksSTLCON.XENOSPHERE_PORTAL.get().defaultBlockState(), 3);
            }
        }

        return entity;
    }
}

 

I've tried several ways but no luck, unfortunately, and ideas?

 

Link to comment
Share on other sites

15 hours ago, Feroov said:

I've tried several ways but no luck, unfortunately, and ideas?

You need to call `Entity#setPortalCooldown` twice: once when the entity is within the portal with a cooldown, and once when the entity is teleported by the portal. The first check prevents the entity from being teleported back if they haven't left the portal for a certain period of time. The second is for actually telling the entity that is has been teleported, and that it should verify the first check. You've currently only got the first check in place, which won't do anything on its own. You need to also set the cooldown after the entity has been teleported.

  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.