<commit_before>package com.InfinityRaider.AgriCraft.tileentity;

import com.InfinityRaider.AgriCraft.container.ContainerSeedAnalyzer;
import com.InfinityRaider.AgriCraft.farming.CropPlantHandler;
import com.InfinityRaider.AgriCraft.items.ItemJournal;
import com.InfinityRaider.AgriCraft.reference.Names;
import com.InfinityRaider.AgriCraft.utility.NBTHelper;
import com.InfinityRaider.AgriCraft.utility.SeedHelper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.util.ForgeDirection;

public class TileEntitySeedAnalyzer extends TileEntityAgricraft implements ISidedInventory {
    public ItemStack seed = null;
    public ItemStack journal = null;
    public int progress = 0;
    public ForgeDirection direction;

    //this saves the data on the tile entity
    @Override
    public void writeToNBT(NBTTagCompound tag) {
        if(this.seed!=null && this.seed.getItem()!=null) {
            NBTTagCompound seedTag = new NBTTagCompound();
            this.seed.writeToNBT(seedTag);
            tag.setTag(Names.Objects.seed, seedTag);
        }
        if(this.journal!=null && this.journal.getItem()!=null) {
            NBTTagCompound journalTag = new NBTTagCompound();
            this.journal.writeToNBT(journalTag);
            tag.setTag(Names.Objects.journal, journalTag);
        }
        if(this.direction!=null) {
            tag.setByte("direction", (byte) this.direction.ordinal());
        }
        tag.setInteger("progress", this.progress);
        super.writeToNBT(tag);
    }

    //this loads the saved data for the tile entity
    @Override
    public void readFromNBT(NBTTagCompound tag) {
        if(tag.hasKey(Names.Objects.seed)) {
            this.seed = ItemStack.loadItemStackFromNBT(tag.getCompoundTag(Names.Objects.seed));
        }
        else {
            this.seed = null;
        }
        if(tag.hasKey(Names.Objects.journal)) {
            this.journal = ItemStack.loadItemStackFromNBT(tag.getCompoundTag(Names.Objects.journal));
        }
        else {
            this.journal = null;
        }
        if(tag.hasKey("direction")) {
            this.setDirection(tag.getByte("direction"));
        }
        this.progress = tag.getInteger("progress");
        super.readFromNBT(tag);
    }

    //sets the direction based on an int
    public void setDirection(int direction) {
        this.direction = ForgeDirection.getOrientation(direction);
    }

    //returns the seed currently being processed
    public boolean hasSeed() {
        return CropPlantHandler.isValidSeed(this.seed);
    }

    //calculates the number of ticks it takes to analyze the seed
    public int maxProgress() {
        return seed==null?0: CropPlantHandler.getPlantFromStack(seed).getTier()*20;
    }

    public static boolean isValid(ItemStack stack) {
        if (!CropPlantHandler.isValidSeed(stack)) {
            return false;
        }
        return true;
    }

