Files
data
src
build.gradle
gradle.properties
settings.gradle
Code
45 771
src/main/java/com/kodari/glowskies/GlowstoneSkies.java
package com.kodari.glowskies;
import java.util.Set;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.creativetab.v1.CreativeModeTabEvents;
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.entity.player.Player;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
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;
public final class GlowstoneSkies implements ModInitializer {
public static final String MOD_ID = "glowskies";
public static final ResourceKey<Level> SKY_DIMENSION = ResourceKey.create(
Registries.DIMENSION,
Identifier.fromNamespaceAndPath(MOD_ID, "sky")
);
@Override
public void onInitialize() {
ModContent.initialize();
registerCreativeContent();
UseBlockCallback.EVENT.register((player, level, hand, hitResult) -> {
ItemStack stack = player.getItemInHand(hand);
BlockPos clicked = hitResult.getBlockPos();
if (!stack.is(item -> item.value() == Items.FLINT_AND_STEEL)
|| !level.getBlockState(clicked).is(Blocks.GLOWSTONE)) {
return InteractionResult.PASS;
}
PortalFrame frame = findPortalFrame(level, clicked);
if (frame == null) {
return InteractionResult.PASS;
}
if (!level.isClientSide()) {
for (int across = 1; across <= 2; across++) {
for (int height = 1; height <= 3; height++) {
BlockPos portalPos = frame.bottomLeft().offset(
frame.alongX() ? across : 0,
height,
frame.alongX() ? 0 : across
);
level.setBlock(portalPos, ModContent.SKY_PORTAL.defaultBlockState(), 3, 512);
}
}
stack.hurtAndBreak(1, player, hand);
}
return InteractionResult.SUCCESS;
});
}
private static void registerCreativeContent() {
ResourceKey<CreativeModeTab> buildingBlocks = ResourceKey.create(
Registries.CREATIVE_MODE_TAB,
Identifier.withDefaultNamespace("building_blocks")
);
CreativeModeTabEvents.modifyOutputEvent(buildingBlocks).register(output -> {
output.accept(new ItemStack(ModContent.SKYWOOD_LOG_ITEM), CreativeModeTab.TabVisibility.PARENT_AND_SEARCH_TABS);
output.accept(new ItemStack(ModContent.SKYWOOD_LEAVES_ITEM), CreativeModeTab.TabVisibility.PARENT_AND_SEARCH_TABS);
output.accept(new ItemStack(ModContent.SKYWOOD_PLANKS_ITEM), CreativeModeTab.TabVisibility.PARENT_AND_SEARCH_TABS);
});
}
private static PortalFrame findPortalFrame(Level level, BlockPos clicked) {
for (int axis = 0; axis < 2; axis++) {
boolean alongX = axis == 0;
for (int across = -3; across <= 0; across++) {
for (int down = -4; down <= 0; down++) {
BlockPos bottomLeft = clicked.offset(alongX ? across : 0, down, alongX ? 0 : across);
if (isPortalFrame(level, bottomLeft, alongX)) {
return new PortalFrame(bottomLeft, alongX);
}
}
}
}
return null;
}
private static boolean isPortalFrame(Level level, BlockPos bottomLeft, boolean alongX) {
for (int across = 0; across < 4; across++) {
for (int height = 0; height < 5; height++) {
boolean border = across == 0 || across == 3 || height == 0 || height == 4;
BlockPos pos = bottomLeft.offset(alongX ? across : 0, height, alongX ? 0 : across);
if (border ? !level.getBlockState(pos).is(Blocks.GLOWSTONE)
: level.getBlockState(pos).getBlock() != Blocks.AIR) {
return false;
}
}
}
return true;
}
public static void travel(ServerPlayer player) {
ServerLevel source = player.level();
ResourceKey<Level> destinationKey = source.dimension().equals(SKY_DIMENSION) ? Level.OVERWORLD : SKY_DIMENSION;
ServerLevel destination = source.getServer().getLevel(destinationKey);
if (destination == null) {
return;
}
int x = (int) Math.floor(player.getX());
int z = (int) Math.floor(player.getZ());
int y = destination.getHeight(Heightmap.Types.MOTION_BLOCKING, x, z);
boolean needsPlatform = y <= destination.getMinY() + 1;
if (needsPlatform) {
y = 80;
}
createDestinationPortal(destination, x, y, z, needsPlatform);
if (player.teleportTo(destination, x + 0.5, y, z + 0.5, Set.of(), player.getYRot(), player.getXRot(), true)) {
player.setPortalCooldown();
}
}
private static void createDestinationPortal(ServerLevel level, int x, int y, int z, boolean needsPlatform) {
if (needsPlatform) {
for (int dx = -2; dx <= 3; dx++) {
for (int dz = -2; dz <= 2; dz++) {
level.setBlock(new BlockPos(x + dx, y - 1, z + dz), Blocks.GLOWSTONE.defaultBlockState(), 3, 512);
}
}
}
BlockPos bottomLeft = new BlockPos(x - 1, y - 1, z);
for (int across = 0; across < 4; across++) {
for (int height = 0; height < 5; height++) {
if (across == 0 || across == 3 || height == 0 || height == 4) {
level.setBlock(bottomLeft.offset(across, height, 0), Blocks.GLOWSTONE.defaultBlockState(), 3, 512);
}
}
}
for (int across = 1; across <= 2; across++) {
for (int height = 1; height <= 3; height++) {
level.setBlock(bottomLeft.offset(across, height, 0), ModContent.SKY_PORTAL.defaultBlockState(), 3, 512);
}
}
}
private record PortalFrame(BlockPos bottomLeft, boolean alongX) {
}
}