From 9fb96750d66760a8aeeecb6df89bdcf79203ccb6 Mon Sep 17 00:00:00 2001 From: sukasa Date: Sun, 4 Nov 2012 19:33:06 -0800 Subject: [PATCH 1/2] Modification to allow force-returning of TileEntity objects via additional param. Useful for dealing with mod-installed TileEntities not natively supported by Substrate --- SubstrateCS/Source/AlphaBlockCollection.cs | 12 ++++++-- SubstrateCS/Source/Core/BlockTileEntities.cs | 4 +-- SubstrateCS/Source/TileEntityFactory.cs | 30 ++++++++++++++++++-- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/SubstrateCS/Source/AlphaBlockCollection.cs b/SubstrateCS/Source/AlphaBlockCollection.cs index 6f05a2d..9c1b331 100644 --- a/SubstrateCS/Source/AlphaBlockCollection.cs +++ b/SubstrateCS/Source/AlphaBlockCollection.cs @@ -677,9 +677,15 @@ namespace Substrate } /// - public TileEntity GetTileEntity (int x, int y, int z) + public TileEntity GetTileEntity(int x, int y, int z) { - return _tileEntityManager.GetTileEntity(x, y, z); + return GetTileEntity(x, y, z, false); + } + + /// + public TileEntity GetTileEntity(int x, int y, int z, bool unregistered) + { + return _tileEntityManager.GetTileEntity(x, y, z, unregistered); } internal TileEntity GetTileEntity (int index) @@ -687,7 +693,7 @@ namespace Substrate int x, y, z; _blocks.GetMultiIndex(index, out x, out y, out z); - return _tileEntityManager.GetTileEntity(x, y, z); + return GetTileEntity(x, y, z, false); } /// diff --git a/SubstrateCS/Source/Core/BlockTileEntities.cs b/SubstrateCS/Source/Core/BlockTileEntities.cs index 4876da6..0790add 100644 --- a/SubstrateCS/Source/Core/BlockTileEntities.cs +++ b/SubstrateCS/Source/Core/BlockTileEntities.cs @@ -31,7 +31,7 @@ namespace Substrate.Core BuildTileEntityCache(); } - public TileEntity GetTileEntity (int x, int y, int z) + public TileEntity GetTileEntity (int x, int y, int z, bool unregistered) { BlockKey key = (TranslateCoordinates != null) ? TranslateCoordinates(x, y, z) @@ -43,7 +43,7 @@ namespace Substrate.Core return null; } - return TileEntityFactory.Create(te); + return unregistered ? TileEntityFactory.CreateAlways(te) : TileEntityFactory.Create(te); } public void SetTileEntity (int x, int y, int z, TileEntity te) diff --git a/SubstrateCS/Source/TileEntityFactory.cs b/SubstrateCS/Source/TileEntityFactory.cs index cb2f033..5d7865a 100644 --- a/SubstrateCS/Source/TileEntityFactory.cs +++ b/SubstrateCS/Source/TileEntityFactory.cs @@ -36,12 +36,13 @@ namespace Substrate /// /// A representing a single Tile Entity, containing an 'id' field of the Tile Entity's registered name. /// A new instance of a concrete type, or null if no type was registered with the given name. - public static TileEntity Create (TagNodeCompound tree) + public static TileEntity Create(TagNodeCompound tree) { string type = tree["id"].ToTagString(); Type t; - if (!_registry.TryGetValue(type, out t)) { + if (!_registry.TryGetValue(type, out t)) + { return null; } @@ -50,6 +51,31 @@ namespace Substrate return te.LoadTreeSafe(tree); } + /// + /// Create a new instance of a type by NBT node, or a blank TileEntity otherwise. + /// + /// A representing a single Tile Entity, containing an 'id' field of the Tile Entity's registered name. + /// A new instance of a concrete type, or null if no type was registered with the given name. + public static TileEntity CreateAlways(TagNodeCompound tree) + { + string type = tree["id"].ToTagString(); + + Type t; + + TileEntity te; + + if (!_registry.TryGetValue(type, out t)) + { + te = new TileEntity(""); + } + else + { + te = Activator.CreateInstance(t) as TileEntity; + } + + return te.LoadTreeSafe(tree); + } + /// /// Lookup a concrete type by name. /// From 46eb7c3244623181759adb49ce80777d781896d2 Mon Sep 17 00:00:00 2001 From: sukasa Date: Mon, 5 Nov 2012 21:35:57 -0800 Subject: [PATCH 2/2] Undid previous changes. Added CreateGeneric() function to TileEntityFactory that returns a TileEntity class if no registered class exists for a given NBT tree. Changed GetTileEntity to in BlockTileEntities to use the new function. --- SubstrateCS/Source/AlphaBlockCollection.cs | 12 +++--------- SubstrateCS/Source/Core/BlockTileEntities.cs | 4 ++-- SubstrateCS/Source/TileEntityFactory.cs | 14 +++++--------- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/SubstrateCS/Source/AlphaBlockCollection.cs b/SubstrateCS/Source/AlphaBlockCollection.cs index 9c1b331..6f05a2d 100644 --- a/SubstrateCS/Source/AlphaBlockCollection.cs +++ b/SubstrateCS/Source/AlphaBlockCollection.cs @@ -677,15 +677,9 @@ namespace Substrate } /// - public TileEntity GetTileEntity(int x, int y, int z) + public TileEntity GetTileEntity (int x, int y, int z) { - return GetTileEntity(x, y, z, false); - } - - /// - public TileEntity GetTileEntity(int x, int y, int z, bool unregistered) - { - return _tileEntityManager.GetTileEntity(x, y, z, unregistered); + return _tileEntityManager.GetTileEntity(x, y, z); } internal TileEntity GetTileEntity (int index) @@ -693,7 +687,7 @@ namespace Substrate int x, y, z; _blocks.GetMultiIndex(index, out x, out y, out z); - return GetTileEntity(x, y, z, false); + return _tileEntityManager.GetTileEntity(x, y, z); } /// diff --git a/SubstrateCS/Source/Core/BlockTileEntities.cs b/SubstrateCS/Source/Core/BlockTileEntities.cs index 0790add..a934a3b 100644 --- a/SubstrateCS/Source/Core/BlockTileEntities.cs +++ b/SubstrateCS/Source/Core/BlockTileEntities.cs @@ -31,7 +31,7 @@ namespace Substrate.Core BuildTileEntityCache(); } - public TileEntity GetTileEntity (int x, int y, int z, bool unregistered) + public TileEntity GetTileEntity (int x, int y, int z) { BlockKey key = (TranslateCoordinates != null) ? TranslateCoordinates(x, y, z) @@ -43,7 +43,7 @@ namespace Substrate.Core return null; } - return unregistered ? TileEntityFactory.CreateAlways(te) : TileEntityFactory.Create(te); + return TileEntityFactory.CreateGeneric(te); } public void SetTileEntity (int x, int y, int z, TileEntity te) diff --git a/SubstrateCS/Source/TileEntityFactory.cs b/SubstrateCS/Source/TileEntityFactory.cs index 5d7865a..6704c64 100644 --- a/SubstrateCS/Source/TileEntityFactory.cs +++ b/SubstrateCS/Source/TileEntityFactory.cs @@ -52,27 +52,23 @@ namespace Substrate } /// - /// Create a new instance of a type by NBT node, or a blank TileEntity otherwise. + /// Create a new instance of a concrete type by NBT node. /// /// A representing a single Tile Entity, containing an 'id' field of the Tile Entity's registered name. /// A new instance of a concrete type, or null if no type was registered with the given name. - public static TileEntity CreateAlways(TagNodeCompound tree) + public static TileEntity CreateGeneric(TagNodeCompound tree) { string type = tree["id"].ToTagString(); Type t; - TileEntity te; - if (!_registry.TryGetValue(type, out t)) { - te = new TileEntity(""); - } - else - { - te = Activator.CreateInstance(t) as TileEntity; + t = typeof (TileEntity); } + TileEntity te = Activator.CreateInstance(t, true) as TileEntity; + return te.LoadTreeSafe(tree); }