EnergonRelics/src/main/java/com/thebrokenrail/energonrelics/block/structure/StructureGeneratorPiece.java

57 lines
2.2 KiB
Java

package com.thebrokenrail.energonrelics.block.structure;
import com.thebrokenrail.energonrelics.block.entity.structure.StructureGeneratorBlockEntity;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.structure.StructurePiece;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.math.BlockBox;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.StructureWorldAccess;
import net.minecraft.world.gen.StructureAccessor;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import java.util.Random;
class StructureGeneratorPiece extends StructurePiece {
private final StructureGeneratorBlock block;
private final BlockRotation rotation;
private final BlockPos pos;
StructureGeneratorPiece(StructureGeneratorBlock block, BlockRotation rotation, BlockPos pos) {
super(block.piece, 0);
this.block = block;
this.rotation = rotation;
this.pos = pos;
boundingBox = BlockBox.create(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1);
}
StructureGeneratorPiece(StructureGeneratorBlock block, CompoundTag tag) {
this(block, BlockRotation.valueOf(tag.getString("Rot")), new BlockPos(tag.getInt("X"), tag.getInt("Y"), tag.getInt("Z")));
}
@Override
protected void toNbt(CompoundTag tag) {
tag.putString("Rot", rotation.name());
tag.putInt("X", pos.getX());
tag.putInt("Y", pos.getY());
tag.putInt("Z", pos.getZ());
}
@Override
public boolean generate(StructureWorldAccess world, StructureAccessor structureAccessor, ChunkGenerator chunkGenerator, Random random, BlockBox boundingBox, ChunkPos chunkPos, BlockPos blockPos) {
BlockState state = block.getDefaultState().rotate(rotation);
world.setBlockState(pos, state, 3);
BlockEntity entity = world.getBlockEntity(pos);
if (entity instanceof StructureGeneratorBlockEntity) {
((StructureGeneratorBlockEntity) entity).setSeed(random.nextLong());
}
block.schedule(world, pos);
return true;
}
}