Browse Source

Check if block type is correct

tags/v2.0.0
NorbiPeti 2 years ago
parent
commit
b31eaa20c0
Signed by: NorbiPeti <szatmari.norbert.peter@gmail.com> GPG Key ID: DBA4C4549A927E56
1 changed files with 14 additions and 10 deletions
  1. +14
    -10
      TechbloxModdingAPI/Block.cs

+ 14
- 10
TechbloxModdingAPI/Block.cs View File

@@ -87,23 +87,27 @@ namespace TechbloxModdingAPI
remove => BlockEventsEngine.Removed -= value;
}

internal static readonly Dictionary<ExclusiveBuildGroup, Func<EGID, Block>> GroupToConstructor =
new Dictionary<ExclusiveBuildGroup, Func<EGID, Block>>
private static readonly Dictionary<ExclusiveBuildGroup, (Func<EGID, Block> Constructor, Type Type)> GroupToConstructor =
new Dictionary<ExclusiveBuildGroup, (Func<EGID, Block>, 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()}");
}

/// <summary>
@@ -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.
/// </summary>
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.");
}

/// <summary>


Loading…
Cancel
Save