Beispiel #1
0
        public void TestWrite()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space = pack.Namespace("space");

            //test
            LootTable lootTable = space.Loottable("mytable", new LootPool(new ItemEntry(ID.Item.cobblestone)
            {
                Weight  = 5,
                Changes = new BaseChange[]
                {
                    new SmeltChange()
                    {
                        Conditions = new RandomCondition(0.5)
                    }
                },
                Conditions = new RandomCondition(0.3) & !new RandomCondition(0.3)
            }, new MCRange(1, 3)), LootTable.TableType.block, BaseFile.WriteSetting.Auto);
            TextWriter writer = (pack.FileCreator.GetWriters().First(w => w.path == "datapacks/pack/data/space/loot_tables/mytable.json").writer as TextWriter) !;

            Assert.AreEqual("{\"type\":\"block\",\"pools\":[" +
                            "{\"entries\":[" +
                            "{\"conditions\":[" +
                            "{\"condition\":\"minecraft:inverted\",\"term\":{\"condition\":\"minecraft:alternative\",\"terms\":[" +
                            "{\"condition\":\"minecraft:inverted\",\"term\":{\"chance\":0.3,\"condition\":\"minecraft:random_chance\"}},{\"chance\":0.3,\"condition\":\"minecraft:random_chance\"}" +
                            "]}}" +
                            "],\"functions\":[" +
                            "{\"conditions\":[{\"chance\":0.5,\"condition\":\"minecraft:random_chance\"}],\"function\":\"minecraft:furnace_smelt\"}" +
                            "],\"name\":\"minecraft:cobblestone\",\"type\":\"minecraft:item\",\"weight\":5}" +
                            "],\"rolls\":{\"max\":3,\"min\":1}}" +
                            "]}", writer.ToString());
            Assert.IsNull(lootTable.Pools, "Pools wasn't cleared");
        }
 private ChildAdvancement GetChildAdvancement(PackNamespace space)
 {
     return(space.Advancement("child", GetParentAdvancement(space), new EnchantedItemTrigger()
     {
         Levels = 5, Item = ID.Item.wooden_sword
     }, null !, new JsonText.Text("Name"), new JsonText.Text("Description"), ID.Item.stone, ID.AdvancementFrame.goal, true, false, true));
 }
Beispiel #3
0
        public void TestCraftingRecipe()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space     = pack.Namespace("space");
            ItemGroup     woolGroup = space.Group("wool", new List <IItemType>()
            {
                ID.Item.white_wool, ID.Item.light_gray_wool
            });

            //test
            CraftingRecipe recipe = space.Recipe("recipe", new IItemType?[, ]
            {
                { ID.Item.String, null },
                { ID.Item.slime_ball, woolGroup },
                { ID.Item.String, ID.Item.air },
            }, ID.Item.cobweb, 8, "web");
            string recipeString = pack.FileCreator.GetWriters().Single(w => w.path == "datapacks/pack/data/space/recipes/recipe.json").writer.ToString() !;

            Assert.AreEqual("{\"type\":\"minecraft:crafting_shaped\",\"group\":\"web\",\"pattern\":[\"0 \",\"12\",\"0 \"],\"key\":{\"0\":{\"item\":\"minecraft:string\"},\"1\":{\"item\":\"minecraft:slime_ball\"},\"2\":{\"tag\":\"space:wool\"}},\"result\":{\"item\":\"minecraft:cobweb\",\"count\":8}}", recipeString, "recipe file wasn't written correctly");
            Assert.IsNull(recipe.Recipe, "Recipe wasn't cleared");

            //exceptions
            Assert.ThrowsException <ArgumentException>(() => space.Recipe("testrecipe1", new IItemType[, ] {
                { ID.Item.stone }
            }, ID.Item.air, 1), "Recipe may not output air");
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => space.Recipe("testrecipe2", new IItemType[, ] {
                { ID.Item.stone }
            }, ID.Item.stone, 0), "Item count under 1 shouldn't be allowed");
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => space.Recipe("testrecipe3", new IItemType[, ] {
                { ID.Item.stone }
            }, ID.Item.stone, 65), "Item count over 64 shouldn't be allowed");
            Assert.ThrowsException <ArgumentException>(() => space.Recipe("testrecipe4", new IItemType[, ] {
                { null !, ID.Item.air }
            }, ID.Item.stone, 1), "Recipe can't be empty");
