<commit_before>package com.raoulvdberge.refinedstorage.integration.forgeenergy;

import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReader;
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReaderWriterChannel;
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReaderWriterHandler;
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IWriter;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.EnergyStorage;
import net.minecraftforge.energy.IEnergyStorage;

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;

public class ReaderWriterHandlerForgeEnergy implements IReaderWriterHandler {
    public static final String ID = "forgeenergy";

    private static final String NBT_ENERGY_STORED = "EnergyStored";

    private EnergyStorage storage;
    private EnergyStorageReaderWriter storageReader, storageWriter;

    public ReaderWriterHandlerForgeEnergy(@Nullable NBTTagCompound tag) {
        this.storage = new EnergyStorage(16000);
        this.storageReader = new EnergyStorageReaderWriter(storage, false, true);
        this.storageWriter = new EnergyStorageReaderWriter(storage, true, false);

        if (tag != null && tag.hasKey(NBT_ENERGY_STORED)) {
            storage.receiveEnergy(tag.getInteger(NBT_ENERGY_STORED), false);
        }
    }

    @Override
    public void update(IReaderWriterChannel channel) {
        if (channel.getWriters().isEmpty()) {
            return;
        }

        int toSend = (int) Math.floor((float) storage.getEnergyStored() / (float) channel.getWriters().size());
        int toExtract = 0;

        for (IWriter writer : channel.getWriters()) {
            TileEntity tile = writer.getWorld().getTileEntity(writer.getPos().offset(writer.getDirection()));

            if (tile != null && tile.hasCapability(CapabilityEnergy.ENERGY, writer.getDirection().getOpposite())) {
                toExtract += tile.getCapability(CapabilityEnergy.ENERGY, writer.getDirection().getOpposite()).receiveEnergy(storage.extractEnergy(toSend, false), false);
            }<commit_msg>Only send energy to active writers<commit_after>package com.raoulvdberge.refinedstorage.integration.forgeenergy;

import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReader;
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReaderWriterChannel;
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IReaderWriterHandler;
import com.raoulvdberge.refinedstorage.api.network.readerwriter.IWriter;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.EnergyStorage;
import net.minecraftforge.energy.IEnergyStorage;

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;

public class ReaderWriterHandlerForgeEnergy implements IReaderWriterHandler {
    public static final String ID = "forgeenergy";

    private static final String NBT_ENERGY_STORED = "EnergyStored";

    private EnergyStorage storage;
    private EnergyStorageReaderWriter storageReader, storageWriter;

    public ReaderWriterHandlerForgeEnergy(@Nullable NBTTagCompound tag) {
        this.storage = new EnergyStorage(16000);
        this.storageReader = new EnergyStorageReaderWriter(storage, false, true);
        this.storageWriter = new EnergyStorageReaderWriter(storage, true, false);

        if (tag != null && tag.hasKey(NBT_ENERGY_STORED)) {
            storage.receiveEnergy(tag.getInteger(NBT_ENERGY_STORED), false);
        }
    }

    @Override
    public void update(IReaderWriterChannel channel) {
        if (channel.getWriters().isEmpty()) {
            return;
        }

        int toSend = (int) Math.floor((float) storage.getEnergyStored() / (float) channel.getWriters().size());
        int toExtract = 0;

        for (IWriter writer : channel.getWriters()) {
            if (writer.canUpdate()) {
                TileEntity tile = writer.getWorld().getTileEntity(writer.getPos().offset(writer.getDirection()));

                if (tile != null && tile.hasCapability(CapabilityEnergy.ENERGY, writer.getDirection().getOpposite())) {
                    toExtract += tile.getCapability(CapabilityEnergy.ENERGY, writer.getDirection().getOpposite()).receiveEnergy(storage.extractEnergy(toSend, false), false);
                }
            }
{'fraction_non_alphanumeric': 0.0994031693764149, 'fraction_numerical': 0.002469643959662482, 'mean_word_length': 4.35832414553473, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3026772', 'n_tokens_mistral': 1416, 'n_tokens_neox': 1345, 'n_words': 235}