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

125 lines
4.8 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.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.aura.container.BasicAuraContainer;
import de.ellpeck.naturesaura.api.aura.container.IAuraContainer;
2020-01-22 23:21:52 +01:00
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticles;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.Blocks;
2020-01-21 21:04:44 +01:00
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.item.ItemStack;
2020-01-21 21:04:44 +01:00
import net.minecraft.item.Items;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
import net.minecraft.tileentity.ITickableBlockEntity;
2020-01-21 21:04:44 +01:00
import net.minecraft.util.EntityPredicates;
import net.minecraft.util.math.AxisAlignedBB;
2018-12-22 14:57:19 +01:00
import net.minecraft.util.math.BlockPos;
import java.util.List;
2021-12-04 15:40:09 +01:00
public class BlockEntityEndFlower extends BlockEntityImpl implements ITickableBlockEntity {
2018-12-22 14:57:19 +01:00
private 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) {
int amount = super.drainAura(amountToDrain, simulate);
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-04 15:40:09 +01:00
public BlockEntityEndFlower() {
2020-01-22 01:32:26 +01:00
super(ModTileEntities.END_FLOWER);
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-04 15:40:09 +01:00
List<ItemEntity> items = this.level.getEntitiesWithinAABB(ItemEntity.class,
new AxisAlignedBB(this.worldPosition).grow(1), EntityPredicates.IS_ALIVE);
2019-10-20 22:30:49 +02:00
for (ItemEntity item : items) {
if (item.cannotPickup())
continue;
ItemStack 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;
2020-01-21 21:04:44 +01:00
item.remove();
2021-12-04 15:40:09 +01:00
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32,
2020-01-28 18:08:56 +01:00
new PacketParticles((float) item.getPosX(), (float) item.getPosY(), (float) item.getPosZ(), PacketParticles.Type.END_FLOWER_CONSUME, this.container.getAuraColor()));
break;
}
2018-12-22 14:57:19 +01:00
} else {
int 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-04 15:40:09 +01:00
this.level.setBlockState(this.worldPosition, Blocks.DEAD_BUSH.getDefaultState());
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-04 15:40:09 +01:00
this.worldPosition.getX() + 0.25F + this.level.rand.nextFloat() * 0.5F,
this.worldPosition.getY() + 0.25F + this.level.rand.nextFloat() * 0.5F,
this.worldPosition.getZ() + 0.25F + this.level.rand.nextFloat() * 0.5F,
this.level.rand.nextGaussian() * 0.05F,
this.level.rand.nextFloat() * 0.1F,
this.level.rand.nextGaussian() * 0.05F,
this.container.getAuraColor(), this.level.rand.nextFloat() * 2F + 1F, 50, 0F, false, true);
2018-12-22 14:57:19 +01:00
}
}
@Override
2020-10-19 21:26:32 +02:00
public IAuraContainer getAuraContainer() {
2018-12-22 14:57:19 +01:00
return this.container;
}
@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");
}
}
}