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.
EnergonRelics/src/main/java/com/thebrokenrail/energonrelics/structure/feature/ResearchComplexGenerator.java

83 lines
3.3 KiB
Java

package com.thebrokenrail.energonrelics.structure.feature;
import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.block.entity.structure.ResearchComplexGeneratorBlockEntity;
import com.thebrokenrail.energonrelics.block.structure.ResearchComplexGeneratorBlock;
import com.thebrokenrail.energonrelics.util.MissingCaseException;
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.util.math.Direction;
import net.minecraft.world.ServerWorldAccess;
import net.minecraft.world.gen.StructureAccessor;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import java.util.List;
import java.util.Random;
public class ResearchComplexGenerator {
static void addPieces(BlockPos pos, BlockRotation rotation, List<StructurePiece> pieces) {
pieces.add(new Piece(rotation, pos));
}
public static class Piece extends StructurePiece {
private final BlockRotation rotation;
private final BlockPos pos;
private Piece(BlockRotation rotation, BlockPos pos) {
super(EnergonRelics.RESEARCH_COMPLEX_STRUCTURE_PIECE, 0);
this.rotation = rotation;
this.pos = pos;
boundingBox = BlockBox.create(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1);
}
public Piece(CompoundTag tag) {
super(EnergonRelics.RESEARCH_COMPLEX_STRUCTURE_PIECE, tag);
rotation = BlockRotation.valueOf(tag.getString("Rot"));
pos = 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(ServerWorldAccess world, StructureAccessor structureAccessor, ChunkGenerator chunkGenerator, Random random, BlockBox boundingBox, ChunkPos chunkPos, BlockPos blockPos) {
world.setBlockState(pos, EnergonRelics.RESEARCH_COMPLEX_GENERATOR_BLOCK.getDefaultState().with(ResearchComplexGeneratorBlock.HORIZONTAL_FACING, blockRotationToDirection(rotation)), 4);
BlockEntity entity = world.getBlockEntity(pos);
if (entity instanceof ResearchComplexGeneratorBlockEntity) {
((ResearchComplexGeneratorBlockEntity) entity).setSeed(random.nextLong());
}
return true;
}
private static Direction blockRotationToDirection(BlockRotation rotation) {
switch (rotation) {
case NONE: {
return Direction.NORTH;
}
case CLOCKWISE_90: {
return Direction.EAST;
}
case CLOCKWISE_180: {
return Direction.SOUTH;
}
case COUNTERCLOCKWISE_90: {
return Direction.WEST;
}
default: {
throw new MissingCaseException(rotation);
}
}
}
}
}