Beispiel #4
0
        public void TestAddCommand()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space              = pack.Namespace("space");
            Function      autoFunction       = space.Function("autofunction");
            TextWriter    autoFunctionWriter = (pack.FileCreator.GetWriters().First(w => w.path == "datapacks/pack/data/space/functions/autofunction.mcfunction").writer as TextWriter) !;
            Function      onDisposeFunction  = space.Function("disposefunction", BaseFile.WriteSetting.OnDispose);

            //test
            autoFunction.AddCommand(new SayCommand("hello world"));
            autoFunction.AddCommand(new ClearCommand(ID.Selector.s));
            Assert.AreEqual(0, autoFunction.Commands.Count, "Commands wasn't removed from command list");
            Assert.AreEqual("say hello world" + Environment.NewLine + "clear @s" + Environment.NewLine, autoFunctionWriter.ToString(), "AddCommand failed to write the commands correctly");

            //text execute
            autoFunction       = space.Function("autofunction2");
            autoFunctionWriter = (pack.FileCreator.GetWriters().First(w => w.path == "datapacks/pack/data/space/functions/autofunction2.mcfunction").writer as TextWriter) !;
            autoFunction.AddCommand(new ExecuteAs(ID.Selector.s));
            Assert.AreEqual("", autoFunctionWriter.ToString(), "Execute command with no end shouldn't write anything yet");
            autoFunction.AddCommand(new ExecuteAt(ID.Selector.s));
            Assert.AreEqual(1, autoFunction.Commands.Count, "Commands wasn't added correctly to command list");
            autoFunction.AddCommand(new SayCommand("end"));
            Assert.AreEqual("execute as @s at @s run say end" + Environment.NewLine, autoFunctionWriter.ToString(), "Execute command with end should write");
        }
 private ParentAdvancement GetParentAdvancement(PackNamespace space)
 {
     return(space.Advancement("parent", new EnchantedItemTrigger()
     {
         Levels = 5
     }, new Reward()
     {
         Experience = 5
     }, new JsonText.Text("Name"), new JsonText.Text("Description"), ID.Item.String, "background"));
 }
Beispiel #6
0
 /// <summary>
 /// Call when constructors are done
 /// </summary>
 protected virtual void FinishedConstructing()
 {
     PackNamespace.AddFile(this);
     if (IsAuto())
     {
         StreamWriter = GetStream();
         WriteFile(StreamWriter);
         Dispose();
     }
 }
        public void TestWriteInvalid()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space = pack.Namespace("space");

            //test
            space.Advancement("invalid");
            string advancementString = pack.FileCreator.GetWriters().Single(w => w.path == "datapacks/pack/data/space/advancements/invalid.json").writer.ToString() !;

            Assert.AreEqual("{\"invalid\":true}", advancementString);
        }
Beispiel #8
0
        public void TestWrite()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space = pack.Namespace("space");

            //test
            Predicate predicate       = space.Predicate("predicate", new Conditions.RandomCondition(0.5));
            string    predicateString = pack.FileCreator.GetWriters().Single(w => w.path == "datapacks/pack/data/space/predicates/predicate.json").writer.ToString() !;

            Assert.AreEqual("{\"chance\":0.5,\"condition\":\"minecraft:random_chance\"}", predicateString, "file wasn't written correctly");
            Assert.IsNull(predicate.Condition, "Condition wasn't cleared");
        }
