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

115 lines
4.5 KiB
Java
Raw Normal View History

2018-12-22 14:57:19 +01:00
package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.container.BasicAuraContainer;
2020-01-22 23:21:52 +01:00
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticles;
2021-12-08 00:31:29 +01:00
import net.minecraft.core.BlockPos;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2021-12-08 00:31:29 +01:00
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
2018-12-22 14:57:19 +01:00
2021-12-04 15:40:09 +01:00
public class BlockEntityEndFlower extends BlockEntityImpl implements ITickableBlockEntity {
2018-12-22 14:57:19 +01:00
2024-03-10 15:54:58 +01:00
public final BasicAuraContainer container = new BasicAuraContainer(null, 500000) {
2018-12-22 14:57:19 +01:00
{
this.aura = this.maxAura;
}
@Override
public int storeAura(int amountToStore, boolean simulate) {
return 0;
}
@Override
public int drainAura(int amountToDrain, boolean simulate) {
2021-12-15 16:30:22 +01:00
var amount = super.drainAura(amountToDrain, simulate);
2018-12-22 14:57:19 +01:00
if (amount > 0 && !simulate)
2021-12-04 15:40:09 +01:00
BlockEntityEndFlower.this.sendToClients();
2018-12-22 14:57:19 +01:00
return amount;
}
@Override
public int getAuraColor() {
return 0x6a25dd;
}
};
public boolean isDrainMode;
2021-12-08 00:31:29 +01:00
public BlockEntityEndFlower(BlockPos pos, BlockState state) {
2021-12-19 15:32:45 +01:00
super(ModBlockEntities.END_FLOWER, pos, state);
2020-01-21 21:04:44 +01:00
}
2018-12-22 14:57:19 +01:00
@Override
2020-01-21 21:04:44 +01:00
public void tick() {
2021-12-04 15:40:09 +01:00
if (!this.level.isClientSide) {
if (this.level.getGameTime() % 10 != 0)
2018-12-22 14:57:19 +01:00
return;
if (!this.isDrainMode) {
2021-12-15 16:30:22 +01:00
var items = this.level.getEntitiesOfClass(ItemEntity.class, new AABB(this.worldPosition).inflate(1), Entity::isAlive);
for (var item : items) {
2021-12-08 00:31:29 +01:00
if (item.hasPickUpDelay())
continue;
2021-12-15 16:30:22 +01:00
var stack = item.getItem();
if (stack.getCount() != 1)
continue;
if (stack.getItem() != Items.ENDER_EYE)
continue;
2018-12-22 14:57:19 +01:00
this.isDrainMode = true;
2021-12-08 00:31:29 +01:00
item.kill();
2021-12-04 15:40:09 +01:00
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32,
2021-12-08 00:31:29 +01:00
new PacketParticles((float) item.getX(), (float) item.getY(), (float) item.getZ(), PacketParticles.Type.END_FLOWER_CONSUME, this.container.getAuraColor()));
break;
}
2018-12-22 14:57:19 +01:00
} else {
2021-12-15 16:30:22 +01:00
var toDrain = Math.min(5000, this.container.getStoredAura());
2018-12-22 14:57:19 +01:00
this.container.drainAura(toDrain, false);
2021-03-30 15:44:31 +02:00
this.generateAura(toDrain);
2018-12-22 14:57:19 +01:00
if (this.container.getStoredAura() <= 0) {
2021-12-08 00:31:29 +01:00
this.level.setBlockAndUpdate(this.worldPosition, Blocks.DEAD_BUSH.defaultBlockState());
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.END_FLOWER_DECAY, this.container.getAuraColor()));
2018-12-22 14:57:19 +01:00
}
}
} else {
2021-12-04 15:40:09 +01:00
if (this.isDrainMode && this.level.getGameTime() % 5 == 0)
2018-12-22 14:57:19 +01:00
NaturesAuraAPI.instance().spawnMagicParticle(
2021-12-08 00:31:29 +01:00
this.worldPosition.getX() + 0.25F + this.level.random.nextFloat() * 0.5F,
this.worldPosition.getY() + 0.25F + this.level.random.nextFloat() * 0.5F,
this.worldPosition.getZ() + 0.25F + this.level.random.nextFloat() * 0.5F,
this.level.random.nextGaussian() * 0.05F,
this.level.random.nextFloat() * 0.1F,
this.level.random.nextGaussian() * 0.05F,
this.container.getAuraColor(), this.level.random.nextFloat() * 2F + 1F, 50, 0F, false, true);
2018-12-22 14:57:19 +01:00
}
}
@Override
2021-12-04 15:40:09 +01:00
public void writeNBT(CompoundTag compound, SaveType type) {
2018-12-22 14:57:19 +01:00
super.writeNBT(compound, type);
if (type != SaveType.BLOCK) {
this.container.writeNBT(compound);
2020-01-21 21:04:44 +01:00
compound.putBoolean("drain_mode", this.isDrainMode);
2018-12-22 14:57:19 +01:00
}
}
@Override
2021-12-04 15:40:09 +01:00
public void readNBT(CompoundTag compound, SaveType type) {
2018-12-22 14:57:19 +01:00
super.readNBT(compound, type);
if (type != SaveType.BLOCK) {
this.container.readNBT(compound);
this.isDrainMode = compound.getBoolean("drain_mode");
}
}
2024-03-10 15:54:58 +01:00
2018-12-22 14:57:19 +01:00
}