<commit_before>package mcjty.deepresonance.blocks.pedestal;

import elec332.core.world.WorldHelper;
import mcjty.deepresonance.blocks.ModBlocks;
import mcjty.deepresonance.blocks.collector.EnergyCollectorSetup;
import mcjty.deepresonance.blocks.collector.EnergyCollectorTileEntity;
import mcjty.deepresonance.blocks.crystals.ResonatingCrystalTileEntity;
import mcjty.deepresonance.config.ConfigMachines;
import mcjty.lib.container.DefaultSidedInventory;
import mcjty.lib.container.InventoryHelper;
import mcjty.lib.container.InventoryLocator;
import mcjty.lib.entity.GenericTileEntity;
import mcjty.lib.varia.BlockTools;
import mcjty.lib.varia.SoundTools;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.util.FakePlayerFactory;

public class PedestalTileEntity extends GenericTileEntity implements DefaultSidedInventory, ITickable {
    private InventoryHelper inventoryHelper = new InventoryHelper(this, PedestalContainer.factory, 1);

    private int checkCounter = 0;

    // Cache for the inventory used to put the spent crystal material in.
    private InventoryLocator inventoryLocator = new InventoryLocator();

    private BlockPos cachedLocator = null;

    @Override
    public InventoryHelper getInventoryHelper() {
        return inventoryHelper;
    }

    @Override
    protected boolean needsCustomInvWrapper() {
        return true;
    }

    @Override
    public void update() {
        if (!worldObj.isRemote){
            checkStateServer();
        }
    }

    protected void checkStateServer() {
        checkCounter--;
        if (checkCounter > 0) {
            return;
        }
        checkCounter = 20;

        IBlockState state = worldObj.getBlockState(getPos());
        EnumFacing orientation = BlockTools.getOrientation(state.getBlock().getMetaFromState(state));
        BlockPos b = pos.offset(orientation);
        if (worldObj.isAirBlock(b)) {
            // Nothing in front. We can place a new crystal if we have one.
            placeCrystal(b);
        } else if (WorldHelper.getBlockAt(worldObj, b) == ModBlocks.resonatingCrystalBlock) {
            // Check if the crystal in front of us still has power.
            // If not we will remove it.
            checkCrystal(b);
        } // else we can do nothing.
    }

    private void placeCrystal(BlockPos pos) {
        ItemStack crystalStack = inventoryHelper.getStackInSlot(PedestalContainer.SLOT_CRYSTAL);
        if (crystalStack != null && crystalStack.stackSize > 0) {
            if (crystalStack.getItem() instanceof ItemBlock) {
                ItemBlock itemBlock = (ItemBlock) (crystalStack.getItem());
                itemBlock.placeBlockAt(crystalStack, FakePlayerFactory.getMinecraft((WorldServer) worldObj), worldObj, pos, null, 0, 0, 0, itemBlock.getBlock().getStateFromMeta(0));
                inventoryHelper.decrStackSize(PedestalContainer.SLOT_CRYSTAL, 1);
                SoundTools.playSound(worldObj, ModBlocks.resonatingCrystalBlock.getSoundType().breakSound, getPos().getX(), getPos().getY(), getPos().getZ(), 1.0f, 1.0f);

                if (findCollector(pos)) {
                    TileEntity tileEntity = WorldHelper.getTileAt(worldObj, new BlockPos(cachedLocator));
                    if (tileEntity instanceof EnergyCollectorTileEntity) {
                        EnergyCollectorTileEntity energyCollectorTileEntity = (EnergyCollectorTileEntity) tileEntity;
                        energyCollectorTileEntity.addCrystal(pos.getX(), pos.getY(), pos.getZ());
                    }
                }
            }
        }
    }

    private static EnumFacing[] directions = new EnumFacing[] {
            EnumFacing.EAST, EnumFacing.WEST, EnumFacing.NORTH, EnumFacing.SOUTH,
            EnumFacing.UP, EnumFacing.DOWN
    };

