<commit_before>package mcjty.rftoolsdim.blocks.energyextractor;

import mcjty.lib.McJtyLib;
import mcjty.lib.compat.RedstoneFluxCompatibility;
import mcjty.lib.tileentity.GenericEnergyStorageTileEntity;
import mcjty.lib.varia.EnergyTools;
import mcjty.rftoolsdim.config.MachineConfiguration;
import mcjty.rftoolsdim.dimensions.DimensionStorage;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.energy.CapabilityEnergy;

public class EnergyExtractorTileEntity extends GenericEnergyStorageTileEntity implements ITickable {

    public EnergyExtractorTileEntity() {
        super(MachineConfiguration.EXTRACTOR_MAXENERGY, MachineConfiguration.EXTRACTOR_SENDPERTICK);
    }

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

    private void checkStateServer() {
        int energyStored = getEnergyStored();

        if (energyStored < MachineConfiguration.EXTRACTOR_MAXENERGY) {
            // Get energy out of the dimension.
            DimensionStorage storage = DimensionStorage.getDimensionStorage(getWorld());
            int dimensionEnergy = storage.getEnergyLevel(getWorld().provider.getDimension());
            int needed = MachineConfiguration.EXTRACTOR_MAXENERGY - energyStored;
            if (needed > dimensionEnergy) {
                needed = dimensionEnergy;
            }

            if (needed > 0) {
                energyStored += needed;
                dimensionEnergy -= needed;
                modifyEnergyStored(needed);

                storage.setEnergyLevel(getWorld().provider.getDimension(), dimensionEnergy);
                storage.save();
            }
        }

        if (energyStored <= 0) {
            return;
        }

        int rf = MachineConfiguration.EXTRACTOR_SENDPERTICK;

        for (EnumFacing facing : EnumFacing.values()) {
            BlockPos pos = getPos().offset(facing);
            TileEntity te = getWorld().getTileEntity(pos);
            EnumFacing opposite = facing.getOpposite();
            if (EnergyTools.isEnergyTE(te, opposite) || (te != null && te.hasCapability(CapabilityEnergy.ENERGY, opposite))) {
                int rfToGive = rf <= energyStored ? rf : energyStored;
                int received;

                if (McJtyLib.redstoneflux && RedstoneFluxCompatibility.isEnergyConnection(te)) {
                    if (RedstoneFluxCompatibility.canConnectEnergy(te, opposite)) {
                        received = (int) EnergyTools.receiveEnergy(te, opposite, rfToGive);
                    } else {
                        received = 0;
                    }
                } else {
                    // Forge unit
                    received = (int) EnergyTools.receiveEnergy(te, opposite, rfToGive);
                }
                energyStored -= storage.extractEnergy(received, false);
                if (energyStored <= 0) {
                    break;
                }
            }
        }
    }
}<commit_msg>Simplify energy extractor<commit_after>package mcjty.rftoolsdim.blocks.energyextractor;

import mcjty.lib.tileentity.GenericEnergyStorageTileEntity;
import mcjty.lib.varia.EnergyTools;
import mcjty.rftoolsdim.config.MachineConfiguration;
import mcjty.rftoolsdim.dimensions.DimensionStorage;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;

public class EnergyExtractorTileEntity extends GenericEnergyStorageTileEntity implements ITickable {

    public EnergyExtractorTileEntity() {
        super(MachineConfiguration.EXTRACTOR_MAXENERGY, MachineConfiguration.EXTRACTOR_SENDPERTICK);
    }

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

    private void checkStateServer() {
        int energyStored = getEnergyStored();

        if (energyStored < MachineConfiguration.EXTRACTOR_MAXENERGY) {
            // Get energy out of the dimension.
            DimensionStorage storage = DimensionStorage.getDimensionStorage(getWorld());
            int dimensionEnergy = storage.getEnergyLevel(getWorld().provider.getDimension());
            int needed = Math.min(MachineConfiguration.EXTRACTOR_MAXENERGY - energyStored, dimensionEnergy);

            if (needed > 0) {
                energyStored += needed;
                dimensionEnergy -= needed;
                modifyEnergyStored(needed);

                storage.setEnergyLevel(getWorld().provider.getDimension(), dimensionEnergy);
                storage.save();
            }
        }

        if (energyStored <= 0) {
            return;
        }

        for (EnumFacing facing : EnumFacing.values()) {
            BlockPos pos = getPos().offset(facing);
            TileEntity te = getWorld().getTileEntity(pos);
            EnumFacing opposite = facing.getOpposite();
            if (EnergyTools.isEnergyTE(te, opposite)) {
                int rfToGive = Math.min(MachineConfiguration.EXTRACTOR_SENDPERTICK, energyStored);
                int received = (int) EnergyTools.receiveEnergy(te, opposite, rfToGive);
                energyStored -= storage.extractEnergy(received, false);
                if (energyStored <= 0) {
                    break;
                }
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08938036921952111, 'fraction_numerical': 0.0012794735880095047, 'mean_word_length': 2.3264437689969606, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 6, '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': '2565701', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1421, 'n_words': 259}