2019-02-24 17:44:53 +01:00
|
|
|
package de.ellpeck.naturesaura.blocks.tiles;
|
|
|
|
|
2020-01-22 23:21:52 +01:00
|
|
|
import com.google.common.primitives.Ints;
|
|
|
|
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.util.Mth;
|
|
|
|
import net.minecraft.world.entity.Entity;
|
|
|
|
import net.minecraft.world.entity.item.ItemEntity;
|
|
|
|
import net.minecraft.world.entity.projectile.FireworkRocketEntity;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.item.Items;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
|
|
import net.minecraft.world.phys.AABB;
|
2019-02-24 17:44:53 +01:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
public class BlockEntityFireworkGenerator extends BlockEntityImpl implements ITickableBlockEntity {
|
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;
|
|
|
|
|
2021-12-08 00:31:29 +01:00
|
|
|
public BlockEntityFireworkGenerator(BlockPos pos, BlockState state) {
|
2021-12-19 15:32:45 +01:00
|
|
|
super(ModBlockEntities.FIREWORK_GENERATOR, pos, state);
|
2020-01-21 21:04:44 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 17:44:53 +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) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var items = this.level.getEntitiesOfClass(ItemEntity.class, new AABB(this.worldPosition).inflate(4), Entity::isAlive);
|
|
|
|
for (var item : items) {
|
2021-12-08 00:31:29 +01:00
|
|
|
if (item.hasPickUpDelay())
|
2019-02-24 17:44:53 +01:00
|
|
|
continue;
|
2021-12-15 16:30:22 +01:00
|
|
|
var 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) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var entity = new FireworkRocketEntity(this.level, item.getX(), item.getY(), item.getZ(), stack);
|
2019-02-24 17:44:53 +01:00
|
|
|
this.trackedEntity = entity;
|
2019-02-25 13:36:05 +01:00
|
|
|
this.trackedItem = stack.copy();
|
2021-12-08 00:31:29 +01:00
|
|
|
this.level.addFreshEntity(entity);
|
2019-02-24 17:44:53 +01:00
|
|
|
}
|
|
|
|
stack.shrink(1);
|
2021-12-08 00:31:29 +01:00
|
|
|
if (stack.isEmpty()) {
|
|
|
|
item.kill();
|
|
|
|
} else {
|
2019-02-24 17:44:53 +01:00
|
|
|
item.setItem(stack);
|
2021-12-08 00:31:29 +01:00
|
|
|
}
|
2019-02-24 17:44:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var compound = this.trackedItem.getTag();
|
|
|
|
var fireworks = compound.getCompound("Fireworks");
|
2019-02-24 17:44:53 +01:00
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var flightTime = fireworks.getInt("Flight");
|
|
|
|
var explosions = fireworks.getList("Explosions", 10);
|
2019-02-25 12:28:41 +01:00
|
|
|
if (!explosions.isEmpty()) {
|
|
|
|
generateFactor += flightTime;
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
for (var base : explosions) {
|
|
|
|
var explosion = (CompoundTag) base;
|
2019-02-25 12:28:41 +01:00
|
|
|
generateFactor += 1.5F;
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var flicker = explosion.getBoolean("Flicker");
|
2019-02-25 12:28:41 +01:00
|
|
|
if (flicker)
|
|
|
|
generateFactor += 1;
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var trail = explosion.getBoolean("Trail");
|
2019-02-25 12:28:41 +01:00
|
|
|
if (trail)
|
|
|
|
generateFactor += 8;
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var type = explosion.getByte("Type");
|
2019-02-25 12:28:41 +01:00
|
|
|
generateFactor += new float[]{0, 1, 0.5F, 20, 0.5F}[type];
|
|
|
|
|
|
|
|
Set<Integer> colors = new HashSet<>();
|
2021-12-15 16:30:22 +01:00
|
|
|
for (var color : explosion.getIntArray("Colors")) {
|
2019-02-25 12:28:41 +01:00
|
|
|
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) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var toAdd = Mth.ceil(generateFactor * 10000F);
|
2021-03-30 15:44:31 +02:00
|
|
|
if (this.canGenerateRightNow(toAdd)) {
|
2019-02-25 12:28:41 +01:00
|
|
|
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<>();
|
2021-12-04 15:40:09 +01:00
|
|
|
data.add(this.worldPosition.getX());
|
|
|
|
data.add(this.worldPosition.getY());
|
|
|
|
data.add(this.worldPosition.getZ());
|
2019-02-25 12:28:41 +01:00
|
|
|
data.addAll(usedColors);
|
2021-12-04 15:40:09 +01:00
|
|
|
PacketHandler.sendToAllLoaded(this.level, this.worldPosition, new PacketParticles(
|
2021-12-08 00:31:29 +01:00
|
|
|
(float) this.trackedEntity.getX(), (float) this.trackedEntity.getY(), (float) this.trackedEntity.getZ(),
|
2020-01-26 00:16:06 +01:00
|
|
|
PacketParticles.Type.FIREWORK_GEN, 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) {
|
2021-03-30 15:44:31 +02:00
|
|
|
this.generateAura(this.toRelease);
|
|
|
|
this.toRelease = 0;
|
2019-02-24 17:44:53 +01:00
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
PacketHandler.sendToAllLoaded(this.level, this.worldPosition,
|
|
|
|
new PacketParticles(this.worldPosition.getX(), this.worldPosition.getY(), this.worldPosition.getZ(), PacketParticles.Type.FLOWER_GEN_AURA_CREATION));
|
2019-02-24 17:44:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-25 13:36:05 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean wantsLimitRemover() {
|
|
|
|
return true;
|
|
|
|
}
|
2019-02-24 17:44:53 +01:00
|
|
|
}
|