60 lines
2.0 KiB
Java
60 lines
2.0 KiB
Java
package com.thebrokenrail.energonrelics.block.entity.structure;
|
|
|
|
import com.thebrokenrail.energonrelics.block.structure.StructureGeneratorBlock;
|
|
import com.thebrokenrail.energonrelics.structure.StructurePart;
|
|
import com.thebrokenrail.energonrelics.structure.StructurePlacer;
|
|
import net.minecraft.block.BlockState;
|
|
import net.minecraft.block.entity.BlockEntity;
|
|
import net.minecraft.block.entity.BlockEntityType;
|
|
import net.minecraft.nbt.CompoundTag;
|
|
import net.minecraft.util.math.Direction;
|
|
import net.minecraft.world.World;
|
|
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
import java.util.Random;
|
|
|
|
public class StructureGeneratorBlockEntity extends BlockEntity {
|
|
public interface StructurePartFactory {
|
|
StructurePart<?> create(World world, Random random, List<StructurePart.Transformation> transformations);
|
|
}
|
|
|
|
private long seed = 0;
|
|
private boolean generated = false;
|
|
|
|
private final StructurePartFactory factory;
|
|
|
|
public StructureGeneratorBlockEntity(BlockEntityType<?> type, StructurePartFactory factory) {
|
|
super(type);
|
|
this.factory = factory;
|
|
}
|
|
|
|
public void setSeed(long seed) {
|
|
this.seed = seed;
|
|
}
|
|
|
|
@Override
|
|
public CompoundTag toTag(CompoundTag tag) {
|
|
super.toTag(tag);
|
|
tag.putLong("Seed", seed);
|
|
tag.putBoolean("Generated", generated);
|
|
return tag;
|
|
}
|
|
|
|
@Override
|
|
public void fromTag(BlockState state, CompoundTag tag) {
|
|
super.fromTag(state, tag);
|
|
seed = tag.getLong("Seed");
|
|
generated = tag.getBoolean("Generated");
|
|
}
|
|
|
|
public void generate() {
|
|
if (hasWorld() && !Objects.requireNonNull(getWorld()).isClient() && !generated) {
|
|
Direction facing = getCachedState().get(StructureGeneratorBlock.HORIZONTAL_FACING);
|
|
generated = true;
|
|
new StructurePlacer(factory.create(getWorld(), new Random(seed), Collections.singletonList(StructurePart.directionToTransformation(facing)))).place(getWorld(), getPos());
|
|
}
|
|
}
|
|
}
|