    private void checkCrystal(BlockPos p) {
        TileEntity tileEntity = WorldHelper.getTileAt(worldObj, p);
        if (tileEntity instanceof ResonatingCrystalTileEntity) {
            ResonatingCrystalTileEntity resonatingCrystalTileEntity = (ResonatingCrystalTileEntity) tileEntity;
            if (resonatingCrystalTileEntity.getPower() <= EnergyCollectorTileEntity.CRYSTAL_MIN_POWER) {
                ItemStack spentCrystal = new ItemStack(ModBlocks.resonatingCrystalBlock, 1);
                NBTTagCompound tagCompound = new NBTTagCompound();
                resonatingCrystalTileEntity.writeToNBT(tagCompound);
                spentCrystal.setTagCompound(tagCompound);
                inventoryLocator.ejectStack(worldObj, getPos(), spentCrystal, pos, directions);
                worldObj.setBlockToAir(p);
                SoundTools.playSound(worldObj, ModBlocks.resonatingCrystalBlock.getSoundType().breakSound, p.getX(), p.getY(), p.getZ(), 1.0f, 1.0f);
            }
        }
    }

    private boolean findCollector(BlockPos crystalLocation) {
        if (cachedLocator != null) {
            if (WorldHelper.getBlockAt(worldObj, crystalLocation) == EnergyCollectorSetup.energyCollectorBlock) {<commit_msg>Refactor PedestalTileEntity<commit_after>package mcjty.deepresonance.blocks.pedestal;

import elec332.core.world.WorldHelper;
import mcjty.deepresonance.blocks.ModBlocks;
import mcjty.deepresonance.blocks.collector.EnergyCollectorSetup;
import mcjty.deepresonance.blocks.collector.EnergyCollectorTileEntity;
import mcjty.deepresonance.blocks.crystals.ResonatingCrystalTileEntity;
import mcjty.deepresonance.config.ConfigMachines;
import mcjty.lib.container.DefaultSidedInventory;
import mcjty.lib.container.InventoryHelper;
import mcjty.lib.container.InventoryLocator;
import mcjty.lib.entity.GenericTileEntity;
import mcjty.lib.varia.BlockTools;
import mcjty.lib.varia.SoundTools;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.util.FakePlayerFactory;

import java.util.Optional;

public class PedestalTileEntity extends GenericTileEntity implements DefaultSidedInventory, ITickable {
    private InventoryHelper inventoryHelper = new InventoryHelper(this, PedestalContainer.factory, 1);

    private int checkCounter = 0;

    // Cache for the inventory used to put the spent crystal material in.
    private InventoryLocator inventoryLocator = new InventoryLocator();

    private BlockPos cachedLocator = null;

    @Override
    public InventoryHelper getInventoryHelper() {
        return inventoryHelper;
    }

    @Override
    protected boolean needsCustomInvWrapper() {
        return true;
    }

    @Override
    public void update() {
        if (!worldObj.isRemote){
            checkStateServer();
        }
    }

    protected void checkStateServer() {
        checkCounter--;
        if (checkCounter > 0) {
            return;
        }
        checkCounter = 20;

        BlockPos b = this.getCrystalPosition();
        if (worldObj.isAirBlock(b)) {
            // Nothing in front. We can place a new crystal if we have one.
            placeCrystal();
        } else if (WorldHelper.getBlockAt(worldObj, b) == ModBlocks.resonatingCrystalBlock) {
            // Check if the crystal in front of us still has power.
            // If not we will remove it.
            checkCrystal();
        } // else we can do nothing.
    }

    public BlockPos getCrystalPosition() {
        IBlockState state = worldObj.getBlockState(getPos());
        EnumFacing orientation = BlockTools.getOrientation(state.getBlock().getMetaFromState(state));
        return pos.offset(orientation);
    }

    public Optional<ResonatingCrystalTileEntity> getCrystal() {
        BlockPos p = this.getCrystalPosition();
        TileEntity t = WorldHelper.getTileAt(worldObj, p);
        if (t instanceof ResonatingCrystalTileEntity) {
            return Optional.of((ResonatingCrystalTileEntity)t);
        }
        return Optional.empty();
    }

    public boolean crystalPresent() {
        return WorldHelper.getBlockAt(worldObj, this.getCrystalPosition()) == ModBlocks.resonatingCrystalBlock;
    }

    private void placeCrystal() {
        BlockPos pos = getCrystalPosition();
        ItemStack crystalStack = inventoryHelper.getStackInSlot(PedestalContainer.SLOT_CRYSTAL);
        if (crystalStack != null && crystalStack.stackSize > 0) {
            if (crystalStack.getItem() instanceof ItemBlock) {
                ItemBlock itemBlock = (ItemBlock) (crystalStack.getItem());
                itemBlock.placeBlockAt(crystalStack, FakePlayerFactory.getMinecraft((WorldServer) worldObj), worldObj, pos, null, 0, 0, 0, itemBlock.getBlock().getStateFromMeta(0));
                inventoryHelper.decrStackSize(PedestalContainer.SLOT_CRYSTAL, 1);
                SoundTools.playSound(worldObj, ModBlocks.resonatingCrystalBlock.getSoundType().breakSound, getPos().getX(), getPos().getY(), getPos().getZ(), 1.0f, 1.0f);

                if (findCollector()) {
                    TileEntity tileEntity = WorldHelper.getTileAt(worldObj, new BlockPos(cachedLocator));
                    if (tileEntity instanceof EnergyCollectorTileEntity) {
                        EnergyCollectorTileEntity energyCollectorTileEntity = (EnergyCollectorTileEntity) tileEntity;
                        energyCollectorTileEntity.addCrystal(pos.getX(), pos.getY(), pos.getZ());
                    }
                }
            }
        }
    }

    private static EnumFacing[] directions = new EnumFacing[] {
            EnumFacing.EAST, EnumFacing.WEST, EnumFacing.NORTH, EnumFacing.SOUTH,
            EnumFacing.UP, EnumFacing.DOWN
    };

    private void checkCrystal() {
        BlockPos p = getCrystalPosition();
        Optional<Boolean> powerLow = getCrystal().map(tile -> tile.getPower() <= EnergyCollectorTileEntity.CRYSTAL_MIN_POWER);
        if (powerLow.orElse(false)) {
            dropCrystal();
        }
    }

    public void dropCrystal() {
        Optional<ResonatingCrystalTileEntity> crystal = getCrystal();
        if (!crystal.isPresent()) {
            return;
        }
        ResonatingCrystalTileEntity resonatingCrystalTileEntity = crystal.get();
        BlockPos p = resonatingCrystalTileEntity.getPos();
        ItemStack spentCrystal = new ItemStack(ModBlocks.resonatingCrystalBlock, 1);
        NBTTagCompound tagCompound = new NBTTagCompound();
        resonatingCrystalTileEntity.writeToNBT(tagCompound);
        spentCrystal.setTagCompound(tagCompound);
        inventoryLocator.ejectStack(worldObj, getPos(), spentCrystal, pos, directions);
        worldObj.setBlockToAir(p);
        SoundTools.playSound(worldObj, ModBlocks.resonatingCrystalBlock.getSoundType().breakSound, p.getX(), p.getY(), p.getZ(), 1.0f, 1.0f);
    }

    private boolean findCollector() {
        BlockPos crystalLocation = getCrystalPosition();
        if (cachedLocator != null) {
            if (WorldHelper.getBlockAt(worldObj, crystalLocation) == EnergyCollectorSetup.energyCollectorBlock) {
{'fraction_non_alphanumeric': 0.09266110298545986, 'fraction_numerical': 0.0039576701367977285, 'mean_word_length': 3.200939645825804, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5565317', 'n_tokens_mistral': 3451, 'n_tokens_neox': 3261, 'n_words': 637}