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

137 lines
5.8 KiB
Java
Raw Normal View History

2018-11-04 16:38:09 +01:00
package de.ellpeck.naturesaura.blocks.tiles;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
2018-11-11 13:26:19 +01:00
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
2020-01-22 23:21:52 +01:00
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticleStream;
import de.ellpeck.naturesaura.packet.PacketParticles;
2018-11-04 16:38:09 +01:00
import net.minecraft.block.Block;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.BlockState;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
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-04-29 16:38:50 +02:00
import net.minecraft.tags.BlockTags;
2021-12-04 15:40:09 +01:00
import net.minecraft.tileentity.ITickableBlockEntity;
2018-11-04 16:38:09 +01:00
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
2020-01-21 21:04:44 +01:00
import net.minecraftforge.registries.ForgeRegistries;
2018-11-04 16:38:09 +01:00
import org.apache.commons.lang3.mutable.MutableInt;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
2021-12-04 15:40:09 +01:00
public class BlockEntityFlowerGenerator extends BlockEntityImpl implements ITickableBlockEntity {
2018-11-04 16:38:09 +01:00
2019-10-20 22:30:49 +02:00
private final Map<BlockState, MutableInt> consumedRecently = new HashMap<>();
2018-11-04 16:38:09 +01:00
2021-12-04 15:40:09 +01:00
public BlockEntityFlowerGenerator() {
2020-01-22 01:32:26 +01:00
super(ModTileEntities.FLOWER_GENERATOR);
2020-01-21 21:04:44 +01:00
}
2018-11-04 16:38:09 +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 && this.level.getGameTime() % 10 == 0) {
2018-11-04 16:38:09 +01:00
List<BlockPos> possible = new ArrayList<>();
int range = 3;
for (int x = -range; x <= range; x++) {
for (int y = -1; y <= 1; y++) {
for (int z = -range; z <= range; z++) {
2021-12-04 15:40:09 +01:00
BlockPos offset = this.worldPosition.add(x, y, z);
BlockState state = this.level.getBlockState(offset);
2020-04-29 16:38:50 +02:00
if (BlockTags.SMALL_FLOWERS.contains(state.getBlock()))
possible.add(offset);
2018-11-04 16:38:09 +01:00
}
}
}
if (possible.isEmpty())
return;
2021-12-04 15:40:09 +01:00
BlockPos pos = possible.get(this.level.rand.nextInt(possible.size()));
BlockState state = this.level.getBlockState(pos);
2018-11-04 16:38:09 +01:00
MutableInt curr = this.consumedRecently.computeIfAbsent(state, s -> new MutableInt());
int addAmount = 25000;
int toAdd = Math.max(0, addAmount - curr.getValue() * 100);
2018-11-04 16:38:09 +01:00
if (toAdd > 0) {
2021-12-04 15:40:09 +01:00
if (IAuraType.forLevel(this.level).isSimilar(NaturesAuraAPI.TYPE_OVERWORLD) && this.canGenerateRightNow(toAdd)) {
2021-03-30 15:44:31 +02:00
this.generateAura(toAdd);
} else {
toAdd = 0;
2021-03-30 15:44:31 +02:00
}
2018-11-04 16:38:09 +01:00
}
2019-10-20 22:30:49 +02:00
for (Map.Entry<BlockState, MutableInt> entry : this.consumedRecently.entrySet()) {
2018-11-04 16:38:09 +01:00
if (entry.getKey() != state) {
MutableInt val = entry.getValue();
if (val.getValue() > 0)
val.subtract(1);
}
}
curr.add(5);
2021-12-04 15:40:09 +01:00
this.level.removeBlock(pos, false);
2018-11-04 16:38:09 +01:00
int color = Helper.blendColors(0x5ccc30, 0xe53c16, toAdd / (float) addAmount);
2018-11-04 16:38:09 +01:00
if (toAdd > 0) {
2021-12-04 15:40:09 +01:00
for (int i = this.level.rand.nextInt(5) + 5; i >= 0; i--)
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32, new PacketParticleStream(
pos.getX() + 0.25F + this.level.rand.nextFloat() * 0.5F,
pos.getY() + 0.25F + this.level.rand.nextFloat() * 0.5F,
pos.getZ() + 0.25F + this.level.rand.nextFloat() * 0.5F,
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.nextFloat() * 0.02F + 0.1F, color, 1F
2018-11-04 16:38:09 +01:00
));
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.FLOWER_GEN_AURA_CREATION));
2018-11-04 16:38:09 +01:00
}
2021-12-04 15:40:09 +01:00
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32, new PacketParticles(pos.getX(), pos.getY(), pos.getZ(), PacketParticles.Type.FLOWER_GEN_CONSUME, color));
2018-11-04 16:38:09 +01:00
}
}
@Override
public boolean wantsLimitRemover() {
return true;
}
2018-11-04 16:38:09 +01:00
@Override
2021-12-04 15:40:09 +01:00
public void writeNBT(CompoundTag compound, SaveType type) {
2018-11-04 16:38:09 +01:00
super.writeNBT(compound, type);
if (type != SaveType.SYNC && !this.consumedRecently.isEmpty()) {
2019-10-20 22:30:49 +02:00
ListNBT list = new ListNBT();
for (Map.Entry<BlockState, MutableInt> entry : this.consumedRecently.entrySet()) {
BlockState state = entry.getKey();
2018-11-04 16:38:09 +01:00
Block block = state.getBlock();
2021-12-04 15:40:09 +01:00
CompoundTag tag = new CompoundTag();
2020-01-21 21:04:44 +01:00
tag.putString("block", block.getRegistryName().toString());
tag.putInt("amount", entry.getValue().intValue());
list.add(tag);
2018-11-04 16:38:09 +01:00
}
2020-01-21 21:04:44 +01:00
compound.put("consumed_recently", list);
2018-11-04 16:38:09 +01:00
}
}
@Override
2021-12-04 15:40:09 +01:00
public void readNBT(CompoundTag compound, SaveType type) {
2018-11-04 16:38:09 +01:00
super.readNBT(compound, type);
if (type != SaveType.SYNC) {
this.consumedRecently.clear();
2020-01-21 21:04:44 +01:00
ListNBT list = compound.getList("consumed_recently", 10);
for (INBT base : list) {
2021-12-04 15:40:09 +01:00
CompoundTag tag = (CompoundTag) base;
2018-11-04 16:38:09 +01:00
Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(tag.getString("block")));
2020-01-21 21:04:44 +01:00
if (block != null)
this.consumedRecently.put(block.getDefaultState(), new MutableInt(tag.getInt("amount")));
2018-11-04 16:38:09 +01:00
}
}
}
}