MCPcopy Create free account
hub / github.com/McJty/TutorialV3 / PortalBlock

Class PortalBlock

src/main/java/com/example/tutorialv3/blocks/PortalBlock.java:20–54  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18import net.minecraft.world.phys.shapes.VoxelShape;
19
20public class PortalBlock extends Block {
21
22 // Our block is lower then a normal block. That causes the player to sink in it when he stands on the block
23 // And that in turn causes our 'entityInside' test to detect the player
24 private static final VoxelShape SHAPE = Shapes.box(0, 0, 0, 1, .8, 1);
25
26 public PortalBlock() {
27 super(Properties.of(Material.METAL)
28 .sound(SoundType.METAL)
29 .strength(-1.0F, 3600000.0F)
30 .noDrops());
31 }
32
33 @Override
34 public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
35 return SHAPE;
36 }
37
38 // This works because our block isn't a full block
39 @Override
40 public void entityInside(BlockState state, Level level, BlockPos pos, Entity entity) {
41 if (entity instanceof ServerPlayer player) {
42 if (level.dimension().equals(Dimensions.MYSTERIOUS)) {
43 teleportTo(player, pos.north(), Level.OVERWORLD);
44 } else {
45 teleportTo(player, pos.north(), Dimensions.MYSTERIOUS);
46 }
47 }
48 }
49
50 private void teleportTo(ServerPlayer player, BlockPos pos, ResourceKey<Level> id) {
51 ServerLevel world = player.getServer().getLevel(id);
52 Tools.teleport(player, world, new BlockPos(pos.getX(), pos.getY(), pos.getZ()), true);
53 }
54}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected