diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index dd12d4a..e537368 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -19,7 +19,7 @@ jobs: - name: Setup .NET uses: actions/setup-dotnet@v3 with: - dotnet-version: 6.0.x + dotnet-version: 8.0.x - name: Restore dependencies run: dotnet restore SteveSharp.csproj - name: Build diff --git a/Core/Bossbar.cs b/Core/Bossbar.cs index 4ea4a35..26aa367 100644 --- a/Core/Bossbar.cs +++ b/Core/Bossbar.cs @@ -1,8 +1,9 @@ using System.Text.Json; using SteveSharp.Generic; using SteveSharp.JsonShapes; +using Str = SteveSharp.Core.Strings; -namespace SteveSharp; +namespace SteveSharp.Core; public class Bossbar { @@ -21,56 +22,38 @@ public enum Style Notched12, Notched20 } - public static string Add(string id, TextComponent text) => $"bossbar add {id} {JsonSerializer.Serialize(text)}"; - public static string Add(string id, TextComponent[] text) => $"bossbar add {id} {JsonSerializer.Serialize(text)}"; - public static string Get(string id, GetField field) - { - Dictionary fieldStr = new() { - {GetField.Max, "max"}, - {GetField.Players, "players"}, - {GetField.Value, "value"}, - {GetField.Visible, "visible"} - }; - return $"bossbar get {id} {fieldStr[field]}"; - } - public static string Get(string id, string field) => $"bossbar get {id} {field}"; - public static string List() => "bossbar list"; - public static string Remove(string id) => $"bossbar remove {id}"; - public static string SetColor(string id, Color color) => $"bossbar set {id} color {ColorHandler.Get(color)}"; - public static string SetColor(string id, string color) => $"bossbar set {id} color {color}"; - public static string SetMax(string id, int max) => $"bossbar set {id} max {max}"; - public static string SetName(string id, TextComponent name) => $"bossbar set {id} name {JsonSerializer.Serialize(name)}"; - public static string SetName(string id, TextComponent[] name) => $"bossbar set {id} name {JsonSerializer.Serialize(name)}"; - public static string SetPlayers(string id, string targets) => $"bossbar set {id} players {targets}"; - public static string SetStyle(string id, Style style) - { - Dictionary styleStr = new() { - {Style.Notched6, "notched_6"}, - {Style.Notched10, "notched_10"}, - {Style.Notched12, "notched_12"}, - {Style.Notched20, "notched_20"} - }; - return $"bossbar set {id} style {styleStr[style]}"; - } - public static string SetStyle(string id, string style) => $"bossbar set {id} style {style}"; - public static string SetValue(string id, int value) => $"bossbar set {id} value {value}"; - public static string SetVisible(string id, bool visible) => $"bossbar set {id} visible {visible.ToString().ToLower()}"; + public static void Add(string id, TextComponent text) => FunctionBuilder.Add(Str.Bossbar.Add(id, text)); + public static void Add(string id, TextComponent[] text) => FunctionBuilder.Add(Str.Bossbar.Add(id, text)); + public static void Get(string id, GetField field) => FunctionBuilder.Add(Str.Bossbar.Get(id, field)); + public static void Get(string id, string field) => FunctionBuilder.Add(Str.Bossbar.Get(id, field)); + public static void List() => FunctionBuilder.Add(Str.Bossbar.List()); + public static void Remove(string id) => FunctionBuilder.Add(Str.Bossbar.Remove(id)); + public static void SetColor(string id, Color color) => FunctionBuilder.Add(Str.Bossbar.SetColor(id, color)); + public static void SetColor(string id, string color) => FunctionBuilder.Add(Str.Bossbar.SetColor(id, color)); + public static void SetMax(string id, int max) => FunctionBuilder.Add(Str.Bossbar.SetMax(id, max)); + public static void SetName(string id, TextComponent name) => FunctionBuilder.Add(Str.Bossbar.SetName(id, name)); + public static void SetName(string id, TextComponent[] name) => FunctionBuilder.Add(Str.Bossbar.SetName(id, name)); + public static void SetPlayers(string id, string targets) => FunctionBuilder.Add(Str.Bossbar.SetPlayers(id, targets)); + public static void SetStyle(string id, Style style) => FunctionBuilder.Add(Str.Bossbar.SetStyle(id, style)); + public static void SetStyle(string id, string style) => FunctionBuilder.Add(Str.Bossbar.SetStyle(id, style)); + public static void SetValue(string id, int value) => FunctionBuilder.Add(Str.Bossbar.SetValue(id, value)); + public static void SetVisible(string id, bool visible) => FunctionBuilder.Add(Str.Bossbar.SetVisible(id, visible)); public Bossbar(string id) { Id = id; } - public string Add(TextComponent text) => Add(this.Id!, text); - public string Add(TextComponent[] text) => Add(this.Id!, text); - public string Get(GetField field) => Get(this.Id!, field); - public string Get(string field) => Get(this.Id!, field); - public string SetColor(Color color) => SetColor(this.Id!, color); - public string SetColor(string color) => SetColor(this.Id!, color); - public string SetMax(int max) => SetMax(this.Id!, max); - public string SetName(TextComponent name) => SetName(this.Id!, name); - public string SetName(TextComponent[] name) => SetName(this.Id!, name); - public string SetPlayers(string targets) => SetPlayers(this.Id!, targets); - public string SetStyle(Style style) => SetStyle(this.Id!, style); - public string SetStyle(string style) => SetStyle(this.Id!, style); - public string SetValue(int value) => SetValue(this.Id!, value); - public string SetVisible(bool visible) => SetVisible(this.Id!, visible); + public void Add(TextComponent text) => Add(this.Id!, text); + public void Add(TextComponent[] text) => Add(this.Id!, text); + public void Get(GetField field) => Get(this.Id!, field); + public void Get(string field) => Get(this.Id!, field); + public void SetColor(Color color) => SetColor(this.Id!, color); + public void SetColor(string color) => SetColor(this.Id!, color); + public void SetMax(int max) => SetMax(this.Id!, max); + public void SetName(TextComponent name) => SetName(this.Id!, name); + public void SetName(TextComponent[] name) => SetName(this.Id!, name); + public void SetPlayers(string targets) => SetPlayers(this.Id!, targets); + public void SetStyle(Style style) => SetStyle(this.Id!, style); + public void SetStyle(string style) => SetStyle(this.Id!, style); + public void SetValue(int value) => SetValue(this.Id!, value); + public void SetVisible(bool visible) => SetVisible(this.Id!, visible); } \ No newline at end of file diff --git a/Core/Chat.cs b/Core/Chat.cs index 2908e6b..ee1c2f5 100644 --- a/Core/Chat.cs +++ b/Core/Chat.cs @@ -1,23 +1,18 @@ -using SteveSharp.JsonShapes; +using SteveSharp.Generic; +using SteveSharp.JsonShapes; using System.Text.Json; +using Str = SteveSharp.Core.Strings; namespace SteveSharp.Core { public static class Chat { - public static string Say(string msg) - { - return $"say {msg}"; - } - public static string Out(string selector, TextComponent[] text) - { - string command = "tellraw " + selector + " " + JsonSerializer.Serialize(text); - return command; - } - public static string Out(string selector, TextComponent text) - { - string command = "tellraw " + selector + " " + JsonSerializer.Serialize(text); - return command; - } + public static void Say(string msg) => FunctionBuilder.Add(Str.Chat.Say(msg)); + public static void Tell(Targets targets, string message) => FunctionBuilder.Add(Str.Chat.Tell(targets, message)); + public static void Tell(string targets, string message) => FunctionBuilder.Add(Str.Chat.Tell(targets, message)); + public static void Tellraw(Targets targets, TextComponent[] text) => FunctionBuilder.Add(Str.Chat.Tellraw(targets, text)); + public static void Tellraw(string targets, TextComponent[] text) => FunctionBuilder.Add(Str.Chat.Tellraw(targets, text)); + public static void Tellraw(Targets targets, TextComponent text) => FunctionBuilder.Add(Str.Chat.Tellraw(targets, text)); + public static void Tellraw(string targets, TextComponent text) => FunctionBuilder.Add(Str.Chat.Tellraw(targets, text)); } } diff --git a/Core/Effect.cs b/Core/Effect.cs new file mode 100644 index 0000000..150a7b8 --- /dev/null +++ b/Core/Effect.cs @@ -0,0 +1,29 @@ +using SteveSharp.Generic; +using Str = SteveSharp.Core.Strings; + +namespace SteveSharp.Core; + +public static class Effect { + public static void Give(Targets targets, StatusEffect effect, bool infinite = false, int amplifier = 0) => FunctionBuilder.Add(Str.Effect.Give(targets, effect, infinite, amplifier)); + public static void Give(string targets, StatusEffect effect, bool infinite = false, int amplifier = 0) => FunctionBuilder.Add(Str.Effect.Give(targets, effect, infinite, amplifier)); + + public static void Give(Targets targets, StatusEffect effect, int seconds, int amplifier = 0) => FunctionBuilder.Add(Str.Effect.Give(targets, effect, seconds, amplifier)); + public static void Give(string targets, StatusEffect effect, int seconds, int amplifier = 0) => FunctionBuilder.Add(Str.Effect.Give(targets, effect, seconds, amplifier)); + + public static void Give(Targets targets, string effect, bool infinite = false, int amplifier = 0) => FunctionBuilder.Add(Str.Effect.Give(targets, effect, infinite, amplifier)); + public static void Give(string targets, string effect, bool infinite = false, int amplifier = 0) => FunctionBuilder.Add(Str.Effect.Give(targets, effect, infinite, amplifier)); + + public static void Give(Targets targets, string effect, int seconds, int amplifier = 0) => FunctionBuilder.Add(Str.Effect.Give(targets, effect, seconds, amplifier)); + public static void Give(string targets, string effect, int seconds, int amplifier = 0) => FunctionBuilder.Add(Str.Effect.Give(targets, effect, seconds, amplifier)); + + public static void Clear() => FunctionBuilder.Add(Str.Effect.Clear()); + + public static void Clear(Targets targets) => FunctionBuilder.Add(Str.Effect.Clear(targets)); + public static void Clear(string targets) => FunctionBuilder.Add(Str.Effect.Clear(targets)); + + public static void Clear(Targets targets, StatusEffect effect) => FunctionBuilder.Add(Str.Effect.Clear(targets, effect)); + public static void Clear(string targets, StatusEffect effect) => FunctionBuilder.Add(Str.Effect.Clear(targets, effect)); + + public static void Clear(Targets targets, string effect) => FunctionBuilder.Add(Str.Effect.Clear(targets, effect)); + public static void Clear(string targets, string effect) => FunctionBuilder.Add(Str.Effect.Clear(targets, effect)); +} \ No newline at end of file diff --git a/Core/EnchantCommand.cs b/Core/EnchantCommand.cs new file mode 100644 index 0000000..47f4177 --- /dev/null +++ b/Core/EnchantCommand.cs @@ -0,0 +1,15 @@ +using SteveSharp.Generic; +using Str = SteveSharp.Core.Strings; + +namespace SteveSharp.Core; + +public static class EnchantCommand { + public static void Enchant(Targets targets, Enchantment enchantment) => FunctionBuilder.Add(Str.EnchantCommand.Enchant(targets, enchantment)); + public static void Enchant(Targets targets, Enchantment enchantment, int level) => FunctionBuilder.Add(Str.EnchantCommand.Enchant(targets, enchantment, level)); + public static void Enchant(Targets targets, string enchantment) => FunctionBuilder.Add(Str.EnchantCommand.Enchant(targets, enchantment)); + public static void Enchant(Targets targets, string enchantment, int level) => FunctionBuilder.Add(Str.EnchantCommand.Enchant(targets, enchantment, level)); + public static void Enchant(string targets, string enchantment) => FunctionBuilder.Add(Str.EnchantCommand.Enchant(targets, enchantment)); + public static void Enchant(string targets, string enchantment, int level) => FunctionBuilder.Add(Str.EnchantCommand.Enchant(targets, enchantment, level)); + public static void Enchant(string targets, Enchantment enchantment) => FunctionBuilder.Add(Str.EnchantCommand.Enchant(targets, enchantment)); + public static void Enchant(string targets, Enchantment enchantment, int level) => FunctionBuilder.Add(Str.EnchantCommand.Enchant(targets, enchantment, level)); +} \ No newline at end of file diff --git a/Core/Entity.cs b/Core/Entity.cs index 50d968c..58da9f2 100644 --- a/Core/Entity.cs +++ b/Core/Entity.cs @@ -1,65 +1,36 @@ -namespace SteveSharp.Core +using SteveSharp.Generic; +using Str = SteveSharp.Core.Strings; + +namespace SteveSharp.Core { public static class Entity { - public static string Self(string match = "") { if (match == "") return "@s"; else return "@s[" + match + "]"; } - public static string Everyone(string match = "") { if (match == "") return "@a"; else return "@a[" + match + "]"; } - public static string Random(string match = "") { if (match == "") return "@r"; else return "@r[" + match + "]"; } - public static string Closest(string match = "") { if (match == "") return "@p"; else return "@p[" + match + "]"; } - public static string AllEntities(string match = "") { if (match == "") return "@e"; else return "@e[" + match + "]"; } + public static string SelectedMatch(string match) => "@s[" + match + "]"; + public static string AllPlayersMatch(string match) => "@a[" + match + "]"; + public static string RandomPlayerMatch(string match) => "@r[" + match + "]"; + public static string NearestPlayerMatch(string match) => "@p[" + match + "]"; + public static string AllEntitiesMatch(string match) => "@e[" + match + "]"; /// /// This method selects an entity with specific matches, recommended if you want to select an entity with specific matches. /// /// - public static string Custom(string selector, int? limit = null, string[]? tags = null, string[]? scores = null, string? team = null, string? type = null, string? distance = null, string? area = null, string? level = null, string? gamemode = null, string? horizontalRotation = null, string? verticalRotation = null, string? sort = null) { - string match = ""; - match += selector + "["; - if(limit != null) match += "limit=" + limit + ','; - if(tags?.Length >= 1){ - foreach(string tag in tags){ - match += "tag=" + tag + ","; - } - } - if(scores?.Length >= 1){ - match += "scores={"; - foreach(string score in scores){ - match += score + ','; - } - match += "},"; - } - if(team != null) match += "team=" + team + ','; - if(type != null && selector != "@a" || selector != "@r" || selector != "@p") - match += "type=" + type + ','; - if(distance != null) match += "distance=" + distance + ','; - if(area != null) match += area + ','; - if(level != null) match += "level=" + level + ','; - if(gamemode != null && selector != "@e") match += "gamemode=" + gamemode + ','; - if(horizontalRotation != null) match += "y_rotation=" + horizontalRotation + ','; - if(verticalRotation != null) match += "x_rotation=" + verticalRotation + ','; - if(sort != null) match += "sort=" + sort + ','; - if (match.EndsWith(',')) match.Remove(match.Length - 1); - match += ']'; - return match; - } - public static string Teleport(string selector, string to) - { - return $"tp {selector} {to}"; - } - public static string Summon(string entity, string[] pos, string nbt = "{}") - { - return $"summon {entity} {pos[0]} {pos[1]} {pos[2]} {nbt}"; - } - public static string AddTag(string selector, string tag) - { - return $"tag {selector} add {tag}"; - } - public static string RemoveTag(string selector, string tag) - { - return $"tag {selector} remove {tag}"; - } - public static string Kill(string selector) - { - return $"kill {selector}"; - } + public static void Custom(Targets targets, int? limit = null, string[]? tags = null, string[]? scores = null, string? team = null, string? type = null, string? distance = null, string? area = null, string? level = null, string? gamemode = null, string? horizontalRotation = null, string? verticalRotation = null, string? sort = null) + => FunctionBuilder.Add(Str.Entity.Custom(targets, limit, tags, scores, team, type, distance, area, level, gamemode, horizontalRotation, verticalRotation, sort)); + public static void Custom(string targets, int? limit = null, string[]? tags = null, string[]? scores = null, string? team = null, string? type = null, string? distance = null, string? area = null, string? level = null, string? gamemode = null, string? horizontalRotation = null, string? verticalRotation = null, string? sort = null) + => FunctionBuilder.Add(Str.Entity.Custom(targets, limit, tags, scores, team, type, distance, area, level, gamemode, horizontalRotation, verticalRotation, sort)); + public static void Teleport(Targets targets, string to) => FunctionBuilder.Add(Str.Entity.Teleport(targets, to)); + public static void Teleport(string targets, string to) => FunctionBuilder.Add(Str.Entity.Teleport(targets, to)); + public static void Summon(EntityEnum entity, (int X, int Y, int Z) pos, string nbt = "{}") => FunctionBuilder.Add(Str.Entity.Summon(entity, pos, nbt)); + public static void Summon(EntityEnum entity, (string X, string Y, string Z) rel, string nbt = "{}") => FunctionBuilder.Add(Str.Entity.Summon(entity, rel, nbt)); + public static void Summon(EntityEnum entity, string[] pos, string nbt = "{}") => FunctionBuilder.Add(Str.Entity.Summon(entity, pos, nbt)); + public static void Summon(string entity, (int X, int Y, int Z) pos, string nbt = "{}") => FunctionBuilder.Add(Str.Entity.Summon(entity, pos, nbt)); + public static void Summon(string entity, (string X, string Y, string Z) rel, string nbt = "{}") => FunctionBuilder.Add(Str.Entity.Summon(entity, rel, nbt)); + public static void Summon(string entity, string[] pos, string nbt = "{}") => FunctionBuilder.Add(Str.Entity.Summon(entity, pos, nbt)); + public static void AddTag(Targets targets, string tag) => FunctionBuilder.Add(Str.Entity.AddTag(targets, tag)); + public static void AddTag(string targets, string tag) => FunctionBuilder.Add(Str.Entity.AddTag(targets, tag)); + public static void RemoveTag(Targets targets, string tag) => FunctionBuilder.Add(Str.Entity.RemoveTag(targets, tag)); + public static void RemoveTag(string targets, string tag) => FunctionBuilder.Add(Str.Entity.RemoveTag(targets, tag)); + public static void Kill(Targets targets) => FunctionBuilder.Add(Str.Entity.Kill(targets)); + public static void Kill(string targets) => FunctionBuilder.Add(Str.Entity.Kill(targets)); } -} +} \ No newline at end of file diff --git a/Core/Execute.cs b/Core/Execute.cs index 508af94..fe49bed 100644 --- a/Core/Execute.cs +++ b/Core/Execute.cs @@ -1,55 +1,23 @@ -namespace SteveSharp.Core +using SteveSharp.Generic; +using Str = SteveSharp.Core.Strings; + +namespace SteveSharp.Core { public static class Execute { const string[] defaults = null; - public static string Write(string execute, string[] commands = defaults) - { - string allCommands = ""; - foreach (string command in commands) - { - allCommands += "execute " + execute + "run " + command + "\n"; - } - return allCommands; - } - public static string As(string selector, string addition = "") - { - return "as " + selector + " " + addition; - } - public static string At(string selector, string addition = "") - { - return "at " + selector + " " + addition; - } - public static string Asat(string selector, string addition = "") - { - return "as " + selector + " at @s " + addition; - } - public static string Unless(string arguments, string addition = "") - { - return "unless " + arguments + " " + addition; - } - public static string If(string arguments, string addition = "") - { - return "if " + arguments + " " + addition; - } - public static string Summon(string entity, string[] pos, string addition = "") - { - return "summon " + entity + " " + pos[0] + " " + pos[1] + " " + pos[2] + " " + addition; - } - public static string Store(string where, string at, string arguments, string addition = "") - { - return "store " + where + " " + at + " " + arguments + " " + addition; - } - public static string StoreScore(string where, Score score, string selector = "", string addition = "") - { - if (selector == "") - { - return "store " + where + " score " + "#" + score.id + " " + score.id + " " + addition; - } - else - { - return "store " + where + " score " + selector + " " + score.id + " " + addition; - } - } + public static void Write(string execute, string[] commands = defaults) => FunctionBuilder.Add(Str.Execute.Write(execute, commands)); + public static void As(Targets targets, string addition = "") => FunctionBuilder.Add(Str.Execute.As(targets, addition)); + public static void As(string targets, string addition = "") => FunctionBuilder.Add(Str.Execute.As(targets, addition)); + public static void At(Targets targets, string addition = "") => FunctionBuilder.Add(Str.Execute.At(targets, addition)); + public static void At(string targets, string addition = "") => FunctionBuilder.Add(Str.Execute.At(targets, addition)); + public static void Asat(Targets targets, string addition = "") => FunctionBuilder.Add(Str.Execute.Asat(targets, addition)); + public static void Asat(string targets, string addition = "") => FunctionBuilder.Add(Str.Execute.Asat(targets, addition)); + public static void Unless(string arguments, string addition = "") => FunctionBuilder.Add(Str.Execute.Unless(arguments, addition)); + public static void If(string arguments, string addition = "") => FunctionBuilder.Add(Str.Execute.If(arguments, addition)); + public static void Summon(string entity, string[] pos, string addition = "") => FunctionBuilder.Add(Str.Execute.Summon(entity, pos, addition)); + public static void Store(string where, string at, string arguments, string addition = "") => FunctionBuilder.Add(Str.Execute.Store(where, at, arguments, addition)); + public static void StoreScore(string where, Score score, Targets targets, string addition = "") => FunctionBuilder.Add(Str.Execute.StoreScore(where, score, targets, addition)); + public static void StoreScore(string where, Score score, string targets = "", string addition = "") => FunctionBuilder.Add(Str.Execute.StoreScore(where, score, targets, addition)); } } diff --git a/Core/For.cs b/Core/For.cs index 525958f..0d6a7dd 100644 --- a/Core/For.cs +++ b/Core/For.cs @@ -1,20 +1,10 @@ +using Str = SteveSharp.Core.Strings; + namespace SteveSharp.Core; public static class For { - public static string Loop(int to, Func> block, int from = 0) { - List> fullBlock = new(); - for(int i = from; i <= to; i++) { - fullBlock.Add(block(i)); - } - List ret = new(); - foreach(var codeBlock in fullBlock) { - foreach(var command in codeBlock) { - ret.Add(command); - } - } - return string.Join("\n", ret); - } + public static void Loop(int to, Func> block, int from = 0) => FunctionBuilder.Add(Str.For.Loop(to, block, from)); public static List Functions(int to, Func block, int from = 0) { List fullBlock = new(); for(int i = from; i <= to; i++) { diff --git a/Core/Recipe.cs b/Core/Recipe.cs index 30c5a9e..b62d12a 100644 --- a/Core/Recipe.cs +++ b/Core/Recipe.cs @@ -1,6 +1,11 @@ +using SteveSharp.Generic; +using Str = SteveSharp.Core.Strings; + namespace SteveSharp.Core; public static class Recipe { - public static string Give(string targets, string recipe) => $"recipe give {targets} {recipe}"; - public static string Take(string targets, string recipe) => $"recipe take {targets} {recipe}"; + public static void Give(Targets targets, string recipe) => FunctionBuilder.Add(Str.Recipe.Give(targets, recipe)); + public static void Give(string targets, string recipe) => FunctionBuilder.Add(Str.Recipe.Give(targets, recipe)); + public static void Take(Targets targets, string recipe) => FunctionBuilder.Add(Str.Recipe.Take(targets, recipe)); + public static void Take(string targets, string recipe) => FunctionBuilder.Add(Str.Recipe.Take(targets, recipe)); } \ No newline at end of file diff --git a/Core/Score.cs b/Core/Score.cs index 4aec040..7d45e72 100644 --- a/Core/Score.cs +++ b/Core/Score.cs @@ -1,73 +1,89 @@ -namespace SteveSharp.Core +using SteveSharp.Generic; +using Str = SteveSharp.Core.Strings; + +namespace SteveSharp.Core { public class Score { - public int count = 0; public string id; public string type; public string name; - public Score(string id, string type = "dummy", string name = "", int count = 0) + public Score(string id, string type = "dummy", string name = "") { this.id = id; - this.count = count; this.type = type; this.name = name; + AddObjective(); } - public string AddObjective() - { - return $"scoreboard objectives add {this.id} {this.type} {this.name}"; - } - public static string AddObjectives(Score[] scores) - { + public void AddObjective() => FunctionBuilder.Add(Str.Score.AddObjective(id, type, name)); + public static void AddObjective(string id, string type, string name) => FunctionBuilder.Add(Str.Score.AddObjective(id, type, name)); + public static void AddObjectives(Score[] scores) { string commands = ""; foreach(Score score in scores) { - commands += $"scoreboard objectives add {score.id} {score.type} {score.name}\n"; + commands += Str.Score.AddObjective(score.id, score.type, score.name) + "\n"; } - return commands; + FunctionBuilder.Add(commands); } - public string Set(int count, string selector = "") + public void Set(int count, string targets = "") { - if(selector == "") - { - return $"scoreboard players set #{this.id} {this.id} {count}"; - } else - { - return $"scoreboard players set {selector} {this.id} {count}"; - } + FunctionBuilder.Add(Str.Score.Set(id, count, targets)); } - public string Add(int count, string selector = "") + public void Set(int count, Targets targets) { - if (selector == "") - { - return $"scoreboard players add #{this.id} {this.id} {count}"; - } - else - { - return $"scoreboard players add {selector} {this.id} {count}"; - } + FunctionBuilder.Add(Str.Score.Set(id, count, targets)); } - public string Remove(string selector, int count) + public static void Set(string id, int count, string targets = "") { - if (selector == "") - { - return $"scoreboard players remove #{this.id} {this.id} {count}"; - } - else - { - return $"scoreboard players remove {selector} {this.id} {count}"; - } + FunctionBuilder.Add(Str.Score.Set(id, count, targets)); } - public string Reset(string selector = "") + public void Add(int count, string targets = "") { - if (selector == "") - { - return $"scoreboard players reset #{this.id} {this.id}"; - } - else - { - return $"scoreboard players reset {selector} {this.id}"; - } + FunctionBuilder.Add(Str.Score.Add(id, count, targets)); + } + public void Add(int count, Targets targets) + { + FunctionBuilder.Add(Str.Score.Add(id, count, targets)); + } + public static void Add(string id, int count, string targets = "") + { + FunctionBuilder.Add(Str.Score.Add(id, count, targets)); + } + public static void Add(string id, int count, Targets targets) + { + FunctionBuilder.Add(Str.Score.Add(id, count, targets)); + } + public void Remove(int count, string targets = "") + { + FunctionBuilder.Add(Str.Score.Remove(id, count, targets)); + } + public void Remove(int count, Targets targets) + { + FunctionBuilder.Add(Str.Score.Remove(id, count, targets)); + } + public static void Remove(string id, int count, string targets = "") + { + FunctionBuilder.Add(Str.Score.Remove(id, count, targets)); + } + public static void Remove(string id, int count, Targets targets) + { + FunctionBuilder.Add(Str.Score.Remove(id, count, targets)); + } + public void Reset(string targets = "") + { + FunctionBuilder.Add(Str.Score.Reset(id, targets)); + } + public void Reset(Targets targets) + { + FunctionBuilder.Add(Str.Score.Reset(id, targets)); + } + public static void Reset(string id, string targets = "") + { + FunctionBuilder.Add(Str.Score.Reset(id, targets)); + } + public static void Reset(string id, Targets targets) + { + FunctionBuilder.Add(Str.Score.Reset(id, targets)); } /// /// Only for use in scores={} cases @@ -76,5 +92,231 @@ public string Reset(string selector = "") public string Matches(int value){ return this.id + '=' + value; } + /// + /// Only for use in scores={} cases + /// + /// + public static string Matches(string id, int value){ + return id + '=' + value; + } +#region operators + public static Score operator + (Score a, int b) { + FunctionBuilder.Add($"scoreboard players add #{a.id} {a.id} {b}"); + return a; + } + public static Score operator - (Score a, int b) { + FunctionBuilder.Add($"scoreboard players remove #{a.id} {a.id} {b}"); + return a; + } + public static Score operator * (Score a, int b) { + FunctionBuilder.Add( + $"scoreboard players set #{a.id}_temp {a.id} {b}\n"+ + $"scoreboard players operation #{a.id} {a.id} *= #{a.id}_temp {a.id}\n"+ + $"scoreboard players reset #{a.id}_temp {a.id}" + ); + return a; + } + public static Score operator / (Score a, int b) { + FunctionBuilder.Add( + $"scoreboard players set #{a.id}_temp {a.id} {b}\n"+ + $"scoreboard players operation #{a.id} {a.id} /= #{a.id}_temp {a.id}\n"+ + $"scoreboard players reset #{a.id}_temp {a.id}" + ); + return a; + } + public static Score operator % (Score a, int b) { + FunctionBuilder.Add( + $"scoreboard players set #{a.id}_temp {a.id} {b}\n"+ + $"scoreboard players operation #{a.id} {a.id} %= #{a.id}_temp {a.id}\n"+ + $"scoreboard players reset #{a.id}_temp {a.id}" + ); + return a; + } + public static Score operator + (Score a, Score b) { + FunctionBuilder.Add($"scoreboard players operation #{a.id} {a.id} += #{b.id} {b.id}"); + return a; + } + public static Score operator - (Score a, Score b) { + FunctionBuilder.Add($"scoreboard players operation #{a.id} {a.id} -= #{b.id} {b.id}"); + return a; + } + public static Score operator * (Score a, Score b) { + FunctionBuilder.Add($"scoreboard players operation #{a.id} {a.id} *= #{b.id} {b.id}"); + return a; + } + public static Score operator / (Score a, Score b) { + FunctionBuilder.Add($"scoreboard players operation #{a.id} {a.id} /= #{b.id} {b.id}"); + return a; + } + public static Score operator % (Score a, Score b) { + FunctionBuilder.Add($"scoreboard players operation #{a.id} {a.id} %= #{b.id} {b.id}"); + return a; + } + public static Score operator + (Score a, Targets b) { + FunctionBuilder.Add($"scoreboard players operation #{a.id} {a.id} += {TargetsHandler.Get(b)} {a.id}"); + return a; + } + public static Score operator - (Score a, Targets b) { + FunctionBuilder.Add($"scoreboard players operation #{a.id} {a.id} -= {TargetsHandler.Get(b)} {a.id}"); + return a; + } + public static Score operator * (Score a, Targets b) { + FunctionBuilder.Add($"scoreboard players operation #{a.id} {a.id} *= {TargetsHandler.Get(b)} {a.id}"); + return a; + } + public static Score operator / (Score a, Targets b) { + FunctionBuilder.Add($"scoreboard players operation #{a.id} {a.id} /= {TargetsHandler.Get(b)} {a.id}"); + return a; + } + public static Score operator % (Score a, Targets b) { + FunctionBuilder.Add($"scoreboard players operation #{a.id} {a.id} %= {TargetsHandler.Get(b)} {a.id}"); + return a; + } + public static Score operator + (Score a, (string targets, Score scoreboard) b) { + FunctionBuilder.Add($"scoreboard players operation #{a.id} {a.id} += {b.targets} {b.scoreboard.id}"); + return a; + } + public static Score operator - (Score a, (string targets, Score scoreboard) b) { + FunctionBuilder.Add($"scoreboard players operation #{a.id} {a.id} -= {b.targets} {b.scoreboard.id}"); + return a; + } + public static Score operator * (Score a, (string targets, Score scoreboard) b) { + FunctionBuilder.Add($"scoreboard players operation #{a.id} {a.id} *= {b.targets} {b.scoreboard.id}"); + return a; + } + public static Score operator / (Score a, (string targets, Score scoreboard) b) { + FunctionBuilder.Add($"scoreboard players operation #{a.id} {a.id} /= {b.targets} {b.scoreboard.id}"); + return a; + } + public static Score operator % (Score a, (string targets, Score scoreboard) b) { + FunctionBuilder.Add($"scoreboard players operation #{a.id} {a.id} %= {b.targets} {b.scoreboard.id}"); + return a; + } + public static Score operator + (Score a, string b) { + FunctionBuilder.Add( + $"execute store result score #{a.id}_temp {a.id} run {b}\n"+ + $"scoreboard players operation #{a.id} {a.id} += #{a.id}_temp {a.id}\n"+ + $"scoreboard players reset #{a.id}_temp" + ); + return a; + } + public static Score operator - (Score a, string b) { + FunctionBuilder.Add( + $"execute store result score #{a.id}_temp {a.id} run {b}\n"+ + $"scoreboard players operation #{a.id} {a.id} -= #{a.id}_temp {a.id}\n"+ + $"scoreboard players reset #{a.id}_temp" + ); + return a; + } + public static Score operator * (Score a, string b) { + FunctionBuilder.Add( + $"execute store result score #{a.id}_temp {a.id} run {b}\n"+ + $"scoreboard players operation #{a.id} {a.id} *= #{a.id}_temp {a.id}\n"+ + $"scoreboard players reset #{a.id}_temp" + ); + return a; + } + public static Score operator / (Score a, string b) { + FunctionBuilder.Add( + $"execute store result score #{a.id}_temp {a.id} run {b}\n"+ + $"scoreboard players operation #{a.id} {a.id} /= #{a.id}_temp {a.id}\n"+ + $"scoreboard players reset #{a.id}_temp" + ); + return a; + } + public static Score operator % (Score a, string b) { + FunctionBuilder.Add( + $"execute store result score #{a.id}_temp {a.id} run {b}\n"+ + $"scoreboard players operation #{a.id} {a.id} %= #{a.id}_temp {a.id}\n"+ + $"scoreboard players reset #{a.id}_temp" + ); + return a; + } + public static bool operator == (Score a, int b) { + FunctionBuilder.Add($"execute if score #{a.id} myVariable matches {b} run \\"); + return true; + } + public static bool operator != (Score a, int b) { + FunctionBuilder.Add($"execute unless score #{a.id} {a.id} matches {b} run \\"); + return true; + } + public static bool operator >= (Score a, int b) { + FunctionBuilder.Add($"execute if score #{a.id} {a.id} matches {b}.. run \\"); + return true; + } + public static bool operator <= (Score a, int b) { + FunctionBuilder.Add($"execute if score #{a.id} {a.id} matches ..{b} run \\"); + return true; + } + public static bool operator > (Score a, int b) { + FunctionBuilder.Add($"execute unless score #{a.id} {a.id} matches ..{b}"); + return true; + } + public static bool operator < (Score a, int b) { + FunctionBuilder.Add($"execute unless score #{a.id} {a.id} matches {b}.."); + return true; + } + public static bool operator == (Score a, Score b) { + FunctionBuilder.Add($"execute if score #{a.id} {a.id} = #{b.id} {b.id} run \\"); + return true; + } + public static bool operator != (Score a, Score b) { + FunctionBuilder.Add($"execute unless score #{a.id} {a.id} = #{b.id} {b.id} run \\"); + return true; + } + public static bool operator >= (Score a, Score b) { + FunctionBuilder.Add($"execute if score #{a.id} {a.id} >= #{b.id} {b.id} run \\"); + return true; + } + public static bool operator <= (Score a, Score b) { + FunctionBuilder.Add($"execute if score #{a.id} {a.id} <= #{b.id} {b.id} run \\"); + return true; + } + public static bool operator > (Score a, Score b) { + FunctionBuilder.Add($"execute if score #{a.id} {a.id} > #{b.id} {b.id} run \\"); + return true; + } + public static bool operator < (Score a, Score b) { + FunctionBuilder.Add($"execute if score #{a.id} {a.id} < #{b.id} {b.id} run \\"); + return true; + } +#endregion + public override bool Equals(object? obj) { + if(obj is Score s) { + return true; + } else { + return false; + } + } + + public override int GetHashCode() => GetHashCode(); + + public static void Enum(string name, string[] keys, out ScoreEnum scoreEnum) { + Dictionary keyValuePairs = new(); + List commands = [$"scoreboard objectives add {name} dummy"]; + int i = 0; + scoreEnum = []; + scoreEnum.Name = name; + foreach(var key in keys) { + commands.Add($"scoreboard players set #{key} {name} {i}"); + keyValuePairs[key] = i; + scoreEnum.Add(key, keyValuePairs[key]); + i++; + } + i = 0; + foreach(var key in keys) { + scoreEnum.EnumMap.Add(i, key); + i++; + } + + FunctionBuilder.Add(commands); + } + public static void Trigger(string name, Targets targets, Function onTrigger, ref Trigger trigger) { + trigger = new Trigger(name, targets, onTrigger); + } + + public static void Trigger(string name, string targets, Function onTrigger, out Trigger trigger) { + trigger = new Trigger(name, targets, onTrigger); + } } -} +} \ No newline at end of file diff --git a/Core/ScoreEnum.cs b/Core/ScoreEnum.cs new file mode 100644 index 0000000..874d13a --- /dev/null +++ b/Core/ScoreEnum.cs @@ -0,0 +1,57 @@ +using SteveSharp.Generic; + +namespace SteveSharp.Core; + +public class ScoreEnum() : Dictionary { + public string? Name { get; set; } + public Dictionary EnumMap { get; set; } = []; + public void Switch(Targets targets, Dictionary cases) { + List commands = []; + + foreach(var codeBlock in cases) { + foreach(var command in codeBlock.Value) { + commands.Add($"execute as {TargetsHandler.Get(targets)} if score @s {Name} = #{EnumMap[codeBlock.Key]} {Name} run {command}"); + } + } + + FunctionBuilder.Add(commands); + } + public void Switch(Targets asat, Dictionary> cases) { + List commands = []; + + foreach(var codeBlock in cases) { + foreach(var command in codeBlock.Value) { + commands.Add($"execute as {TargetsHandler.Get(asat)} at @s if score @s {Name} = #{EnumMap[codeBlock.Key]} {Name} run {command}"); + } + } + + FunctionBuilder.Add(commands); + } + + public void Reset(Targets targets) => FunctionBuilder.Add($"scoreboard players reset {TargetsHandler.Get(targets)} {Name}"); + public void Reset(string targets) => FunctionBuilder.Add($"scoreboard players reset {targets} {Name}"); + public void Next(Targets targets) { + FunctionBuilder.Add([ + $"execute as {TargetsHandler.Get(targets)} if score @s {Name} >= #{EnumMap[EnumMap.Count-1]} {Name} run scoreboard players set @s {Name} -1", + $"execute as {TargetsHandler.Get(targets)} unless score @s {Name} > #{EnumMap[EnumMap.Count-1]} {Name} run scoreboard players add @s {Name} 1" + ]); + } + public void Next(string targets) { + FunctionBuilder.Add([ + $"execute as {targets} if score @s {Name} >= #{EnumMap[EnumMap.Count-1]} {Name} run scoreboard players set @s {Name} -1", + $"execute as {targets} unless score @s {Name} > #{EnumMap[EnumMap.Count-1]} {Name} run scoreboard players add @s {Name} 1" + ]); + } + public void Past(Targets targets) { + FunctionBuilder.Add([ + $"execute as {TargetsHandler.Get(targets)} if score @s {Name} matches ..0 run scoreboard players set @s {Name} {Count-1}", + $"execute as {TargetsHandler.Get(targets)} unless score @s {Name} matches ..1 run scoreboard players remove @s {Name} 1" + ]); + } + public void Past(string targets) { + FunctionBuilder.Add([ + $"execute as {targets} if score @s {Name} matches ..0 run scoreboard players set @s {Name} {Count-1}", + $"execute as {targets} unless score @s {Name} matches ..1 run scoreboard players remove @s {Name} 1" + ]); + } +} \ No newline at end of file diff --git a/Core/Strings/Bossbar.cs b/Core/Strings/Bossbar.cs new file mode 100644 index 0000000..00942d4 --- /dev/null +++ b/Core/Strings/Bossbar.cs @@ -0,0 +1,63 @@ +using SteveSharp.JsonShapes; +using SteveSharp.Generic; +using System.Text.Json; +using BB = SteveSharp.Core.Bossbar; + +namespace SteveSharp.Core.Strings; + +public class Bossbar +{ + public string? Id { get; set; } + public static string Add(string id, TextComponent text) => $"bossbar add {id} {JsonSerializer.Serialize(text)}"; + public static string Add(string id, TextComponent[] text) => $"bossbar add {id} {JsonSerializer.Serialize(text)}"; + public static string Get(string id, BB.GetField field) + { + Dictionary fieldStr = new() { + {BB.GetField.Max, "max"}, + {BB.GetField.Players, "players"}, + {BB.GetField.Value, "value"}, + {BB.GetField.Visible, "visible"} + }; + return $"bossbar get {id} {fieldStr[field]}"; + } + public static string Get(string id, string field) => $"bossbar get {id} {field}"; + public static string List() => "bossbar list"; + public static string Remove(string id) => $"bossbar remove {id}"; + public static string SetColor(string id, Color color) => $"bossbar set {id} color {ColorHandler.Get(color)}"; + public static string SetColor(string id, string color) => $"bossbar set {id} color {color}"; + public static string SetMax(string id, int max) => $"bossbar set {id} max {max}"; + public static string SetName(string id, TextComponent name) => $"bossbar set {id} name {JsonSerializer.Serialize(name)}"; + public static string SetName(string id, TextComponent[] name) => $"bossbar set {id} name {JsonSerializer.Serialize(name)}"; + public static string SetPlayers(string id, string targets) => $"bossbar set {id} players {targets}"; + public static string SetStyle(string id, BB.Style style) + { + Dictionary styleStr = new() { + {BB.Style.Notched6, "notched_6"}, + {BB.Style.Notched10, "notched_10"}, + {BB.Style.Notched12, "notched_12"}, + {BB.Style.Notched20, "notched_20"} + }; + return $"bossbar set {id} style {styleStr[style]}"; + } + public static string SetStyle(string id, string style) => $"bossbar set {id} style {style}"; + public static string SetValue(string id, int value) => $"bossbar set {id} value {value}"; + public static string SetVisible(string id, bool visible) => $"bossbar set {id} visible {visible.ToString().ToLower()}"; + public Bossbar(string id) + { + Id = id; + } + public string Add(TextComponent text) => Add(this.Id!, text); + public string Add(TextComponent[] text) => Add(this.Id!, text); + public string Get(BB.GetField field) => Get(this.Id!, field); + public string Get(string field) => Get(this.Id!, field); + public string SetColor(Color color) => SetColor(this.Id!, color); + public string SetColor(string color) => SetColor(this.Id!, color); + public string SetMax(int max) => SetMax(this.Id!, max); + public string SetName(TextComponent name) => SetName(this.Id!, name); + public string SetName(TextComponent[] name) => SetName(this.Id!, name); + public string SetPlayers(string targets) => SetPlayers(this.Id!, targets); + public string SetStyle(BB.Style style) => SetStyle(this.Id!, style); + public string SetStyle(string style) => SetStyle(this.Id!, style); + public string SetValue(int value) => SetValue(this.Id!, value); + public string SetVisible(bool visible) => SetVisible(this.Id!, visible); +} \ No newline at end of file diff --git a/Core/Strings/Chat.cs b/Core/Strings/Chat.cs new file mode 100644 index 0000000..120dcd8 --- /dev/null +++ b/Core/Strings/Chat.cs @@ -0,0 +1,40 @@ +using SteveSharp.Generic; +using SteveSharp.JsonShapes; +using System.Text.Json; + +namespace SteveSharp.Core.Strings +{ + public static class Chat + { + public static string Say(string msg) + { + return $"say {msg}"; + } + public static string Tell(Targets targets, string message) { + return $"tell {TargetsHandler.Get(targets)} {message}"; + } + public static string Tell(string targets, string message) { + return $"tell {targets} {message}"; + } + public static string Tellraw(Targets targets, TextComponent[] text) + { + string command = "tellraw " + TargetsHandler.Get(targets) + " " + JsonSerializer.Serialize(text); + return command; + } + public static string Tellraw(string targets, TextComponent[] text) + { + string command = "tellraw " + targets + " " + JsonSerializer.Serialize(text); + return command; + } + public static string Tellraw(Targets targets, TextComponent text) + { + string command = "tellraw " + TargetsHandler.Get(targets) + " " + JsonSerializer.Serialize(text); + return command; + } + public static string Tellraw(string targets, TextComponent text) + { + string command = "tellraw " + targets + " " + JsonSerializer.Serialize(text); + return command; + } + } +} diff --git a/Core/Strings/Effect.cs b/Core/Strings/Effect.cs new file mode 100644 index 0000000..bbacc49 --- /dev/null +++ b/Core/Strings/Effect.cs @@ -0,0 +1,67 @@ +using SteveSharp.Generic; + +namespace SteveSharp.Core.Strings; + +public static class Effect { + public static string Give(Targets targets, StatusEffect effect, bool infinite = false, int amplifier = 0) { + if(infinite) { + return $"effect give {TargetsHandler.Get(targets)} {StatusEffectHandler.Get(effect)} infinite {amplifier}"; + } else { + return $"effect give {TargetsHandler.Get(targets)} {StatusEffectHandler.Get(effect)}"; + } + } + public static string Give(string targets, StatusEffect effect, bool infinite = false, int amplifier = 0) { + if(infinite) { + return $"effect give {targets} {StatusEffectHandler.Get(effect)} infinite {amplifier}"; + } else { + return $"effect give {targets} {StatusEffectHandler.Get(effect)}"; + } + } + public static string Give(Targets targets, StatusEffect effect, int seconds, int amplifier = 0) { + return $"effect give {TargetsHandler.Get(targets)} {StatusEffectHandler.Get(effect)} {seconds} {amplifier}"; + } + public static string Give(string targets, StatusEffect effect, int seconds, int amplifier = 0) { + return $"effect give {targets} {StatusEffectHandler.Get(effect)} {seconds} {amplifier}"; + } + public static string Give(Targets targets, string effect, bool infinite = false, int amplifier = 0) { + if(infinite) { + return $"effect give {TargetsHandler.Get(targets)} {effect} infinite {amplifier}"; + } else { + return $"effect give {TargetsHandler.Get(targets)} {effect}"; + } + } + public static string Give(string targets, string effect, bool infinite = false, int amplifier = 0) { + if(infinite) { + return $"effect give {targets} {effect} infinite {amplifier}"; + } else { + return $"effect give {targets} {effect}"; + } + } + public static string Give(Targets targets, string effect, int seconds, int amplifier = 0) { + return $"effect give {TargetsHandler.Get(targets)} {effect} {seconds} {amplifier}"; + } + public static string Give(string targets, string effect, int seconds, int amplifier = 0) { + return $"effect give {targets} {effect} {seconds} {amplifier}"; + } + public static string Clear() { + return "effect clear"; + } + public static string Clear(Targets targets) { + return $"effect clear {TargetsHandler.Get(targets)}"; + } + public static string Clear(string targets) { + return $"effect clear {targets}"; + } + public static string Clear(Targets targets, StatusEffect effect) { + return $"effect clear {TargetsHandler.Get(targets)} {StatusEffectHandler.Get(effect)}"; + } + public static string Clear(string targets, StatusEffect effect) { + return $"effect clear {targets} {StatusEffectHandler.Get(effect)}"; + } + public static string Clear(Targets targets, string effect) { + return $"effect clear {TargetsHandler.Get(targets)} {effect}"; + } + public static string Clear(string targets, string effect) { + return $"effect clear {targets} {effect}"; + } +} \ No newline at end of file diff --git a/Core/Strings/EnchantCommand.cs b/Core/Strings/EnchantCommand.cs new file mode 100644 index 0000000..8819cac --- /dev/null +++ b/Core/Strings/EnchantCommand.cs @@ -0,0 +1,14 @@ +using SteveSharp.Generic; + +namespace SteveSharp.Core.Strings; + +public static class EnchantCommand { + public static string Enchant(Targets targets, Enchantment enchantment) => $"enchant {TargetsHandler.Get(targets)} {EnchantmentHandler.Get(enchantment)}"; + public static string Enchant(Targets targets, Enchantment enchantment, int level) => $"enchant {TargetsHandler.Get(targets)} {EnchantmentHandler.Get(enchantment)} {level}"; + public static string Enchant(Targets targets, string enchantment) => $"enchant {TargetsHandler.Get(targets)} {enchantment}"; + public static string Enchant(Targets targets, string enchantment, int level) => $"enchant {TargetsHandler.Get(targets)} {enchantment} {level}"; + public static string Enchant(string targets, string enchantment) => $"enchant {targets} {enchantment}"; + public static string Enchant(string targets, string enchantment, int level) => $"enchant {targets} {enchantment} {level}"; + public static string Enchant(string targets, Enchantment enchantment) => $"enchant {targets} {EnchantmentHandler.Get(enchantment)}"; + public static string Enchant(string targets, Enchantment enchantment, int level) => $"enchant {targets} {EnchantmentHandler.Get(enchantment)} {level}"; +} \ No newline at end of file diff --git a/Core/Strings/Entity.cs b/Core/Strings/Entity.cs new file mode 100644 index 0000000..f1725a5 --- /dev/null +++ b/Core/Strings/Entity.cs @@ -0,0 +1,103 @@ +using SteveSharp.Generic; + +namespace SteveSharp.Core.Strings +{ + public static class Entity + { + /// + /// This method selects an entity with specific matches, recommended if you want to select an entity with specific matches. + /// + /// + public static string Custom(Targets targets, int? limit = null, string[]? tags = null, string[]? scores = null, string? team = null, string? type = null, string? distance = null, string? area = null, string? level = null, string? gamemode = null, string? horizontalRotation = null, string? verticalRotation = null, string? sort = null) + => Custom(TargetsHandler.Get(targets), limit, tags, scores, team, type, distance, area, level, gamemode, horizontalRotation, verticalRotation, sort); + /// + /// This method selects an entity with specific matches, recommended if you want to select an entity with specific matches. + /// + /// + public static string Custom(string targets, int? limit = null, string[]? tags = null, string[]? scores = null, string? team = null, string? type = null, string? distance = null, string? area = null, string? level = null, string? gamemode = null, string? horizontalRotation = null, string? verticalRotation = null, string? sort = null) { + string match = ""; + match += targets + "["; + if(limit != null) match += "limit=" + limit + ','; + if(tags?.Length >= 1){ + foreach(string tag in tags){ + match += "tag=" + tag + ","; + } + } + if(scores?.Length >= 1){ + match += "scores={"; + foreach(string score in scores){ + match += score + ','; + } + match += "},"; + } + if(team != null) match += "team=" + team + ','; + if(type != null && targets != "@a" || targets != "@r" || targets != "@p") + match += "type=" + type + ','; + if(distance != null) match += "distance=" + distance + ','; + if(area != null) match += area + ','; + if(level != null) match += "level=" + level + ','; + if(gamemode != null && targets != "@e") match += "gamemode=" + gamemode + ','; + if(horizontalRotation != null) match += "y_rotation=" + horizontalRotation + ','; + if(verticalRotation != null) match += "x_rotation=" + verticalRotation + ','; + if(sort != null) match += "sort=" + sort + ','; + if (match.EndsWith(',')) match.Remove(match.Length - 1); + match += ']'; + return match; + } + public static string Teleport(Targets targets, string to) + { + return $"tp {TargetsHandler.Get(targets)} {to}"; + } + public static string Teleport(string targets, string to) + { + return $"tp {targets} {to}"; + } + public static string Summon(EntityEnum entity, (int X, int Y, int Z) pos, string nbt = "{}") + { + return $"summon {EntityHandler.Get(entity)} {pos.X} {pos.Y} {pos.Z} {nbt}"; + } + public static string Summon(EntityEnum entity, (string X, string Y, string Z) rel, string nbt = "{}") + { + return $"summon {EntityHandler.Get(entity)} {rel.X} {rel.Y} {rel.Z} {nbt}"; + } + public static string Summon(EntityEnum entity, string[] pos, string nbt = "{}") { + return Summon(EntityHandler.Get(entity), pos, nbt); + } + public static string Summon(string entity, (int X, int Y, int Z) pos, string nbt = "{}") + { + return $"summon {entity} {pos.X} {pos.Y} {pos.Z} {nbt}"; + } + public static string Summon(string entity, (string X, string Y, string Z) rel, string nbt = "{}") + { + return $"summon {entity} {rel.X} {rel.Y} {rel.Z} {nbt}"; + } + public static string Summon(string entity, string[] pos, string nbt = "{}") + { + return $"summon {entity} {pos[0]} {pos[1]} {pos[2]} {nbt}"; + } + public static string AddTag(Targets targets, string tag) + { + return $"tag {TargetsHandler.Get(targets)} add {tag}"; + } + public static string AddTag(string targets, string tag) + { + return $"tag {targets} add {tag}"; + } + public static string RemoveTag(Targets targets, string tag) + { + return $"tag {TargetsHandler.Get(targets)} remove {tag}"; + } + public static string RemoveTag(string targets, string tag) + { + return $"tag {targets} remove {tag}"; + } + public static string Kill(Targets targets) + { + return $"kill {TargetsHandler.Get(targets)}"; + } + public static string Kill(string targets) + { + return $"kill {targets}"; + } + } +} diff --git a/Core/Strings/Execute.cs b/Core/Strings/Execute.cs new file mode 100644 index 0000000..3590c9e --- /dev/null +++ b/Core/Strings/Execute.cs @@ -0,0 +1,78 @@ +using SteveSharp.Generic; +using Core = SteveSharp.Core; + +namespace SteveSharp.Core.Strings +{ + public static class Execute + { + const string[] defaults = null; + public static string Write(string execute, string[] commands = defaults) + { + string allCommands = ""; + foreach (string command in commands) + { + allCommands += "execute " + execute + "run " + command + "\n"; + } + return allCommands; + } + public static string As(Targets targets, string addition = "") + { + return "as " + TargetsHandler.Get(targets) + " " + addition; + } + public static string As(string targets, string addition = "") + { + return "as " + targets + " " + addition; + } + public static string At(Targets targets, string addition = "") + { + return "at " + TargetsHandler.Get(targets) + " " + addition; + } + public static string At(string targets, string addition = "") + { + return "at " + targets + " " + addition; + } + public static string Asat(Targets targets, string addition = "") + { + return "as " + TargetsHandler.Get(targets) + " at @s " + addition; + } + public static string Asat(string targets, string addition = "") + { + return "as " + targets + " at @s " + addition; + } + public static string Unless(string arguments, string addition = "") + { + return "unless " + arguments + " " + addition; + } + public static string If(string arguments, string addition = "") + { + return "if " + arguments + " " + addition; + } + public static string Summon(EntityEnum entity, string[] pos, string addition = "") + { + return "summon " + EntityHandler.Get(entity) + " " + pos[0] + " " + pos[1] + " " + pos[2] + " " + addition; + } + public static string Summon(string entity, string[] pos, string addition = "") + { + return "summon " + entity + " " + pos[0] + " " + pos[1] + " " + pos[2] + " " + addition; + } + public static string Store(string where, string at, string arguments, string addition = "") + { + return "store " + where + " " + at + " " + arguments + " " + addition; + } + public static string StoreScore(string where, Core.Score score, Targets targets, string addition = "") + { + return "store " + where + " score " + TargetsHandler.Get(targets) + " " + score.id + " " + addition; + } + public static string StoreScore(string where, Core.Score score, string targets = "", string addition = "") + { + if (targets == "") + { + return "store " + where + " score " + "#" + score.id + " " + score.id + " " + addition; + } + else + { + return "store " + where + " score " + targets + " " + score.id + " " + addition; + } + } + } +} diff --git a/Core/Strings/For.cs b/Core/Strings/For.cs new file mode 100644 index 0000000..c993867 --- /dev/null +++ b/Core/Strings/For.cs @@ -0,0 +1,18 @@ +namespace SteveSharp.Core.Strings; + +public static class For +{ + public static string Loop(int to, Func> block, int from = 0) { + List> fullBlock = new(); + for(int i = from; i <= to; i++) { + fullBlock.Add(block(i)); + } + List ret = new(); + foreach(var codeBlock in fullBlock) { + foreach(var command in codeBlock) { + ret.Add(command); + } + } + return string.Join("\n", ret); + } +} \ No newline at end of file diff --git a/Core/Strings/Function.cs b/Core/Strings/Function.cs new file mode 100644 index 0000000..36c87fb --- /dev/null +++ b/Core/Strings/Function.cs @@ -0,0 +1,13 @@ +namespace SteveSharp.Core.Strings; + +public static class Function +{ + public static string Return(int i) => "return " + i; + public static string Call(string function) => "function " + function; + public static string Schedule(string function, string time, bool append = false, bool replace = false) { + if (append && !replace) return $"schedule function {function} {time} append"; + else if (!append && replace) return $"schedule function {function} {time} replace"; + else return $"schedule function {function} {time}"; + } + public static string ClearSchedule(string function) => $"schedule clear {function}"; +} \ No newline at end of file diff --git a/Core/Strings/Recipe.cs b/Core/Strings/Recipe.cs new file mode 100644 index 0000000..ac273ba --- /dev/null +++ b/Core/Strings/Recipe.cs @@ -0,0 +1,10 @@ +using SteveSharp.Generic; + +namespace SteveSharp.Core.Strings; + +public static class Recipe { + public static string Give(Targets targets, string recipe) => $"recipe give {TargetsHandler.Get(targets)} {recipe}"; + public static string Give(string targets, string recipe) => $"recipe give {targets} {recipe}"; + public static string Take(Targets targets, string recipe) => $"recipe take {TargetsHandler.Get(targets)} {recipe}"; + public static string Take(string targets, string recipe) => $"recipe take {targets} {recipe}"; +} \ No newline at end of file diff --git a/Core/Strings/Score.cs b/Core/Strings/Score.cs new file mode 100644 index 0000000..59f5e17 --- /dev/null +++ b/Core/Strings/Score.cs @@ -0,0 +1,144 @@ +using SteveSharp.Generic; + +namespace SteveSharp.Core.Strings +{ + public class Score + { + public int count = 0; + public string id; + public string type; + public string name; + public Score(string id, string type = "dummy", string name = "", int count = 0) + { + this.id = id; + this.count = count; + this.type = type; + this.name = name; + } + public string AddObjective() + { + return AddObjective(id, type, name); + } + public static string AddObjective(string id, string type, string name = "") + { + if(name == "") { + return $"scoreboard objectives add {id} {type}"; + } else { + return $"scoreboard objectives add {id} {type} {name}"; + } + } + public static string AddObjectives(Score[] scores) + { + string commands = ""; + foreach(Score score in scores) + { + commands += AddObjective(score.id, score.type, score.name) + "\n"; + } + return commands; + } + public string Set(int count, string targets = "") + { + return Set(id, count, targets); + } + public string Set(int count, Targets targets) + { + return Set(id, count, TargetsHandler.Get(targets)); + } + public static string Set(string id, int count, string targets = "") + { + if(targets == "") + { + return $"scoreboard players set #{id} {id} {count}"; + } else + { + return $"scoreboard players set {targets} {id} {count}"; + } + } + public static string Set(string id, int count, Targets targets) + { + return $"scoreboard players set {TargetsHandler.Get(targets)} {id} {count}"; + } + public string Add(int count, string targets = "") + { + return Add(id, count, targets); + } + public string Add(int count, Targets targets) { + return Add(id, count, TargetsHandler.Get(targets)); + } + public static string Add(string id, int count, string targets = "") + { + if (targets == "") + { + return $"scoreboard players add #{id} {id} {count}"; + } + else + { + return $"scoreboard players add {targets} {id} {count}"; + } + } + public static string Add(string id, int count, Targets targets) + { + return $"scoreboard players add {TargetsHandler.Get(targets)} {id} {count}"; + } + public string Remove(int count, string targets = "") + { + return Remove(id, count, targets); + } + public string Remove(int count, Targets targets) + { + return Remove(id, count, targets); + } + public static string Remove(string id, int count, string targets = "") + { + if (targets == "") + { + return $"scoreboard players remove #{id} {id} {count}"; + } + else + { + return $"scoreboard players remove {targets} {id} {count}"; + } + } + public static string Remove(string id, int count, Targets targets) + { + return $"scoreboard players remove {TargetsHandler.Get(targets)} {id} {count}"; + } + public string Reset(string targets = "") + { + return Reset(id, targets); + } + public string Reset(Targets targets) + { + return Reset(id, TargetsHandler.Get(targets)); + } + public static string Reset(string id, string targets = "") + { + if (targets == "") + { + return $"scoreboard players reset #{id} {id}"; + } + else + { + return $"scoreboard players reset {targets} {id}"; + } + } + public static string Reset(string id, Targets targets) + { + return $"scoreboard players reset {TargetsHandler.Get(targets)} {id}"; + } + /// + /// Only for use in scores={} cases + /// + /// + public string Matches(int value){ + return Matches(id, value); + } + /// + /// Only for use in scores={} cases + /// + /// + public static string Matches(string id, int value){ + return id + '=' + value; + } + } +} diff --git a/Core/Strings/Title.cs b/Core/Strings/Title.cs new file mode 100644 index 0000000..104cd38 --- /dev/null +++ b/Core/Strings/Title.cs @@ -0,0 +1,74 @@ +using SteveSharp.Generic; +using SteveSharp.JsonShapes; +using System.Text.Json; + +namespace SteveSharp.Core.Strings; + +public static class Title { +#region /title actionbar + public static string Actionbar(Targets targets, TextComponent text) { + return $"title {TargetsHandler.Get(targets)} actionbar {JsonSerializer.Serialize(text)}"; + } + public static string Actionbar(Targets targets, TextComponent[] text) { + return $"title {TargetsHandler.Get(targets)} actionbar {JsonSerializer.Serialize(text)}"; + } + public static string Actionbar(string targets, TextComponent text) { + return $"title {targets} actionbar {JsonSerializer.Serialize(text)}"; + } + public static string Actionbar(string targets, TextComponent[] text) { + return $"title {targets} actionbar {JsonSerializer.Serialize(text)}"; + } +#endregion +#region /title title + public static string ShowTitle(Targets targets, TextComponent text) { + return $"title {TargetsHandler.Get(targets)} title {JsonSerializer.Serialize(text)}"; + } + public static string ShowTitle(Targets targets, TextComponent[] text) { + return $"title {TargetsHandler.Get(targets)} title {JsonSerializer.Serialize(text)}"; + } + public static string ShowTitle(string targets, TextComponent text) { + return $"title {targets} title {JsonSerializer.Serialize(text)}"; + } + public static string ShowTitle(string targets, TextComponent[] text) { + return $"title {targets} title {JsonSerializer.Serialize(text)}"; + } +#endregion +#region /title subtitle + public static string Subtitle(Targets targets, TextComponent text) { + return $"title {TargetsHandler.Get(targets)} subtitle {JsonSerializer.Serialize(text)}"; + } + public static string Subtitle(Targets targets, TextComponent[] text) { + return $"title {TargetsHandler.Get(targets)} subtitle {JsonSerializer.Serialize(text)}"; + } + public static string Subtitle(string targets, TextComponent text) { + return $"title {targets} subtitle {JsonSerializer.Serialize(text)}"; + } + public static string Subtitle(string targets, TextComponent[] text) { + return $"title {targets} subtitle {JsonSerializer.Serialize(text)}"; + } +#endregion +#region /title clear + public static string Clear(Targets target) { + return $"title {TargetsHandler.Get(target)} clear"; + } + public static string Clear(string targets) { + return $"title {targets} clear"; + } +#endregion +#region /title reset + public static string Reset(Targets targets) { + return $"title {TargetsHandler.Get(targets)} reset"; + } + public static string Reset(string targets) { + return $"title {targets} reset"; + } +#endregion +#region /title times + public static string Times(Targets targets, string fadeIn, string stay, string fadeOut) { + return $"title {TargetsHandler.Get(targets)} times {fadeIn} {stay} {fadeOut}"; + } + public static string Times(string targets, string fadeIn, string stay, string fadeOut) { + return $"title {targets} times {fadeIn} {stay} {fadeOut}"; + } +#endregion +} \ No newline at end of file diff --git a/Core/Strings/XP.cs b/Core/Strings/XP.cs new file mode 100644 index 0000000..d0eadb7 --- /dev/null +++ b/Core/Strings/XP.cs @@ -0,0 +1,86 @@ +using SteveSharp.Generic; + +namespace SteveSharp.Core.Strings; + +public static class XP +{ + public static string AddXP(Targets targets, int amount) + { + return $"xp add {TargetsHandler.Get(targets)} {amount}"; + } + + public static string AddXP(string targets, int amount) + { + return $"xp add {targets} {amount}"; + } + + public static string AddXPLevels(Targets targets, int amount) + { + return $"xp add {TargetsHandler.Get(targets)} {amount} levels"; + } + + public static string AddXPLevels(string targets, int amount) + { + return $"xp add {targets} {amount} levels"; + } + + public static string AddXPPoints(Targets targets, int amount) + { + return $"xp add {TargetsHandler.Get(targets)} {amount} points"; + } + + public static string AddXPPoints(string targets, int amount) + { + return $"xp add {targets} {amount} points"; + } + + public static string XPLevelsQuery(Targets targets) + { + return $"xp query {TargetsHandler.Get(targets)} levels"; + } + + public static string XPLevelsQuery(string targets) + { + return $"xp query {targets} levels"; + } + + public static string XPQuery(Targets targets) + { + return $"xp query {TargetsHandler.Get(targets)} points"; + } + + public static string XPQuery(string targets) + { + return $"xp query {targets} points"; + } + + public static string SetXP(Targets targets, int amount) + { + return $"xp set {TargetsHandler.Get(targets)} {amount}"; + } + + public static string SetXP(string targets, int amount) + { + return $"xp set {targets} {amount}"; + } + + public static string SetXPLevels(Targets targets, int amount) + { + return $"xp set {TargetsHandler.Get(targets)} {amount} levels"; + } + + public static string SetXPLevels(string targets, int amount) + { + return $"xp set {targets} {amount} levels"; + } + + public static string SetXPPoints(Targets targets, int amount) + { + return $"xp set {TargetsHandler.Get(targets)} {amount} points"; + } + + public static string SetXPPoints(string targets, int amount) + { + return $"xp set {targets} {amount} points"; + } +} \ No newline at end of file diff --git a/Core/Text.cs b/Core/Text.cs deleted file mode 100644 index c892675..0000000 --- a/Core/Text.cs +++ /dev/null @@ -1,29 +0,0 @@ -using SteveSharp.JsonShapes; -using System.Text.Json; - -namespace SteveSharp.Core -{ - public static class Text - { - public static TextComponent New(string text, string? color = null, bool italic = false, bool bold = false, bool underlined = false, bool obfuscated = false, bool strikethrough = false, string? insertion = null, string? clickEventAction = null, string? clickEventValue = null) - { - TextComponent textComponent = new TextComponent - { - text = text, - color = color, - italic = italic, - bold = bold, - underlined = underlined, - obfuscated = obfuscated, - strikethrough = strikethrough, - insertion = insertion, - clickEvent = new clickEvent - { - action = clickEventAction, - value = clickEventValue - } - }; - return textComponent; - } - } -} diff --git a/Core/Time.cs b/Core/Time.cs new file mode 100644 index 0000000..81ce2b1 --- /dev/null +++ b/Core/Time.cs @@ -0,0 +1,8 @@ +namespace SteveSharp.Core; + +public class Time { + public static string Ticks(int ticks) => ticks + "t"; + public static string Seconds(int seconds) => seconds + "s"; + public static string Days(int days) => days + "d"; + public static string GetTimestamp() => "time query gametime"; +} \ No newline at end of file diff --git a/Core/Title.cs b/Core/Title.cs new file mode 100644 index 0000000..ea4e235 --- /dev/null +++ b/Core/Title.cs @@ -0,0 +1,38 @@ +using SteveSharp.Generic; +using SteveSharp.JsonShapes; +using Str = SteveSharp.Core.Strings; + +namespace SteveSharp.Core; + +public static class Title { +#region /title actionbar + public static void Actionbar(Targets targets, TextComponent text) => FunctionBuilder.Add(Str.Title.Actionbar(targets, text)); + public static void Actionbar(Targets targets, TextComponent[] text) => FunctionBuilder.Add(Str.Title.Actionbar(targets, text)); + public static void Actionbar(string targets, TextComponent text) => FunctionBuilder.Add(Str.Title.Actionbar(targets, text)); + public static void Actionbar(string targets, TextComponent[] text) => FunctionBuilder.Add(Str.Title.Actionbar(targets, text)); +#endregion +#region /title title + public static void ShowTitle(Targets targets, TextComponent text) => FunctionBuilder.Add(Str.Title.ShowTitle(targets, text)); + public static void ShowTitle(Targets targets, TextComponent[] text) => FunctionBuilder.Add(Str.Title.ShowTitle(targets, text)); + public static void ShowTitle(string targets, TextComponent text) => FunctionBuilder.Add(Str.Title.ShowTitle(targets, text)); + public static void ShowTitle(string targets, TextComponent[] text) => FunctionBuilder.Add(Str.Title.ShowTitle(targets, text)); +#endregion +#region /title subtitle + public static void Subtitle(Targets targets, TextComponent text) => FunctionBuilder.Add(Str.Title.Subtitle(targets, text)); + public static void Subtitle(Targets targets, TextComponent[] text) => FunctionBuilder.Add(Str.Title.Subtitle(targets, text)); + public static void Subtitle(string targets, TextComponent text) => FunctionBuilder.Add(Str.Title.Subtitle(targets, text)); + public static void Subtitle(string targets, TextComponent[] text) => FunctionBuilder.Add(Str.Title.Subtitle(targets, text)); +#endregion +#region /title clear + public static void Clear(Targets target) => FunctionBuilder.Add(Str.Title.Clear(target)); + public static void Clear(string target) => FunctionBuilder.Add(Str.Title.Clear(target)); +#endregion +#region /title reset + public static void Reset(Targets targets) => FunctionBuilder.Add(Str.Title.Reset(targets)); + public static void Reset(string targets) => FunctionBuilder.Add(Str.Title.Reset(targets)); +#endregion +#region /title times + public static void Times(Targets targets, string fadeIn, string stay, string fadeOut) => FunctionBuilder.Add(Str.Title.Times(targets, fadeIn, stay, fadeOut)); + public static void Times(string targets, string fadeIn, string stay, string fadeOut) => FunctionBuilder.Add(Str.Title.Times(targets, fadeIn, stay, fadeOut)); +#endregion +} \ No newline at end of file diff --git a/Core/Trigger.cs b/Core/Trigger.cs new file mode 100644 index 0000000..d3a09d2 --- /dev/null +++ b/Core/Trigger.cs @@ -0,0 +1,31 @@ +using SteveSharp.Generic; + +namespace SteveSharp.Core; + +public class Trigger : Score { + public string Name { get; set; } = "trigger_"+new Random().Next(1,64); + public string Targets { get; set; } = "@a"; + public Function OnTriggerFn { get; set; } + + public Trigger(string name, Targets targets, Function onTrigger) : base(name, "trigger") { + Name = name; + Targets = TargetsHandler.Get(targets); + OnTriggerFn = onTrigger; + } + public Trigger(string name, string targets, Function onTrigger) : base(name, "trigger") { + Name = name; + Targets = targets; + OnTriggerFn = onTrigger; + } + + public void Enable() { + FunctionBuilder.Add($"scoreboard players enable {Targets} {Name}"); + } + + public void IfTriggered(Targets targets, string matches) { + FunctionBuilder.Add($"execute as {TargetsHandler.Get(targets)} if score @s {Name} matches {matches} run function {OnTriggerFn.Name}"); + } + public void IfTriggered(string targets, string matches) { + FunctionBuilder.Add($"execute as {targets} if score @s {Name} matches {matches} run function {OnTriggerFn.Name}"); + } +} \ No newline at end of file diff --git a/Core/XP.cs b/Core/XP.cs new file mode 100644 index 0000000..9dad260 --- /dev/null +++ b/Core/XP.cs @@ -0,0 +1,91 @@ +using SteveSharp.Generic; +using Str = SteveSharp.Core.Strings; + +namespace SteveSharp.Core; + +public static class XP +{ + #region Add + public static void AddXP(Targets targets, int amount) + { + FunctionBuilder.Add(Str.XP.AddXP(targets, amount)); + } + + public static void AddXP(string targets, int amount) + { + FunctionBuilder.Add(Str.XP.AddXP(targets, amount)); + } + + public static void AddXPLevels(Targets targets, int amount) + { + FunctionBuilder.Add(Str.XP.AddXPLevels(targets, amount)); + } + + public static void AddXPLevels(string targets, int amount) + { + FunctionBuilder.Add(Str.XP.AddXPLevels(targets, amount)); + } + + public static void AddXPPoints(Targets targets, int amount) + { + FunctionBuilder.Add(Str.XP.AddXPPoints(targets, amount)); + } + + public static void AddXPPoints(string targets, int amount) + { + FunctionBuilder.Add(Str.XP.AddXPPoints(targets, amount)); + } + #endregion + #region Query + public static void XPLevelsQuery(Targets targets) + { + FunctionBuilder.Add(Str.XP.XPLevelsQuery(targets)); + } + + public static void XPLevelsQuery(string targets) + { + FunctionBuilder.Add(Str.XP.XPLevelsQuery(targets)); + } + + public static void XPQuery(Targets targets) + { + FunctionBuilder.Add(Str.XP.XPQuery(targets)); + } + + public static void XPQuery(string targets) + { + FunctionBuilder.Add(Str.XP.XPQuery(targets)); + } + #endregion + #region Set + public static void SetXP(Targets targets, int amount) + { + FunctionBuilder.Add(Str.XP.SetXP(targets, amount)); + } + + public static void SetXP(string targets, int amount) + { + FunctionBuilder.Add(Str.XP.SetXP(targets, amount)); + } + + public static void SetXPLevels(Targets targets, int amount) + { + FunctionBuilder.Add(Str.XP.SetXPLevels(targets, amount)); + } + + public static void SetXPLevels(string targets, int amount) + { + FunctionBuilder.Add(Str.XP.SetXPLevels(targets, amount)); + } + + public static void SetXPPoints(Targets targets, int amount) + { + FunctionBuilder.Add(Str.XP.SetXPPoints(targets, amount)); + } + + public static void SetXPPoints(string targets, int amount) + { + FunctionBuilder.Add(Str.XP.SetXPPoints(targets, amount)); + } + #endregion +} \ No newline at end of file diff --git a/Core/XYZ.cs b/Core/XYZ.cs index aa21c92..278f1c0 100644 --- a/Core/XYZ.cs +++ b/Core/XYZ.cs @@ -2,12 +2,19 @@ { public static class XYZ { - public static string[] Set(string X, string Y, string Z) - { - return new string[] { X, Y, Z }; + public static (int x, int y) Vec2(int x, int y) => (x, y); + public static (string x, string y) Vec2(string x, string y) => (x, y); + public static (int x, int y, int z) Vec3(int x, int y, int z) => (x, y, z); + public static (string x, string y, string z) Vec3(string x, string y, string z) => (x, y, z); + public static (string x, string y, string z) Rel(int x, int y, int z) + => ('~' + (x == 0 ? "" : x.ToString()), + '~' + (y == 0 ? "" : y.ToString()), + '~' + (z == 0 ? "" : z.ToString())); + public static string[] Pos(int a, int b, int c) { + return ["^"+a, "^"+b, "^"+c]; } - public static string[] Rel(string X, string Y, string Z) { - return Set("~"+X, "~"+Y, "~"+Z); + public static string[] Pos(string a, string b, string c) { + return ['^'+a, '^'+b, '^'+c]; } } } diff --git a/Displays.cs b/Displays.cs index 39e372c..d5edba9 100644 --- a/Displays.cs +++ b/Displays.cs @@ -1,15 +1,5 @@ internal static class Displays { - internal static void ExtendedFrom(string path, string origin) - { - Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.BackgroundColor = ConsoleColor.Yellow; - Console.Write(" EXT/F "); - Console.ResetColor(); - Console.ForegroundColor = ConsoleColor.Yellow; - Console.WriteLine($" Extended {path} from {origin}"); - Console.ResetColor(); - } internal static void NewFunction(string path) { Console.ForegroundColor = ConsoleColor.DarkGreen; @@ -50,16 +40,6 @@ internal static void WrittenJson(string path) Console.WriteLine($" Written {path} JSON file"); Console.ResetColor(); } - internal static void ExtendedFunction(string path) - { - Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.BackgroundColor = ConsoleColor.Yellow; - Console.Write(" EXT "); - Console.ResetColor(); - Console.ForegroundColor = ConsoleColor.Yellow; - Console.WriteLine($" Extended {path}"); - Console.ResetColor(); - } internal static void SteveSharpDisplay(string name) { Console.Title = "SteveSharp Log"; @@ -77,16 +57,4 @@ internal static void ProjectCreated() Console.WriteLine(" Project created succesfully!"); Console.ResetColor(); } - internal static void ProjectNotCreated(IOException e) - { - Console.ForegroundColor = ConsoleColor.DarkRed; - Console.BackgroundColor = ConsoleColor.Red; - Console.Write(" P/E "); - Console.ResetColor(); - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(" Project wasn't created succesfully or completely. Check the errors:\n"); - Console.ForegroundColor = ConsoleColor.DarkRed; - Console.WriteLine(e.Message); - Console.ResetColor(); - } } \ No newline at end of file diff --git a/Exceptions/DatapackFeatureException.cs b/Exceptions/DatapackFeatureException.cs new file mode 100644 index 0000000..880ce8c --- /dev/null +++ b/Exceptions/DatapackFeatureException.cs @@ -0,0 +1,11 @@ +namespace SteveSharp.Exceptions; + +public class DatapackFeatureException : Exception { + public object Feature { get; } + public PackFormat PackFormat { get; } + public override string Message => $"The feature \"{Feature}\" isn't allowed to be used. You must update your project's pack format to {(int)PackFormat} to use this feature."; + public DatapackFeatureException(object feature, PackFormat packFormat) { + Feature = feature; + PackFormat = packFormat; + } +} \ No newline at end of file diff --git a/Exceptions/NBTParsingException.cs b/Exceptions/NBTParsingException.cs new file mode 100644 index 0000000..9366304 --- /dev/null +++ b/Exceptions/NBTParsingException.cs @@ -0,0 +1,7 @@ +using SteveSharp.NBT; + +namespace SteveSharp.Exceptions; +public class NBTParsingException(NBTTag nbt) : Exception { + public NBTTag NBT { get => nbt; } + public override string Message => $"Could not parse NBT Tag \"{NBT.Name}\". Please check the value type and tag type:\n\tValue Type: {NBT.Value.GetType()}\n\tTag Type: {NBT.Type}\nPlease check if it isn't supported by the NBT Tag parser."; +} \ No newline at end of file diff --git a/Exceptions/StrictConventionException.cs b/Exceptions/StrictConventionException.cs new file mode 100644 index 0000000..57e5d34 --- /dev/null +++ b/Exceptions/StrictConventionException.cs @@ -0,0 +1,10 @@ +namespace SteveSharp.Exceptions; +public class StrictConventionException : Exception { + public string Convention { get; set; } + public string Reason { get; } + public override string Message => $"MC Datapacks convention violation!\nConvention: {Convention}\nReason: \"{Reason}\".\nPlease check the convention and realize necessary changes."; + public StrictConventionException(string convention, string reason) { + Convention = convention; + Reason = reason; + } +} \ No newline at end of file diff --git a/Function.cs b/Function.cs index 088126d..7bd3d94 100644 --- a/Function.cs +++ b/Function.cs @@ -1,30 +1,36 @@ -namespace SteveSharp +using Str = SteveSharp.Core.Strings; + +namespace SteveSharp { public class Function { public string Name = ""; - public List Body { get; set; } - public Function(string name, List body) + public Func Body { get; set; } + public Function(string name, Func body) { Name = name; Body = body; Displays.NewFunction(name); } - public static string Return(int i) - { - return "return " + i; - } - public static string Call(string function) - { - return "function " + function; + public static void Return(int i) => FunctionBuilder.Add(Str.Function.Return(i)); + public static void Call(string function) => FunctionBuilder.Add(Str.Function.Call(function)); + public static void Schedule(string function, string time, bool append = false, bool replace = false) { + FunctionBuilder.Add(Str.Function.Schedule(function, time, append, replace)); } - public static string Schedule(string function, string time, bool append = false, bool replace = false) { - if(append && !replace) return $"schedule function {function} {time} append"; - else if(!append && replace) return $"schedule function {function} {time} replace"; - else return $"schedule function {function} {time}"; + public void ScheduleSelf(string time, bool append = false, bool replace = false) => FunctionBuilder.Add(Str.Function.Schedule(Name, time, append, replace)); + public static void ClearSchedule(string function) => FunctionBuilder.Add(Str.Function.ClearSchedule(function)); + public void ClearSchedule() => ClearSchedule(Name); + + /// + /// Extend to another function from the current function + /// + /// The new function to assign + /// The actual function context + public static void Extend(Function function, FunctionContext ctx) { + FunctionBuilder.Add("function "+function.Name); + + // List the another function to build on project construction. + ctx.Project.FunctionIndex.Add(function.Name, function); } - public string ScheduleSelf(string time, bool append = false, bool replace = false) => Schedule(Name, time, append, replace); - public static string ClearSchedule(string function) => $"schedule clear {function}"; - public string ClearSchedule() => ClearSchedule(Name); } } diff --git a/FunctionBuilder.cs b/FunctionBuilder.cs new file mode 100644 index 0000000..680b1db --- /dev/null +++ b/FunctionBuilder.cs @@ -0,0 +1,38 @@ +namespace SteveSharp; + +public static class FunctionBuilder { + public static List Commands { get; set; } = []; + + public static void Add(string command) { + Commands.Add(command); + } + + public static void Add(List commands) { + Commands = [ + ..Commands, + ..commands + ]; + } + + public static string[] Collect() { + return Commands.ToArray(); + } + + public static void Clear() { + Commands.Clear(); + } + + public static void BuildFunction(Function function, string id, int packFormat, Project project) { + // Create context for the function and then, register the contents + var ctx = new FunctionContext( + function.Name, + id, + FileOrganizer.GetFunctionPath(function.Name), + packFormat, + project + ); + project.FunctionContents.Add(function.Name, function.Body(ctx)); + // Clear stored commands for last function + Clear(); + } +} \ No newline at end of file diff --git a/FunctionContext.cs b/FunctionContext.cs new file mode 100644 index 0000000..9618a7b --- /dev/null +++ b/FunctionContext.cs @@ -0,0 +1,22 @@ +namespace SteveSharp; + +public class FunctionContext { + public FunctionContext(string functionName, string id, string functionPath, int packFormat, Project projectReference) { + FunctionName = functionName; + Namespace = id; + FunctionPath = functionPath; + PackFormat = packFormat; + Project = projectReference; + } + public string FunctionName { get; set; } + public string Namespace { get; set; } + public string FunctionPath { get; set; } + public int PackFormat { get; set; } + public Project Project { get; set; } + public Dictionary Variables { + get { + return Project.Variables; + } + set {} + } +} \ No newline at end of file diff --git a/Generic/ColorHandler.cs b/Generic/ColorHandler.cs index 47d783d..1efa281 100644 --- a/Generic/ColorHandler.cs +++ b/Generic/ColorHandler.cs @@ -1,7 +1,7 @@ namespace SteveSharp.Generic; internal static class ColorHandler { - public static string Get(Color color) { + internal static string Get(Color color) { Dictionary colorStr = new() { {Color.Blue, "blue"}, {Color.Green, "green"}, diff --git a/Generic/Enchantment.cs b/Generic/Enchantment.cs new file mode 100644 index 0000000..79099e7 --- /dev/null +++ b/Generic/Enchantment.cs @@ -0,0 +1,56 @@ +namespace SteveSharp.Generic; + +// https://www.digminecraft.com/lists/enchantment_list_pc.php +public enum Enchantment { + AquaAffinity, + BaneOfArthropods, + BlastProtection, + /// + /// Minecraft 1.21 + /// + Breach, + Channeling, + CurseOfBinding, + CurseOfVanishing, + /// + /// Minecraft 1.21 + /// + Density, + DepthStrider, + Efficiency, + FeatherFalling, + FireAspect, + FireProtection, + Flame, + Fortune, + FrostWalker, + Impaling, + Infinity, + Knockback, + Looting, + Loyalty, + LuckOfTheSea, + Lure, + Mending, + Multishot, + Piercing, + Power, + ProjectileProtection, + Protection, + Punch, + QuickCharge, + Respiration, + Riptide, + Sharpness, + SilkTouck, + Smite, + SoulSpeed, + SweepingEdge, + SwiftSneak, + Thorns, + Unbreaking, + /// + /// 1.21 + /// + WindBurst +} \ No newline at end of file diff --git a/Generic/EnchantmentHandler.cs b/Generic/EnchantmentHandler.cs new file mode 100644 index 0000000..e308b8b --- /dev/null +++ b/Generic/EnchantmentHandler.cs @@ -0,0 +1,50 @@ +namespace SteveSharp.Generic; + +// https://www.digminecraft.com/lists/enchantment_list_pc.php +internal static class EnchantmentHandler { + internal static string Get(Enchantment enchantment) => enchantment switch { + Enchantment.AquaAffinity => "minecraft:aqua_affinity", + Enchantment.BaneOfArthropods => "minecraft:bane_of_arthropods", + Enchantment.BlastProtection => "minecraft:blast_protection", + Enchantment.Breach => "minecraft:breach", + Enchantment.Channeling => "minecraft:channeling", + Enchantment.CurseOfBinding => "minecraft:curse_of_binding", + Enchantment.CurseOfVanishing => "minecraft:curse_of_vanishing", + Enchantment.Density => "minecraft:density", + Enchantment.DepthStrider => "minecraft:depth_strider", + Enchantment.Efficiency => "minecraft:efficiency", + Enchantment.FeatherFalling => "minecraft:feather_falling", + Enchantment.FireAspect => "minecraft:fire_aspect", + Enchantment.FireProtection => "minecraft:fire_protection", + Enchantment.Flame => "minecraft:flame", + Enchantment.Fortune => "minecraft:fortune", + Enchantment.FrostWalker => "minecraft:frost_walker", + Enchantment.Impaling => "minecraft:impaling", + Enchantment.Infinity => "minecraft:infinity", + Enchantment.Knockback => "minecraft:knockback", + Enchantment.Looting => "minecraft:looting", + Enchantment.Loyalty => "minecraft:loyalty", + Enchantment.LuckOfTheSea => "minecraft:luck_of_the_sea", + Enchantment.Lure => "minecraft:lure", + Enchantment.Mending => "minecraft:mending", + Enchantment.Multishot => "minecraft:multishot", + Enchantment.Piercing => "minecraft:piercing", + Enchantment.Power => "minecraft:power", + Enchantment.ProjectileProtection => "minecraft:proyectile_protection", + Enchantment.Protection => "minecraft:protection", + Enchantment.Punch => "minecraft:punch", + Enchantment.QuickCharge => "minecraft:quick_charge", + Enchantment.Respiration => "minecraft:respiration", + Enchantment.Riptide => "minecraft:riptide", + Enchantment.Sharpness => "minecraft:sharpness", + Enchantment.SilkTouck => "minecraft:silk_touch", + Enchantment.Smite => "minecraft:smite", + Enchantment.SoulSpeed => "minecraft:soul_speed", + Enchantment.SweepingEdge => "minecraft:sweeping_edge", + Enchantment.SwiftSneak => "minecraft:swift_sneak", + Enchantment.Thorns => "minecraft:thorns", + Enchantment.Unbreaking => "minecraft:unbreaking", + Enchantment.WindBurst => "minecraft:wind_burst", + _ => "Invalid input (enchantment not found)" + }; +} \ No newline at end of file diff --git a/Generic/EntityEnum.cs b/Generic/EntityEnum.cs new file mode 100644 index 0000000..120e00b --- /dev/null +++ b/Generic/EntityEnum.cs @@ -0,0 +1,130 @@ +namespace SteveSharp.Generic; + +// https://www.digminecraft.com/lists/entity_list_pc.php +public enum EntityEnum { + Allay, + AreaEffectCloud, + Armadillo, + ArmorStand, + Arrow, + Axolotl, + Bat, + Bee, + Blaze, + BlockDisplay, + Boat, + Bogged, + Breeze, + Camel, + Cat, + CaveSpider, + BoatWithChest, + MinecartWithChest, + Chicken, + Cod, + MinecartWithCommandBlock, + Cow, + Creeper, + Dolphin, + Donkey, + DragonFireball, + Drowned, + Egg, + ElderGuardian, + EndCrystal, + EnderDragon, + EnderPearl, + Enderman, + Endermite, + Evoker, + EvokerFangs, + ExperienceBottle, + ExperienceOrb, + EyeOfEnder, + FallingBlock, + Fireball, + FireworkRocket, + Fox, + Frog, + MinecartWithFurnace, + Ghast, + Giant, + GlowItemFrame, + GlowSquid, + Goat, + Guardian, + Hoglin, + MinecartWithHopper, + Horse, + Husk, + Illusioner, + Interaction, + IronGolem, + Item, + ItemDisplay, + ItemFrame, + LeashKnot, + LightningBolt, + Llama, + LlamaSpit, + MagmaCube, + Marker, + Minecart, + Mooshroom, + Mule, + Ocelot, + Painting, + Panda, + Parrot, + Phantom, + Pig, + Piglin, + PiglinBrute, + Pillager, + PolarBear, + Potion, + Pufferfish, + Rabbit, + Ravager, + Salmon, + Sheep, + Shulker, + ShulkerBullet, + Silverfish, + Skeleton, + SkeletonHorse, + Slime, + SmallFireball, + Sniffer, + SnowGolem, + Snowball, + MinecartWithSpawner, + SpectralArrow, + Spider, + Squid, + Stray, + Strider, + Tadpole, + TextDisplay, + TNT, + MinecartWithTNT, + TraderLlama, + Trident, + TropicalFish, + Turtle, + Vex, + Villager, + Vindicator, + WanderingTrader, + Warden, + Witch, + Wither, + WitherSkeleton, + WitherSkeletonSkull, + Wolf, + Zoglin, + Zombie, + ZombieHorse, + ZombieVillager, + ZombifiedPiglin +} \ No newline at end of file diff --git a/Generic/EntityHandler.cs b/Generic/EntityHandler.cs new file mode 100644 index 0000000..653b9b5 --- /dev/null +++ b/Generic/EntityHandler.cs @@ -0,0 +1,134 @@ +namespace SteveSharp.Generic; + +internal static class EntityHandler { + internal static string Get(EntityEnum entityEnum) { + // https://www.digminecraft.com/lists/entity_list_pc.php + Dictionary entityEnumStr = new() { + {EntityEnum.Allay, "minecraft:allay"}, + {EntityEnum.AreaEffectCloud, "minecraft:area_effect_cloud"}, + {EntityEnum.Armadillo, "minecraft:armadillo"}, + {EntityEnum.ArmorStand, "minecraft:armor_stand"}, + {EntityEnum.Arrow, "minecraft:arrow"}, + {EntityEnum.Axolotl, "minecraft:axolotl"}, + {EntityEnum.Bat, "minecraft:bat"}, + {EntityEnum.Bee, "minecraft:bee"}, + {EntityEnum.Blaze, "minecraft:blaze"}, + {EntityEnum.BlockDisplay, "minecraft:block_display"}, + {EntityEnum.Boat, "minecraft:boat"}, + {EntityEnum.Bogged, "minecraft:bogged"}, + {EntityEnum.Breeze, "minecraft:breeze"}, + {EntityEnum.Camel, "minecraft:camel"}, + {EntityEnum.Cat, "minecraft:cat"}, + {EntityEnum.CaveSpider, "minecraft:cave_spider"}, + {EntityEnum.BoatWithChest, "minecraft:chest_boat"}, + {EntityEnum.MinecartWithChest, "minecraft:chest_minecart"}, + {EntityEnum.Chicken, "minecraft:chicken"}, + {EntityEnum.Cod, "minecraft:cod"}, + {EntityEnum.MinecartWithCommandBlock, "minecraft:command_block_minecart"}, + {EntityEnum.Cow, "minecraft:cow"}, + {EntityEnum.Creeper, "minecraft:creeper"}, + {EntityEnum.Dolphin, "minecraft:dolphin"}, + {EntityEnum.Donkey, "minecraft:donkey"}, + {EntityEnum.DragonFireball, "minecraft:dragon_fireball"}, + {EntityEnum.Drowned, "minecraft:drowned"}, + {EntityEnum.Egg, "minecraft:egg"}, + {EntityEnum.ElderGuardian, "minecraft:elder_guardian"}, + {EntityEnum.EndCrystal, "minecraft:end_crystal"}, + {EntityEnum.EnderDragon, "minecraft:ender_dragon"}, + {EntityEnum.EnderPearl, "minecraft:ender_pearl"}, + {EntityEnum.Enderman, "minecraft:enderman"}, + {EntityEnum.Endermite, "minecraft:endermite"}, + {EntityEnum.Evoker, "minecraft:evoker"}, + {EntityEnum.EvokerFangs, "minecraft:evoker_fangs"}, + {EntityEnum.ExperienceBottle, "minecraft:experience_bottle"}, + {EntityEnum.ExperienceOrb, "minecraft:experience_orb"}, + {EntityEnum.FallingBlock, "minecraft:falling_block"}, + {EntityEnum.Fireball, "minecraft:fireball"}, + {EntityEnum.FireworkRocket, "minecraft:firework_rocket"}, + {EntityEnum.Fox, "minecraft:fox"}, + {EntityEnum.Frog, "minecraft:frog"}, + {EntityEnum.MinecartWithFurnace, "minecraft:furnace_minecart"}, + {EntityEnum.Ghast, "minecraft:ghast"}, + {EntityEnum.Giant, "minecraft:giant"}, + {EntityEnum.GlowItemFrame, "minecraft:glow_item_frame"}, + {EntityEnum.GlowSquid, "minecraft:glow_squid"}, + {EntityEnum.Goat, "minecraft:goat"}, + {EntityEnum.Guardian, "minecraft:guardian"}, + {EntityEnum.Hoglin, "minecraft:hoglin"}, + {EntityEnum.MinecartWithHopper, "minecraft:hopper_minecart"}, + {EntityEnum.Horse, "minecraft:horse"}, + {EntityEnum.Husk, "minecraft:husk"}, + {EntityEnum.Illusioner, "minecraft:illusioner"}, + {EntityEnum.Interaction, "minecraft:interaction"}, + {EntityEnum.IronGolem, "minecraft:iron_golem"}, + {EntityEnum.Item, "minecraft:item"}, + {EntityEnum.ItemDisplay, "minecraft:item_display"}, + {EntityEnum.ItemFrame, "minecraft:item_frame"}, + {EntityEnum.LeashKnot, "minecraft:leash_knot"}, + {EntityEnum.LightningBolt, "minecraft:lightning_bolt"}, + {EntityEnum.Llama, "minecraft:llama"}, + {EntityEnum.LlamaSpit, "minecraft:llama_spit"}, + {EntityEnum.MagmaCube, "minecraft:magma_cube"}, + {EntityEnum.Marker, "minecraft:marker"}, + {EntityEnum.Minecart, "minecraft:minecart"}, + {EntityEnum.Mooshroom, "minecraft:mooshroom"}, + {EntityEnum.Mule, "minecraft:mule"}, + {EntityEnum.Ocelot, "minecraft:ocelot"}, + {EntityEnum.Painting, "minecraft:painting"}, + {EntityEnum.Panda, "minecraft:panda"}, + {EntityEnum.Parrot, "minecraft:parrot"}, + {EntityEnum.Phantom, "minecraft:phantom"}, + {EntityEnum.Pig, "minecraft:pig"}, + {EntityEnum.Piglin, "minecraft:piglin"}, + {EntityEnum.PiglinBrute, "minecraft:piglin_brute"}, + {EntityEnum.Pillager, "minecraft:pillager"}, + {EntityEnum.PolarBear, "minecraft:polar_bear"}, + {EntityEnum.Potion, "minecraft:potion"}, + {EntityEnum.Pufferfish, "minecraft:pufferfish"}, + {EntityEnum.Rabbit, "minecraft:rabbit"}, + {EntityEnum.Ravager, "minecraft:ravager"}, + {EntityEnum.Salmon, "minecraft:salmon"}, + {EntityEnum.Sheep, "minecraft:sheep"}, + {EntityEnum.Shulker, "minecraft:shulker"}, + {EntityEnum.ShulkerBullet, "minecraft:shulker_bullet"}, + {EntityEnum.Silverfish, "minecraft:silverfish"}, + {EntityEnum.Skeleton, "minecraft:skeleton"}, + {EntityEnum.SkeletonHorse, "minecraft:skeleton_horse"}, + {EntityEnum.Slime, "minecraft:slime"}, + {EntityEnum.SmallFireball, "minecraft:small_fireball"}, + {EntityEnum.Sniffer, "minecraft:sniffer"}, + {EntityEnum.SnowGolem, "minecraft:snow_golem"}, + {EntityEnum.Snowball, "minecraft:snowball"}, + {EntityEnum.MinecartWithSpawner, "minecraft:spawner_minecart"}, + {EntityEnum.SpectralArrow, "minecraft:spectral_arrow"}, + {EntityEnum.Spider, "minecraft:spider"}, + {EntityEnum.Squid, "minecraft:squid"}, + {EntityEnum.Stray, "minecraft:stray"}, + {EntityEnum.Strider, "minecraft:strider"}, + {EntityEnum.Tadpole, "minecraft:tadpole"}, + {EntityEnum.TextDisplay, "minecraft:text_display"}, + {EntityEnum.TNT, "minecraft:tnt"}, + {EntityEnum.MinecartWithTNT, "minecraft:tnt_minecart"}, + {EntityEnum.TraderLlama, "minecraft:trader_llama"}, + {EntityEnum.Trident, "minecraft:trident"}, + {EntityEnum.TropicalFish, "minecraft:tropical_fish"}, + {EntityEnum.Turtle, "minecraft:turtle"}, + {EntityEnum.Vex, "minecraft:vex"}, + {EntityEnum.Villager, "minecraft:villager"}, + {EntityEnum.Vindicator, "minecraft:vindicator"}, + {EntityEnum.WanderingTrader, "minecraft:wandering_trader"}, + {EntityEnum.Warden, "minecraft:warden"}, + {EntityEnum.Witch, "minecraft:witch"}, + {EntityEnum.Wither, "minecraft:wither"}, + {EntityEnum.WitherSkeleton, "minecraft:wither_skeleton"}, + {EntityEnum.WitherSkeletonSkull, "minecraft:wither_skeleton_skull"}, + {EntityEnum.Wolf, "minecraft:wolf"}, + {EntityEnum.Zoglin, "minecraft:zoglin"}, + {EntityEnum.Zombie, "minecraft:zombie"}, + {EntityEnum.ZombieHorse, "minecraft:zombie_horse"}, + {EntityEnum.ZombieVillager, "minecraft:zombie_villager"}, + {EntityEnum.ZombifiedPiglin, "minecraft:zombified_piglin"} + }; + return entityEnumStr[entityEnum]; + } +} \ No newline at end of file diff --git a/Generic/StatusEffect.cs b/Generic/StatusEffect.cs new file mode 100644 index 0000000..d227add --- /dev/null +++ b/Generic/StatusEffect.cs @@ -0,0 +1,62 @@ +namespace SteveSharp.Generic; + +// https://www.digminecraft.com/lists/effect_list_pc.php +public enum StatusEffect { + Absorption, + BadLuck, + BadOmen, + Blindness, + ConduitPower, + Darkness, + DolphinsGrace, + FireResistance, + Glowing, + Haste, + HealthBoost, + HeroOfTheVillage, + Hunger, + /// + /// Minecraft 1.21 + /// + Infested, + InstantDamage, + InstantHealth, + Invisibility, + JumpBoost, + Levitation, + Luck, + MiningFatigue, + Nausea, + NightVision, + /// + /// Minecraft 1.21 + /// + Oozing, + Poison, + /// + /// Minecraft 1.21 + /// + RaidOmen, + Regeneration, + Resistance, + Saturation, + SlowFalling, + Slowness, + Speed, + Strength, + /// + /// Minecraft 1.21 + /// + TrialOmen, + WaterBreathing, + Weakness, + /// + /// Minecraft 1.21 + /// + Weaving, + /// + /// Minecraft 1.21 + /// + WindCharged, + Wither +} \ No newline at end of file diff --git a/Generic/StatusEffectHandler.cs b/Generic/StatusEffectHandler.cs new file mode 100644 index 0000000..52d7b61 --- /dev/null +++ b/Generic/StatusEffectHandler.cs @@ -0,0 +1,49 @@ +namespace SteveSharp.Generic; + +// https://www.digminecraft.com/lists/effect_list_pc.php +internal static class StatusEffectHandler { + internal static string Get(StatusEffect effect) { + Dictionary statusEffectStr = new() { + {StatusEffect.Absorption, "absorption"}, + {StatusEffect.BadLuck, "unluck"}, + {StatusEffect.BadOmen, "bad_omen"}, + {StatusEffect.Blindness, "blindness"}, + {StatusEffect.ConduitPower, "conduit_power"}, + {StatusEffect.Darkness, "darkness"}, + {StatusEffect.DolphinsGrace, "dolphins_grace"}, + {StatusEffect.FireResistance, "fire_resistance"}, + {StatusEffect.Glowing, "glowing"}, + {StatusEffect.Haste, "haste"}, + {StatusEffect.HealthBoost, "health_boost"}, + {StatusEffect.HeroOfTheVillage, "hero_of_the_village"}, + {StatusEffect.Hunger, "hunger"}, + {StatusEffect.Infested, "infested"}, + {StatusEffect.InstantDamage, "instant_damage"}, + {StatusEffect.InstantHealth, "instant_health"}, + {StatusEffect.Invisibility, "invisibility"}, + {StatusEffect.JumpBoost, "jump_boost"}, + {StatusEffect.Levitation, "levitation"}, + {StatusEffect.Luck, "luck"}, + {StatusEffect.MiningFatigue, "mining_fatigue"}, + {StatusEffect.Nausea, "nausea"}, + {StatusEffect.NightVision, "night_vision"}, + {StatusEffect.Oozing, "oozing"}, + {StatusEffect.Poison, "poison"}, + {StatusEffect.RaidOmen, "raid_omen"}, + {StatusEffect.Regeneration, "regeneration"}, + {StatusEffect.Resistance, "resistance"}, + {StatusEffect.Saturation, "saturation"}, + {StatusEffect.SlowFalling, "slow_falling"}, + {StatusEffect.Slowness, "slowness"}, + {StatusEffect.Speed, "speed"}, + {StatusEffect.Strength, "strength"}, + {StatusEffect.TrialOmen, "trial_omen"}, + {StatusEffect.WaterBreathing, "water_breathing"}, + {StatusEffect.Weakness, "weakness"}, + {StatusEffect.Weaving, "weaving"}, + {StatusEffect.WindCharged, "wind_charged"}, + {StatusEffect.Wither, "wither"} + }; + return statusEffectStr[effect]; + } +} \ No newline at end of file diff --git a/Generic/Targets.cs b/Generic/Targets.cs new file mode 100644 index 0000000..aadf26f --- /dev/null +++ b/Generic/Targets.cs @@ -0,0 +1,13 @@ +namespace SteveSharp.Generic; + +public enum Targets { + NearestPlayer, + /// + /// Minecraft 1.21 + /// + NearestEntity, + RandomPlayer, + AllPlayers, + AllEntities, + Selected +} \ No newline at end of file diff --git a/Generic/TargetsHandler.cs b/Generic/TargetsHandler.cs new file mode 100644 index 0000000..933c5cf --- /dev/null +++ b/Generic/TargetsHandler.cs @@ -0,0 +1,16 @@ +namespace SteveSharp.Generic; + +internal static class TargetsHandler { + internal static string Get(Targets targets) { + Dictionary TargetsStr = new() { + {Targets.NearestPlayer, "@p"}, + {Targets.NearestEntity, "@n"}, + {Targets.RandomPlayer, "@r"}, + {Targets.AllPlayers, "@a"}, + {Targets.AllEntities, "@e"}, + {Targets.Selected, "@s"} + }; + + return TargetsStr[targets]; + } +} \ No newline at end of file diff --git a/JsonShapes/PackMetadata.cs b/JsonShapes/PackMetadata.cs index 3f0a6af..157a213 100644 --- a/JsonShapes/PackMetadata.cs +++ b/JsonShapes/PackMetadata.cs @@ -1,12 +1,59 @@ -namespace SteveSharp.JsonShapes +using System.Text.Json.Serialization; + +namespace SteveSharp.JsonShapes { public class PackMetadata { - public Pack? pack { get; set; } + [JsonPropertyName("pack")] + public Pack? Pack { get; set; } } public class Pack { - public string? description { get; set; } - public int pack_format { get; set; } + [JsonPropertyName("description")] + public string? Description { get; set; } + + [JsonPropertyName("pack_format")] + public PackFormat PackFormat { get; set; } + + /// + /// Pack Format 18+ Feature. + /// Values accepted by the pack metadata: + /// SupportedFormats = + /// SupportedFormats = new PackFormat[] {, } + /// SupportedFormats = new List {, } + /// SupportedFormats = new SupportedFormatsRange { + /// MinInclusive = , + /// MaxInclusive = + /// } + /// + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + [JsonPropertyName("supported_formats")] + public dynamic? SupportedFormats { get; set; } + + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] + [JsonPropertyName("overlays")] + public OverlaysField Overlays { get; set; } + } + + public struct SupportedFormatsRange { + [JsonPropertyName("min_inclusive")] + public PackFormat MinInclusive { get; set; } + + [JsonPropertyName("max_inclusive")] + public PackFormat MaxInclusive { get; set; } + } + + public struct OverlaysField { + [JsonPropertyName("entries")] + public OverlayEntry[] Entries { get; set; } + } + + public struct OverlayEntry { + // Same as supported_formats + [JsonPropertyName("formats")] + public dynamic Formats { get; set; } + + [JsonPropertyName("directory")] + public string Directory { get; set; } } } diff --git a/JsonShapes/TextComponent.cs b/JsonShapes/TextComponent.cs index 1d8c676..1403fc4 100644 --- a/JsonShapes/TextComponent.cs +++ b/JsonShapes/TextComponent.cs @@ -4,32 +4,45 @@ namespace SteveSharp.JsonShapes { public class TextComponent { - public string? text { get; set; } + [JsonPropertyName("text")] + public string? Text { get; set; } [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public string? color { get; set; } + [JsonPropertyName("color")] + public string? Color { get; set; } [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] - public bool italic { get; set; } + [JsonPropertyName("italic")] + public bool Italic { get; set; } [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] - public bool bold { get; set; } + [JsonPropertyName("bold")] + public bool Bold { get; set; } [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] - public bool underlined { get; set; } + [JsonPropertyName("underlined")] + public bool Underlined { get; set; } [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] - public bool obfuscated { get; set; } + [JsonPropertyName("obfuscated")] + public bool Obfuscated { get; set; } [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] - public bool strikethrough { get; set; } + [JsonPropertyName("strikethrough")] + public bool Strikethrough { get; set; } [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public string? insertion { get; set; } + [JsonPropertyName("insertion")] + public string? Insertion { get; set; } [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] - public clickEvent? clickEvent { get; set; } + [JsonPropertyName("clickEvent")] + public ClickEvent? ClickEvent { get; set; } [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] - public TextComponent[]? extra { get; set; } + [JsonPropertyName("extra")] + public TextComponent[]? Extra { get; set; } } - public class clickEvent + public class ClickEvent { + [JsonPropertyName("action")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public string? action { get; set; } + public string? Action { get; set; } + [JsonPropertyName("value")] [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] - public string? value { get; set; } + public string? Value { get; set; } } } diff --git a/MCDatapacksConventions/OC.cs b/MCDatapacksConventions/OC.cs new file mode 100644 index 0000000..a147930 --- /dev/null +++ b/MCDatapacksConventions/OC.cs @@ -0,0 +1,22 @@ +using SteveSharp.NBT; + +namespace SteveSharp.MCDatapacksConventions; + +/// +/// 1. Official Conventions +/// +public static class OC +{ + public class CTCMember(string name, object value, NBTType type = NBTType.SingleValue) : NBTTag(name, value, type); + public class Trait(string traitName, bool enabled = true) : NBTTag(traitName, enabled); + + /// + /// Common Trait Convention + /// + public static NBTTag CTC(List members) => new NBTTag("ctc", members, NBTType.Compound); + + public static CTCMember Traits(List traits) => new CTCMember("traits", traits, NBTType.Compound); + public static CTCMember Id(string id) => new CTCMember("id", id); + public static CTCMember From(string namespacedId) => new CTCMember("from", namespacedId); + +} \ No newline at end of file diff --git a/NBT/NBT.cs b/NBT/NBT.cs new file mode 100644 index 0000000..524f2a5 --- /dev/null +++ b/NBT/NBT.cs @@ -0,0 +1,15 @@ +namespace SteveSharp.NBT; + +public class NBTTag +{ + public NBTType Type { get; } + public string Name { get; set; } + public object Value { get; set; } + + public NBTTag(string name, object value, NBTType type = NBTType.SingleValue) + { + Type = type; + Name = name; + Value = value; + } +} \ No newline at end of file diff --git a/NBT/NBTParser.cs b/NBT/NBTParser.cs new file mode 100644 index 0000000..e0de9ea --- /dev/null +++ b/NBT/NBTParser.cs @@ -0,0 +1,117 @@ +using SteveSharp.Exceptions; + +namespace SteveSharp.NBT; + +public class NBTParser +{ + public string Encompass(string nbtText) => '{' + nbtText + '}'; + public string Encompass(List nbtTexts) + { + string s = "{"; + for (int i = 0; i < nbtTexts.Count; i++) + { + s += nbtTexts[i]; + if (i < nbtTexts.Count - 1) // if it's not the last item in the list + s += ", "; + } + s += "}"; + return s; + } + public string Enlist(string nbts) => '[' + nbts + ']'; + public string Enlist(List nbts) + { + string s = "["; + for (int i = 0; i < nbts.Count; i++) + { + s += nbts[i]; + if (i < nbts.Count - 1) // if it's not the last item in the list + s += ", "; + } + s += "]"; + return s; + } + public string ParseValueType(object value) + => value switch + { + string => "\"" + value.ToString() + "\"", + float => value.ToString() + "f", + bool => value.ToString()!.ToLower(), + byte => value.ToString() + "b", + short => value.ToString() + "s", + long => value.ToString() + "l", + _ => value.ToString()! + }; + + public string Parse(NBTTag nbt) + { + string nbtString = nbt.Name + ": "; + switch (nbt.Type) + { + case NBTType.SingleValue: + nbtString += ParseValueType(nbt.Value); + return nbtString; + case NBTType.List: + dynamic? list; + nbtString += "["; + if (nbt.Value is List) + { + list = nbt.Value as List; + } + else if (nbt.Value is List) + { + list = nbt.Value as List; + } + else if (nbt.Value is List) + { + list = nbt.Value as List; + } + else if (nbt.Value is List) + { + list = nbt.Value as List; + } + else if (nbt.Value is List) + { + list = nbt.Value as List; + } + else if (nbt.Value is List) + { + list = nbt.Value as List; + } + else if (nbt.Value is List) + { + list = nbt.Value as List; + } + else if (nbt.Value is List) + { + list = nbt.Value as List; + } + else + { + list = nbt.Value as List; + } + + for (int i = 0; i < list!.Count; i++) + { + nbtString += ParseValueType(list[i]); + if (i < list.Count - 1) // if it's not the last item in the list + nbtString += ", "; + } + nbtString += "]"; + return nbtString; + case NBTType.Compound: + nbtString += "{"; + List? tags = nbt.Value as List; + int left = tags!.Count; + for (int i = 0; i < tags.Count; i++) + { + nbtString += Parse(tags[i]); + if (i < tags.Count - 1) // if it's not the last item in the list + nbtString += ", "; + } + nbtString += "}"; + return nbtString; + default: + throw new NBTParsingException(nbt); + } + } +} \ No newline at end of file diff --git a/NBT/NBTType.cs b/NBT/NBTType.cs new file mode 100644 index 0000000..a1c8fd0 --- /dev/null +++ b/NBT/NBTType.cs @@ -0,0 +1,7 @@ +namespace SteveSharp.NBT; + +public enum NBTType { + SingleValue, + List, + Compound +} \ No newline at end of file diff --git a/PackFormat.cs b/PackFormat.cs new file mode 100644 index 0000000..09f29b0 --- /dev/null +++ b/PackFormat.cs @@ -0,0 +1,76 @@ +namespace SteveSharp; + +public enum PackFormat { + /// + /// Minecraft 1.14.4 + /// + [Obsolete("SteveSharp doesn't support pack format 4 officially")] + Format4 = 4, + /// + /// Minecraft 1.16.1 + /// + [Obsolete("SteveSharp doesn't support pack format 5 officially")] + Format5 = 5, + /// + /// Minecraft 1.16.5 + /// + [Obsolete("SteveSharp doesn't support pack format 6 officially")] + Format6 = 6, + /// + /// Minecraft 1.17.1 + /// + [Obsolete("SteveSharp doesn't support pack format 7 officially")] + Format7 = 7, + /// + /// Minecraft 1.18.1 + /// + [Obsolete("SteveSharp doesn't support pack format 8 officially")] + Format8 = 8, + /// + /// Minecraft 1.18.2 + /// + [Obsolete("SteveSharp doesn't support pack format 9 officially")] + Format9 = 9, + /// + /// Minecraft 1.19.3 + /// + [Obsolete("SteveSharp doesn't support pack format 10 officially")] + Format10 = 10, + /// + /// Minecraft 1.19.4 + /// + [Obsolete("SteveSharp doesn't support pack format 12 officially")] + Format12 = 12, + /// + /// Minecraft 1.20.1 + /// + Format15 = 15, + /// + /// Minecraft 1.20.2 + /// + Format18 = 18, + /// + /// Minecraft 1.20.4 + /// + Format26 = 26, + /// + /// Minecraft 1.20.6 + /// + Format41 = 41, + /// + /// Minecraft 1.21.1 + /// + Format48 = 48, + /// + /// Minecraft 1.21.3 + /// + Format57 = 57, + /// + /// Minecraft 1.21.4 + /// + Format61 = 61, + /// + /// Minecraft 1.21.5 + /// + Format71 = 71 +} \ No newline at end of file diff --git a/Project.cs b/Project.cs index dd2abe8..b509f48 100644 --- a/Project.cs +++ b/Project.cs @@ -1,18 +1,35 @@ -using SteveSharp.JsonShapes; +using SteveSharp.Exceptions; +using SteveSharp.JsonShapes; using System.Text.Json; namespace SteveSharp { public class Project { + private string _name { get; set; } + + private string _description { get; set; } + private PackFormat _packFormat { get; set; } + private string _namespace { get; set; } public Dictionary FunctionIndex { get; set; } private readonly Function _load; private readonly Function _main; private readonly List _functions; - public Project(string name, string description, string id, int pack_format, Function load, Function main, List functions, List> matrix = null!, List jsonFiles = null!) + public Dictionary FunctionContents = new(); + public Dictionary Variables = new(); + + private string[] GetFunctionCommands(string functionName) { + return FunctionContents[functionName]; + } + + public Project(string name, string description, string id, PackFormat packFormat, Function load, Function main, List functions, List> matrix = null!, List jsonFiles = null!, PackMetadata customPackMetadata = null!) { // Display fresh SteveSharp Display Displays.SteveSharpDisplay(name); + _name = name; + _description = description; + _namespace = id; + _packFormat = packFormat; _load = load; _main = main; _functions = functions; @@ -23,31 +40,46 @@ public Project(string name, string description, string id, int pack_format, Func Environment.CurrentDirectory = name; FileOrganizer.CreateFullDirectory($"data/{id}/functions"); FileOrganizer.CreateFullDirectory($"data/minecraft/tags/functions"); - var metadata = new PackMetadata - { - pack = new Pack - { - description = description, - pack_format = pack_format + var metadata = new PackMetadata(); + if(customPackMetadata != null) { + metadata = customPackMetadata; + // Restriction for supported_formats field + // Beware: If you have specified a custom pack metadata and you have + // specified the supported formats field, + // and you specified a pack_format under 18, + // the code will throw an DatapackFeatureException. + if( + metadata.Pack!.SupportedFormats != null + && metadata.Pack!.PackFormat < PackFormat.Format18) { + throw new DatapackFeatureException("supported_formats", PackFormat.Format18); } - }; + } else { + metadata = new PackMetadata { + Pack = new Pack { + Description = description, + PackFormat = _packFormat + } + }; + } File.WriteAllText("pack.mcmeta", JsonSerializer.Serialize(metadata, new JsonSerializerOptions { WriteIndented = true })); File.WriteAllText($"data/minecraft/tags/functions/load.json", JsonSerializer.Serialize(new Tag { - values = new string[] { _load.Name } + values = [_load.Name] }, new JsonSerializerOptions { WriteIndented = true }) ); File.WriteAllText($"data/minecraft/tags/functions/tick.json", JsonSerializer.Serialize(new Tag { - values = new string[] { _main.Name } + values = [_main.Name] }, new JsonSerializerOptions { WriteIndented = true }) ); Displays.ProjectCreated(); - File.WriteAllLines(loadPath, _load.Body); + FunctionBuilder.BuildFunction(_load, _namespace, (int)_packFormat, this); + File.WriteAllLines(loadPath, GetFunctionCommands(_load.Name)); Displays.WrittenFunction(_load.Name); - File.WriteAllLines(mainPath, _main.Body); + FunctionBuilder.BuildFunction(_main, _namespace, (int)_packFormat, this); + File.WriteAllLines(mainPath, GetFunctionCommands(_main.Name)); Displays.WrittenFunction(_main.Name); if (_functions.Count > 0) @@ -85,7 +117,8 @@ public Project(string name, string description, string id, int pack_format, Func { Directory.CreateDirectory(directory); } - File.WriteAllLines(FileOrganizer.GetFunctionPath(function.Value.Name), function.Value.Body); + FunctionBuilder.BuildFunction(function.Value, _namespace, (int)_packFormat, this); + File.WriteAllLines(FileOrganizer.GetFunctionPath(function.Value.Name), GetFunctionCommands(function.Value.Name)); } if (jsonFiles != null && jsonFiles.Count > 0) diff --git a/SteveSharp.sln b/SteveSharp.sln index 8452547..00356b2 100644 --- a/SteveSharp.sln +++ b/SteveSharp.sln @@ -5,10 +5,7 @@ VisualStudioVersion = 17.5.33627.172 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SteveSharp", "SteveSharp.csproj", "{6D99B2F6-7D41-4357-B210-E5E909B653D8}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PersonalTests", "PersonalTests", "{08CE972B-3385-4258-A89A-E901C870028E}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "PersonalTests\Tests\Tests.csproj", "{6642C65E-FF8E-4F33-A3E5-2552AEBD6CFB}" -EndProject + Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -19,10 +16,6 @@ Global {6D99B2F6-7D41-4357-B210-E5E909B653D8}.Debug|Any CPU.Build.0 = Debug|Any CPU {6D99B2F6-7D41-4357-B210-E5E909B653D8}.Release|Any CPU.ActiveCfg = Release|Any CPU {6D99B2F6-7D41-4357-B210-E5E909B653D8}.Release|Any CPU.Build.0 = Release|Any CPU - {6642C65E-FF8E-4F33-A3E5-2552AEBD6CFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6642C65E-FF8E-4F33-A3E5-2552AEBD6CFB}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6642C65E-FF8E-4F33-A3E5-2552AEBD6CFB}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6642C65E-FF8E-4F33-A3E5-2552AEBD6CFB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -30,7 +23,4 @@ Global GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {F4AAACE1-9BD0-4190-B3B9-8A837F2AAEA5} EndGlobalSection - GlobalSection(NestedProjects) = preSolution - {6642C65E-FF8E-4F33-A3E5-2552AEBD6CFB} = {08CE972B-3385-4258-A89A-E901C870028E} - EndGlobalSection EndGlobal diff --git a/Utils/Hitbox.cs b/Utils/Hitbox.cs index fbef7b4..010b288 100644 --- a/Utils/Hitbox.cs +++ b/Utils/Hitbox.cs @@ -1,72 +1,69 @@ -using SteveSharp; -using SteveSharp.Core; -using SteveSharp.JsonShapes; +using SteveSharp.Core; +using SteveSharp.Generic; +using Str = SteveSharp.Core.Strings; -/* -NOT FOR NOW -namespace SteveSharp.Utils -{ - public class Hitbox - { - public float width = 1.0f; - public float height = 1.0f; - public string workspace; - public string id; - public string[] XYZ; - public string[] onAttack; - public string[] onRightClick; - public Hitbox(string workspace, string id) - { - this.workspace = workspace; - this.id = id; - XYZ = new string[3] { "~", "~", "~" }; - onAttack = new string[] { - Chat.Out( - Entity.Self(), - new TextComponent[] - { - new TextComponent - { - text = "" - } - } - ) - }; - onRightClick = new string[] { - Chat.Out( - Entity.Self(), - new TextComponent[] - { - new TextComponent - { - text = "" - } - } - ) - }; - } - public string Summon(string function) - { - Function f = new Function(FileOrganizer.GetFunctionPath(function)); - return f.Extend( - $"{workspace}:hitbox/" + this.id.ToLower() + "/summon", - new string[] - { - Entity.Summon("interaction",new[]{ "~", "~", "~" }, - "{Tags:[\"" + workspace + "." + this.id + "\"],width:" + this.width + ",height:" + this.height + "}"), - Execute.Write( - Execute.Asat(Entity.AllEntities("type=interaction,tag=" + workspace + "." + this.id)) + - "on attacker ", - onAttack - ), - Execute.Write( - Execute.Asat(Entity.AllEntities("type=interaction,tag=" + workspace + "." + this.id)) + - "on trigger ", - onRightClick - ), - Entity.Kill(Entity.AllEntities("type=interaction,tag=" + workspace + "." + this.id)) - }, true +namespace SteveSharp.Utils; + +public class Hitbox { + public float Width = 1.0f; + public float Height = 1.0f; + + public string Id { get; set; } + public string Workspace; + private FunctionContext _ctx; + public (string x, string y, string z) coords; + + public Hitbox(string id, string workspace, FunctionContext ctx) { + Id = id; + Workspace = workspace; + coords = XYZ.Vec3("~", "~", "~"); + _ctx = ctx; + } + + public void Setup(Action onAttack, Action onRightClick) { + _ctx.Project.FunctionIndex.Add($"{_ctx!.Namespace}:hitbox/{Workspace}/on_attack", OnAttack(onAttack)); + _ctx.Project.FunctionIndex.Add($"{_ctx!.Namespace}:hitbox/{Workspace}/on_right_click", OnRightClick(onRightClick)); + } + + public string Invoke() { + return Str.Function.Call($"{_ctx!.Namespace}:hitbox/{Workspace}/summon"); + } + public Function OnAttack(Action body) { + return new Function( + name: $"{_ctx!.Namespace}:hitbox/{Workspace}/on_attack", + body: (ctx) => { + body(ctx); + return FunctionBuilder.Collect(); + } + ); + } + private Function OnRightClick(Action body) { + return new Function( + name: $"{_ctx!.Namespace}:hitbox/{Workspace}/on_right_click", + body: (ctx) => { + body(ctx); + return FunctionBuilder.Collect(); + } + ); + } + public Function SummonFunction() { + return new Function( + name: $"{_ctx!.Namespace}:hitbox/{Workspace}/summon", + body: (ctx) => { + Entity.Summon(EntityEnum.Interaction, [coords.x, coords.y, coords.y], "{Tags:[\"" + _ctx.Namespace + "." + Id + "\"],width:" + Width + ",height:" + Height + "}"); + Execute.Write( + Str.Execute.Asat(Entity.AllEntitiesMatch("type=interaction,tag=" + _ctx.Namespace + "." + Id)) + + "on attacker ", + [Str.Function.Call($"{_ctx.Namespace}:hitbox/{Workspace}/on_attack")] + ); + Execute.Write( + Str.Execute.Asat(Entity.AllEntitiesMatch("type=interaction,tag=" + _ctx.Namespace + "." + Id)) + + "on target ", + [Str.Function.Call($"{_ctx.Namespace}:hitbox/{Workspace}/on_right_click")] ); - } + Entity.Kill(Entity.AllEntitiesMatch("type=interaction,tag=" + _ctx.Namespace + "." + Id)); + return FunctionBuilder.Collect(); + } + ); } -}*/ \ No newline at end of file +} \ No newline at end of file