Beispiel #9
0
        public void TestGetCondition()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space = pack.Namespace("space");

            //test
            Predicate predicate = space.Predicate("predicate", new Conditions.RandomCondition(0.5));

            Conditions.PredicateCondition condition = predicate.GetCondition();
            Assert.AreSame(condition, predicate.GetCondition(), "Predicate doesn't return the same condition every time");
            Assert.AreSame(predicate, condition.Predicate, "Condition isn't checking for the correct predicate");
        }
        public void TestGetSetting()
        {
            using Datapack pack = new Datapack("a path", "name", ".", 4, new NoneFileCreator());
            PackNamespace space = pack.Namespace("space");

            Assert.IsNull(space.GetSetting <TestSetting>(), "Setting isn't added yet and should return null");

            space.AddSetting(new OtherSetting(15));
            space.AddSetting(new TestSetting(10));
            TestSetting setting = (space.GetSetting <TestSetting>() as TestSetting) !;

            Assert.IsNotNull(space.GetSetting <TestSetting>(), "A setting should have been returned since there is one");
            Assert.AreEqual(10, setting.ANumber, "The wrong setting returned");
        }
        public void TestFileAddListener()
        {
            using Datapack pack = new Datapack("a path", "name", ".", 4, new NoneFileCreator());
            bool          fileAdded = false;
            PackNamespace space     = pack.Namespace("space");

            space.Function("test1");
            pack.AddNewFileListener((file) =>
            {
                fileAdded = true;
            });
            Assert.IsFalse(fileAdded, "file listener shouldn't have been called yet.");
            space.Function("test2");
            Assert.IsTrue(fileAdded, "file listener should have been called after file was added.");
        }
Beispiel #12
0
        public void TestConstantScores()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space    = pack.Namespace("space");
            Function      function = space.Function("function", BaseFile.WriteSetting.OnDispose);

            //test
            function.Entity.Score.Operation(ID.Selector.s, new Objective("scores"), ID.Operation.Divide, 3);
            function.Entity.Score.Operation(ID.Selector.s, new Objective("scores"), ID.Operation.Multiply, 5);
            function.Entity.Score.Operation(ID.Selector.s, new Objective("scores"), ID.Operation.GetHigher, 3);

            Assert.AreSame((function.Commands[0] as ScoreboardOperationCommand) !.Selector2, (function.Commands[2] as ScoreboardOperationCommand) !.Selector2, "Constant value giver doesn't return same selector for same number");
            Assert.AreEqual("5", ((function.Commands[1] as ScoreboardOperationCommand) !.Selector2 as NameSelector) !.Name, "Name selector for selecting constant values are incorrect.");
        }
Beispiel #13
0
        public void TestNewChild()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space = pack.Namespace("space");

            //test
            ChildAdvancement advancement = GetChildAdvancement(space);

            advancement.NewChild("childchild", new EnchantedItemTrigger()
            {
                Levels = 5
            }, null !, new JsonText.Text("Name"), new JsonText.Text("Description"), ID.Item.stone);
            Assert.IsTrue(pack.FileCreator.GetDirectories().Any(d => d == "datapacks/pack/data/space/advancements/"), "Directory wasn't created");
            Assert.IsTrue(pack.FileCreator.GetWriters().Any(w => w.path == "datapacks/pack/data/space/advancements/childchild.json"), "File wasn't created");
        }
Beispiel #14
0
        public void TestWriteHidden()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space = pack.Namespace("space");

            //test
            _ = space.Advancement("hidden", new Requirement(new IRequirementItem[] { new EnchantedItemTrigger()
                                                                                     {
                                                                                         Levels = 5
                                                                                     }, new BredAnimalsTrigger() }), null !);
            string advancementString = pack.FileCreator.GetWriters().Single(w => w.path == "datapacks/pack/data/space/advancements/hidden.json").writer.ToString() !;

            Assert.AreEqual("{\"requirements\":[[\"trigger_0\",\"trigger_1\"]],\"criteria\":" +
                            "{\"trigger_0\":{\"conditions\":{\"levels\":{\"max\":5,\"min\":5}},\"trigger\":\"minecraft:enchanted_item\"},\"trigger_1\":{\"trigger\":\"minecraft:bred_animals\"}}" +
                            "}", advancementString, "hidden file wasn't written correctly");
        }
        public void TestDIsposeFileSetting()
        {
            using Datapack pack = new Datapack("a path", "name", ".", 4, new NoneFileCreator());
            PackNamespace space = pack.Namespace("space");

            space.AddSetting(NamespaceSettings.GetSettings().ForceDisposeWriteFiles());

            BaseFile file1 = space.Function("file1", BaseFile.WriteSetting.Auto);
            BaseFile file2 = space.Function("file2", BaseFile.WriteSetting.LockedAuto);
            BaseFile file3 = space.Function("file3", BaseFile.WriteSetting.LockedOnDispose);
            BaseFile file4 = space.Function("file4", BaseFile.WriteSetting.OnDispose);

            Assert.AreEqual(BaseFile.WriteSetting.OnDispose, file1.Setting, "Auto should have changed to OnDispose");
            Assert.AreEqual(BaseFile.WriteSetting.LockedOnDispose, file2.Setting, "LockedAuto should have changed to LockedOnDispose");
            Assert.AreEqual(BaseFile.WriteSetting.LockedOnDispose, file3.Setting, "LockedOnDispose shouldn't change");
            Assert.AreEqual(BaseFile.WriteSetting.OnDispose, file4.Setting, "OnDispose shouldn't change");
        }
