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

140 lines
5.7 KiB
Java
Raw Normal View History

2019-02-24 17:44:53 +01:00
package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
2019-10-20 22:30:49 +02:00
import net.minecraft.entity.item.FireworkRocketEntity;
import net.minecraft.entity.item.ItemEntity;
2019-02-24 17:44:53 +01:00
import net.minecraft.item.ItemStack;
2020-01-21 21:04:44 +01:00
import net.minecraft.item.Items;
2019-10-20 22:30:49 +02:00
import net.minecraft.nbt.CompoundNBT;
2020-01-21 21:04:44 +01:00
import net.minecraft.nbt.INBT;
2019-10-20 22:30:49 +02:00
import net.minecraft.nbt.ListNBT;
2020-01-21 21:04:44 +01:00
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntityType;
2019-10-20 22:30:49 +02:00
import net.minecraft.util.EntityPredicates;
2019-02-24 17:44:53 +01:00
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
2020-01-21 21:04:44 +01:00
public class TileEntityFireworkGenerator extends TileEntityImpl implements ITickableTileEntity {
2019-02-24 17:44:53 +01:00
2019-10-20 22:30:49 +02:00
private FireworkRocketEntity trackedEntity;
2019-02-24 17:44:53 +01:00
private ItemStack trackedItem;
private int toRelease;
private int releaseTimer;
2020-01-21 21:04:44 +01:00
public TileEntityFireworkGenerator(TileEntityType<?> tileEntityTypeIn) {
super(tileEntityTypeIn);
}
2019-02-24 17:44:53 +01:00
@Override
2020-01-21 21:04:44 +01:00
public void tick() {
2019-02-24 17:44:53 +01:00
if (!this.world.isRemote) {
2020-01-21 21:04:44 +01:00
if (this.world.getGameTime() % 10 == 0) {
2019-10-20 22:30:49 +02:00
List<ItemEntity> items = this.world.getEntitiesWithinAABB(ItemEntity.class,
new AxisAlignedBB(this.pos).grow(4), EntityPredicates.IS_ALIVE);
for (ItemEntity item : items) {
2019-02-24 17:44:53 +01:00
if (item.cannotPickup())
continue;
ItemStack stack = item.getItem();
2020-01-21 21:04:44 +01:00
if (stack.isEmpty() || stack.getItem() != Items.FIREWORK_ROCKET)
2019-02-24 17:44:53 +01:00
continue;
if (this.trackedEntity == null && this.releaseTimer <= 0) {
2019-10-20 22:30:49 +02:00
FireworkRocketEntity entity = new FireworkRocketEntity(this.world, item.posX, item.posY, item.posZ, stack);
2019-02-24 17:44:53 +01:00
this.trackedEntity = entity;
this.trackedItem = stack.copy();
2020-01-21 21:04:44 +01:00
this.world.addEntity(entity);
2019-02-24 17:44:53 +01:00
}
stack.shrink(1);
if (stack.isEmpty())
2020-01-21 21:04:44 +01:00
item.remove();
2019-02-24 17:44:53 +01:00
else
item.setItem(stack);
}
}
2020-01-21 21:04:44 +01:00
if (this.trackedEntity != null && !this.trackedEntity.isAlive()) {
if (this.trackedItem.hasTag()) {
2019-02-25 12:28:41 +01:00
float generateFactor = 0;
Set<Integer> usedColors = new HashSet<>();
2019-02-24 17:44:53 +01:00
2020-01-21 21:04:44 +01:00
CompoundNBT compound = this.trackedItem.getTag();
CompoundNBT fireworks = compound.getCompound("Fireworks");
2019-02-24 17:44:53 +01:00
2020-01-21 21:04:44 +01:00
int flightTime = fireworks.getInt("Flight");
ListNBT explosions = fireworks.getList("Explosions", 10);
2019-02-25 12:28:41 +01:00
if (!explosions.isEmpty()) {
generateFactor += flightTime;
2020-01-21 21:04:44 +01:00
for (INBT base : explosions) {
2019-10-20 22:30:49 +02:00
CompoundNBT explosion = (CompoundNBT) base;
2019-02-25 12:28:41 +01:00
generateFactor += 1.5F;
boolean flicker = explosion.getBoolean("Flicker");
if (flicker)
generateFactor += 1;
boolean trail = explosion.getBoolean("Trail");
if (trail)
generateFactor += 8;
byte type = explosion.getByte("Type");
generateFactor += new float[]{0, 1, 0.5F, 20, 0.5F}[type];
Set<Integer> colors = new HashSet<>();
for (int color : explosion.getIntArray("Colors")) {
usedColors.add(color);
colors.add(color);
}
generateFactor += 0.75F * colors.size();
2019-02-24 17:44:53 +01:00
}
}
2019-02-25 12:28:41 +01:00
if (generateFactor > 0) {
int toAdd = MathHelper.ceil(generateFactor * 10000F);
if (this.canGenerateRightNow(35, toAdd)) {
this.toRelease = toAdd;
this.releaseTimer = 15 * flightTime + 40;
}
2019-02-24 17:44:53 +01:00
2019-02-25 12:28:41 +01:00
List<Integer> data = new ArrayList<>();
data.add(this.pos.getX());
data.add(this.pos.getY());
data.add(this.pos.getZ());
data.addAll(usedColors);
2020-01-21 21:04:44 +01:00
// TODO particles
/* PacketHandler.sendToAllLoaded(this.world, this.pos, new PacketParticles(
2019-02-25 12:28:41 +01:00
(float) this.trackedEntity.posX, (float) this.trackedEntity.posY, (float) this.trackedEntity.posZ,
2020-01-21 21:04:44 +01:00
24, Ints.toArray(data)));*/
2019-02-25 12:28:41 +01:00
}
}
2019-02-24 17:44:53 +01:00
this.trackedEntity = null;
this.trackedItem = null;
}
if (this.releaseTimer > 0) {
this.releaseTimer--;
if (this.releaseTimer <= 0) {
while (this.toRelease > 0) {
BlockPos spot = IAuraChunk.getLowestSpot(this.world, this.pos, 35, this.pos);
this.toRelease -= IAuraChunk.getAuraChunk(this.world, spot).storeAura(spot, this.toRelease);
}
2020-01-21 21:04:44 +01:00
/* PacketHandler.sendToAllLoaded(this.world, this.pos,
new PacketParticles(this.pos.getX(), this.pos.getY(), this.pos.getZ(), 8));*/
2019-02-24 17:44:53 +01:00
}
}
}
}
@Override
public boolean wantsLimitRemover() {
return true;
}
2019-02-24 17:44:53 +01:00
}