Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 189
src/main/java/com/kodari/phasemansions/PhaseMansionsMod.java
package com.kodari.phasemansions;
import com.mojang.brigadier.arguments.IntegerArgumentType;
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.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.decoration.ItemFrame;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
public final class PhaseMansionsMod implements ModInitializer {
private static final Block[] PHASE_MATERIALS = {
Blocks.DIRT,
Blocks.GOLD_BLOCK,
Blocks.DIAMOND_BLOCK,
Blocks.NETHERITE_BLOCK
};
@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, buildContext, selection) -> dispatcher.register(
Commands.literal("mansion")
.then(Commands.argument("phase", IntegerArgumentType.integer(1, 4))
.then(Commands.argument("number", IntegerArgumentType.integer(1, 25))
.executes(context -> buildMansion(
context.getSource(),
IntegerArgumentType.getInteger(context, "phase"),
IntegerArgumentType.getInteger(context, "number")
))))
));
}
private static int buildMansion(CommandSourceStack source, int phase, int number) throws CommandSyntaxException {
var player = source.getPlayerOrException();
int mansionIndex = (phase - 1) * 25 + number;
int width = 12 + mansionIndex;
int depth = width - 2;
int stories = 1 + (mansionIndex - 1) / 17;
BlockState material = PHASE_MATERIALS[phase - 1].defaultBlockState();
BlockPos origin = player.blockPosition().offset(-width / 2, 0, 6);
buildStructure(source.getLevel(), origin, width, depth, stories, material);
source.sendSuccess(
() -> Component.literal("Built Phase " + phase + " Mansion " + number + " (" + width + "×" + depth + ", " + stories + " stories) with its own armory."),
false
);
return 1;
}
private static void buildStructure(ServerLevel level, BlockPos origin, int width, int depth, int stories, BlockState material) {
for (int story = 0; story < stories; story++) {
int floorY = story * 6;
for (int x = 0; x < width; x++) {
for (int z = 0; z < depth; z++) {
place(level, origin, x, floorY, z, material);
}
}
for (int y = floorY + 1; y <= floorY + 4; y++) {
for (int x = 0; x < width; x++) {
placeWall(level, origin, x, y, 0, x, 0, y, width, depth, story, material);
placeWall(level, origin, x, y, depth - 1, x, depth - 1, y, width, depth, story, material);
}
for (int z = 1; z < depth - 1; z++) {
placeWall(level, origin, 0, y, z, 0, z, y, width, depth, story, material);
placeWall(level, origin, width - 1, y, z, width - 1, z, y, width, depth, story, material);
}
}
for (int x = 0; x < width; x++) {
for (int z = 0; z < depth; z++) {
BlockState ceiling = x % 8 == 3 && z % 8 == 3
? Blocks.GLOWSTONE.defaultBlockState()
: material;
place(level, origin, x, floorY + 5, z, ceiling);
}
}
if (story == 0) {
buildArmory(level, origin, width, depth, material);
}
}
}
private static void placeWall(ServerLevel level, BlockPos origin, int x, int y, int z, int wallX, int wallZ, int worldY, int width, int depth, int story, BlockState material) {
boolean entrance = story == 0 && wallZ == 0 && wallX >= width / 2 && wallX <= width / 2 + 1 && worldY <= 2;
if (entrance) {
return;
}
boolean windowHeight = worldY == story * 6 + 2 || worldY == story * 6 + 3;
boolean frontWindow = wallZ == 0 && wallX > 1 && wallX < width - 2 && (wallX % 12 == 5 || wallX % 12 == 6);
boolean sideWindow = (wallX == 0 || wallX == width - 1) && wallZ > 1 && wallZ < depth - 2 && (wallZ % 12 == 5 || wallZ % 12 == 6);
BlockState state = windowHeight && (frontWindow || sideWindow)
? Blocks.GLASS.defaultBlockState()
: material;
place(level, origin, x, y, z, state);
}
private static void buildArmory(ServerLevel level, BlockPos origin, int width, int depth, BlockState material) {
int armoryPartition = depth - Math.max(4, depth / 4) - 1;
int doorway = width / 2;
for (int x = 1; x < width - 1; x++) {
for (int y = 1; y <= 4; y++) {
if ((x == doorway || x == doorway + 1) && y <= 2) {
continue;
}
place(level, origin, x, y, armoryPartition, material);
}
}
for (int x = 2; x < width - 2; x += 5) {
addWallGun(level, origin, x, 2, depth - 2);
addWallGun(level, origin, x, 4, depth - 2);
}
}
private static void addWallGun(ServerLevel level, BlockPos origin, int x, int y, int z) {
BlockPos framePosition = origin.offset(x, y, z);
ItemFrame frame = new ItemFrame(level, framePosition, Direction.NORTH);
frame.setItem(new ItemStack(Items.CROSSBOW));
level.addFreshEntity(frame);
}
private static void place(ServerLevel level, BlockPos origin, int x, int y, int z, BlockState state) {
level.setBlock(origin.offset(x, y, z), state, Block.UPDATE_ALL, 512);
}
}