NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/tiles/BlockEntityChorusGenerator.java

123 lines
4.8 KiB
Java
Raw Normal View History

2020-05-04 23:39:57 +02:00
package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticles;
2021-12-06 14:38:12 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2021-12-06 14:38:12 +01:00
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
2020-05-04 23:39:57 +02:00
2021-01-14 23:15:02 +01:00
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
2020-05-04 23:39:57 +02:00
2021-12-04 15:40:09 +01:00
public class BlockEntityChorusGenerator extends BlockEntityImpl implements ITickableBlockEntity {
2020-05-04 23:39:57 +02:00
private final Deque<BlockPos> currentlyBreaking = new ArrayDeque<>();
private int auraPerBlock;
2021-12-06 14:38:12 +01:00
public BlockEntityChorusGenerator(BlockPos pos, BlockState state) {
super(ModTileEntities.CHORUS_GENERATOR, pos, state);
2020-05-04 23:39:57 +02:00
}
@Override
public void tick() {
2021-12-04 15:40:09 +01:00
if (this.level.isClientSide)
2020-05-04 23:39:57 +02:00
return;
2021-12-04 15:40:09 +01:00
if (this.level.getGameTime() % 5 != 0)
2020-05-04 23:39:57 +02:00
return;
if (this.currentlyBreaking.isEmpty())
return;
2021-12-15 16:30:22 +01:00
var pos = this.currentlyBreaking.removeLast();
var state = this.level.getBlockState(pos);
2020-05-04 23:39:57 +02:00
if (state.getBlock() != Blocks.CHORUS_PLANT && state.getBlock() != Blocks.CHORUS_FLOWER) {
this.currentlyBreaking.clear();
return;
}
2021-12-04 15:40:09 +01:00
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32, new PacketParticles(this.worldPosition.getX(), this.worldPosition.getY(), this.worldPosition.getZ(), PacketParticles.Type.CHORUS_GENERATOR, pos.getX(), pos.getY(), pos.getZ()));
this.level.removeBlock(pos, false);
this.level.playSound(null, this.worldPosition.getX() + 0.5, this.worldPosition.getY() + 0.5, this.worldPosition.getZ() + 0.5,
2021-12-06 14:38:12 +01:00
SoundEvents.CHORUS_FRUIT_TELEPORT, SoundSource.BLOCKS, 0.5F, 1F);
2021-03-30 15:44:31 +02:00
this.generateAura(this.auraPerBlock);
2020-05-04 23:39:57 +02:00
}
@Override
public void onRedstonePowerChange(int newPower) {
if (this.redstonePower <= 0 && newPower > 0 && this.currentlyBreaking.isEmpty()) {
2021-12-15 16:30:22 +01:00
var range = 2;
2020-05-04 23:39:57 +02:00
xyz:
2021-12-15 16:30:22 +01:00
for (var x = -range; x <= range; x++) {
for (var y = -range; y <= range; y++) {
for (var z = -range; z <= range; z++) {
var offset = this.worldPosition.offset(x, y, z);
var below = this.level.getBlockState(offset.below());
2020-05-04 23:39:57 +02:00
if (below.getBlock() != Blocks.END_STONE)
continue;
2021-12-15 16:30:22 +01:00
var state = this.level.getBlockState(offset);
2020-05-04 23:39:57 +02:00
if (state.getBlock() != Blocks.CHORUS_PLANT)
continue;
List<BlockPos> plants = new ArrayList<>();
this.collectChorusPlant(offset, plants);
if (plants.size() <= 1)
continue;
this.currentlyBreaking.addAll(plants);
this.currentlyBreaking.addFirst(offset);
2021-12-15 16:30:22 +01:00
var aura = plants.size() * plants.size() * 300;
2020-05-04 23:39:57 +02:00
this.auraPerBlock = aura / plants.size();
break xyz;
}
}
}
}
super.onRedstonePowerChange(newPower);
}
private void collectChorusPlant(BlockPos pos, List<BlockPos> blocks) {
2021-12-15 16:30:22 +01:00
for (var dir : Direction.values()) {
2020-05-04 23:39:57 +02:00
if (dir == Direction.DOWN)
continue;
2021-12-15 16:30:22 +01:00
var offset = pos.relative(dir);
2020-05-04 23:39:57 +02:00
if (blocks.contains(offset))
continue;
2021-12-15 16:30:22 +01:00
var state = this.level.getBlockState(offset);
2020-05-04 23:39:57 +02:00
if (state.getBlock() != Blocks.CHORUS_PLANT && state.getBlock() != Blocks.CHORUS_FLOWER)
continue;
blocks.add(offset);
this.collectChorusPlant(offset, blocks);
}
}
2020-05-05 00:05:38 +02:00
@Override
2021-12-04 15:40:09 +01:00
public void writeNBT(CompoundTag compound, SaveType type) {
2020-05-05 00:05:38 +02:00
super.writeNBT(compound, type);
if (type == SaveType.TILE) {
2021-12-15 16:30:22 +01:00
var list = new ListTag();
for (var pos : this.currentlyBreaking)
2021-12-06 14:38:12 +01:00
list.add(NbtUtils.writeBlockPos(pos));
2020-05-05 00:05:38 +02:00
compound.put("breaking", list);
compound.putInt("aura", this.auraPerBlock);
}
}
@Override
2021-12-04 15:40:09 +01:00
public void readNBT(CompoundTag compound, SaveType type) {
2020-05-05 00:05:38 +02:00
super.readNBT(compound, type);
if (type == SaveType.TILE) {
this.currentlyBreaking.clear();
2021-12-15 16:30:22 +01:00
var list = compound.getList("breaking", 10);
for (var i = 0; i < list.size(); i++)
2021-12-06 14:38:12 +01:00
this.currentlyBreaking.add(NbtUtils.readBlockPos(list.getCompound(i)));
2020-05-05 00:05:38 +02:00
this.auraPerBlock = compound.getInt("aura");
}
}
2020-05-04 23:39:57 +02:00
}