2018-10-14 14:27:18 +02:00
|
|
|
package de.ellpeck.naturesaura.blocks.tiles;
|
|
|
|
|
|
|
|
import de.ellpeck.naturesaura.Helper;
|
|
|
|
import de.ellpeck.naturesaura.NaturesAura;
|
2018-10-20 21:19:08 +02:00
|
|
|
import de.ellpeck.naturesaura.aura.Capabilities;
|
2018-10-21 12:51:13 +02:00
|
|
|
import de.ellpeck.naturesaura.aura.chunk.AuraChunk;
|
|
|
|
import de.ellpeck.naturesaura.aura.container.BasicAuraContainer;
|
|
|
|
import de.ellpeck.naturesaura.aura.container.IAuraContainer;
|
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-18 13:34:37 +02:00
|
|
|
import de.ellpeck.naturesaura.recipes.AltarRecipe;
|
2018-10-14 14:27:18 +02:00
|
|
|
import net.minecraft.block.BlockStoneBrick;
|
|
|
|
import net.minecraft.block.BlockStoneBrick.EnumType;
|
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.init.Blocks;
|
2018-10-24 12:42:04 +02:00
|
|
|
import net.minecraft.init.SoundEvents;
|
2018-10-18 13:34:37 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
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;
|
2018-10-24 12:42:04 +02:00
|
|
|
import net.minecraft.util.SoundCategory;
|
2018-10-14 14:27:18 +02:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
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
|
|
|
|
|
|
|
private static final BlockPos[] BRICK_POSITIONS = new BlockPos[]{
|
|
|
|
new BlockPos(-2, -1, 0),
|
|
|
|
new BlockPos(-3, -1, 0),
|
|
|
|
new BlockPos(-4, 0, 0),
|
|
|
|
new BlockPos(-4, 1, 0),
|
|
|
|
|
|
|
|
new BlockPos(2, -1, 0),
|
|
|
|
new BlockPos(3, -1, 0),
|
|
|
|
new BlockPos(4, 0, 0),
|
|
|
|
new BlockPos(4, 1, 0),
|
|
|
|
|
|
|
|
new BlockPos(0, -1, -2),
|
|
|
|
new BlockPos(0, -1, -3),
|
|
|
|
new BlockPos(0, 0, -4),
|
|
|
|
new BlockPos(0, 1, -4),
|
|
|
|
|
|
|
|
new BlockPos(0, -1, 2),
|
|
|
|
new BlockPos(0, -1, 3),
|
|
|
|
new BlockPos(0, 0, 4),
|
|
|
|
new BlockPos(0, 1, 4)
|
|
|
|
};
|
|
|
|
private static final BlockPos[] MOSSY_POSITIONS = new BlockPos[]{
|
|
|
|
new BlockPos(-4, 2, 0),
|
|
|
|
new BlockPos(4, 2, 0),
|
|
|
|
new BlockPos(0, 2, -4),
|
|
|
|
new BlockPos(0, 2, 4),
|
|
|
|
|
|
|
|
new BlockPos(-2, 0, -2),
|
|
|
|
new BlockPos(2, 0, -2),
|
|
|
|
new BlockPos(2, 0, 2),
|
|
|
|
new BlockPos(-2, 0, 2)
|
|
|
|
};
|
|
|
|
private static final BlockPos[] CHISELED_POSITIONS = new BlockPos[]{
|
|
|
|
new BlockPos(1, -1, 1),
|
|
|
|
new BlockPos(-1, -1, 1),
|
|
|
|
new BlockPos(-1, -1, -1),
|
|
|
|
new BlockPos(1, -1, -1)
|
|
|
|
};
|
2018-10-14 17:46:00 +02:00
|
|
|
private static final BlockPos[] WOOD_POSITIONS = new BlockPos[]{
|
2018-10-14 14:27:18 +02:00
|
|
|
new BlockPos(-1, -1, 0),
|
|
|
|
new BlockPos(1, -1, 0),
|
|
|
|
new BlockPos(0, -1, -1),
|
|
|
|
new BlockPos(0, -1, 1),
|
|
|
|
|
|
|
|
new BlockPos(-2, -1, -1),
|
|
|
|
new BlockPos(-3, -1, -1),
|
|
|
|
new BlockPos(-2, -1, 1),
|
|
|
|
new BlockPos(-3, -1, 1),
|
|
|
|
|
|
|
|
new BlockPos(2, -1, -1),
|
|
|
|
new BlockPos(3, -1, -1),
|
|
|
|
new BlockPos(2, -1, 1),
|
|
|
|
new BlockPos(3, -1, 1),
|
|
|
|
|
|
|
|
new BlockPos(-1, -1, -2),
|
|
|
|
new BlockPos(-1, -1, -3),
|
|
|
|
new BlockPos(1, -1, -2),
|
|
|
|
new BlockPos(1, -1, -3),
|
|
|
|
|
|
|
|
new BlockPos(-1, -1, 2),
|
|
|
|
new BlockPos(-1, -1, 3),
|
|
|
|
new BlockPos(1, -1, 2),
|
|
|
|
new BlockPos(1, -1, 3),
|
|
|
|
};
|
|
|
|
|
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) {
|
2018-10-20 21:19:08 +02:00
|
|
|
return AltarRecipe.forInput(stack) != null || stack.hasCapability(Capabilities.auraContainer, null);
|
2018-10-18 13:34:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean canExtract(ItemStack stack, int slot, int amount) {
|
|
|
|
return AltarRecipe.forInput(stack) == null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-24 13:06:24 +02:00
|
|
|
private final BasicAuraContainer container = new BasicAuraContainer(5000);
|
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-10-14 17:46:00 +02:00
|
|
|
boolean fine = this.check(BRICK_POSITIONS, Blocks.STONEBRICK.getDefaultState(), false)
|
|
|
|
&& this.check(MOSSY_POSITIONS, Blocks.STONEBRICK.getDefaultState().withProperty(BlockStoneBrick.VARIANT, EnumType.MOSSY), false)
|
|
|
|
&& this.check(CHISELED_POSITIONS, Blocks.STONEBRICK.getDefaultState().withProperty(BlockStoneBrick.VARIANT, EnumType.CHISELED), false)
|
|
|
|
&& this.check(WOOD_POSITIONS, Blocks.PLANKS.getDefaultState(), true);
|
2018-10-14 14:27:18 +02:00
|
|
|
if (fine != this.structureFine) {
|
|
|
|
this.structureFine = fine;
|
|
|
|
this.sendToClients();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.structureFine) {
|
2018-10-21 12:51:13 +02:00
|
|
|
int space = this.container.storeAura(3, true);
|
|
|
|
if (space > 0) {
|
|
|
|
int toStore = Math.min(AuraChunk.getAuraInArea(this.world, this.pos, 10), space);
|
|
|
|
if (toStore > 0) {
|
|
|
|
BlockPos spot = AuraChunk.getClosestSpot(this.world, this.pos, 10, this.pos);
|
|
|
|
AuraChunk chunk = AuraChunk.getAuraChunk(this.world, spot);
|
2018-10-14 14:27:18 +02:00
|
|
|
|
2018-10-21 12:51:13 +02:00
|
|
|
chunk.drainAura(spot, toStore);
|
|
|
|
this.container.storeAura(toStore, false);
|
2018-10-14 14:27:18 +02:00
|
|
|
|
2018-10-21 12:51:13 +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(
|
2018-10-21 12:51:13 +02:00
|
|
|
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,
|
2018-10-21 12:51:13 +02:00
|
|
|
rand.nextFloat() * 0.05F + 0.05F, 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-10-20 21:19:08 +02:00
|
|
|
if (!stack.isEmpty() && stack.hasCapability(Capabilities.auraContainer, null)) {
|
|
|
|
IAuraContainer container = stack.getCapability(Capabilities.auraContainer, null);
|
|
|
|
int theoreticalDrain = this.container.drainAura(10, true);
|
|
|
|
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 = AltarRecipe.forInput(stack);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (stack.isEmpty() || !stack.isItemEqual(this.currentRecipe.input)) {
|
|
|
|
this.currentRecipe = null;
|
2018-10-22 11:23:48 +02:00
|
|
|
this.timer = 0;
|
2018-10-20 21:19:08 +02:00
|
|
|
} else {
|
|
|
|
int req = this.currentRecipe.aura / this.currentRecipe.time;
|
|
|
|
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;
|
2018-10-24 12:42:04 +02:00
|
|
|
|
|
|
|
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) {
|
|
|
|
NaturesAura.proxy.spawnMagicParticle(this.world,
|
|
|
|
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(200) + 100, -0.025F, true, true);
|
|
|
|
}
|
|
|
|
if (this.container.getStoredAura() >= fourths) {
|
|
|
|
NaturesAura.proxy.spawnMagicParticle(this.world,
|
|
|
|
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(200) + 100, -0.025F, true, true);
|
|
|
|
}
|
|
|
|
if (this.container.getStoredAura() >= fourths * 2) {
|
|
|
|
NaturesAura.proxy.spawnMagicParticle(this.world,
|
|
|
|
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(200) + 100, -0.025F, true, true);
|
|
|
|
}
|
|
|
|
if (this.container.getStoredAura() >= fourths * 3) {
|
|
|
|
NaturesAura.proxy.spawnMagicParticle(this.world,
|
|
|
|
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(200) + 100, -0.025F, true, true);
|
|
|
|
}
|
2018-10-14 16:12:33 +02:00
|
|
|
|
2018-10-14 14:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-14 17:46:00 +02:00
|
|
|
private boolean check(BlockPos[] positions, IBlockState state, boolean blockOnly) {
|
2018-10-16 01:36:30 +02:00
|
|
|
return Helper.checkMultiblock(this.world, this.pos, positions, state, blockOnly);
|
2018-10-14 14:27:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeNBT(NBTTagCompound compound, boolean syncing) {
|
|
|
|
super.writeNBT(compound, syncing);
|
2018-10-18 13:34:37 +02:00
|
|
|
compound.setTag("items", this.items.serializeNBT());
|
2018-10-14 14:27:18 +02:00
|
|
|
compound.setBoolean("fine", this.structureFine);
|
|
|
|
this.container.writeNBT(compound);
|
2018-10-18 17:12:20 +02:00
|
|
|
|
|
|
|
if (!syncing) {
|
|
|
|
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
|
|
|
|
public void readNBT(NBTTagCompound compound, boolean syncing) {
|
|
|
|
super.readNBT(compound, syncing);
|
2018-10-18 13:34:37 +02:00
|
|
|
this.items.deserializeNBT(compound.getCompoundTag("items"));
|
2018-10-14 14:27:18 +02:00
|
|
|
this.structureFine = compound.getBoolean("fine");
|
|
|
|
this.container.readNBT(compound);
|
2018-10-18 17:12:20 +02:00
|
|
|
|
|
|
|
if (!syncing) {
|
2018-10-22 00:21:37 +02:00
|
|
|
if (compound.hasKey("recipe")) {
|
2018-10-22 00:14:52 +02:00
|
|
|
this.currentRecipe = AltarRecipe.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
|
|
|
}
|