Files
gradle
src
build.gradle
gradle.properties
gradlew
gradlew.bat
settings.gradle
Code
10 508
src/main/java/com/kodari/realisticdetails/RealisticDetailsMod.java
package com.kodari.realisticdetails;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.BlockSoundGroup;
import net.minecraft.block.DirectionalBlock;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.DirectionProperty;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
import net.minecraft.world.WorldView;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.block.ShapeContext;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class RealisticDetailsMod implements ModInitializer {
public static final String MOD_ID = "realisticdetails";
private static final List<Item> DECORATION_ITEMS = new ArrayList<>();
private static Block iconBlock;
@Override
public void onInitialize() {
registerDecorations();
registerItemGroup();
}
private static void registerDecorations() {
try (InputStream input = RealisticDetailsMod.class.getResourceAsStream("/decorations.csv")) {
if (input == null) {
throw new IllegalStateException("Missing decorations.csv resource");
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.trim().isEmpty() || line.trim().startsWith("#")) {
continue;
}
String[] fields = line.split("\\|", -1);
String id = fields[0];
Mount mount = Mount.valueOf(fields[2].toUpperCase(Locale.ROOT));
double width = Double.parseDouble(fields[4]);
double height = Double.parseDouble(fields[5]);
double depth = Double.parseDouble(fields[6]);
Identifier identifier = new Identifier(MOD_ID, id);
DetailBlock block = new DetailBlock(width, height, depth, mount);
Registry.register(Registries.BLOCK, identifier, block);
Item item = Registry.register(Registries.ITEM, identifier, new BlockItem(block, new Item.Settings()));
DECORATION_ITEMS.add(item);
if (id.equals("outlet_white")) {
iconBlock = block;
}
}
}
} catch (IOException exception) {
throw new IllegalStateException("Unable to load decorative block catalog", exception);
}
}
private static void registerItemGroup() {
if (iconBlock == null) {
throw new IllegalStateException("The decoration catalog must contain outlet_white");
}
Registry.register(Registries.ITEM_GROUP, new Identifier(MOD_ID, "building_details"),
FabricItemGroup.builder()
.icon(() -> new ItemStack(iconBlock))
.displayName(Text.translatable("itemGroup.realisticdetails.building_details"))
.entries((context, entries) -> DECORATION_ITEMS.forEach(entries::add))
.build());
}
private enum Mount {
WALL,
FLOOR,
CEILING,
FLEX
}
private static final class DetailBlock extends Block {
private static final DirectionProperty FACING = DirectionalBlock.FACING;
private final double width;
private final double height;
private final double depth;
private final Mount mount;
private DetailBlock(double width, double height, double depth, Mount mount) {
super(Block.Settings.create().strength(0.2F).sounds(BlockSoundGroup.METAL).nonOpaque());
this.width = width;
this.height = height;
this.depth = depth;
this.mount = mount;
setDefaultState(getStateManager().getDefaultState().with(FACING, Direction.NORTH));
}
@Override
public BlockState getPlacementState(ItemPlacementContext context) {
Direction clickedFace = context.getSide();
if (mount == Mount.WALL && !clickedFace.getAxis().isHorizontal()) {
return null;
}
if (mount == Mount.FLOOR && clickedFace != Direction.UP) {
return null;
}
if (mount == Mount.CEILING && clickedFace != Direction.DOWN) {
return null;
}
return getDefaultState().with(FACING, clickedFace);
}
@Override
public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) {
Direction facing = state.get(FACING);
BlockPos supportPos = pos.offset(facing.getOpposite());
return world.getBlockState(supportPos).isSideSolidFullSquare(world, supportPos, facing);
}
@Override
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
Direction facing = state.get(FACING);
double halfWidth = width / 2.0;
double halfHeight = height / 2.0;
double halfDepth = depth / 2.0;
if (facing == Direction.UP) {
return VoxelShapes.cuboid(0.5 - halfWidth, 0.0, 0.5 - halfDepth,
0.5 + halfWidth, height, 0.5 + halfDepth);
}
if (facing == Direction.DOWN) {
return VoxelShapes.cuboid(0.5 - halfWidth, 1.0 - height, 0.5 - halfDepth,
0.5 + halfWidth, 1.0, 0.5 + halfDepth);
}
if (facing == Direction.NORTH) {
return VoxelShapes.cuboid(0.5 - halfWidth, 0.5 - halfHeight, 0.0,
0.5 + halfWidth, 0.5 + halfHeight, depth);
}
if (facing == Direction.SOUTH) {
return VoxelShapes.cuboid(0.5 - halfWidth, 0.5 - halfHeight, 1.0 - depth,
0.5 + halfWidth, 0.5 + halfHeight, 1.0);
}
if (facing == Direction.EAST) {
return VoxelShapes.cuboid(1.0 - depth, 0.5 - halfHeight, 0.5 - halfWidth,
1.0, 0.5 + halfHeight, 0.5 + halfWidth);
}
return VoxelShapes.cuboid(0.0, 0.5 - halfHeight, 0.5 - halfWidth,
depth, 0.5 + halfHeight, 0.5 + halfWidth);
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(FACING);
}
}
}