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

196 lines
8.6 KiB
Java
Raw Normal View History

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;
import net.minecraft.world.World;
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)
};
public ItemStack stack = ItemStack.EMPTY;
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-16 11:49:30 +02:00
if (this.isRitualOkay(this.world)) {
this.timer++;
2018-10-16 18:07:03 +02:00
if (this.timer % 3 == 0 && this.timer < this.totalTime / 2) {
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-16 18:07:03 +02:00
(float) pos.getX() + 0.5F, (float) pos.getY() + 1.25F, (float) pos.getZ() + 0.5F,
2018-10-16 11:49:30 +02:00
this.ritualPos.getX() + 0.5F, this.ritualPos.getY() + 2.5F, this.ritualPos.getZ() + 0.5F,
this.world.rand.nextFloat() * 0.02F + 0.02F, 0xFF00FF, this.world.rand.nextFloat() * 1F + 1F
));
}
}
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-16 11:49:30 +02:00
stand.stack = ItemStack.EMPTY;
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) {
if (pos.distanceSq(start) >= 15 * 15) {
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);
}
}
}
private boolean isRitualOkay(World world) {
2018-10-16 18:07:03 +02:00
for (Map.Entry<BlockPos, ItemStack> entry : this.involvedStands.entrySet()) {
TileEntity tile = world.getTileEntity(entry.getKey());
if (!(tile instanceof TileEntityWoodStand) || (this.timer < this.totalTime / 2 && !((TileEntityWoodStand) tile).stack.isItemEqual(entry.getValue()))) {
2018-10-16 11:49:30 +02:00
return false;
}
}
return Helper.checkMultiblock(world, this.ritualPos, TileEntityWoodStand.GOLD_POWDER_POSITIONS, ModBlocks.GOLD_POWDER.getDefaultState(), true);
}
@Override
public void writeNBT(NBTTagCompound compound, boolean syncing) {
super.writeNBT(compound, syncing);
compound.setTag("item", this.stack.writeToNBT(new NBTTagCompound()));
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);
this.stack = new ItemStack(compound.getCompoundTag("item"));
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
}
}