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

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import mcjty.deepresonance.blocks.ModBlocks;
import mcjty.deepresonance.blocks.crystals.ResonatingCrystalTileEntity;
import mcjty.deepresonance.blocks.generator.GeneratorConfiguration;
import mcjty.deepresonance.blocks.generator.GeneratorTileEntity;
import mcjty.deepresonance.config.ConfigMachines;
import mcjty.deepresonance.generatornetwork.DRGeneratorNetwork;
import mcjty.deepresonance.radiation.DRRadiationManager;
import mcjty.deepresonance.varia.Broadcaster;
import mcjty.lib.entity.GenericTileEntity;
import mcjty.lib.varia.Coordinate;
import mcjty.lib.varia.GlobalCoordinate;
import mcjty.lib.varia.Logging;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;

import java.util.HashSet;
import java.util.Set;

public class EnergyCollectorTileEntity extends GenericTileEntity {

    // Minimum power before we stop using a crystal.
    public static final float CRYSTAL_MIN_POWER = .00001f;

    public static int MAXTICKS = 20;        // Update radiation every second.

    // Relative coordinates of active crystals.
    private Set<Coordinate> crystals = new HashSet<Coordinate>();
    private boolean lasersActive = false;
    private int laserStartup = 0;        // A mirror (for the client) of the network startup counter.
    private int radiationUpdateCount = MAXTICKS;
    private int networkID = -1;

    public EnergyCollectorTileEntity() {
        super();
    }

    @Override
    protected void checkStateServer() {
        boolean active = false;
        int startup = 0;
        DRGeneratorNetwork.Network network = null;

        TileEntity te = worldObj.getTileEntity(xCoord, yCoord-1, zCoord);
        if (te instanceof GeneratorTileEntity) {
            GeneratorTileEntity generatorTileEntity = (GeneratorTileEntity) te;
            DRGeneratorNetwork generatorNetwork = DRGeneratorNetwork.getChannels(worldObj);

            if (networkID != generatorTileEntity.getNetworkId()) {
                if (networkID != -1) {
                    generatorNetwork.getOrCreateNetwork(networkID).decCollectorBlocks();
                }

                networkID = generatorTileEntity.getNetworkId();
                generatorTileEntity.getNetwork().incCollectorBlocks();
                generatorNetwork.save(worldObj);
            }


            network = generatorTileEntity.getNetwork();
            if (network != null) {
                if (network.isActive()) {
                    int rfPerTick = calculateRF();
                    network.setLastRfPerTick(rfPerTick);
                    int newEnergy = network.getEnergy() + rfPerTick;
                    int maxEnergy = network.getGeneratorBlocks() * GeneratorConfiguration.rfPerGeneratorBlock;
                    if (newEnergy > maxEnergy) {
                        newEnergy = maxEnergy;
                    }
                    if (network.getEnergy() != newEnergy) {
                        network.setEnergy(newEnergy);
                        generatorNetwork.save(worldObj);
                    }

                    active = true;
                } else {
                    network.setLastRfPerTick(0);
                }
                startup = network.getStartupCounter();
            }
        } else {
            if (networkID != -1) {
                networkID = -1;
                markDirty();
            }
        }

        if (active != lasersActive || startup != laserStartup) {
            // Only when active changes and the lasert started recently do we check for new crystals.
            boolean doFind = lasersActive != active || (laserStartup > (GeneratorConfiguration.startupTime - 5));
            lasersActive = active;
            laserStartup = startup;
            markDirty();
            worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);

            if (doFind) {
                findCrystals(network);
            }
        }
    }

    public void disableCrystalGlow() {
        for (Coordinate coordinate : crystals) {
            TileEntity te = worldObj.getTileEntity(coordinate.getX() + xCoord, coordinate.getY() + yCoord, coordinate.getZ() + zCoord);
            if (te instanceof ResonatingCrystalTileEntity) {
                ResonatingCrystalTileEntity resonatingCrystalTileEntity = (ResonatingCrystalTileEntity) te;
                resonatingCrystalTileEntity.setGlowing(false);
            }
        }
    }<commit_msg>Fixed a bug where breaking an active generator with an energy collector on top would cause a crash<commit_after>package mcjty.deepresonance.blocks.collector;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import mcjty.deepresonance.blocks.ModBlocks;
