2018-10-16 11:49:30 +02:00
|
|
|
package de.ellpeck.naturesaura.blocks.tiles;
|
|
|
|
|
|
|
|
import de.ellpeck.naturesaura.Helper;
|
|
|
|
import de.ellpeck.naturesaura.blocks.ModBlocks;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketHandler;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketParticleStream;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketParticles;
|
|
|
|
import net.minecraft.block.BlockLeaves;
|
|
|
|
import net.minecraft.block.BlockLog;
|
|
|
|
import net.minecraft.block.state.IBlockState;
|
2018-10-16 17:48:36 +02:00
|
|
|
import net.minecraft.entity.item.EntityItem;
|
2018-10-16 11:49:30 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2018-10-16 18:07:03 +02:00
|
|
|
import net.minecraft.nbt.NBTBase;
|
2018-10-16 11:49:30 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2018-10-16 18:07:03 +02:00
|
|
|
import net.minecraft.nbt.NBTTagList;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2018-10-16 11:49:30 +02:00
|
|
|
import net.minecraft.util.EnumFacing;
|
|
|
|
import net.minecraft.util.ITickable;
|
|
|
|
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-16 11:49:30 +02:00
|
|
|
|
2018-10-16 18:07:03 +02:00
|
|
|
import java.util.HashMap;
|
2018-10-16 11:49:30 +02:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
public class TileEntityWoodStand extends TileEntityImpl implements ITickable {
|
|
|
|
|
|
|
|
public static final BlockPos[] GOLD_POWDER_POSITIONS = new BlockPos[]{
|
|
|
|
new BlockPos(-2, 0, 0),
|
|
|
|
new BlockPos(2, 0, 0),
|
|
|
|
new BlockPos(0, 0, -2),
|
|
|
|
new BlockPos(0, 0, 2),
|
|
|
|
new BlockPos(-1, 0, -1),
|
|
|
|
new BlockPos(-1, 0, 1),
|
|
|
|
new BlockPos(1, 0, 1),
|
|
|
|
new BlockPos(1, 0, -1),
|
|
|
|
new BlockPos(2, 0, -1),
|
|
|
|
new BlockPos(2, 0, 1),
|
|
|
|
new BlockPos(-2, 0, -1),
|
|
|
|
new BlockPos(-2, 0, 1),
|
|
|
|
new BlockPos(1, 0, 2),
|
|
|
|
new BlockPos(-1, 0, 2),
|
|
|
|
new BlockPos(1, 0, -2),
|
|
|
|
new BlockPos(-1, 0, -2)
|
|
|
|
};
|
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;
|
|
|
|
}
|
|
|
|
};
|
2018-10-16 11:49:30 +02:00
|
|
|
|
|
|
|
private BlockPos ritualPos;
|
2018-10-16 18:07:03 +02:00
|
|
|
private Map<BlockPos, ItemStack> involvedStands;
|
|
|
|
private ItemStack output;
|
|
|
|
private int totalTime;
|
2018-10-16 11:49:30 +02:00
|
|
|
private int timer;
|
|
|
|
|
2018-10-16 18:07:03 +02:00
|
|
|
public void setRitual(BlockPos pos, ItemStack output, int totalTime, Map<BlockPos, ItemStack> involvedStands) {
|
2018-10-16 11:49:30 +02:00
|
|
|
this.ritualPos = pos;
|
2018-10-16 18:07:03 +02:00
|
|
|
this.output = output;
|
|
|
|
this.totalTime = totalTime;
|
2018-10-16 11:49:30 +02:00
|
|
|
this.involvedStands = involvedStands;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void update() {
|
|
|
|
if (!this.world.isRemote) {
|
2018-10-16 18:07:03 +02:00
|
|
|
if (this.ritualPos != null && this.involvedStands != null && this.output != null && this.totalTime > 0) {
|
2018-10-17 15:02:29 +02:00
|
|
|
if (this.isRitualOkay()) {
|
2018-10-16 11:49:30 +02:00
|
|
|
this.timer++;
|
|
|
|
|
2018-10-17 15:02:29 +02:00
|
|
|
if (this.timer % 5 == 0 && this.timer < this.totalTime / 2) {
|
2018-10-16 18:07:03 +02:00
|
|
|
for (BlockPos pos : this.involvedStands.keySet()) {
|
2018-10-16 11:49:30 +02:00
|
|
|
PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticleStream(
|
2018-10-17 18:41:53 +02:00
|
|
|
(float) pos.getX() + 0.2F + this.world.rand.nextFloat() * 0.6F,
|
|
|
|
(float) pos.getY() + 0.85F,
|
|
|
|
(float) pos.getZ() + 0.2F + this.world.rand.nextFloat() * 0.6F,
|
|
|
|
this.ritualPos.getX() + 0.5F, this.ritualPos.getY() + this.world.rand.nextFloat() * 3F + 2F, this.ritualPos.getZ() + 0.5F,
|
2018-10-17 15:02:29 +02:00
|
|
|
this.world.rand.nextFloat() * 0.02F + 0.02F, 0x89cc37, this.world.rand.nextFloat() * 1F + 1F
|
2018-10-16 11:49:30 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.timer % 5 == 0) {
|
2018-10-16 17:48:36 +02:00
|
|
|
PacketHandler.sendToAllAround(this.world, this.ritualPos, 32,
|
|
|
|
new PacketParticles(this.ritualPos.getX(), this.ritualPos.getY(), this.ritualPos.getZ(), 0));
|
2018-10-16 11:49:30 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 18:07:03 +02:00
|
|
|
if (this.timer >= this.totalTime) {
|
2018-10-16 11:49:30 +02:00
|
|
|
this.recurseTreeDestruction(this.ritualPos, this.ritualPos);
|
2018-10-16 17:48:36 +02:00
|
|
|
for (BlockPos offset : GOLD_POWDER_POSITIONS) {
|
|
|
|
this.world.setBlockToAir(this.ritualPos.add(offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
EntityItem item = new EntityItem(this.world,
|
|
|
|
this.ritualPos.getX() + 0.5, this.ritualPos.getY() + 4.5, this.ritualPos.getZ() + 0.5,
|
2018-10-16 18:07:03 +02:00
|
|
|
this.output.copy());
|
2018-10-16 17:48:36 +02:00
|
|
|
this.world.spawnEntity(item);
|
|
|
|
|
|
|
|
PacketHandler.sendToAllAround(this.world, this.pos, 32,
|
|
|
|
new PacketParticles((float) item.posX, (float) item.posY, (float) item.posZ, 3));
|
2018-10-16 11:49:30 +02:00
|
|
|
|
|
|
|
this.ritualPos = null;
|
|
|
|
this.involvedStands = null;
|
2018-10-16 18:07:03 +02:00
|
|
|
this.output = null;
|
|
|
|
this.totalTime = 0;
|
2018-10-16 11:49:30 +02:00
|
|
|
this.timer = 0;
|
2018-10-16 18:07:03 +02:00
|
|
|
} else if (this.timer == this.totalTime / 2) {
|
|
|
|
for (BlockPos pos : this.involvedStands.keySet()) {
|
|
|
|
TileEntityWoodStand stand = (TileEntityWoodStand) this.world.getTileEntity(pos);
|
2018-10-16 17:48:36 +02:00
|
|
|
PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticles(stand.pos.getX(), stand.pos.getY(), stand.pos.getZ(), 1));
|
2018-10-18 13:34:37 +02:00
|
|
|
stand.items.setStackInSlot(0, ItemStack.EMPTY);
|
2018-10-16 11:49:30 +02:00
|
|
|
stand.sendToClients();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
this.ritualPos = null;
|
|
|
|
this.involvedStands = null;
|
2018-10-16 18:07:03 +02:00
|
|
|
this.output = null;
|
|
|
|
this.totalTime = 0;
|
2018-10-16 11:49:30 +02:00
|
|
|
this.timer = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void recurseTreeDestruction(BlockPos pos, BlockPos start) {
|
2018-10-16 21:10:38 +02:00
|
|
|
if (Math.abs(pos.getX() - start.getX()) >= 6
|
|
|
|
|| Math.abs(pos.getZ() - start.getZ()) >= 6
|
|
|
|
|| Math.abs(pos.getY() - start.getY()) >= 16) {
|
2018-10-16 11:49:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (EnumFacing facing : EnumFacing.VALUES) {
|
|
|
|
BlockPos offset = pos.offset(facing);
|
|
|
|
IBlockState state = this.world.getBlockState(offset);
|
|
|
|
if (state.getBlock() instanceof BlockLog || state.getBlock() instanceof BlockLeaves) {
|
|
|
|
this.world.setBlockToAir(offset);
|
2018-10-16 17:48:36 +02:00
|
|
|
PacketHandler.sendToAllAround(this.world, this.pos, 32, new PacketParticles(offset.getX(), offset.getY(), offset.getZ(), 2));
|
2018-10-16 11:49:30 +02:00
|
|
|
|
|
|
|
this.recurseTreeDestruction(offset, start);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 15:02:29 +02:00
|
|
|
private boolean isRitualOkay() {
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
IBlockState state = this.world.getBlockState(this.ritualPos.up(i));
|
|
|
|
if (!(state.getBlock() instanceof BlockLog)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-10-16 18:07:03 +02:00
|
|
|
for (Map.Entry<BlockPos, ItemStack> entry : this.involvedStands.entrySet()) {
|
2018-10-17 15:02:29 +02:00
|
|
|
TileEntity tile = this.world.getTileEntity(entry.getKey());
|
2018-10-18 13:34:37 +02:00
|
|
|
if (!(tile instanceof TileEntityWoodStand)
|
|
|
|
|| (this.timer < this.totalTime / 2 && !((TileEntityWoodStand) tile).items.getStackInSlot(0).isItemEqual(entry.getValue()))) {
|
2018-10-16 11:49:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-10-17 15:02:29 +02:00
|
|
|
return Helper.checkMultiblock(this.world, this.ritualPos, TileEntityWoodStand.GOLD_POWDER_POSITIONS, ModBlocks.GOLD_POWDER.getDefaultState(), true);
|
2018-10-16 11:49:30 +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-16 18:07:03 +02:00
|
|
|
|
2018-10-16 18:08:32 +02:00
|
|
|
if (!syncing) {
|
|
|
|
if (this.ritualPos != null && this.involvedStands != null && this.output != null && this.totalTime > 0) {
|
|
|
|
compound.setLong("ritual_pos", this.ritualPos.toLong());
|
|
|
|
compound.setInteger("timer", this.timer);
|
|
|
|
compound.setInteger("total_time", this.totalTime);
|
|
|
|
compound.setTag("output", this.output.writeToNBT(new NBTTagCompound()));
|
|
|
|
|
|
|
|
NBTTagList list = new NBTTagList();
|
|
|
|
for (Map.Entry<BlockPos, ItemStack> entry : this.involvedStands.entrySet()) {
|
|
|
|
NBTTagCompound tag = new NBTTagCompound();
|
|
|
|
tag.setLong("pos", entry.getKey().toLong());
|
|
|
|
tag.setTag("item", entry.getValue().writeToNBT(new NBTTagCompound()));
|
|
|
|
list.appendTag(tag);
|
|
|
|
}
|
|
|
|
compound.setTag("stands", list);
|
2018-10-16 18:07:03 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 11:49:30 +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-16 18:07:03 +02:00
|
|
|
|
2018-10-16 18:08:32 +02:00
|
|
|
if (!syncing) {
|
|
|
|
if (compound.hasKey("ritual_pos") && compound.hasKey("stands") && compound.hasKey("output") && compound.hasKey("total_time")) {
|
|
|
|
this.ritualPos = BlockPos.fromLong(compound.getLong("ritual_pos"));
|
|
|
|
this.timer = compound.getInteger("timer");
|
|
|
|
this.totalTime = compound.getInteger("total_time");
|
|
|
|
this.output = new ItemStack(compound.getCompoundTag("output"));
|
|
|
|
|
|
|
|
this.involvedStands = new HashMap<>();
|
|
|
|
NBTTagList list = compound.getTagList("stands", 10);
|
|
|
|
for (NBTBase base : list) {
|
|
|
|
NBTTagCompound tag = (NBTTagCompound) base;
|
|
|
|
this.involvedStands.put(
|
|
|
|
BlockPos.fromLong(tag.getLong("pos")),
|
|
|
|
new ItemStack(tag.getCompoundTag("item"))
|
|
|
|
);
|
|
|
|
}
|
2018-10-16 18:07:03 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 11:49:30 +02:00
|
|
|
}
|
2018-10-18 13:34:37 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public IItemHandlerModifiable getItemHandler(EnumFacing facing) {
|
|
|
|
return this.items;
|
|
|
|
}
|
2018-10-16 11:49:30 +02:00
|
|
|
}
|