Beispiel #16
0
        public void TestWriteChild()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space = pack.Namespace("space");

            //test
            ChildAdvancement childAdvancement  = GetChildAdvancement(space);
            string           advancementString = pack.FileCreator.GetWriters().Single(w => w.path == "datapacks/pack/data/space/advancements/child.json").writer.ToString() !;

            Assert.AreEqual("{\"requirements\":[[\"trigger_0\"]],\"criteria\":" +
                            "{\"trigger_0\":{\"conditions\":{\"item\":{\"item\":\"minecraft:wooden_sword\"},\"levels\":{\"max\":5,\"min\":5}},\"trigger\":\"minecraft:enchanted_item\"}}" +
                            ",\"display\":{\"icon\":{\"item\":\"minecraft:stone\"},\"title\":{\"text\":\"Name\"},\"description\":{\"text\":\"Description\"},\"frame\":\"goal\",\"show_toast\":false,\"announce_to_chat\":true,\"hidden\":true},\"parent\":\"space:parent\"}", advancementString, "Child file wasn't written correctly");
            Assert.IsNull(childAdvancement.Requirements, "requirements weren't cleared");
            Assert.IsNull(childAdvancement.Reward, "reward wasn't cleared");
            Assert.IsNull(childAdvancement.Description, "description wasn't cleared");
            Assert.IsNull(childAdvancement.Name, "name wasn't cleared");
        }
Beispiel #17
0
        public void TestPredicate()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space = pack.Namespace("space");

            //test
            space.Predicate("MyPredicate", new Conditions.RandomCondition(0.5));
            Assert.IsTrue(pack.FileCreator.GetDirectories().Any(d => d == "datapacks/pack/data/space/predicates/"), "Directory wasn't created");
            Assert.IsTrue(pack.FileCreator.GetWriters().Any(w => w.path == "datapacks/pack/data/space/predicates/mypredicate.json"), "File wasn't created");

            space.Predicate("folder/otherpredicate", new Conditions.RandomCondition(0.5), BaseFile.WriteSetting.OnDispose);
            Assert.IsFalse(pack.FileCreator.GetDirectories().Any(d => d == "datapacks/pack/data/space/predicates/folder/"), "Directory wasn't supposed to be created yet since its OnDispose");
            Assert.IsFalse(pack.FileCreator.GetWriters().Any(w => w.path == "datapacks/pack/data/space/predicates/folder/otherpredicate.json"), "File wasn't supposed to be created yet since its OnDispose");

            pack.Dispose();
            Assert.IsTrue(pack.FileCreator.GetDirectories().Any(d => d == "datapacks/pack/data/space/predicates/folder/"), "Directory wasn't created for file with directory in name");
            Assert.IsTrue(pack.FileCreator.GetWriters().Any(w => w.path == "datapacks/pack/data/space/predicates/folder/otherpredicate.json"), "File is supposed to have been created now since Dispose was ran");
        }
