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/block/structure/StructureGeneratorPiece.java

68 lines
2.6 KiB
Java
Raw Normal View History

2020-07-22 01:23:33 +00:00
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;
2020-07-29 18:18:16 +00:00
import net.minecraft.world.StructureWorldAccess;
2020-07-22 01:23:33 +00:00
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;
2020-08-31 23:19:54 +00:00
private boolean placed;
2020-07-22 01:23:33 +00:00
2020-08-31 23:19:54 +00:00
private StructureGeneratorPiece(StructureGeneratorBlock block, BlockRotation rotation, BlockPos pos, boolean placed) {
2020-07-22 01:23:33 +00:00
super(block.piece, 0);
this.block = block;
this.rotation = rotation;
this.pos = pos;
2020-08-31 23:19:54 +00:00
this.placed = placed;
2020-07-22 01:23:33 +00:00
boundingBox = BlockBox.create(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1);
}
2020-08-31 23:19:54 +00:00
StructureGeneratorPiece(StructureGeneratorBlock block, BlockRotation rotation, BlockPos pos) {
this(block, rotation, pos, false);
}
2020-07-22 01:23:33 +00:00
StructureGeneratorPiece(StructureGeneratorBlock block, CompoundTag tag) {
2020-08-31 23:19:54 +00:00
this(block, BlockRotation.valueOf(tag.getString("Rot")), new BlockPos(tag.getInt("X"), tag.getInt("Y"), tag.getInt("Z")), tag.getBoolean("Placed"));
2020-07-22 01:23:33 +00:00
}
@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());
2020-08-31 23:19:54 +00:00
tag.putBoolean("Placed", placed);
2020-07-22 01:23:33 +00:00
}
@Override
2020-07-29 18:18:16 +00:00
public boolean generate(StructureWorldAccess world, StructureAccessor structureAccessor, ChunkGenerator chunkGenerator, Random random, BlockBox boundingBox, ChunkPos chunkPos, BlockPos blockPos) {
2020-08-31 23:19:54 +00:00
if (!placed) {
BlockState state = block.getDefaultState().rotate(rotation);
2020-07-22 01:23:33 +00:00
2020-08-31 23:19:54 +00:00
world.setBlockState(pos, state, 3);
BlockEntity entity = world.getBlockEntity(pos);
if (entity instanceof StructureGeneratorBlockEntity) {
((StructureGeneratorBlockEntity) entity).setSeed(random.nextLong());
}
block.schedule(world, pos);
2020-07-22 01:23:33 +00:00
2020-08-31 23:19:54 +00:00
placed = true;
}
2020-07-22 01:23:33 +00:00
return true;
}
}