    //gets called every tick
    @Override
    public void updateEntity() {
        boolean change = false;
        if(this.isAnalyzing()) {
            //increment progress counter
            this.progress=progress<this.maxProgress()?progress+1:this.maxProgress();
            //if progress is complete analyze the seed
            if(progress == this.maxProgress() && !worldObj.isRemote) {
                this.analyze();
                change = true;
            }
        }
        if(change) {
            this.markForUpdate();
            this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.worldObj.getBlock(this.xCoord, this.yCoord, this.zCoord), 0, 0);
            this.worldObj.notifyBlockChange(this.xCoord, this.yCoord, this.zCoord, this.getBlockType());
        }
    }

    //analyzes the current seed
    public void analyze() {
        //analyze the seed
        if(this.seed.hasTagCompound()) {
            NBTTagCompound tag = this.seed.getTagCompound();
            if(tag.hasKey(Names.NBT.growth) && tag.hasKey(Names.NBT.gain) && tag.hasKey(Names.NBT.strength)) {
                tag.setBoolean(Names.NBT.analyzed, true);
            } else {
                SeedHelper.setNBT(tag, (short) 0, (short) 0, (short) 0, true);
            }
        }
        else {
            this.seed.setTagCompound(new NBTTagCompound());
            SeedHelper.setNBT(this.seed.stackTagCompound, (short) 0, (short) 0, (short) 0, true);
        }
        //register the seed in the journal if there is a journal present
        if(this.hasJournal()) {
            //check if the journal has NBT and if it doesn't, create a new one
            if(!this.journal.hasTagCompound()) {
                this.journal.setTagCompound(new NBTTagCompound());
            }
            NBTTagCompound tag = this.journal.stackTagCompound;
            //check if the NBT tag has a list of discovered seeds and if it doesn't, create a new one
            NBTTagList list;
            if(tag.hasKey(Names.NBT.discoveredSeeds)) {
                list = tag.getTagList(Names.NBT.discoveredSeeds, 10);
            }
            else {
                list = new NBTTagList();
            }
            //add the analyzed seed to the NBT tag list if it doesn't already have it
            if(!NBTHelper.listContainsStack(list, this.seed)) {
                NBTTagCompound seedTag = new NBTTagCompound();
                ItemStack write = this.seed.copy();
                write.stackSize = 1;
                write.stackTagCompound = null;
                write.writeToNBT(seedTag);
                list.appendTag(seedTag);
            }<commit_msg>Update TileEntitySeedAnalyzer.java<commit_after>package com.InfinityRaider.AgriCraft.tileentity;

import com.InfinityRaider.AgriCraft.container.ContainerSeedAnalyzer;
import com.InfinityRaider.AgriCraft.farming.CropPlantHandler;
import com.InfinityRaider.AgriCraft.items.ItemJournal;
import com.InfinityRaider.AgriCraft.reference.Names;
import com.InfinityRaider.AgriCraft.utility.NBTHelper;
import com.InfinityRaider.AgriCraft.utility.SeedHelper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.util.ForgeDirection;

public class TileEntitySeedAnalyzer extends TileEntityAgricraft implements ISidedInventory {
    public ItemStack seed = null;
    public ItemStack journal = null;
    public int progress = 0;
    public ForgeDirection direction;

    //this saves the data on the tile entity
    @Override
    public void writeToNBT(NBTTagCompound tag) {
        if(this.seed!=null && this.seed.getItem()!=null) {
            NBTTagCompound seedTag = new NBTTagCompound();
            this.seed.writeToNBT(seedTag);
            tag.setTag(Names.Objects.seed, seedTag);
        }
        if(this.journal!=null && this.journal.getItem()!=null) {
            NBTTagCompound journalTag = new NBTTagCompound();
            this.journal.writeToNBT(journalTag);
            tag.setTag(Names.Objects.journal, journalTag);
        }
        if(this.direction!=null) {
            tag.setByte("direction", (byte) this.direction.ordinal());
        }
        tag.setInteger("progress", this.progress);
        super.writeToNBT(tag);
    }

    //this loads the saved data for the tile entity
    @Override
    public void readFromNBT(NBTTagCompound tag) {
        if(tag.hasKey(Names.Objects.seed)) {
            this.seed = ItemStack.loadItemStackFromNBT(tag.getCompoundTag(Names.Objects.seed));
        }
        else {
            this.seed = null;
        }
        if(tag.hasKey(Names.Objects.journal)) {
            this.journal = ItemStack.loadItemStackFromNBT(tag.getCompoundTag(Names.Objects.journal));
        }
        else {
            this.journal = null;
        }
        if(tag.hasKey("direction")) {
            this.setDirection(tag.getByte("direction"));
        }
        this.progress = tag.getInteger("progress");
        super.readFromNBT(tag);
    }

    //sets the direction based on an int
    public void setDirection(int direction) {
        this.direction = ForgeDirection.getOrientation(direction);
    }

    //returns the seed currently being processed
    public boolean hasSeed() {
        return CropPlantHandler.isValidSeed(this.seed);
    }

    //calculates the number of ticks it takes to analyze the seed
    public int maxProgress() {
        return seed==null?0: CropPlantHandler.getPlantFromStack(seed).getTier()*20;
    }

    public static boolean isValid(ItemStack stack) {
        if (!CropPlantHandler.isValidSeed(stack)) {
            return false;
        }
        return true;
    }

    //gets called every tick
    @Override
    public void updateEntity() {
        boolean change = false;
        if(this.isAnalyzing()) {
            //increment progress counter
            this.progress=progress<this.maxProgress()?progress+1:this.maxProgress();
            //if progress is complete analyze the seed
            if(progress == this.maxProgress() && !worldObj.isRemote) {
                this.analyze();
                change = true;
            }
        }
        if(change) {
            this.markForUpdate();
            this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.worldObj.getBlock(this.xCoord, this.yCoord, this.zCoord), 0, 0);
            this.worldObj.notifyBlockChange(this.xCoord, this.yCoord, this.zCoord, this.getBlockType());
        }
    }

    //analyzes the current seed
    public void analyze() {
        //analyze the seed
        if(this.seed.hasTagCompound()) {
            NBTTagCompound tag = this.seed.getTagCompound();
            if(tag.hasKey(Names.NBT.growth) && tag.hasKey(Names.NBT.gain) && tag.hasKey(Names.NBT.strength)) {
                tag.setBoolean(Names.NBT.analyzed, true);
            } else {
                SeedHelper.setNBT(tag, (short) 0, (short) 0, (short) 0, true);
            }
        }
        else {
            this.seed.setTagCompound(new NBTTagCompound());
            SeedHelper.setNBT(this.seed.stackTagCompound, (short) 0, (short) 0, (short) 0, true);
        }
        //register the seed in the journal if there is a journal present
        if(this.hasJournal()) {
            //check if the journal has NBT and if it doesn't, create a new one
            if(!this.journal.hasTagCompound()) {
                this.journal.setTagCompound(new NBTTagCompound());
            }
            NBTTagCompound tag = this.journal.stackTagCompound;
            //check if the NBT tag has a list of discovered seeds and if it doesn't, create a new one
            NBTTagList list;
            if(tag.hasKey(Names.NBT.discoveredSeeds)) {
                list = tag.getTagList(Names.NBT.discoveredSeeds, 10);
                NBTHelper.clearEmptyStacksFromNBT(list);
            }
            else {
                list = new NBTTagList();
            }
            //add the analyzed seed to the NBT tag list if it doesn't already have it
            if(!NBTHelper.listContainsStack(list, this.seed)) {
                NBTTagCompound seedTag = new NBTTagCompound();
                ItemStack write = this.seed.copy();
                write.stackSize = 1;
                write.stackTagCompound = null;
                write.writeToNBT(seedTag);
                list.appendTag(seedTag);
            }
{'fraction_non_alphanumeric': 0.10401175860280132, 'fraction_numerical': 0.0027667300708974583, 'mean_word_length': 2.303913167666381, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4048259', 'n_tokens_mistral': 3457, 'n_tokens_neox': 3367, 'n_words': 688}