Files
src
build.gradle
gradle.properties
settings.gradle
Code
12 365
src/main/java/com/kodari/underworldmod/UnderworldMod.java
package com.kodari.underworldmod;
import java.util.Set;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.creativetab.v1.CreativeModeTabEvents;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Relative;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.world.level.Level;
public class UnderworldMod implements ModInitializer {
public static final String MOD_ID = "underworldmod";
private static final ResourceKey<Level> UNDERWORLD_KEY = ResourceKey.create(
Registries.DIMENSION,
Identifier.fromNamespaceAndPath(MOD_ID, "underworld")
);
private static final ResourceKey<Item> SIGIL_KEY = ResourceKey.create(
Registries.ITEM,
Identifier.fromNamespaceAndPath(MOD_ID, "underworld_sigil")
);
public static final Item UNDERWORLD_SIGIL = Registry.register(
BuiltInRegistries.ITEM,
SIGIL_KEY,
new Item(new Item.Properties().setId(SIGIL_KEY))
);
@Override
public void onInitialize() {
CreativeModeTabEvents.modifyOutputEvent(ResourceKey.create(
Registries.CREATIVE_MODE_TAB,
Identifier.fromNamespaceAndPath("minecraft", "tools_and_utilities")
)).register(output -> output.accept(
new ItemStack(UNDERWORLD_SIGIL),
CreativeModeTab.TabVisibility.PARENT_AND_SEARCH_TABS
));
UseItemCallback.EVENT.register((player, level, hand) -> {
if (player.getItemInHand(hand).getItem() != UNDERWORLD_SIGIL) {
return InteractionResult.PASS;
}
if (level.isClientSide()) {
return InteractionResult.SUCCESS;
}
if (!(player instanceof ServerPlayer serverPlayer)) {
return InteractionResult.PASS;
}
MinecraftServer server = level.getServer();
if (server == null) {
return InteractionResult.PASS;
}
ResourceKey<Level> destinationKey = level.dimension().equals(UNDERWORLD_KEY)
? Level.OVERWORLD
: UNDERWORLD_KEY;
ServerLevel destination = server.getLevel(destinationKey);
if (destination == null) {
return InteractionResult.PASS;
}
int x = (int) Math.floor(player.getX());
int z = (int) Math.floor(player.getZ());
int y = destination.getHeight(Heightmap.Types.MOTION_BLOCKING, x, z);
serverPlayer.teleportTo(
destination,
x + 0.5,
y + 1.0,
z + 0.5,
Set.of(),
serverPlayer.getYRot(),
serverPlayer.getXRot(),
true
);
return InteractionResult.SUCCESS;
});
}
}