<commit_before>﻿using System.Linq;
using StardewValley;
using StardewValley.Objects;
using Object = StardewValley.Object;

namespace FarmAutomation.ItemCollector.Processors
{
    public class MaterialHelper
    {
        private readonly int[] _ores = {
            Object.copper,
            Object.iron,
            Object.gold,
            Object.iridium
        };

        public Object FindMaterialForMachine(string machineName, Chest chest)
        {
            if (chest == null)
            {
                return null;
            }

            switch (machineName)
            {
                case "Keg":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsKegMaterial(i));
                case "Preserves Jar":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsPreservesJarMaterial(i));
                case "Cheese Press":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsCheesePressMaterial(i));
                case "Mayonnaise Machine":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsMayonnaiseMachineMaterial(i));
                case "Loom":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsLoomMaterial(i));
                case "Oil Maker":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsOilMakerMaterial(i));
                case "Recycling Machine":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsRecyclingMachineMaterial(i));
                case "Furnace":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsFurnaceMaterial(i));
                case "Coal":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && i.parentSheetIndex == Object.coal);
                case "Seed Maker":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsSeedMakerMaterial(i));
                case "Crab Pot":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsCrabPotMaterial(i));
                case "Charcoal Kiln":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && i.parentSheetIndex == Object.wood);
                case "Slime Egg-Press":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && i.Name == "Slime");
                default:
                    return null;
            }
        }

        private bool IsCrabPotMaterial(Item item)
        {
            return item.category == Object.baitCategory;
        }

        private bool IsSeedMakerMaterial(Item item)
        {
            return item.category == Object.VegetableCategory 
                || item.category == Object.GreensCategory 
                || item.category == Object.FruitsCategory
                || item.category == Object.flowersCategory;
        }

        /// <summary>
        /// most machines only take 1 object, a few exceptions have to be handled though
        /// </summary>
        /// <param name="machineName"></param>
        /// <param name="material"></param><commit_msg>Seedmaker now reads the games crop data to determine dropin items. any mod adding new crops should be supported, as long as they use the games system properly.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework.Content;
using StardewValley;
using StardewValley.Objects;
using Object = StardewValley.Object;

namespace FarmAutomation.ItemCollector.Processors
{
    public class MaterialHelper
    {
        private readonly int[] _ores = {
            Object.copper,
            Object.iron,
            Object.gold,
            Object.iridium
        };

        private int[] _seedmakerDropIns;

        public Object FindMaterialForMachine(string machineName, Chest chest)
        {
            if (chest == null)
            {
                return null;
            }

            switch (machineName)
            {
                case "Keg":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsKegMaterial(i));
                case "Preserves Jar":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsPreservesJarMaterial(i));
                case "Cheese Press":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsCheesePressMaterial(i));
                case "Mayonnaise Machine":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsMayonnaiseMachineMaterial(i));
                case "Loom":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsLoomMaterial(i));
                case "Oil Maker":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsOilMakerMaterial(i));
                case "Recycling Machine":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsRecyclingMachineMaterial(i));
                case "Furnace":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsFurnaceMaterial(i));
                case "Coal":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && i.parentSheetIndex == Object.coal);
                case "Seed Maker":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsSeedMakerMaterial(i));
                case "Crab Pot":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && IsCrabPotMaterial(i));
                case "Charcoal Kiln":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && i.parentSheetIndex == Object.wood);
                case "Slime Egg-Press":
                    return (Object)chest.items.FirstOrDefault(i => i is Object && i.Name == "Slime");
                default:
                    return null;
            }
        }

        private bool IsCrabPotMaterial(Item item)
        {
            return item.category == Object.baitCategory;
        }

        private bool IsSeedMakerMaterial(Item item)
        {
            if (_seedmakerDropIns == null)
            {
                if (Game1.temporaryContent == null)
                {
                    Game1.temporaryContent = new ContentManager(Game1.content.ServiceProvider, Game1.content.RootDirectory);
                }
                Dictionary<int, string> dictionary = Game1.temporaryContent.Load<Dictionary<int, string>>("Data\\Crops");
                _seedmakerDropIns = dictionary.Values.Select(v => v.Split('/')).Select(s => Convert.ToInt32(s[3])).ToArray();
            }
            return _seedmakerDropIns.Contains(item.parentSheetIndex);
        }

        /// <summary>
        /// most machines only take 1 object, a few exceptions have to be handled though
        /// </summary>
        /// <param name="machineName"></param>
        /// <param name="material"></param>
{'fraction_non_alphanumeric': 0.1038961038961039, 'fraction_numerical': 0.001411631846414455, 'mean_word_length': 1.8115079365079365, 'pattern_counts': {'":': 26, '<': 18, '<?xml version=': 0, '>': 46, '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': '4040722', 'n_tokens_mistral': 1902, 'n_tokens_neox': 1801, 'n_words': 435}