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

250 lines
12 KiB
Java
Raw Normal View History

2018-10-14 14:27:18 +02:00
package de.ellpeck.naturesaura.blocks.tiles;
2018-11-11 13:26:19 +01:00
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.aura.container.BasicAuraContainer;
import de.ellpeck.naturesaura.api.aura.container.IAuraContainer;
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
2018-11-11 13:26:19 +01:00
import de.ellpeck.naturesaura.api.recipes.AltarRecipe;
2018-11-07 13:33:49 +01:00
import de.ellpeck.naturesaura.blocks.multi.Multiblocks;
2018-10-14 14:27:18 +02:00
import de.ellpeck.naturesaura.packet.PacketHandler;
2018-10-16 01:36:30 +02:00
import de.ellpeck.naturesaura.packet.PacketParticleStream;
2018-10-18 17:12:20 +02:00
import de.ellpeck.naturesaura.packet.PacketParticles;
2018-10-14 14:27:18 +02:00
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.SoundEvents;
2018-10-18 13:34:37 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.Ingredient;
2018-10-14 14:27:18 +02:00
import net.minecraft.nbt.NBTTagCompound;
2018-10-18 13:34:37 +02:00
import net.minecraft.util.EnumFacing;
2018-10-14 14:27:18 +02:00
import net.minecraft.util.ITickable;
2018-10-22 00:14:52 +02:00
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
2018-10-14 14:27:18 +02:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2018-10-18 13:34:37 +02:00
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemStackHandler;
2018-10-14 14:27:18 +02:00
import java.util.Random;
2018-10-20 21:19:08 +02:00
public class TileEntityNatureAltar extends TileEntityImpl implements ITickable {
2018-10-14 14:27:18 +02:00
2018-10-18 13:34:37 +02:00
public final ItemStackHandler items = new ItemStackHandlerNA(1, this, true) {
@Override
public int getSlotLimit(int slot) {
return 1;
}
@Override
protected boolean canInsert(ItemStack stack, int slot) {
return TileEntityNatureAltar.this.getRecipeForInput(stack) != null || stack.hasCapability(NaturesAuraAPI.capAuraContainer, null);
2018-10-18 13:34:37 +02:00
}
@Override
protected boolean canExtract(ItemStack stack, int slot, int amount) {
2018-11-12 22:04:40 +01:00
if (stack.hasCapability(NaturesAuraAPI.capAuraContainer, null))
return stack.getCapability(NaturesAuraAPI.capAuraContainer, null).storeAura(1, true) <= 0;
else
return TileEntityNatureAltar.this.getRecipeForInput(stack) == null;
2018-10-18 13:34:37 +02:00
}
};
@SideOnly(Side.CLIENT)
public int bobTimer;
private final BasicAuraContainer container = new BasicAuraContainer(NaturesAuraAPI.TYPE_OVERWORLD, 500000);
private final ItemStack[] catalysts = new ItemStack[4];
2018-10-14 14:27:18 +02:00
public boolean structureFine;
2018-10-18 17:12:20 +02:00
private AltarRecipe currentRecipe;
private int timer;
2018-10-14 14:27:18 +02:00
private int lastAura;
@Override
public void update() {
Random rand = this.world.rand;
if (!this.world.isRemote) {
if (this.world.getTotalWorldTime() % 40 == 0) {
2018-11-07 13:33:49 +01:00
boolean fine = Multiblocks.ALTAR.isComplete(this.world, this.pos);
2018-10-14 14:27:18 +02:00
if (fine != this.structureFine) {
this.structureFine = fine;
this.sendToClients();
}
if (this.structureFine) {
int index = 0;
for (int x = -2; x <= 2; x += 4) {
for (int z = -2; z <= 2; z += 4) {
BlockPos offset = this.pos.add(x, 1, z);
IBlockState state = this.world.getBlockState(offset);
this.catalysts[index] = state.getBlock().getItem(this.world, offset, state);
index++;
}
}
}
2018-10-14 14:27:18 +02:00
}
if (this.structureFine) {
int space = this.container.storeAura(300, true);
if (space > 0 && this.container.isAcceptableType(IAuraType.forWorld(this.world))) {
2018-11-11 13:26:19 +01:00
int toStore = Math.min(IAuraChunk.getAuraInArea(this.world, this.pos, 20), space);
if (toStore > 0) {
2018-11-11 13:26:19 +01:00
BlockPos spot = IAuraChunk.getHighestSpot(this.world, this.pos, 20, this.pos);
IAuraChunk chunk = IAuraChunk.getAuraChunk(this.world, spot);
2018-10-14 14:27:18 +02:00
chunk.drainAura(spot, toStore);
this.container.storeAura(toStore, false);
2018-10-14 14:27:18 +02:00
if (this.world.getTotalWorldTime() % 3 == 0)
2018-10-16 01:36:30 +02:00
PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticleStream(
this.pos.getX() + (float) rand.nextGaussian() * 10F,
this.pos.getY() + rand.nextFloat() * 10F,
this.pos.getZ() + (float) rand.nextGaussian() * 10F,
2018-10-14 14:27:18 +02:00
this.pos.getX() + 0.5F, this.pos.getY() + 0.5F, this.pos.getZ() + 0.5F,
rand.nextFloat() * 0.1F + 0.1F, 0x89cc37, rand.nextFloat() * 1F + 1F
2018-10-14 14:27:18 +02:00
));
}
}
2018-10-18 17:12:20 +02:00
ItemStack stack = this.items.getStackInSlot(0);
2018-11-12 22:04:40 +01:00
if (!stack.isEmpty() && stack.hasCapability(NaturesAuraAPI.capAuraContainer, null)) {
IAuraContainer container = stack.getCapability(NaturesAuraAPI.capAuraContainer, null);
2019-01-29 16:01:10 +01:00
int theoreticalDrain = this.container.drainAura(1000, true);
2018-10-20 21:19:08 +02:00
if (theoreticalDrain > 0) {
int stored = container.storeAura(theoreticalDrain, false);
if (stored > 0) {
this.container.drainAura(stored, false);
2018-10-18 17:12:20 +02:00
2018-10-20 21:19:08 +02:00
if (this.world.getTotalWorldTime() % 4 == 0) {
2018-10-18 17:12:20 +02:00
PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticles(this.pos.getX(), this.pos.getY(), this.pos.getZ(), 4));
}
2018-10-20 21:19:08 +02:00
}
}
} else {
if (this.currentRecipe == null) {
if (!stack.isEmpty()) {
this.currentRecipe = this.getRecipeForInput(stack);
2018-10-20 21:19:08 +02:00
}
} else {
if (stack.isEmpty() || !this.currentRecipe.input.apply(stack)) {
2018-10-20 21:19:08 +02:00
this.currentRecipe = null;
2018-10-22 11:23:48 +02:00
this.timer = 0;
} else {
int req = MathHelper.ceil(this.currentRecipe.aura / (double) this.currentRecipe.time);
2018-10-20 21:19:08 +02:00
if (this.container.getStoredAura() >= req) {
this.container.drainAura(req, false);
if (this.timer % 4 == 0) {
PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticles(this.pos.getX(), this.pos.getY(), this.pos.getZ(), 4));
}
this.timer++;
if (this.timer >= this.currentRecipe.time) {
this.items.setStackInSlot(0, this.currentRecipe.output.copy());
this.currentRecipe = null;
this.timer = 0;
this.world.playSound(null, this.pos.getX() + 0.5, this.pos.getY() + 0.5, this.pos.getZ() + 0.5,
SoundEvents.ENTITY_ARROW_HIT_PLAYER, SoundCategory.BLOCKS, 0.65F, 1F);
2018-10-20 21:19:08 +02:00
}
2018-10-18 17:12:20 +02:00
}
}
}
}
2018-10-14 14:27:18 +02:00
}
if (this.world.getTotalWorldTime() % 10 == 0 && this.lastAura != this.container.getStoredAura()) {
this.lastAura = this.container.getStoredAura();
this.sendToClients();
}
} else {
if (this.structureFine) {
if (rand.nextFloat() >= 0.7F) {
int fourths = this.container.getMaxAura() / 4;
if (this.container.getStoredAura() > 0) {
2018-11-13 00:36:47 +01:00
NaturesAuraAPI.instance().spawnMagicParticle(
2018-10-14 14:27:18 +02:00
this.pos.getX() - 4F + rand.nextFloat(), this.pos.getY() + 3F, this.pos.getZ() + rand.nextFloat(),
0F, 0F, 0F, this.container.getAuraColor(), rand.nextFloat() * 3F + 1F, rand.nextInt(100) + 50, -0.05F, true, true);
2018-10-14 14:27:18 +02:00
}
if (this.container.getStoredAura() >= fourths) {
2018-11-13 00:36:47 +01:00
NaturesAuraAPI.instance().spawnMagicParticle(
2018-10-14 14:27:18 +02:00
this.pos.getX() + 4F + rand.nextFloat(), this.pos.getY() + 3F, this.pos.getZ() + rand.nextFloat(),
0F, 0F, 0F, this.container.getAuraColor(), rand.nextFloat() * 3F + 1F, rand.nextInt(100) + 50, -0.05F, true, true);
2018-10-14 14:27:18 +02:00
}
if (this.container.getStoredAura() >= fourths * 2) {
2018-11-13 00:36:47 +01:00
NaturesAuraAPI.instance().spawnMagicParticle(
2018-10-14 14:27:18 +02:00
this.pos.getX() + rand.nextFloat(), this.pos.getY() + 3F, this.pos.getZ() - 4F + rand.nextFloat(),
0F, 0F, 0F, this.container.getAuraColor(), rand.nextFloat() * 3F + 1F, rand.nextInt(100) + 50, -0.05F, true, true);
2018-10-14 14:27:18 +02:00
}
if (this.container.getStoredAura() >= fourths * 3) {
2018-11-13 00:36:47 +01:00
NaturesAuraAPI.instance().spawnMagicParticle(
2018-10-14 14:27:18 +02:00
this.pos.getX() + rand.nextFloat(), this.pos.getY() + 3F, this.pos.getZ() + 4F + rand.nextFloat(),
0F, 0F, 0F, this.container.getAuraColor(), rand.nextFloat() * 3F + 1F, rand.nextInt(100) + 50, -0.05F, true, true);
2018-10-14 14:27:18 +02:00
}
2018-10-14 16:12:33 +02:00
2018-10-14 14:27:18 +02:00
}
}
this.bobTimer++;
2018-10-14 14:27:18 +02:00
}
}
private AltarRecipe getRecipeForInput(ItemStack input) {
2018-11-11 13:26:19 +01:00
for (AltarRecipe recipe : NaturesAuraAPI.ALTAR_RECIPES.values()) {
if (recipe.input.apply(input)) {
if (recipe.catalyst == Ingredient.EMPTY)
return recipe;
for (ItemStack stack : this.catalysts)
if (recipe.catalyst.apply(stack))
return recipe;
2018-11-11 13:26:19 +01:00
}
}
return null;
}
2018-10-14 14:27:18 +02:00
@Override
2018-11-04 16:38:09 +01:00
public void writeNBT(NBTTagCompound compound, SaveType type) {
super.writeNBT(compound, type);
2018-11-05 16:36:10 +01:00
if (type != SaveType.BLOCK) {
2018-11-20 11:48:45 +01:00
compound.setTag("items", this.items.serializeNBT());
2018-11-05 16:36:10 +01:00
compound.setBoolean("fine", this.structureFine);
this.container.writeNBT(compound);
}
2018-11-04 16:38:09 +01:00
if (type == SaveType.TILE) {
2018-10-18 17:12:20 +02:00
if (this.currentRecipe != null) {
2018-10-22 00:14:52 +02:00
compound.setString("recipe", this.currentRecipe.name.toString());
2018-10-18 17:12:20 +02:00
compound.setInteger("timer", this.timer);
}
}
2018-10-14 14:27:18 +02:00
}
@Override
2018-11-04 16:38:09 +01:00
public void readNBT(NBTTagCompound compound, SaveType type) {
super.readNBT(compound, type);
2018-11-05 16:36:10 +01:00
if (type != SaveType.BLOCK) {
2018-11-20 11:48:45 +01:00
this.items.deserializeNBT(compound.getCompoundTag("items"));
2018-11-05 16:36:10 +01:00
this.structureFine = compound.getBoolean("fine");
this.container.readNBT(compound);
}
2018-11-04 16:38:09 +01:00
if (type == SaveType.TILE) {
2018-10-22 00:21:37 +02:00
if (compound.hasKey("recipe")) {
2018-11-11 13:26:19 +01:00
this.currentRecipe = NaturesAuraAPI.ALTAR_RECIPES.get(new ResourceLocation(compound.getString("recipe")));
2018-10-18 17:12:20 +02:00
this.timer = compound.getInteger("timer");
}
}
2018-10-14 14:27:18 +02:00
}
@Override
2018-10-20 21:19:08 +02:00
public IAuraContainer getAuraContainer(EnumFacing facing) {
2018-10-14 14:27:18 +02:00
return this.container;
}
2018-10-14 17:46:00 +02:00
2018-10-18 13:34:37 +02:00
@Override
public IItemHandlerModifiable getItemHandler(EnumFacing facing) {
return this.items;
}
2018-10-14 14:27:18 +02:00
}