private static void AppendObjectiveTick(ScoreEventHandler h, MCFunction func)
 {
     Namespace.TickFunction.AddScoreTick(new KeyValuePair <ScoreEventHandler, MCFunction>(h, func));
 }
 private static void AppendObjectiveCreation(ScoreEventHandler h, MCFunction func)
 {
     Namespace.LoadFunction.AddScoreCreation(new KeyValuePair <ScoreEventHandler, MCFunction>(h, func));
 }
        public static void Compile(object instance, MethodInfo m, string path)
        {
            Console.WriteLine("Compiling method " + m.DeclaringType + " " + m);
            Expand e = m.GetCustomAttribute <Expand>();

            if (e != null)
            {
                foreach (string s in e.All)
                {
                    Function = new MCFunction(new ResourceLocation(Namespace, path + Utils.LowerCase(m.Name) + "/" + s));
                    if (m.GetParameters().Length == 1 && m.GetParameters()[0].ParameterType == typeof(string))
                    {
                        m.Invoke(null, new object[] { s });
                    }
                    else
                    {
                        m.Invoke(null, null);
                    }

                    Function.WriteFile();
                    Execute.Clear();
                }
                return;
            }
            ResourceLocation id = new ResourceLocation(Namespace, path + Utils.LowerCase(m.Name));

            Function = new MCFunction(id);
            if (m.GetCustomAttribute <Tick>() != null)
            {
                if (Namespace.TickFunction == null)
                {
                    Namespace.Datapack.CreateTickTag(id);
                    Namespace.TickFunction = Function;
                }
                Function = Namespace.TickFunction;

                foreach (KeyValuePair <ScoreEventHandler, MCFunction> h in Namespace.PendingScoreHandlers)
                {
                    Function.AddScoreTick(h);
                }
            }
            if (m.GetCustomAttribute <Load>() != null)
            {
                if (Namespace.LoadFunction == null)
                {
                    Namespace.Datapack.CreateLoadTag(id);
                    Namespace.LoadFunction = Function;
                }
                Function = Namespace.LoadFunction;


                foreach (KeyValuePair <ScoreEventHandler, MCFunction> h in Namespace.PendingScoreHandlers)
                {
                    Function.AddScoreCreation(h);
                }
            }
            ScoreEventHandler scoreHandler = m.GetCustomAttribute <ScoreEventHandler>();
            ScoreTrigger      trigger      = m.GetCustomAttribute <ScoreTrigger>();

            if (trigger != null)
            {
                scoreHandler = new ScoreEventHandler(trigger.Objective ?? Utils.LowerCase(m.Name), "trigger", trigger.Range);
            }
            if (scoreHandler != null)
            {
                if (Namespace.LoadFunction == null)
                {
                    Namespace.PendingScoreHandlers.Add(scoreHandler, Function);
                }
                else
                {
                    AppendObjectiveCreation(scoreHandler, Function);
                }

                if (Namespace.TickFunction == null)
                {
                    Namespace.PendingScoreHandlers.Add(scoreHandler, Function);
                }
                else
                {
                    AppendObjectiveTick(scoreHandler, Function);
                }
            }


            Desc d = m.GetCustomAttribute <Desc>();

            Write("#################################################");
            Write($"# Created on {DateTime.Now.ToShortDateString()}");
            if (d != null)
            {
                if (d.Caller != null)
                {
                    Write("# Called by: " + d.Caller);
                }
                Write("# Description: " + d.Value);
            }
            Write("#################################################");
            Write("");

            object returnValue = m.Invoke(instance, null);

            GiveItem give = m.GetCustomAttribute <GiveItem>();

            if (give != null && returnValue != null)
            {
                if (returnValue is Item)
                {
                    Function.AddLine("give @s " + returnValue);
                }
                else if (returnValue is IEnumerable <Item> items)
                {
                    if (give.Container == null)
                    {
                        foreach (var i in items)
                        {
                            Function.AddLine("give @s " + i);
                        }
                    }
                    else
                    {
                        Function.AddLine("give @s " + Item.Of(give.Container).SetBlockContainerItems(items));
                    }
                }
            }

            Function.WriteFile();

            Execute.Clear();
        }