Beispiel #18
0
        public void TestLootTable()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space = pack.Namespace("space");

            //test
            space.Loottable("myTable", new LootPool[] { new LootPool(new EmptyEntry(), 1) }, null, BaseFile.WriteSetting.Auto);
            Assert.IsTrue(pack.FileCreator.GetDirectories().Any(d => d == "datapacks/pack/data/space/loot_tables/"), "Directory wasn't created");
            Assert.IsTrue(pack.FileCreator.GetWriters().Any(w => w.path == "datapacks/pack/data/space/loot_tables/mytable.json"), "File wasn't created");

            space.Loottable("folder/otherTable", new LootPool[] { new LootPool(new EmptyEntry(), 1) }, null, BaseFile.WriteSetting.OnDispose);
            Assert.IsFalse(pack.FileCreator.GetDirectories().Any(d => d == "datapacks/pack/data/space/loot_tables/folder/"), "Directory wasn't supposed to be created yet since its OnDispose");
            Assert.IsFalse(pack.FileCreator.GetWriters().Any(w => w.path == "datapacks/pack/data/space/loot_tables/folder/othertable.json"), "File wasn't supposed to be created yet since its OnDispose");

            pack.Dispose();
            Assert.IsTrue(pack.FileCreator.GetWriters().Any(w => w.path == "datapacks/pack/data/space/loot_tables/folder/othertable.json"), "File is supposed to have been created now since Dispose was ran");
            Assert.IsTrue(pack.FileCreator.GetDirectories().Any(d => d == "datapacks/pack/data/space/loot_tables/folder/"), "Directory wasn't created for file with directory in name");
        }
Beispiel #19
0
        public void TestRecipe()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space = pack.Namespace("space");

            //test
            space.Recipe("myrecipe");
            Assert.IsTrue(pack.FileCreator.GetDirectories().Any(d => d == "datapacks/pack/data/space/recipes/"), "Directory wasn't created");
            Assert.IsTrue(pack.FileCreator.GetWriters().Any(w => w.path == "datapacks/pack/data/space/recipes/myrecipe.json"), "File wasn't created");

            space.Recipe("folder/otherrecipe", SmeltRecipe.SmeltType.smelting, ID.Item.dirt, ID.Item.coarse_dirt, 2, null, null, BaseFile.WriteSetting.OnDispose);
            Assert.IsFalse(pack.FileCreator.GetDirectories().Any(d => d == "datapacks/pack/data/space/recipes/folder/"), "Directory wasn't supposed to be created yet since its OnDispose");
            Assert.IsFalse(pack.FileCreator.GetWriters().Any(w => w.path == "datapacks/pack/data/space/recipes/folder/otherrecipe.json"), "File wasn't supposed to be created yet since its OnDispose");

            pack.Dispose();
            Assert.IsTrue(pack.FileCreator.GetDirectories().Any(d => d == "datapacks/pack/data/space/recipes/folder/"), "Directory wasn't created for file with directory in name");
            Assert.IsTrue(pack.FileCreator.GetWriters().Any(w => w.path == "datapacks/pack/data/space/recipes/folder/otherrecipe.json"), "File is supposed to have been created now since Dispose was ran");
        }
Beispiel #20
0
        public void TestNewChild()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space    = pack.Namespace("space");
            Function      function = space.Function("function", BaseFile.WriteSetting.Auto);

            //test
            Function child = function.NewChild(null, f =>
            {
                f.AddCommand(new SayCommand("hello"));
            }, BaseFile.WriteSetting.Auto);

            Assert.IsTrue(pack.FileCreator.GetDirectories().Any(d => d == "datapacks/pack/data/space/functions/"), "Child directory wasn't created correctly");
            TextWriter writer = (pack.FileCreator.GetWriters().SingleOrDefault(w => w.path == "datapacks/pack/data/space/functions/1.mcfunction").writer as TextWriter) !;

            Assert.IsNotNull(writer, "Child file wasn't created");
            Assert.AreEqual("say hello" + Environment.NewLine, writer.ToString(), "FunctionCreator didn't run correctly");
        }
Beispiel #21
0
        public void TestAdvancement()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space = pack.Namespace("space");

            //test
            space.Advancement("myadvancement");
            Assert.IsTrue(pack.FileCreator.GetDirectories().Any(d => d == "datapacks/pack/data/space/advancements/"), "Directory wasn't created");
            Assert.IsTrue(pack.FileCreator.GetWriters().Any(w => w.path == "datapacks/pack/data/space/advancements/myadvancement.json"), "File wasn't created");

            space.Advancement("folder/otherAdvancement", new BredAnimalsTrigger(), null !, BaseFile.WriteSetting.OnDispose);
            Assert.IsFalse(pack.FileCreator.GetDirectories().Any(d => d == "datapacks/pack/data/space/advancements/folder/"), "Directory wasn't supposed to be created yet since its OnDispose");
            Assert.IsFalse(pack.FileCreator.GetWriters().Any(w => w.path == "datapacks/pack/data/space/advancements/folder/otheradvancement.json"), "File wasn't supposed to be created yet since its OnDispose");

            pack.Dispose();
            Assert.IsTrue(pack.FileCreator.GetDirectories().Any(d => d == "datapacks/pack/data/space/advancements/folder/"), "Directory wasn't created for file with directory in name");
            Assert.IsTrue(pack.FileCreator.GetWriters().Any(w => w.path == "datapacks/pack/data/space/advancements/folder/otheradvancement.json"), "File is supposed to have been created now since Dispose was ran");
        }
