diff --git a/GamecraftModdingAPI/Block.cs b/GamecraftModdingAPI/Block.cs index 1a8673a..46fb00c 100644 --- a/GamecraftModdingAPI/Block.cs +++ b/GamecraftModdingAPI/Block.cs @@ -71,13 +71,35 @@ namespace GamecraftModdingAPI public Block(EGID id) { Id = id; + if (!BlockEngine.BlockExists(Id)) + { + Sync(); + if (!BlockEngine.BlockExists(Id)) + { + throw new BlockDoesNotExistException($"Block {Id.entityID} must be placed using PlaceNew(...) since it does not exist yet"); + } + } } public Block(uint id) { Id = new EGID(id, CommonExclusiveGroups.OWNED_BLOCKS_GROUP); + if (!BlockEngine.BlockExists(Id)) + { + Sync(); + if (!BlockEngine.BlockExists(Id)) + { + throw new BlockDoesNotExistException($"Block {Id.entityID} must be placed using PlaceNew(...) since it does not exist yet"); + } + } } + /// + /// Synchronize newly created entity components with entities DB. + /// This forces a partial game tick, so it may be slow. + /// This also has the potential to make Gamecraft unstable. + /// Use this sparingly. + /// protected static void Sync() { DeterministicStepCompositionRootPatch.SubmitEntitiesNow(); @@ -219,10 +241,8 @@ namespace GamecraftModdingAPI /// Convert the block to a specialised block class. /// /// The block. - /// Force an entity sync when true. - /// Only set this to false when the block was not placed the same tick this was called. /// The specialised block type. - public T Specialise(bool sameTick = true) where T : Block + public T Specialise() where T : Block { // What have I gotten myself into? // C# can't cast to a child of Block unless the object was originally that child type @@ -233,7 +253,6 @@ namespace GamecraftModdingAPI { throw new BlockSpecializationException("Specialized block constructor does not accept an EGID"); } - if (sameTick) Sync(); return (T)ctor.Invoke(new object[] { Id }); } diff --git a/GamecraftModdingAPI/Blocks/BlockExceptions.cs b/GamecraftModdingAPI/Blocks/BlockExceptions.cs index 47af014..557282a 100644 --- a/GamecraftModdingAPI/Blocks/BlockExceptions.cs +++ b/GamecraftModdingAPI/Blocks/BlockExceptions.cs @@ -40,4 +40,15 @@ namespace GamecraftModdingAPI.Blocks { } } + + public class BlockDoesNotExistException : BlockException + { + public BlockDoesNotExistException() + { + } + + public BlockDoesNotExistException(string message) : base(message) + { + } + } } diff --git a/GamecraftModdingAPI/Blocks/ConsoleBlock.cs b/GamecraftModdingAPI/Blocks/ConsoleBlock.cs index b4fcaa4..da9c895 100644 --- a/GamecraftModdingAPI/Blocks/ConsoleBlock.cs +++ b/GamecraftModdingAPI/Blocks/ConsoleBlock.cs @@ -21,7 +21,6 @@ namespace GamecraftModdingAPI.Blocks { EGID id = PlacementEngine.PlaceBlock(BlockIDs.ConsoleBlock, color, darkness, position, uscale, scale, player, rotation); - Sync(); return new ConsoleBlock(id); } catch (Exception e) diff --git a/GamecraftModdingAPI/Blocks/Motor.cs b/GamecraftModdingAPI/Blocks/Motor.cs index 13a246f..8cafccb 100644 --- a/GamecraftModdingAPI/Blocks/Motor.cs +++ b/GamecraftModdingAPI/Blocks/Motor.cs @@ -29,7 +29,6 @@ namespace GamecraftModdingAPI.Blocks { EGID id = PlacementEngine.PlaceBlock(block, color, darkness, position, uscale, scale, player, rotation); - Sync(); return new Motor(id); } catch (Exception e) diff --git a/GamecraftModdingAPI/Blocks/Piston.cs b/GamecraftModdingAPI/Blocks/Piston.cs index 1e69df6..2f7064c 100644 --- a/GamecraftModdingAPI/Blocks/Piston.cs +++ b/GamecraftModdingAPI/Blocks/Piston.cs @@ -29,7 +29,6 @@ namespace GamecraftModdingAPI.Blocks { EGID id = PlacementEngine.PlaceBlock(block, color, darkness, position, uscale, scale, player, rotation); - Sync(); return new Piston(id); } catch (Exception e) diff --git a/GamecraftModdingAPI/Blocks/Servo.cs b/GamecraftModdingAPI/Blocks/Servo.cs index 1c22d0b..dd2cc52 100644 --- a/GamecraftModdingAPI/Blocks/Servo.cs +++ b/GamecraftModdingAPI/Blocks/Servo.cs @@ -29,7 +29,6 @@ namespace GamecraftModdingAPI.Blocks { EGID id = PlacementEngine.PlaceBlock(block, color, darkness, position, uscale, scale, player, rotation); - Sync(); return new Servo(id); } catch (Exception e) diff --git a/GamecraftModdingAPI/Blocks/SignalingBlock.cs b/GamecraftModdingAPI/Blocks/SignalingBlock.cs index d654ca9..9c2f457 100644 --- a/GamecraftModdingAPI/Blocks/SignalingBlock.cs +++ b/GamecraftModdingAPI/Blocks/SignalingBlock.cs @@ -29,7 +29,6 @@ namespace GamecraftModdingAPI.Blocks { EGID id = PlacementEngine.PlaceBlock(block, color, darkness, position, uscale, scale, player, rotation); - Sync(); return new SignalingBlock(id); } catch (Exception e) diff --git a/GamecraftModdingAPI/Blocks/SpawnPoint.cs b/GamecraftModdingAPI/Blocks/SpawnPoint.cs index 8fd1fda..b43dffb 100644 --- a/GamecraftModdingAPI/Blocks/SpawnPoint.cs +++ b/GamecraftModdingAPI/Blocks/SpawnPoint.cs @@ -31,7 +31,6 @@ namespace GamecraftModdingAPI.Blocks { EGID id = PlacementEngine.PlaceBlock(block, color, darkness, position, uscale, scale, player, rotation); - Sync(); return new SpawnPoint(id); } catch (Exception e) diff --git a/GamecraftModdingAPI/Blocks/TextBlock.cs b/GamecraftModdingAPI/Blocks/TextBlock.cs index a905a6d..f2993fb 100644 --- a/GamecraftModdingAPI/Blocks/TextBlock.cs +++ b/GamecraftModdingAPI/Blocks/TextBlock.cs @@ -22,7 +22,6 @@ namespace GamecraftModdingAPI.Blocks { EGID id = PlacementEngine.PlaceBlock(BlockIDs.TextBlock, color, darkness, position, uscale, scale, player, rotation); - Sync(); return new TextBlock(id); } catch (Exception e) diff --git a/GamecraftModdingAPI/Blocks/Timer.cs b/GamecraftModdingAPI/Blocks/Timer.cs index b31f9f8..a53adf1 100644 --- a/GamecraftModdingAPI/Blocks/Timer.cs +++ b/GamecraftModdingAPI/Blocks/Timer.cs @@ -26,7 +26,6 @@ namespace GamecraftModdingAPI.Blocks { EGID id = PlacementEngine.PlaceBlock(BlockIDs.Timer, color, darkness, position, uscale, scale, player, rotation); - Sync(); return new Timer(id); } catch (Exception e)