diff --git a/TechbloxModdingAPI/Block.cs b/TechbloxModdingAPI/Block.cs index ed4e2aa..9d71d24 100644 --- a/TechbloxModdingAPI/Block.cs +++ b/TechbloxModdingAPI/Block.cs @@ -87,23 +87,27 @@ namespace TechbloxModdingAPI remove => BlockEventsEngine.Removed -= value; } - internal static readonly Dictionary> GroupToConstructor = - new Dictionary> + private static readonly Dictionary Constructor, Type Type)> GroupToConstructor = + new Dictionary, Type)> { - {CommonExclusiveGroups.DAMPEDSPRING_BLOCK_GROUP, id => new DampedSpring(id)}, - {CommonExclusiveGroups.ENGINE_BLOCK_BUILD_GROUP, id => new Engine(id)} + {CommonExclusiveGroups.DAMPEDSPRING_BLOCK_GROUP, (id => new DampedSpring(id), typeof(DampedSpring))}, + {CommonExclusiveGroups.ENGINE_BLOCK_BUILD_GROUP, (id => new Engine(id), typeof(Engine))} }; internal static Block New(EGID egid) { return GroupToConstructor.ContainsKey(egid.groupID) - ? GroupToConstructor[egid.groupID](egid) + ? GroupToConstructor[egid.groupID].Constructor(egid) : new Block(egid); } public Block(EGID id) { - Id = id; //TODO: Check if block type is correct + Id = id; + Type expectedType; + if (GroupToConstructor.ContainsKey(id.groupID) && + !GetType().IsAssignableFrom(expectedType = GroupToConstructor[id.groupID].Type)) + throw new BlockSpecializationException($"Incorrect block type! Expected: {expectedType} Actual: {GetType()}"); } /// @@ -111,11 +115,11 @@ namespace TechbloxModdingAPI /// 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) + public Block(uint id) : this(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.")) { - 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."); } ///