Beispiel #22
0
        public void TestCustomSummonExecute()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space    = pack.Namespace("space");
            Function      function = space.Function("function", BaseFile.WriteSetting.Auto);

            //test
            function.Custom.SummonExecute(new Entities.Armorstand()
            {
                Tags = new Tag[] { "ATag" }
            }, new Coords(1, 2, 3), "execute", (f) =>
            {
                f.World.Say("hello");
            });

            Assert.AreEqual("summon minecraft:armor_stand ~1 ~2 ~3 {Tags:[\"ATag\",\"SharpSummon\"]}" + Environment.NewLine +
                            "execute as @e[tag=SharpSummon] at @s run function space:execute" + Environment.NewLine, pack.FileCreator.GetWriters().Single(w => w.path == "datapacks/pack/data/space/functions/function.mcfunction").writer.ToString(), "Summon and execute file written correctly");
            Assert.AreEqual("tag @s remove SharpSummon" + Environment.NewLine +
                            "say hello" + Environment.NewLine, pack.FileCreator.GetWriters().Single(w => w.path == "datapacks/pack/data/space/functions/execute.mcfunction").writer.ToString(), "executing file written correctly");
        }
Beispiel #23
0
        public void TestCustomSetToScoreOperation()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space    = pack.Namespace("space");
            Function      function = space.Function("function", BaseFile.WriteSetting.OnDispose);

            ScoreValue value1 = new ScoreValue(new Selector(ID.Selector.a)
            {
                Limit = 1
            }, new Objective("Cakes"));
            ScoreValue value2 = new ScoreValue(new Selector(ID.Selector.e)
            {
                Limit = 1
            }, new Objective("Tests"));

            //test
            function.Custom.SetToScoreOperation(ID.Selector.s, new Objective("Score"), (value1 + 5) * (value2 + 10));
            Assert.AreEqual(6, function.Commands.Count, "Operation didn't add the correct amount of commands to the function");
            Assert.AreEqual("Score", (function.Commands[5] as ScoreboardOperationCommand) !.Objective1.Name, "Operation didn't end up setting the correct score");
        }
Beispiel #24
0
        public void TestCustomTreeSearch()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space    = pack.Namespace("space");
            Function      function = space.Function("function", BaseFile.WriteSetting.Auto);

            //test
            //2 branches
            function.Custom.TreeSearch(
                (min, max) => new ExecuteIfScoreMatches("#s", new Objective("scores"), new MCRange(min, max)),
                (number) => new SayCommand(number.ToString()),
                5,
                9
                );

            var treeWriters = pack.FileCreator.GetWriters().Where(w => w.path.StartsWith("datapacks/pack/data/space/functions/")).ToList();

            Assert.AreEqual(4, treeWriters.Count, "The correct amount of writers wasn't created");
            Assert.AreEqual("execute if score #s scores matches 5..6 run function space:function/5-6" + Environment.NewLine +
                            "execute if score #s scores matches 7..9 run function space:function/7-9" + Environment.NewLine, treeWriters.Single(w => w.path.EndsWith("function.mcfunction")).writer.ToString(), "Tree base wasn't written correctly");
            Assert.AreEqual("execute if score #s scores matches 7 run say 7" + Environment.NewLine +
                            "execute if score #s scores matches 8..9 run function space:function/8-9" + Environment.NewLine, treeWriters.Single(w => w.path.EndsWith("7-9.mcfunction")).writer.ToString(), "Tree branch wasn't written correctly");

            //4 branches
            Function fourBranches = space.Function("four/function", BaseFile.WriteSetting.Auto);

            fourBranches.Custom.TreeSearch(
                (min, max) => new ExecuteIfScoreMatches("#s", new Objective("scores"), new MCRange(min, max)),
                (number) => new SayCommand(number.ToString()),
                5,
                9,
                4
                );
            treeWriters = pack.FileCreator.GetWriters().Where(w => w.path.StartsWith("datapacks/pack/data/space/functions/four")).ToList();
            Assert.AreEqual(2, treeWriters.Count, "The correct amount of writers wasn't created for a 4 tree search");
            Assert.AreEqual("execute if score #s scores matches 8 run say 8" + Environment.NewLine +
                            "execute if score #s scores matches 9 run say 9" + Environment.NewLine, treeWriters.Single(w => w.path.EndsWith("8-9.mcfunction")).writer.ToString(), "4 Tree branch wasn't written correctly");
        }
