Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 136
src/main/java/com/kodari/candyland/Candyland.java
package com.kodari.candyland;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.world.level.portal.TeleportTransition;
import net.minecraft.world.phys.Vec3;
public class Candyland implements ModInitializer {
private static final ResourceKey<Level> CANDYLAND = ResourceKey.create(
Registries.DIMENSION, Identifier.fromNamespaceAndPath("candyland", "candyland"));
@Override
public void onInitialize() {
UseBlockCallback.EVENT.register((player, level, hand, hit) -> {
if (!player.getItemInHand(hand).is(Items.SUGAR)
|| !level.getBlockState(hit.getBlockPos()).is(Blocks.CAKE)) {
return InteractionResult.PASS;
}
if (!(player instanceof ServerPlayer serverPlayer)) {
return InteractionResult.SUCCESS;
}
ResourceKey<Level> target = level.dimension().equals(CANDYLAND) ? Level.OVERWORLD : CANDYLAND;
ServerLevel destination = serverPlayer.level().getServer().getLevel(target);
if (destination == null) {
return InteractionResult.PASS;
}
BlockPos surface = destination.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES,
new BlockPos(hit.getBlockPos().getX(), 0, hit.getBlockPos().getZ()));
serverPlayer.teleport(new TeleportTransition(destination,
new Vec3(surface.getX() + 0.5, surface.getY(), surface.getZ() + 0.5),
Vec3.ZERO, serverPlayer.getYRot(), serverPlayer.getXRot(), TeleportTransition.DO_NOTHING));
return InteractionResult.SUCCESS_SERVER;
});
}
}