<commit_before>package org.cyclops.integratedtunnels.part;

import net.minecraft.block.Block;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.energy.CapabilityEnergy;
import org.cyclops.cyclopscore.helper.TileHelpers;
import org.cyclops.integrateddynamics.api.network.IEnergyNetwork;
import org.cyclops.integrateddynamics.api.network.INetwork;
import org.cyclops.integrateddynamics.api.network.IPartNetwork;
import org.cyclops.integrateddynamics.api.part.PartPos;
import org.cyclops.integrateddynamics.api.part.PartTarget;
import org.cyclops.integrateddynamics.core.part.PartStateEmpty;
import org.cyclops.integratedtunnels.Capabilities;
import org.cyclops.integratedtunnels.core.part.PartTypeTunnel;

/**
 * @author rubensworks
 */
public class PartTypeInterfaceEnergy extends PartTypeTunnel<PartTypeInterfaceEnergy, PartStateEmpty<PartTypeInterfaceEnergy>> {
    public PartTypeInterfaceEnergy(String name) {
        super(name);
    }

    @Override
    protected PartStateEmpty<PartTypeInterfaceEnergy> constructDefaultState() {
        return new PartStateEmpty<PartTypeInterfaceEnergy>();
    }

    @Override
    public void afterNetworkReAlive(INetwork network, IPartNetwork partNetwork, PartTarget target, PartStateEmpty<PartTypeInterfaceEnergy> state) {
        super.afterNetworkReAlive(network, partNetwork, target, state);
        addToNetwork(network, target.getTarget());
    }

    @Override
    public void onNetworkRemoval(INetwork network, IPartNetwork partNetwork, PartTarget target, PartStateEmpty<PartTypeInterfaceEnergy> state) {
        super.onNetworkRemoval(network, partNetwork, target, state);
        removeFromNetwork(network, target.getTarget());
    }

    @Override
    public void onBlockNeighborChange(INetwork network, IPartNetwork partNetwork, PartTarget target, PartStateEmpty<PartTypeInterfaceEnergy> state, IBlockAccess world, Block neighborBlock) {
        super.onBlockNeighborChange(network, partNetwork, target, state, world, neighborBlock);
        removeFromNetwork(network, target.getTarget());
        addToNetwork(network, target.getTarget());
    }

    protected void addToNetwork(INetwork network, PartPos pos) {
        if (network.hasCapability(Capabilities.NETWORK_ENERGY)
                && TileHelpers.getCapability(pos.getPos(), CapabilityEnergy.ENERGY) != null) {
            IEnergyNetwork energyNetwork = network.getCapability(Capabilities.NETWORK_ENERGY);
            energyNetwork.addEnergyBattery(pos);
        }
    }

    protected void removeFromNetwork(INetwork network, PartPos pos) {
        if (network.hasCapability(Capabilities.NETWORK_ENERGY)) {
            IEnergyNetwork energyNetwork = network.getCapability(Capabilities.NETWORK_ENERGY);
            energyNetwork.removeEnergyBattery(pos);
        }
    }
}<commit_msg>Only allow energy interfacing with extractable and receivables<commit_after>package org.cyclops.integratedtunnels.part;

import net.minecraft.block.Block;
import net.minecraft.world.IBlockAccess;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.IEnergyStorage;
import org.cyclops.cyclopscore.helper.TileHelpers;
import org.cyclops.integrateddynamics.api.network.IEnergyNetwork;
import org.cyclops.integrateddynamics.api.network.INetwork;
import org.cyclops.integrateddynamics.api.network.IPartNetwork;
import org.cyclops.integrateddynamics.api.part.PartPos;
import org.cyclops.integrateddynamics.api.part.PartTarget;
import org.cyclops.integrateddynamics.core.part.PartStateEmpty;
import org.cyclops.integratedtunnels.Capabilities;
import org.cyclops.integratedtunnels.core.part.PartTypeTunnel;

/**
 * @author rubensworks
 */
public class PartTypeInterfaceEnergy extends PartTypeTunnel<PartTypeInterfaceEnergy, PartStateEmpty<PartTypeInterfaceEnergy>> {
    public PartTypeInterfaceEnergy(String name) {
        super(name);
    }

    @Override
    protected PartStateEmpty<PartTypeInterfaceEnergy> constructDefaultState() {
        return new PartStateEmpty<PartTypeInterfaceEnergy>();
    }

    @Override
    public void afterNetworkReAlive(INetwork network, IPartNetwork partNetwork, PartTarget target, PartStateEmpty<PartTypeInterfaceEnergy> state) {
        super.afterNetworkReAlive(network, partNetwork, target, state);
        addToNetwork(network, target.getTarget());
    }

    @Override
    public void onNetworkRemoval(INetwork network, IPartNetwork partNetwork, PartTarget target, PartStateEmpty<PartTypeInterfaceEnergy> state) {
        super.onNetworkRemoval(network, partNetwork, target, state);
        removeFromNetwork(network, target.getTarget());
    }

    @Override
    public void onBlockNeighborChange(INetwork network, IPartNetwork partNetwork, PartTarget target, PartStateEmpty<PartTypeInterfaceEnergy> state, IBlockAccess world, Block neighborBlock) {
        super.onBlockNeighborChange(network, partNetwork, target, state, world, neighborBlock);
        removeFromNetwork(network, target.getTarget());
        addToNetwork(network, target.getTarget());
    }

    protected void addToNetwork(INetwork network, PartPos pos) {
        if (network.hasCapability(Capabilities.NETWORK_ENERGY)) {
            IEnergyStorage energyStorage = TileHelpers.getCapability(pos.getPos(), CapabilityEnergy.ENERGY);
            if (energyStorage != null && energyStorage.canExtract() && energyStorage.canReceive()) {
                IEnergyNetwork energyNetwork = network.getCapability(Capabilities.NETWORK_ENERGY);
                energyNetwork.addEnergyBattery(pos);
            }
        }
    }

    protected void removeFromNetwork(INetwork network, PartPos pos) {
        if (network.hasCapability(Capabilities.NETWORK_ENERGY)) {
            IEnergyNetwork energyNetwork = network.getCapability(Capabilities.NETWORK_ENERGY);
            energyNetwork.removeEnergyBattery(pos);
        }
    }
}
{'fraction_non_alphanumeric': 0.08623040218132243, 'fraction_numerical': 0.0, 'mean_word_length': 4.759568204121688, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '985290', 'n_tokens_mistral': 1653, 'n_tokens_neox': 1565, 'n_words': 277}