From aa0aefd41b4b1bbf1adb55bc1850d6f1e5baf5d3 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 11 Jul 2020 02:26:36 +0200 Subject: [PATCH] Find block when group is unknown --- GamecraftModdingAPI/Block.cs | 10 ++++++++-- GamecraftModdingAPI/Blocks/BlockEngine.cs | 12 ++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/GamecraftModdingAPI/Block.cs b/GamecraftModdingAPI/Block.cs index 939d16c..16a0c81 100644 --- a/GamecraftModdingAPI/Block.cs +++ b/GamecraftModdingAPI/Block.cs @@ -135,8 +135,14 @@ namespace GamecraftModdingAPI Id = id; } - public Block(uint id) : this(new EGID(id, CommonExclusiveGroups.BUILD_STANDARD_BLOCK_GROUP)) - { //TODO: Figure out the block group based on the id + /// + /// This overload searches for the correct group the block is in. + /// It will throw an exception if the block doesn't exist. + /// Use the EGID constructor where possible or subclasses of Block as those specify the group. + /// + public Block(uint id) + { + Id = BlockEngine.FindBlockEGID(id) ?? throw new BlockTypeException("Could not find the appropriate group for the block. The block probably doesn't exist or hasn't been submitted."); } public EGID Id { get; } diff --git a/GamecraftModdingAPI/Blocks/BlockEngine.cs b/GamecraftModdingAPI/Blocks/BlockEngine.cs index 4a90de7..81a1630 100644 --- a/GamecraftModdingAPI/Blocks/BlockEngine.cs +++ b/GamecraftModdingAPI/Blocks/BlockEngine.cs @@ -171,6 +171,18 @@ namespace GamecraftModdingAPI.Blocks return list.ToArray(); } + public EGID? FindBlockEGID(uint id) + { + var groups = entitiesDB.FindGroups(); + foreach (ExclusiveGroupStruct group in groups) + { + if (entitiesDB.Exists(id, group)) + return new EGID(id, group); + } + + return null; + } + /// /// Synchronize newly created entity components with entities DB. /// This forces a partial game tick, so it may be slow.