Beispiel #25
0
        public void TestCustomIfElseCommand()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space    = pack.Namespace("space");
            Function      function = space.Function("function", BaseFile.WriteSetting.OnDispose);

            //test
            function.Custom.IfElse(new ExecuteIfBlock(new Coords(), ID.Block.stone), (ifFunction) =>
            {
                ifFunction.World.Say("block!");
            }, (elseFunction) =>
            {
                elseFunction.World.Say("no block!");
            }, "if", "else");

            Assert.AreEqual("scoreboard players set #ifelse math 0", function.Commands[0].GetCommandString(), "Base function commands aren't generated correctly");
            Assert.AreEqual("execute if block ~ ~ ~ minecraft:stone run function space:if", function.Commands[1].GetCommandString(), "Base function commands aren't generated correctly");
            Assert.AreEqual("execute if score #ifelse math matches 0 run function space:else", function.Commands[2].GetCommandString(), "Base function commands aren't generated correctly");

            Assert.AreEqual("say block!" + Environment.NewLine
                            + "scoreboard players set #ifelse math 1" + Environment.NewLine, pack.FileCreator.GetWriters().Single(w => w.path == "datapacks/pack/data/space/functions/if.mcfunction").writer.ToString());
            Assert.AreEqual("say no block!" + Environment.NewLine, pack.FileCreator.GetWriters().Single(w => w.path == "datapacks/pack/data/space/functions/else.mcfunction").writer.ToString());
        }
Beispiel #26
0
        public void TestCommandListener()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space          = pack.Namespace("space");
            Function      function       = space.Function("function", BaseFile.WriteSetting.Auto);
            Function      otherfunction  = space.Function("function2", BaseFile.WriteSetting.OnDispose);
            bool          commandWritten = false;

            function.AddCommandListener((f, c) =>
            {
                if (c is SayCommand sayCommand)
                {
                    sayCommand.Text = "2";
                }
                commandWritten = true;
            });
            otherfunction.AddCommandListener((f, c) => commandWritten = true);

            //test
            function.World.Say("1");
            Assert.IsTrue(commandWritten, "Command listener wasn't called");
            Assert.AreEqual("say 2" + Environment.NewLine, pack.FileCreator.GetWriters().Single(w => w.path.EndsWith("function.mcfunction")).writer.ToString(), "Command wasn't changed by command listener");

            commandWritten = false;
            function.Execute.As(ID.Selector.s);
            Assert.IsFalse(commandWritten, "Command listener shouldn't have been called (no command was written yet)");
            function.World.Say("123");
            Assert.IsTrue(commandWritten, "Command listener should have been called since execute listener is done");

            commandWritten = false;
            otherfunction.World.Say("123");
            Assert.IsFalse(commandWritten, "Command listener shouldn't have been called (dispose isn't called yet)");
            otherfunction.Dispose();
            Assert.IsTrue(commandWritten, "Command listener should have been called (dispose was called)");
        }