import mcjty.deepresonance.blocks.crystals.ResonatingCrystalTileEntity;
import mcjty.deepresonance.blocks.generator.GeneratorConfiguration;
import mcjty.deepresonance.blocks.generator.GeneratorTileEntity;
import mcjty.deepresonance.config.ConfigMachines;
import mcjty.deepresonance.generatornetwork.DRGeneratorNetwork;
import mcjty.deepresonance.radiation.DRRadiationManager;
import mcjty.deepresonance.varia.Broadcaster;
import mcjty.lib.entity.GenericTileEntity;
import mcjty.lib.varia.Coordinate;
import mcjty.lib.varia.GlobalCoordinate;
import mcjty.lib.varia.Logging;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;

import java.util.HashSet;
import java.util.Set;

public class EnergyCollectorTileEntity extends GenericTileEntity {

    // Minimum power before we stop using a crystal.
    public static final float CRYSTAL_MIN_POWER = .00001f;

    public static int MAXTICKS = 20;        // Update radiation every second.

    // Relative coordinates of active crystals.
    private Set<Coordinate> crystals = new HashSet<Coordinate>();
    private boolean lasersActive = false;
    private int laserStartup = 0;        // A mirror (for the client) of the network startup counter.
    private int radiationUpdateCount = MAXTICKS;
    private int networkID = -1;

    public EnergyCollectorTileEntity() {
        super();
    }

    @Override
    protected void checkStateServer() {
        boolean active = false;
        int startup = 0;
        DRGeneratorNetwork.Network network = null;

        TileEntity te = worldObj.getTileEntity(xCoord, yCoord-1, zCoord);
        if (te instanceof GeneratorTileEntity) {
            GeneratorTileEntity generatorTileEntity = (GeneratorTileEntity) te;
            DRGeneratorNetwork generatorNetwork = DRGeneratorNetwork.getChannels(worldObj);

            if (networkID != generatorTileEntity.getNetworkId()) {
                if (networkID != -1) {
                    generatorNetwork.getOrCreateNetwork(networkID).decCollectorBlocks();
                }

                networkID = generatorTileEntity.getNetworkId();
                generatorTileEntity.getNetwork().incCollectorBlocks();
                generatorNetwork.save(worldObj);
            }


            network = generatorTileEntity.getNetwork();
            if (network != null) {
                if (network.isActive()) {
                    int rfPerTick = calculateRF();
                    network.setLastRfPerTick(rfPerTick);
                    int newEnergy = network.getEnergy() + rfPerTick;
                    int maxEnergy = network.getGeneratorBlocks() * GeneratorConfiguration.rfPerGeneratorBlock;
                    if (newEnergy > maxEnergy) {
                        newEnergy = maxEnergy;
                    }
                    if (network.getEnergy() != newEnergy) {
                        network.setEnergy(newEnergy);
                        generatorNetwork.save(worldObj);
                    }

                    active = true;
                } else {
                    network.setLastRfPerTick(0);
                }
                startup = network.getStartupCounter();
            }
        } else {
            if (networkID != -1) {
                networkID = -1;
                markDirty();
            }
        }

        if (active != lasersActive || startup != laserStartup) {
            // Only when active changes and the lasert started recently do we check for new crystals.
            boolean doFind = lasersActive != active || (laserStartup > (GeneratorConfiguration.startupTime - 5));
            lasersActive = active;
            laserStartup = startup;
            markDirty();
            worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);

            if (doFind && te instanceof GeneratorTileEntity) {
                findCrystals(network);
            }
        }
    }

    public void disableCrystalGlow() {
        for (Coordinate coordinate : crystals) {
            TileEntity te = worldObj.getTileEntity(coordinate.getX() + xCoord, coordinate.getY() + yCoord, coordinate.getZ() + zCoord);
            if (te instanceof ResonatingCrystalTileEntity) {
                ResonatingCrystalTileEntity resonatingCrystalTileEntity = (ResonatingCrystalTileEntity) te;
                resonatingCrystalTileEntity.setGlowing(false);
            }
        }
    }
{'fraction_non_alphanumeric': 0.08196721311475409, 'fraction_numerical': 0.003451251078515962, 'mean_word_length': 2.295309168443497, 'pattern_counts': {'":': 0, '<': 7, '<?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': '537239', 'n_tokens_mistral': 2574, 'n_tokens_neox': 2363, 'n_words': 522}