From 61bd2d32d378c22015f37274441f56a6f54ca668 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Mon, 26 Jan 2015 16:40:32 -0700 Subject: [PATCH] Add block repository support code --- TrueCraft.API/Logic/IBlockRepository.cs | 21 +++++++++++ TrueCraft.API/Server/IMultiplayerServer.cs | 2 ++ TrueCraft.API/TrueCraft.API.csproj | 1 + TrueCraft.Core/Logic/Items/MinecartItem.cs | 4 +-- TrueCraft/BlockRepository.cs | 41 ++++++++++++++++++++++ TrueCraft/MultiplayerServer.cs | 5 +++ TrueCraft/TrueCraft.csproj | 1 + 7 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 TrueCraft.API/Logic/IBlockRepository.cs create mode 100644 TrueCraft/BlockRepository.cs diff --git a/TrueCraft.API/Logic/IBlockRepository.cs b/TrueCraft.API/Logic/IBlockRepository.cs new file mode 100644 index 0000000..80b7816 --- /dev/null +++ b/TrueCraft.API/Logic/IBlockRepository.cs @@ -0,0 +1,21 @@ +using System; + +namespace TrueCraft.API.Logic +{ + /// + /// Providers block providers for a server. + /// + public interface IBlockRepository + { + /// + /// Gets this repository's block provider for the specified block ID. This may return null + /// if the block ID in question has no corresponding block provider. + /// + IBlockProvider GetBlockProvider(byte id); + /// + /// Registers a new block provider. This overrides any existing block providers that use the + /// same block ID. + /// + void RegisterBlockProvider(IBlockProvider provider); + } +} \ No newline at end of file diff --git a/TrueCraft.API/Server/IMultiplayerServer.cs b/TrueCraft.API/Server/IMultiplayerServer.cs index 8c0e84b..655c29e 100644 --- a/TrueCraft.API/Server/IMultiplayerServer.cs +++ b/TrueCraft.API/Server/IMultiplayerServer.cs @@ -4,6 +4,7 @@ using System.Net; using System.Collections.Generic; using TrueCraft.API.World; using TrueCraft.API.Logging; +using TrueCraft.API.Logic; namespace TrueCraft.API.Server { @@ -22,6 +23,7 @@ namespace TrueCraft.API.Server IList Clients { get; } IList Worlds { get; } IEventScheduler Scheduler { get; } + IBlockRepository BlockRepository { get; } void Start(IPEndPoint endPoint); void RegisterPacketHandler(byte packetId, PacketHandler handler); diff --git a/TrueCraft.API/TrueCraft.API.csproj b/TrueCraft.API/TrueCraft.API.csproj index 3f82779..73f39ac 100644 --- a/TrueCraft.API/TrueCraft.API.csproj +++ b/TrueCraft.API/TrueCraft.API.csproj @@ -79,6 +79,7 @@ + diff --git a/TrueCraft.Core/Logic/Items/MinecartItem.cs b/TrueCraft.Core/Logic/Items/MinecartItem.cs index 3504bdc..e27c963 100644 --- a/TrueCraft.Core/Logic/Items/MinecartItem.cs +++ b/TrueCraft.Core/Logic/Items/MinecartItem.cs @@ -16,7 +16,7 @@ namespace TrueCraft.Core.Logic.Items public class MinecartWithChestItem : MinecartItem { - public static readonly short ItemID = 0x156; + public static readonly new short ItemID = 0x156; public override short ID { get { return 0x156; } } @@ -25,7 +25,7 @@ namespace TrueCraft.Core.Logic.Items public class MinecartWithFurnaceItem : MinecartItem { - public static readonly short ItemID = 0x157; + public static readonly new short ItemID = 0x157; public override short ID { get { return 0x157; } } diff --git a/TrueCraft/BlockRepository.cs b/TrueCraft/BlockRepository.cs new file mode 100644 index 0000000..2fe1e0e --- /dev/null +++ b/TrueCraft/BlockRepository.cs @@ -0,0 +1,41 @@ +using System; +using TrueCraft.API.Logic; +using System.Collections.Generic; +using System.Linq; + +namespace TrueCraft +{ + public class BlockRepository : IBlockRepository + { + private readonly IBlockProvider[] BlockProviders = new IBlockProvider[0x100]; + + public IBlockProvider GetBlockProvider(byte id) + { + return BlockProviders[id]; + } + + public void RegisterBlockProvider(IBlockProvider provider) + { + BlockProviders[provider.ID] = provider; + } + + internal void DiscoverBlockProviders() + { + var providerTypes = new List(); + foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) + { + foreach (var type in assembly.GetTypes().Where(t => + typeof(IBlockProvider).IsAssignableFrom(t) && !t.IsAbstract)) + { + providerTypes.Add(type); + } + } + + providerTypes.ForEach(t => + { + var instance = (IBlockProvider)Activator.CreateInstance(t); + RegisterBlockProvider(instance); + }); + } + } +} \ No newline at end of file diff --git a/TrueCraft/MultiplayerServer.cs b/TrueCraft/MultiplayerServer.cs index 2545415..2b7fde5 100644 --- a/TrueCraft/MultiplayerServer.cs +++ b/TrueCraft/MultiplayerServer.cs @@ -11,6 +11,7 @@ using TrueCraft.API.Logging; using TrueCraft.Core.Networking.Packets; using TrueCraft.API; using TrueCraft.Core.Logging; +using TrueCraft.API.Logic; namespace TrueCraft { @@ -24,6 +25,7 @@ namespace TrueCraft public IList Worlds { get; private set; } public IList EntityManagers { get; private set; } public IEventScheduler Scheduler { get; private set; } + public IBlockRepository BlockRepository { get; private set; } private Timer NetworkWorker, EnvironmentWorker; private TcpListener Listener; @@ -43,6 +45,9 @@ namespace TrueCraft EntityManagers = new List(); LogProviders = new List(); Scheduler = new EventScheduler(this); + var blockRepository = new BlockRepository(); + blockRepository.DiscoverBlockProviders(); + BlockRepository = blockRepository; ExecutingTick = false; reader.RegisterCorePackets(); diff --git a/TrueCraft/TrueCraft.csproj b/TrueCraft/TrueCraft.csproj index 25bfd9f..9c5510f 100644 --- a/TrueCraft/TrueCraft.csproj +++ b/TrueCraft/TrueCraft.csproj @@ -57,6 +57,7 @@ +