package net.geforcemods.securitycraft.blockentities;

import net.geforcemods.securitycraft.SCContent;
import net.geforcemods.securitycraft.api.SecurityCraftBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;

public class BlockPocketBlockEntity extends SecurityCraftBlockEntity
{
	private BlockPocketManagerBlockEntity manager;
	private BlockPos managerPos;

	public BlockPocketBlockEntity(BlockPos pos, BlockState state)
	{
		super(SCContent.beTypeBlockPocket, pos, state);
	}

	public void setManager(BlockPocketManagerBlockEntity manager)
	{
		this.manager = manager;
		managerPos = manager.getBlockPos();
	}

	public void removeManager()
	{
		managerPos = null;
		manager = null;
	}

	public BlockPocketManagerBlockEntity getManager()
	{
		return manager;
	}

	public static void tick(Level world, BlockPos pos, BlockState state, BlockPocketBlockEntity te)
	{
		te.tick(world, pos, state);
	}

	@Override
	public void tick(Level world, BlockPos pos, BlockState state)
	{
		super.tick(world, pos, state);

		if(manager == null && managerPos != null)
		{
			if(world.getBlockEntity(managerPos) instanceof BlockPocketManagerBlockEntity manager)
				this.manager = manager;
		}
	}

	@Override
	public void onTileEntityDestroyed()
	{
		super.onTileEntityDestroyed();

		BlockState state = level.getBlockState(worldPosition);

		if(manager != null && state.getBlock() != SCContent.BLOCK_POCKET_WALL.get() && state.getBlock() != SCContent.REINFORCED_CHISELED_CRYSTAL_QUARTZ.get() && state.getBlock() != SCContent.REINFORCED_CRYSTAL_QUARTZ_PILLAR.get())
			manager.disableMultiblock();
	}

	@Override
	public CompoundTag save(CompoundTag tag)
	{
		if(manager != null)
			tag.putLong("ManagerPos", manager.getBlockPos().asLong());
		return super.save(tag);
	}

	@Override
	public void load(CompoundTag tag)
	{
		super.load(tag);

		if(tag.contains("ManagerPos"))
			managerPos = BlockPos.of(tag.getLong("ManagerPos"));
	}
}

@@ -929,147 +929,8 @@
 %0D%0A%0D%0A
-%09public static void tick(Level world, BlockPos pos, BlockState state, BlockPocketBlockEntity te)%0D%0A%09%7B%0D%0A%09%09te.tick(world, pos, state);%0D%0A%09%7D%0D%0A%0D%0A
 %09@Ov

remove unused method