diff --git a/Data/MineSharp.Data.Tests/TestVersions.cs b/Data/MineSharp.Data.Tests/TestVersions.cs index 7b6e0f3d..2f9d2f92 100644 --- a/Data/MineSharp.Data.Tests/TestVersions.cs +++ b/Data/MineSharp.Data.Tests/TestVersions.cs @@ -4,6 +4,7 @@ using MineSharp.Core.Common.Enchantments; using MineSharp.Core.Common.Entities; using MineSharp.Core.Common.Items; +using MineSharp.Core.Common.Particles; using MineSharp.Data.Protocol; namespace MineSharp.Data.Tests; @@ -46,6 +47,7 @@ public void TestLoadData() data.Materials.GetMultiplier(Material.Shovel, ItemType.StoneSword); data.Protocol.GetPacketId(PacketType.CB_Play_Login); data.Recipes.ByItem(ItemType.DiamondShovel); + data.Particles.GetProtocolId(ParticleType.Composter); data.Windows.ById(0); } } diff --git a/Data/MineSharp.Data/Blocks/BlockProvider.cs b/Data/MineSharp.Data/Blocks/BlockProvider.cs index bc60bb61..e963a69b 100644 --- a/Data/MineSharp.Data/Blocks/BlockProvider.cs +++ b/Data/MineSharp.Data/Blocks/BlockProvider.cs @@ -60,7 +60,7 @@ private static BlockInfo FromToken(JToken dataToken, IItemData items) return new( id, - BlockTypeLookup.FromName(NameUtils.GetBiomeName(name)), + BlockTypeLookup.FromName(NameUtils.GetBlockName(name)), name, displayName, hardness, diff --git a/Data/MineSharp.Data/Framework/DataInterfaces.cs b/Data/MineSharp.Data/Framework/DataInterfaces.cs index 43226138..dcbaeec1 100644 --- a/Data/MineSharp.Data/Framework/DataInterfaces.cs +++ b/Data/MineSharp.Data/Framework/DataInterfaces.cs @@ -5,6 +5,7 @@ using MineSharp.Core.Common.Enchantments; using MineSharp.Core.Common.Entities; using MineSharp.Core.Common.Items; +using MineSharp.Core.Common.Particles; using MineSharp.Core.Common.Protocol; using MineSharp.Core.Common.Recipes; using MineSharp.Core.Geometry; @@ -185,3 +186,8 @@ public interface IWindowData /// public WindowInfo ByName(Identifier name); } + +/// +/// Interface for implementing particle data +/// +public interface IParticleData : INameAndProtocolNumberIndexedData; diff --git a/Data/MineSharp.Data/Framework/INameAndProtocolNumberIndexedData.cs b/Data/MineSharp.Data/Framework/INameAndProtocolNumberIndexedData.cs new file mode 100644 index 00000000..f73083bc --- /dev/null +++ b/Data/MineSharp.Data/Framework/INameAndProtocolNumberIndexedData.cs @@ -0,0 +1,48 @@ +using MineSharp.Core.Common; + +namespace MineSharp.Data.Framework; + +/// +/// Interface to implement indexed data, where a single entry does not +/// contain any other data besides a name and a corresponding protocol number. +/// +public interface INameAndProtocolNumberIndexedData +{ + /// + /// The number of data entries + /// + public int Count { get; } + + /// + /// Return the protocol number associated with the given identifier. + /// + /// + /// + public int GetProtocolId(Identifier name); + + /// + /// Return the associated with the given protocol number. + /// + /// + /// + public Identifier GetName(int id); +} + +/// +/// +public interface INameAndProtocolNumberIndexedData : INameAndProtocolNumberIndexedData +{ + /// + /// Return the protocol number associated with the given + /// + /// + /// + public int GetProtocolId(TEnum type); + + /// + /// Return associated with the given protocol number. + /// + /// + /// + public TEnum GetType(int id); +} diff --git a/Data/MineSharp.Data/Internal/NameAndProtocolNumberIndexedData.cs b/Data/MineSharp.Data/Internal/NameAndProtocolNumberIndexedData.cs new file mode 100644 index 00000000..06d40b76 --- /dev/null +++ b/Data/MineSharp.Data/Internal/NameAndProtocolNumberIndexedData.cs @@ -0,0 +1,82 @@ +using System.Collections.Frozen; +using MineSharp.Core.Common; +using MineSharp.Data.Framework; +using MineSharp.Data.Framework.Providers; + +namespace MineSharp.Data.Internal; + +internal class NameAndProtocolNumberIndexedData(IDataProvider> provider) + : IndexedData>(provider), INameAndProtocolNumberIndexedData +{ + public int Count { get; private set; } + + protected IReadOnlyDictionary? ProtocolNumberToName; + protected IReadOnlyDictionary? NameToProtocolNumber; + + protected override void InitializeData(IReadOnlyDictionary data) + { + Count = data.Count; + NameToProtocolNumber = data.ToFrozenDictionary(); + ProtocolNumberToName = NameToProtocolNumber.ToFrozenDictionary(x => x.Value, x => x.Key); + } + + public int GetProtocolId(Identifier name) + { + if (!Loaded) + { + Load(); + } + + return NameToProtocolNumber![name]; + } + + public Identifier GetName(int id) + { + if (!Loaded) + { + Load(); + } + + return ProtocolNumberToName![id]; + } +} + +internal class NameAndProtocolNumberIndexedData(IDataProvider> provider) + : NameAndProtocolNumberIndexedData(provider), INameAndProtocolNumberIndexedData + where TEnum : struct, Enum +{ + private readonly EnumNameLookup enumNameLookup = new(); + private IReadOnlyDictionary? protocolNumberToType; + private IReadOnlyDictionary? typeToProtocolNumber; + + protected override void InitializeData(IReadOnlyDictionary data) + { + base.InitializeData(data); + + protocolNumberToType = ProtocolNumberToName! + .ToFrozenDictionary( + x => x.Key, + x => enumNameLookup.FromName(NameUtils.GetParticleName(x.Value.Name))); + typeToProtocolNumber = protocolNumberToType.ToFrozenDictionary(x => x.Value, x => x.Key); + } + + public int GetProtocolId(TEnum type) + { + if (!Loaded) + { + Load(); + } + + return typeToProtocolNumber![type]; + } + + public TEnum GetType(int id) + { + if (!Loaded) + { + Load(); + } + + return protocolNumberToType![id]; + } +} diff --git a/Data/MineSharp.Data/Internal/NameUtils.cs b/Data/MineSharp.Data/Internal/NameUtils.cs index 1376ecec..c84adf7d 100644 --- a/Data/MineSharp.Data/Internal/NameUtils.cs +++ b/Data/MineSharp.Data/Internal/NameUtils.cs @@ -98,6 +98,11 @@ public static string GetGameState(string name) { return CommonGetName(name); } + + public static string GetParticleName(string name) + { + return CommonGetName(name); + } public static string GetPacketName(string name, string direction, string ns) { diff --git a/Data/MineSharp.Data/MinecraftData.cs b/Data/MineSharp.Data/MinecraftData.cs index 0aaf1e81..57abde55 100644 --- a/Data/MineSharp.Data/MinecraftData.cs +++ b/Data/MineSharp.Data/MinecraftData.cs @@ -10,6 +10,7 @@ using MineSharp.Data.Items; using MineSharp.Data.Language; using MineSharp.Data.Materials; +using MineSharp.Data.Particles; using MineSharp.Data.Protocol; using MineSharp.Data.Recipes; using MineSharp.Data.Windows; @@ -50,6 +51,7 @@ private MinecraftData( IRecipeData recipes, IWindowData windows, ILanguageData language, + IParticleData particles, MinecraftVersion version) { Biomes = biomes; @@ -64,6 +66,7 @@ private MinecraftData( Recipes = recipes; Windows = windows; Language = language; + Particles = particles; Version = version; } @@ -126,6 +129,11 @@ private MinecraftData( /// The language data provider for this version /// public ILanguageData Language { get; } + + /// + /// The particle data for this version + /// + public IParticleData Particles { get; } /// /// The Minecraft version of this instance @@ -165,6 +173,7 @@ public static async Task FromVersion(string version) var materialsToken = await LoadAsset("materials", versionToken); var recipesToken = await LoadAsset("recipes", versionToken); var languageToken = await LoadAsset("language", versionToken); + var particleToken = await LoadAsset("particles", versionToken); var biomes = new BiomeData(new BiomeProvider(biomeToken)); var items = new ItemData(new ItemProvider(itemsToken)); @@ -177,6 +186,7 @@ public static async Task FromVersion(string version) var materials = new MaterialData(new MaterialsProvider(materialsToken, items)); var recipes = new RecipeData(new(recipesToken, items)); var language = new LanguageData(new LanguageProvider(languageToken)); + var particles = new ParticleData(new ParticleDataProvider(particleToken)); var windows = GetWindowData(minecraftVersion); var data = new MinecraftData( @@ -192,6 +202,7 @@ public static async Task FromVersion(string version) recipes, windows, language, + particles, minecraftVersion); LoadedData.Add(version, data); diff --git a/Data/MineSharp.Data/Particles/ParticleData.cs b/Data/MineSharp.Data/Particles/ParticleData.cs new file mode 100644 index 00000000..5b15523a --- /dev/null +++ b/Data/MineSharp.Data/Particles/ParticleData.cs @@ -0,0 +1,10 @@ +using MineSharp.Core.Common; +using MineSharp.Core.Common.Particles; +using MineSharp.Data.Framework; +using MineSharp.Data.Framework.Providers; +using MineSharp.Data.Internal; + +namespace MineSharp.Data.Particles; + +internal class ParticleData(IDataProvider> provider) + : NameAndProtocolNumberIndexedData(provider), IParticleData; diff --git a/Data/MineSharp.Data/Particles/ParticleDataProvider.cs b/Data/MineSharp.Data/Particles/ParticleDataProvider.cs new file mode 100644 index 00000000..ac1a89b9 --- /dev/null +++ b/Data/MineSharp.Data/Particles/ParticleDataProvider.cs @@ -0,0 +1,36 @@ +using System.Collections.Frozen; +using MineSharp.Core.Common; +using MineSharp.Data.Framework.Providers; +using Newtonsoft.Json.Linq; + +namespace MineSharp.Data.Particles; + +internal class ParticleDataProvider : IDataProvider> +{ + private readonly JArray token; + + public ParticleDataProvider(JToken token) + { + if (token.Type != JTokenType.Array) + { + throw new ArgumentException("Expected the token to be an array"); + } + + this.token = (token as JArray)!; + } + + public IReadOnlyDictionary GetData() + { + return token + .Select(FromToken) + .ToFrozenDictionary(x => x.Key, x => x.Value); + } + + private static KeyValuePair FromToken(JToken token) + { + var name = (string)token["name"]!; + var id = (int)token["id"]!; + + return new (Identifier.Parse(name), id); + } +} diff --git a/Data/MineSharp.SourceGenerator/Generators/ParticleGenerator.cs b/Data/MineSharp.SourceGenerator/Generators/ParticleGenerator.cs new file mode 100644 index 00000000..c3f32d02 --- /dev/null +++ b/Data/MineSharp.SourceGenerator/Generators/ParticleGenerator.cs @@ -0,0 +1,21 @@ +using MineSharp.SourceGenerator.Utils; +using Newtonsoft.Json.Linq; + +namespace MineSharp.SourceGenerator.Generators; + +public class ParticleGenerator +{ + private readonly Generator typeGenerator = new("particles", GetName, "ParticleType", "Particles"); + + public Task Run(MinecraftDataWrapper wrapper) + { + return Task.WhenAll( + typeGenerator.Generate(wrapper)); + } + + private static string GetName(JToken token) + { + var name = (string)token.SelectToken("name")!; + return NameUtils.GetParticleName(name); + } +} diff --git a/Data/MineSharp.SourceGenerator/MineSharp.SourceGenerator.csproj b/Data/MineSharp.SourceGenerator/MineSharp.SourceGenerator.csproj index 43fcb6fb..7238608e 100644 --- a/Data/MineSharp.SourceGenerator/MineSharp.SourceGenerator.csproj +++ b/Data/MineSharp.SourceGenerator/MineSharp.SourceGenerator.csproj @@ -4,9 +4,9 @@ Exe enable enable - net7.0;net8.0 12 false + net8.0 diff --git a/Data/MineSharp.SourceGenerator/Program.cs b/Data/MineSharp.SourceGenerator/Program.cs index 79f43b4f..40cc6ed1 100644 --- a/Data/MineSharp.SourceGenerator/Program.cs +++ b/Data/MineSharp.SourceGenerator/Program.cs @@ -10,7 +10,7 @@ { new BiomeGenerator().Run(data), new BlockGenerator().Run(data), new EffectGenerator().Run(data), new EnchantmentGenerator().Run(data), new EntityGenerator().Run(data), new ItemGenerator().Run(data), - new ProtocolGenerator().Run(data) + new ProtocolGenerator().Run(data), new ParticleGenerator().Run(data) }; if (Directory.Exists(DirectoryUtils.GetSourceDirectory())) diff --git a/Data/MineSharp.SourceGenerator/Utils/NameUtils.cs b/Data/MineSharp.SourceGenerator/Utils/NameUtils.cs index c9ad82aa..43fa1332 100644 --- a/Data/MineSharp.SourceGenerator/Utils/NameUtils.cs +++ b/Data/MineSharp.SourceGenerator/Utils/NameUtils.cs @@ -99,6 +99,11 @@ public static string GetGameState(string name) return CommonGetName(name); } + public static string GetParticleName(string name) + { + return CommonGetName(name); + } + public static string GetPacketName(string name, string direction, string ns) { direction = direction == "toClient" ? "CB" : "SB"; diff --git a/MineSharp.Core/Common/Biomes/BiomeCategory.cs b/MineSharp.Core/Common/Biomes/BiomeCategory.cs index 93303664..5d2c8a18 100644 --- a/MineSharp.Core/Common/Biomes/BiomeCategory.cs +++ b/MineSharp.Core/Common/Biomes/BiomeCategory.cs @@ -1,4 +1,4 @@ -/////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////// // This File is generated by MineSharp.SourceGenerator and should not be modified. // /////////////////////////////////////////////////////////////////////////////////////////// @@ -26,7 +26,7 @@ public enum BiomeCategory Mushroom = 15, Underground = 16, Nether = 17, - TheEnd = 18 + TheEnd = 18, } #pragma warning restore CS1591 diff --git a/MineSharp.Core/Common/Biomes/BiomeType.cs b/MineSharp.Core/Common/Biomes/BiomeType.cs index 2a14d0e0..68ea8bf4 100644 --- a/MineSharp.Core/Common/Biomes/BiomeType.cs +++ b/MineSharp.Core/Common/Biomes/BiomeType.cs @@ -1,4 +1,4 @@ -/////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////// // This File is generated by MineSharp.SourceGenerator and should not be modified. // /////////////////////////////////////////////////////////////////////////////////////////// @@ -71,7 +71,7 @@ public enum BiomeType EndBarrens = 60, MangroveSwamp = 61, DeepDark = 62, - CherryGrove = 63 + CherryGrove = 63, } #pragma warning restore CS1591 diff --git a/MineSharp.Core/Common/Blocks/BlockType.cs b/MineSharp.Core/Common/Blocks/BlockType.cs index 489b6dd2..a2bc54b5 100644 --- a/MineSharp.Core/Common/Blocks/BlockType.cs +++ b/MineSharp.Core/Common/Blocks/BlockType.cs @@ -1,4 +1,4 @@ -/////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////// // This File is generated by MineSharp.SourceGenerator and should not be modified. // /////////////////////////////////////////////////////////////////////////////////////////// @@ -1068,7 +1068,7 @@ public enum BlockType Crafter = 1057, TrialSpawner = 1058, Vault = 1059, - HeavyCore = 1060 + HeavyCore = 1060, } #pragma warning restore CS1591 diff --git a/MineSharp.Core/Common/Effects/EffectType.cs b/MineSharp.Core/Common/Effects/EffectType.cs index 8f613ecd..b2cc051b 100644 --- a/MineSharp.Core/Common/Effects/EffectType.cs +++ b/MineSharp.Core/Common/Effects/EffectType.cs @@ -1,4 +1,4 @@ -/////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////// // This File is generated by MineSharp.SourceGenerator and should not be modified. // /////////////////////////////////////////////////////////////////////////////////////////// @@ -47,7 +47,7 @@ public enum EffectType WindCharged = 36, Weaving = 37, Oozing = 38, - Infested = 39 + Infested = 39, } #pragma warning restore CS1591 diff --git a/MineSharp.Core/Common/Enchantments/EnchantmentCategory.cs b/MineSharp.Core/Common/Enchantments/EnchantmentCategory.cs index 6a2cb6f6..6ca8343c 100644 --- a/MineSharp.Core/Common/Enchantments/EnchantmentCategory.cs +++ b/MineSharp.Core/Common/Enchantments/EnchantmentCategory.cs @@ -1,4 +1,4 @@ -/////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////// // This File is generated by MineSharp.SourceGenerator and should not be modified. // /////////////////////////////////////////////////////////////////////////////////////////// @@ -21,7 +21,7 @@ public enum EnchantmentCategory Trident = 10, Crossbow = 11, Vanishable = 12, - ArmorLegs = 13 + ArmorLegs = 13, } #pragma warning restore CS1591 diff --git a/MineSharp.Core/Common/Enchantments/EnchantmentType.cs b/MineSharp.Core/Common/Enchantments/EnchantmentType.cs index 790902fe..e53e1bb3 100644 --- a/MineSharp.Core/Common/Enchantments/EnchantmentType.cs +++ b/MineSharp.Core/Common/Enchantments/EnchantmentType.cs @@ -1,4 +1,4 @@ -/////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////// // This File is generated by MineSharp.SourceGenerator and should not be modified. // /////////////////////////////////////////////////////////////////////////////////////////// @@ -50,7 +50,7 @@ public enum EnchantmentType SweepingEdge = 39, Density = 40, Breach = 41, - WindBurst = 42 + WindBurst = 42, } #pragma warning restore CS1591 diff --git a/MineSharp.Core/Common/Entities/EntityCategory.cs b/MineSharp.Core/Common/Entities/EntityCategory.cs index 84277677..be461614 100644 --- a/MineSharp.Core/Common/Entities/EntityCategory.cs +++ b/MineSharp.Core/Common/Entities/EntityCategory.cs @@ -1,4 +1,4 @@ -/////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////// // This File is generated by MineSharp.SourceGenerator and should not be modified. // /////////////////////////////////////////////////////////////////////////////////////////// @@ -13,7 +13,7 @@ public enum EntityCategory Immobile = 2, Projectiles = 3, HostileMobs = 4, - Vehicles = 5 + Vehicles = 5, } #pragma warning restore CS1591 diff --git a/MineSharp.Core/Common/Entities/EntityType.cs b/MineSharp.Core/Common/Entities/EntityType.cs index d82bc8d4..553ffb32 100644 --- a/MineSharp.Core/Common/Entities/EntityType.cs +++ b/MineSharp.Core/Common/Entities/EntityType.cs @@ -1,4 +1,4 @@ -/////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////// // This File is generated by MineSharp.SourceGenerator and should not be modified. // /////////////////////////////////////////////////////////////////////////////////////////// @@ -137,7 +137,7 @@ public enum EntityType Armadillo = 126, Bogged = 127, BreezeWindCharge = 128, - OminousItemSpawner = 129 + OminousItemSpawner = 129, } #pragma warning restore CS1591 diff --git a/MineSharp.Core/Common/Items/ItemType.cs b/MineSharp.Core/Common/Items/ItemType.cs index 6d850f5b..8aa85062 100644 --- a/MineSharp.Core/Common/Items/ItemType.cs +++ b/MineSharp.Core/Common/Items/ItemType.cs @@ -1,4 +1,4 @@ -/////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////// // This File is generated by MineSharp.SourceGenerator and should not be modified. // /////////////////////////////////////////////////////////////////////////////////////////// @@ -1339,7 +1339,7 @@ public enum ItemType OminousTrialKey = 1328, Vault = 1329, OminousBottle = 1330, - BreezeRod = 1331 + BreezeRod = 1331, } #pragma warning restore CS1591