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;
|
2018-11-11 13:26:19 +01:00
|
|
|
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
|
2019-02-16 21:31:37 +01:00
|
|
|
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
|
2018-11-04 16:38:09 +01:00
|
|
|
import de.ellpeck.naturesaura.packet.PacketHandler;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketParticleStream;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketParticles;
|
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.nbt.NBTBase;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
|
|
|
import net.minecraft.util.ITickable;
|
|
|
|
import net.minecraft.util.ResourceLocation;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraftforge.fml.common.registry.ForgeRegistries;
|
|
|
|
import org.apache.commons.lang3.mutable.MutableInt;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
public class TileEntityFlowerGenerator extends TileEntityImpl implements ITickable {
|
|
|
|
|
|
|
|
private final Map<IBlockState, MutableInt> consumedRecently = new HashMap<>();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void update() {
|
|
|
|
if (!this.world.isRemote && this.world.getTotalWorldTime() % 10 == 0) {
|
|
|
|
List<BlockPos> possible = new ArrayList<>();
|
|
|
|
int range = 3;
|
|
|
|
for (int x = -range; x <= range; x++) {
|
2018-12-16 18:27:56 +01:00
|
|
|
for (int y = -1; y <= 1; y++) {
|
|
|
|
for (int z = -range; z <= range; z++) {
|
|
|
|
BlockPos offset = this.pos.add(x, y, z);
|
|
|
|
IBlockState state = this.world.getBlockState(offset);
|
|
|
|
if (NaturesAuraAPI.FLOWERS.contains(state)) {
|
|
|
|
possible.add(offset);
|
|
|
|
}
|
2018-11-04 16:38:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (possible.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
BlockPos pos = possible.get(this.world.rand.nextInt(possible.size()));
|
|
|
|
IBlockState state = this.world.getBlockState(pos);
|
|
|
|
MutableInt curr = this.consumedRecently.computeIfAbsent(state, s -> new MutableInt());
|
|
|
|
|
2019-01-29 11:46:38 +01:00
|
|
|
int addAmount = 25000;
|
|
|
|
int toAdd = Math.max(0, addAmount - curr.getValue() * 100);
|
2018-11-04 16:38:09 +01:00
|
|
|
if (toAdd > 0) {
|
2019-02-16 21:31:37 +01:00
|
|
|
if (IAuraType.forWorld(this.world).isSimilar(NaturesAuraAPI.TYPE_OVERWORLD) && this.canGenerateRightNow(30, toAdd)) {
|
2018-11-24 15:31:53 +01:00
|
|
|
int remain = toAdd;
|
|
|
|
while (remain > 0) {
|
|
|
|
BlockPos spot = IAuraChunk.getLowestSpot(this.world, this.pos, 30, this.pos);
|
|
|
|
remain -= IAuraChunk.getAuraChunk(this.world, spot).storeAura(spot, remain);
|
|
|
|
}
|
|
|
|
} else
|
2018-11-04 17:13:53 +01:00
|
|
|
toAdd = 0;
|
2018-11-04 16:38:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (Map.Entry<IBlockState, MutableInt> entry : this.consumedRecently.entrySet()) {
|
|
|
|
if (entry.getKey() != state) {
|
|
|
|
MutableInt val = entry.getValue();
|
|
|
|
if (val.getValue() > 0)
|
|
|
|
val.subtract(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curr.add(5);
|
|
|
|
|
|
|
|
this.world.setBlockToAir(pos);
|
|
|
|
|
2018-11-04 17:13:53 +01:00
|
|
|
int color = Helper.blendColors(0x5ccc30, 0xe53c16, toAdd / (float) addAmount);
|
2018-11-04 16:38:09 +01:00
|
|
|
if (toAdd > 0) {
|
|
|
|
for (int i = this.world.rand.nextInt(5) + 5; i >= 0; i--)
|
|
|
|
PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticleStream(
|
|
|
|
pos.getX() + 0.25F + this.world.rand.nextFloat() * 0.5F,
|
|
|
|
pos.getY() + 0.25F + this.world.rand.nextFloat() * 0.5F,
|
|
|
|
pos.getZ() + 0.25F + this.world.rand.nextFloat() * 0.5F,
|
|
|
|
this.pos.getX() + 0.25F + this.world.rand.nextFloat() * 0.5F,
|
|
|
|
this.pos.getY() + 0.25F + this.world.rand.nextFloat() * 0.5F,
|
|
|
|
this.pos.getZ() + 0.25F + this.world.rand.nextFloat() * 0.5F,
|
2018-11-04 18:42:35 +01:00
|
|
|
this.world.rand.nextFloat() * 0.02F + 0.1F, color, 1F
|
2018-11-04 16:38:09 +01:00
|
|
|
));
|
|
|
|
PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticles(this.pos.getX(), this.pos.getY(), this.pos.getZ(), 8));
|
|
|
|
}
|
|
|
|
PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticles(pos.getX(), pos.getY(), pos.getZ(), 7, color));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 13:36:05 +01:00
|
|
|
@Override
|
|
|
|
public boolean wantsLimitRemover() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-04 16:38:09 +01:00
|
|
|
@Override
|
|
|
|
public void writeNBT(NBTTagCompound compound, SaveType type) {
|
|
|
|
super.writeNBT(compound, type);
|
|
|
|
|
|
|
|
if (type != SaveType.SYNC && !this.consumedRecently.isEmpty()) {
|
|
|
|
NBTTagList list = new NBTTagList();
|
|
|
|
for (Map.Entry<IBlockState, MutableInt> entry : this.consumedRecently.entrySet()) {
|
|
|
|
IBlockState state = entry.getKey();
|
|
|
|
Block block = state.getBlock();
|
|
|
|
|
|
|
|
NBTTagCompound tag = new NBTTagCompound();
|
|
|
|
tag.setString("block", block.getRegistryName().toString());
|
|
|
|
tag.setInteger("meta", block.getMetaFromState(state));
|
|
|
|
tag.setInteger("amount", entry.getValue().intValue());
|
|
|
|
list.appendTag(tag);
|
|
|
|
}
|
|
|
|
compound.setTag("consumed_recently", list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readNBT(NBTTagCompound compound, SaveType type) {
|
|
|
|
super.readNBT(compound, type);
|
|
|
|
|
|
|
|
if (type != SaveType.SYNC) {
|
|
|
|
this.consumedRecently.clear();
|
|
|
|
|
|
|
|
NBTTagList list = compound.getTagList("consumed_recently", 10);
|
|
|
|
for (NBTBase base : list) {
|
|
|
|
NBTTagCompound tag = (NBTTagCompound) base;
|
|
|
|
|
|
|
|
Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(tag.getString("block")));
|
|
|
|
if (block != null) {
|
|
|
|
IBlockState state = block.getStateFromMeta(tag.getInteger("meta"));
|
|
|
|
this.consumedRecently.put(state, new MutableInt(tag.getInteger("amount")));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|