Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 260
src/main/java/com/kodari/skygen/SkyGen.java
package com.kodari.skygen;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.context.CommandContext;
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.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.permissions.Permissions;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.ChestBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
public final class SkyGen implements ModInitializer {
public static final String MOD_ID = "skygen";
private static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
private static final Map<UUID, Island> ISLANDS = new HashMap<>();
@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
dispatcher.register(Commands.literal("skygen")
.requires(SkyGen::canManage)
.then(Commands.literal("create").executes(SkyGen::createIsland))
.then(Commands.literal("reset").executes(SkyGen::resetIsland))
.then(Commands.literal("info").executes(SkyGen::showInfo))));
LOGGER.info("SkyGen commands registered");
}
private static boolean canManage(CommandSourceStack source) {
return source.permissions().hasPermission(Permissions.COMMANDS_MODERATOR);
}
private static int createIsland(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
Island previous = ISLANDS.remove(player.getUUID());
if (previous != null) {
restoreIsland(previous);
}
ServerLevel level = player.serverLevel();
BlockPos center = player.blockPosition().below();
Island island = new Island(level, center);
buildIsland(island);
ISLANDS.put(player.getUUID(), island);
context.getSource().sendSuccess(
() -> Component.literal("SkyGen créée en " + center.getX() + ", " + center.getY() + ", " + center.getZ()),
true);
return Command.SINGLE_SUCCESS;
}
private static int resetIsland(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
Island island = ISLANDS.remove(player.getUUID());
if (island == null) {
context.getSource().sendSuccess(() -> Component.literal("Tu n'as pas d'île SkyGen active."), false);
return Command.SINGLE_SUCCESS;
}
restoreIsland(island);
context.getSource().sendSuccess(() -> Component.literal("Ton île SkyGen a été supprimée."), true);
return Command.SINGLE_SUCCESS;
}
private static int showInfo(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
ServerPlayer player = context.getSource().getPlayerOrException();
Island island = ISLANDS.get(player.getUUID());
if (island == null) {
context.getSource().sendSuccess(() -> Component.literal("Tu n'as pas d'île SkyGen active."), false);
return Command.SINGLE_SUCCESS;
}
BlockPos center = island.center;
context.getSource().sendSuccess(
() -> Component.literal("Ton île est en " + center.getX() + ", " + center.getY() + ", " + center.getZ()),
false);
return Command.SINGLE_SUCCESS;
}
private static void buildIsland(Island island) {
ServerLevel level = island.level;
BlockPos center = island.center;
for (int x = -5; x <= 5; x++) {
for (int z = -5; z <= 5; z++) {
int distance = x * x + z * z;
if (distance > 25) {
continue;
}
int depth = distance > 16 ? 1 : distance > 8 ? 2 : 3;
place(island, center.offset(x, 0, z), Blocks.GRASS_BLOCK.defaultBlockState());
for (int layer = 1; layer <= depth; layer++) {
BlockState state = layer == depth
? Blocks.STONE.defaultBlockState()
: Blocks.DIRT.defaultBlockState();
place(island, center.offset(x, -layer, z), state);
}
}
}
for (int y = 1; y <= 3; y++) {
place(island, center.offset(-2, y, -2), Blocks.OAK_LOG.defaultBlockState());
}
for (int x = -3; x <= -1; x++) {
for (int z = -3; z <= -1; z++) {
place(island, center.offset(x, 4, z), Blocks.OAK_LEAVES.defaultBlockState());
}
}
place(island, center.offset(-2, 4, -2), Blocks.OAK_LEAVES.defaultBlockState());
BlockPos chestPos = center.offset(2, 1, 2);
place(island, chestPos, Blocks.CHEST.defaultBlockState());
fillStarterChest(level, chestPos);
place(island, center.offset(2, 1, 0), Blocks.WATER.defaultBlockState());
place(island, center.offset(3, 1, 0), Blocks.COBBLESTONE.defaultBlockState());
place(island, center.offset(4, 1, 0), Blocks.LAVA.defaultBlockState());
}
private static void fillStarterChest(ServerLevel level, BlockPos chestPos) {
if (!(level.getBlockEntity(chestPos) instanceof ChestBlockEntity chest)) {
return;
}
chest.setItem(0, new ItemStack(Items.WOODEN_PICKAXE));
chest.setItem(1, new ItemStack(Items.OAK_SAPLING, 2));
chest.setItem(2, new ItemStack(Items.BREAD, 8));
chest.setItem(3, new ItemStack(Items.DIRT, 2));
chest.setChanged();
}
private static void place(Island island, BlockPos position, BlockState state) {
island.originalBlocks.putIfAbsent(position, island.level.getBlockState(position));
island.level.setBlockAndUpdate(position, state);
}
private static void restoreIsland(Island island) {
for (Map.Entry<BlockPos, BlockState> entry : island.originalBlocks.entrySet()) {
island.level.setBlockAndUpdate(entry.getKey(), entry.getValue());
}
}
private static final class Island {
private final ServerLevel level;
private final BlockPos center;
private final Map<BlockPos, BlockState> originalBlocks = new LinkedHashMap<>();
private Island(ServerLevel level, BlockPos center) {
this.level = level;
this.center = center;
}
}
}