2018-11-04 16:38:09 +01:00
|
|
|
package de.ellpeck.naturesaura.blocks.tiles;
|
|
|
|
|
|
|
|
import de.ellpeck.naturesaura.Helper;
|
2018-11-12 00:32:35 +01:00
|
|
|
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
|
2019-02-16 21:31:37 +01:00
|
|
|
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;
|
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.nbt.ListTag;
|
|
|
|
import net.minecraft.resources.ResourceLocation;
|
2020-04-29 16:38:50 +02:00
|
|
|
import net.minecraft.tags.BlockTags;
|
2021-12-08 00:31:29 +01:00
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
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-08 00:31:29 +01:00
|
|
|
public BlockEntityFlowerGenerator(BlockPos pos, BlockState state) {
|
2021-12-19 15:32:45 +01:00
|
|
|
super(ModBlockEntities.FLOWER_GENERATOR, pos, state);
|
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<>();
|
2021-12-15 16:30:22 +01:00
|
|
|
var range = 3;
|
|
|
|
for (var x = -range; x <= range; x++) {
|
|
|
|
for (var y = -1; y <= 1; y++) {
|
|
|
|
for (var z = -range; z <= range; z++) {
|
|
|
|
var offset = this.worldPosition.offset(x, y, z);
|
|
|
|
var state = this.level.getBlockState(offset);
|
2020-04-29 16:38:50 +02:00
|
|
|
if (BlockTags.SMALL_FLOWERS.contains(state.getBlock()))
|
2018-12-16 18:27:56 +01:00
|
|
|
possible.add(offset);
|
2018-11-04 16:38:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (possible.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var pos = possible.get(this.level.random.nextInt(possible.size()));
|
|
|
|
var state = this.level.getBlockState(pos);
|
|
|
|
var curr = this.consumedRecently.computeIfAbsent(state, s -> new MutableInt());
|
2018-11-04 16:38:09 +01:00
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var addAmount = 25000;
|
|
|
|
var 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 {
|
2018-11-04 17:13:53 +01:00
|
|
|
toAdd = 0;
|
2021-03-30 15:44:31 +02:00
|
|
|
}
|
2018-11-04 16:38:09 +01:00
|
|
|
}
|
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
for (var entry : this.consumedRecently.entrySet()) {
|
2018-11-04 16:38:09 +01:00
|
|
|
if (entry.getKey() != state) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var val = entry.getValue();
|
2018-11-04 16:38:09 +01:00
|
|
|
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
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var color = Helper.blendColors(0x5ccc30, 0xe53c16, toAdd / (float) addAmount);
|
2018-11-04 16:38:09 +01:00
|
|
|
if (toAdd > 0) {
|
2021-12-15 16:30:22 +01:00
|
|
|
for (var i = this.level.random.nextInt(5) + 5; i >= 0; i--)
|
2021-12-04 15:40:09 +01:00
|
|
|
PacketHandler.sendToAllAround(this.level, this.worldPosition, 32, new PacketParticleStream(
|
2021-12-08 00:31:29 +01:00
|
|
|
pos.getX() + 0.25F + this.level.random.nextFloat() * 0.5F,
|
|
|
|
pos.getY() + 0.25F + this.level.random.nextFloat() * 0.5F,
|
|
|
|
pos.getZ() + 0.25F + this.level.random.nextFloat() * 0.5F,
|
|
|
|
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.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 13:36:05 +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()) {
|
2021-12-15 16:30:22 +01:00
|
|
|
var list = new ListTag();
|
|
|
|
for (var entry : this.consumedRecently.entrySet()) {
|
|
|
|
var state = entry.getKey();
|
|
|
|
var block = state.getBlock();
|
2018-11-04 16:38:09 +01:00
|
|
|
|
2021-12-15 16:30:22 +01:00
|
|
|
var 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();
|
2021-12-15 16:30:22 +01:00
|
|
|
var list = compound.getList("consumed_recently", 10);
|
|
|
|
for (var base : list) {
|
|
|
|
var tag = (CompoundTag) base;
|
|
|
|
var block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(tag.getString("block")));
|
2020-01-21 21:04:44 +01:00
|
|
|
if (block != null)
|
2021-12-08 00:31:29 +01:00
|
|
|
this.consumedRecently.put(block.defaultBlockState(), new MutableInt(tag.getInt("amount")));
|
2018-11-04 16:38:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|