package com.thebrokenrail.reliccraft.structure; import com.thebrokenrail.reliccraft.RelicCraft; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.nbt.CompoundTag; import net.minecraft.state.property.Property; import net.minecraft.structure.*; import net.minecraft.structure.processor.BlockIgnoreStructureProcessor; import net.minecraft.util.BlockMirror; import net.minecraft.util.BlockRotation; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockBox; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.ChunkPos; import net.minecraft.world.Heightmap; import net.minecraft.world.ServerWorldAccess; import net.minecraft.world.WorldAccess; import net.minecraft.world.gen.StructureAccessor; import net.minecraft.world.gen.chunk.ChunkGenerator; import java.util.Collection; import java.util.List; import java.util.Random; public class TimeTempleGenerator { public static void addPieces(StructureManager structureManager, BlockPos blockPos, BlockRotation rotation, List list) { list.add(new Piece(structureManager, new Identifier(RelicCraft.NAMESPACE, "time_temple"), blockPos, rotation)); } public static class Piece extends SimpleStructurePiece { private final Identifier template; private final BlockRotation rotation; public Piece(StructureManager manager, Identifier identifier, BlockPos pos, BlockRotation rotation) { super(RelicCraft.TIME_TEMPLE_STRUCTURE_PIECE, 0); this.template = identifier; this.pos = pos; this.rotation = rotation; initializeStructureData(manager); } public Piece(StructureManager manager, CompoundTag tag) { super(RelicCraft.TIME_TEMPLE_STRUCTURE_PIECE, tag); template = new Identifier(tag.getString("Template")); rotation = BlockRotation.valueOf(tag.getString("Rot")); initializeStructureData(manager); } private void initializeStructureData(StructureManager manager) { Structure structure = manager.getStructureOrBlank(template); StructurePlacementData structurePlacementData = new StructurePlacementData().setRotation(rotation).setMirror(BlockMirror.NONE).setIgnoreEntities(true).addProcessor(BlockIgnoreStructureProcessor.IGNORE_STRUCTURE_BLOCKS); setStructureData(structure, pos, structurePlacementData); } @Override protected void toNbt(CompoundTag tag) { super.toNbt(tag); tag.putString("Template", template.toString()); tag.putString("Rot", rotation.name()); } @Override public boolean generate(ServerWorldAccess world, StructureAccessor structureAccessor, ChunkGenerator chunkGenerator, Random random, BlockBox boundingBox, ChunkPos chunkPos, BlockPos blockPos) { int yHeight = world.getTopY(Heightmap.Type.WORLD_SURFACE_WG, this.pos.getX() + 5, this.pos.getZ() + 5); BlockPos originalPos = pos; pos = pos.add(0, yHeight - 90, 0); boolean result = super.generate(world, structureAccessor, chunkGenerator, random, boundingBox, chunkPos, blockPos); if (result) { for (int x = getBoundingBox().minX; x <= getBoundingBox().maxX; x++) { for (int y = getBoundingBox().minY; y <= getBoundingBox().maxY; y++) { for (int z = getBoundingBox().minZ; z <= getBoundingBox().maxZ; z++) { BlockPos newPos = new BlockPos(x, y, z); if (boundingBox.contains(newPos)) { BlockState state = world.getBlockState(newPos); boolean damaged = random.nextFloat() < 0.6f; if (damaged) { if (state.getBlock() == Blocks.STONE_BRICKS) { boolean mossy = random.nextFloat() < 0.5f; world.setBlockState(newPos, convertState(state, mossy ? Blocks.MOSSY_STONE_BRICKS : Blocks.CRACKED_STONE_BRICKS), 3); } else if (world.getBlockState(newPos).getBlock() == Blocks.STONE_BRICK_STAIRS) { world.setBlockState(newPos, convertState(state, Blocks.MOSSY_STONE_BRICK_STAIRS), 3); } else if (world.getBlockState(newPos).getBlock() == Blocks.STONE_BRICK_SLAB) { world.setBlockState(newPos, convertState(state, Blocks.MOSSY_STONE_BRICK_SLAB), 3); } } } } } } } pos = originalPos; return result; } private BlockState convertState(BlockState oldState, Block newBlock) { BlockState newState = newBlock.getDefaultState(); Collection> properties = oldState.getProperties(); //noinspection rawtypes for (Property property : properties) { //noinspection unchecked newState = newState.with(property, oldState.get(property)); } return newState; } @Override protected void handleMetadata(String metadata, BlockPos pos, WorldAccess world, Random random, BlockBox boundingBox) { } } }