Files
src
build.gradle
gradle.properties
settings.gradle
Code
10 281
src/main/java/com/kodari/theshard/TheShard.java
package com.kodari.theshard;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
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.level.Level;
import java.util.Set;
public class TheShard implements ModInitializer {
public static final String MOD_ID = "theshard";
private static final ResourceKey<Level> SHARD = ResourceKey.create(
Registries.DIMENSION,
Identifier.fromNamespaceAndPath(MOD_ID, "shard")
);
@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, buildContext, selection) ->
dispatcher.register(Commands.literal("shard")
.executes(context -> teleport(context.getSource(), SHARD, "Entered The Shard."))
.then(Commands.literal("return")
.executes(context -> teleport(context.getSource(), Level.OVERWORLD, "Returned to the Overworld."))))
);
}
private static int teleport(CommandSourceStack source, ResourceKey<Level> destinationKey, String message) throws CommandSyntaxException {
ServerPlayer player = source.getPlayerOrException();
ServerLevel destination = source.getServer().getLevel(destinationKey);
if (destination == null) {
source.sendFailure(Component.literal("The destination dimension is unavailable."));
return 0;
}
player.teleportTo(destination, 0.5, 100.0, 0.5, Set.of(), 0.0F, 0.0F, true);
source.sendSuccess(() -> Component.literal(message), false);
return 1;
}
}