Beispiel #27
0
        public void TestDispose()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space              = pack.Namespace("space");
            Function      onDisposeFunction  = space.Function("disposefunction", BaseFile.WriteSetting.OnDispose);
            Function      autoFunction       = space.Function("autofunction", BaseFile.WriteSetting.Auto);
            TextWriter    autoFunctionWriter = (pack.FileCreator.GetWriters().First(w => w.path == "datapacks/pack/data/space/functions/autofunction.mcfunction").writer as TextWriter) !;

            //test
            onDisposeFunction.AddCommand(new SayCommand("hello world"));
            onDisposeFunction.AddCommand(new ExecuteAs(ID.Selector.a));
            Assert.AreEqual(2, onDisposeFunction.Commands.Count, "Commands wasn't added to command list");
            onDisposeFunction.Dispose();
            TextWriter onDisposeFunctionWriter = (pack.FileCreator.GetWriters().First(w => w.path == "datapacks/pack/data/space/functions/disposefunction.mcfunction").writer as TextWriter) !;

            Assert.AreEqual("say hello world" + Environment.NewLine + "execute as @a" + Environment.NewLine, onDisposeFunctionWriter.ToString(), "Dispose function isn't writing commands correctly on dispose");
            Assert.IsNull(onDisposeFunction.Commands, "Commands wasn't cleared");

            autoFunction.AddCommand(new SayCommand("hello world"));
            autoFunction.AddCommand(new ExecuteAs(ID.Selector.a));
            autoFunction.Dispose();
            Assert.AreEqual("say hello world" + Environment.NewLine + "execute as @a" + Environment.NewLine, autoFunctionWriter.ToString(), "Auto function isn't writing commands correctly on dispose");
        }
Beispiel #28
0
        public void TestCustomGroupCommands()
        {
            //setup
            using Datapack pack = new Datapack("datapacks", "pack", "a pack", 0, new NoneFileCreator());
            PackNamespace space    = pack.Namespace("space");
            Function      function = space.Function("function", BaseFile.WriteSetting.OnDispose);

            BaseCommand command1 = new ExecuteAt(ID.Selector.s);
            BaseCommand command2 = new SayCommand("123");
            BaseCommand command3 = new SayMeCommand("123");
            BaseCommand command4 = new ExecuteAs(ID.Selector.s);

            //test
            //without execute at start
            function.Custom.GroupCommands((f) =>
            {
                f.AddCommand(command1.ShallowClone());
                f.AddCommand(command2);
                f.AddCommand(command3);
            });
            Assert.AreEqual("execute at @s run say 123", function.Commands[0].GetCommandString(), "Get first grouped command string returned wrong string");
            Assert.AreEqual("me 123", function.Commands[1].GetCommandString(), "Get second grouped command string returned wrong string");

            //with execute at start
            function.Commands.Clear();
            function.AddCommand(command1.ShallowClone());
            function.Custom.GroupCommands((f) =>
            {
                f.AddCommand(command2);
                f.AddCommand(command4.ShallowClone());
                f.AddCommand(command3);
            });
            Assert.AreEqual("execute at @s run say 123", function.Commands[0].GetCommandString(), "Get first executed grouped command string returned wrong string");
            Assert.AreEqual("execute at @s as @s run me 123", function.Commands[1].GetCommandString(), "Get second executed grouped command string returned wrong string");

            //using function
            function.Commands.Clear();
            function.AddCommand(command1.ShallowClone());
            function.Custom.GroupCommands((f) =>
            {
                f.AddCommand(command2);
                f.AddCommand(command1.ShallowClone());
                f.AddCommand(command3);
            }, true);
            Function addedFunction = (((function.Commands[0] as BaseExecuteCommand) !.ExecuteCommand as RunFunctionCommand) !.Function as Function) !;

            Assert.AreEqual("say 123", addedFunction.Commands[0].GetCommandString(), "Get first function grouped command string returned wrong string");
            Assert.AreEqual("execute at @s run me 123", addedFunction.Commands[1].GetCommandString(), "Get second function grouped command string returned wrong string");
            Assert.AreEqual(2, addedFunction.Commands.Count, "function grouped command doesn't contain the correct amount of commands");

            space.AddSetting(NamespaceSettings.GetSettings().FunctionGroupedCommands());
            function.Commands.Clear();
            function.AddCommand(command1.ShallowClone());
            function.Custom.GroupCommands((f) =>
            {
                f.AddCommand(command2);
                f.AddCommand(command1.ShallowClone());
                f.AddCommand(command3);
            });
            addedFunction = (((function.Commands[0] as BaseExecuteCommand) !.ExecuteCommand as RunFunctionCommand) !.Function as Function) !;
            Assert.AreEqual(2, addedFunction.Commands.Count, "automatic function grouped command doesn't get the correct commands");
        }
 protected override void FinishedConstructing()
 {
     PackNamespace.AddFile(this);
 }