package net.Chipperfluff.chipi.block;

import net.minecraft.block.BlockState;
import net.minecraft.block.PillarBlock;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import net.Chipperfluff.chipi.advancement.ModCriteria;

public class ChipperFrameBlock extends PillarBlock {

    public ChipperFrameBlock(Settings settings) {
        super(settings);
    }

    @Override
    public void onPlaced(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
        if (!world.isClient) {
            ChipperPortalShape.tryCreate(world, pos);
        }
    }

    @Override
    public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) {
        if (!world.isClient && state.getBlock() != newState.getBlock()) {
            ChipperPortalShape.destroyNearby(world, pos);

            if (world.getClosestPlayer(pos.getX(), pos.getY(), pos.getZ(), 10, false)
                    instanceof ServerPlayerEntity serverPlayer) {
                ModCriteria.PORTAL_DESTROYED.trigger(serverPlayer);
            }
        }

        super.onStateReplaced(state, world, pos, newState, moved);
    }
}
