This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
RelicCraft/src/main/java/com/thebrokenrail/reliccraft/structure/TimeTempleGenerator.java

128 lines
5.8 KiB
Java

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.SimpleStructurePiece;
import net.minecraft.structure.Structure;
import net.minecraft.structure.StructureManager;
import net.minecraft.structure.StructurePiece;
import net.minecraft.structure.StructurePlacementData;
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.IWorld;
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<StructurePiece> 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).setMirrored(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(IWorld world, ChunkGenerator<?> generator, Random random, BlockBox box, ChunkPos chunkPos) {
int yHeight = 0;
int area = 0;
for (int x = getBoundingBox().minX; x <= getBoundingBox().maxX; x++) {
for (int z = getBoundingBox().minZ; z <= getBoundingBox().maxZ; z++) {
yHeight = yHeight + world.getTopY(Heightmap.Type.WORLD_SURFACE_WG, x, z);
area++;
}
}
yHeight = yHeight / area;
BlockPos originalPos = pos;
pos = pos.add(0, yHeight - 90, 0);
boolean result = super.generate(world, generator, random, box, chunkPos);
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 blockPos = new BlockPos(x, y, z);
if (box.contains(blockPos)) {
BlockState state = world.getBlockState(blockPos);
boolean damaged = random.nextFloat() < 0.6f;
if (damaged) {
if (state.getBlock() == Blocks.STONE_BRICKS) {
boolean mossy = random.nextFloat() < 0.5f;
world.setBlockState(blockPos, convertState(state, mossy ? Blocks.MOSSY_STONE_BRICKS : Blocks.CRACKED_STONE_BRICKS), 3);
} else if (world.getBlockState(blockPos).getBlock() == Blocks.STONE_BRICK_STAIRS) {
world.setBlockState(blockPos, convertState(state, Blocks.MOSSY_STONE_BRICK_STAIRS), 3);
} else if (world.getBlockState(blockPos).getBlock() == Blocks.STONE_BRICK_SLAB) {
world.setBlockState(blockPos, convertState(state, Blocks.MOSSY_STONE_BRICK_SLAB), 3);
}
}
}
}
}
}
}
pos = originalPos;
return result;
}
private BlockState convertState(BlockState oldState, Block newBlock) {
BlockState newState = newBlock.getDefaultState();
Collection<Property<?>> 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, IWorld world, Random random, BlockBox boundingBox) {
}
}
}