Jump to content

1.16.5 - Custom dimension portal not working


JustinB0406

Recommended Posts

I'm attempting to create a portal to my custom dimension, but Minecraft freezes on the "Loading Terrain" screen whenever I enter. However, when I use commands to go to my dimension, it works fine.

I searched through Minecraft's libraries and decided to use a slightly modified version of the end portal's code for my portal, but when I debugged the mod, I noticed that my onEntityCollision method, which contains the code to switch dimensions, runs over and over. I believe this is what leads to the game becoming overloaded each time I use the portal. How would I go about fixing this?

 

The portal class:

package com.justinb.ramwal.blocks;

import com.justinb.ramwal.init.BlockInit;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Direction;
import net.minecraft.util.RegistryKey;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;

public class LemonPortalBlock extends Block {
    public LemonPortalBlock(Properties properties) {
        super(properties);
    }

    public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld worldIn, BlockPos currentPos, BlockPos facingPos) {
        return !isValidSpawn(currentPos, (World) worldIn) ? BlockInit.LEMONPORTAL.get().getDefaultState() : super.updatePostPlacement(stateIn, facing, facingState, worldIn, currentPos, facingPos);
    }

    public void onEntityCollision(BlockState state, World worldIn, BlockPos pos, Entity entityIn) {
        if (worldIn instanceof ServerWorld && !entityIn.isPassenger() && !entityIn.isBeingRidden() && entityIn.canChangeDimension()) {
            RegistryKey<World> dim = RegistryKey.getOrCreateKey(Registry.WORLD_KEY, new ResourceLocation("ramwal:lemondimension"));
            RegistryKey<World> registrykey = worldIn.getDimensionKey() == dim ? World.OVERWORLD : dim;
            ServerWorld serverworld = ((ServerWorld)worldIn).getServer().getWorld(registrykey);

            if (serverworld == null) {
                return;
            }

            entityIn.changeDimension(serverworld);
        }
    }

    public ItemStack getItem(IBlockReader worldIn, BlockPos pos, BlockState state) {
        return ItemStack.EMPTY;
    }

    public static boolean isValidSpawn(BlockPos pos, World worldIn) {
        for (int i = 2; i <= 5; i++)
            if (!(worldIn.getBlockState(pos.offset(Direction.byIndex(i))).getBlock() == BlockInit.GLITCH.get())) return false;
        return true;
    }
}

 

Link to comment
Share on other sites

That worked, thank you!

For anyone with the same issue, here is my code:

onEntityCollision method:

public void onEntityCollision(BlockState state, World worldIn, BlockPos pos, Entity entityIn) {
        if (worldIn instanceof ServerWorld && !entityIn.isPassenger() && !entityIn.isBeingRidden() && entityIn.canChangeDimension()) {
            RegistryKey<World> dim = RegistryKey.getOrCreateKey(Registry.WORLD_KEY, new ResourceLocation("ramwal:lemondimension"));
            RegistryKey<World> registrykey = worldIn.getDimensionKey() == dim ? World.OVERWORLD : dim;
            ServerWorld serverworld = ((ServerWorld)worldIn).getServer().getWorld(registrykey);

            if (serverworld == null) {
                return;
            }

            entityIn.changeDimension(serverworld, new ModTeleporter());
        }
    }

 

ModTeleporter class:

package com.justinb.ramwal.inherited;

import net.minecraft.entity.Entity;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.common.util.ITeleporter;

import java.util.function.Function;

public class ModTeleporter implements ITeleporter {
    @Override
    public Entity placeEntity(Entity entity, ServerWorld currentWorld, ServerWorld destWorld, float yaw, Function<Boolean, Entity> repositionEntity)
    {
        return repositionEntity.apply(false);
    }
}

 

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.