Add block repository support code
This commit is contained in:
parent
e2f9929783
commit
61bd2d32d3
21
TrueCraft.API/Logic/IBlockRepository.cs
Normal file
21
TrueCraft.API/Logic/IBlockRepository.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System;
|
||||
|
||||
namespace TrueCraft.API.Logic
|
||||
{
|
||||
/// <summary>
|
||||
/// Providers block providers for a server.
|
||||
/// </summary>
|
||||
public interface IBlockRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
IBlockProvider GetBlockProvider(byte id);
|
||||
/// <summary>
|
||||
/// Registers a new block provider. This overrides any existing block providers that use the
|
||||
/// same block ID.
|
||||
/// </summary>
|
||||
void RegisterBlockProvider(IBlockProvider provider);
|
||||
}
|
||||
}
|
@ -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<IRemoteClient> Clients { get; }
|
||||
IList<IWorld> Worlds { get; }
|
||||
IEventScheduler Scheduler { get; }
|
||||
IBlockRepository BlockRepository { get; }
|
||||
|
||||
void Start(IPEndPoint endPoint);
|
||||
void RegisterPacketHandler(byte packetId, PacketHandler handler);
|
||||
|
@ -79,6 +79,7 @@
|
||||
<Compile Include="Server\ChatMessageEventArgs.cs" />
|
||||
<Compile Include="BlockFace.cs" />
|
||||
<Compile Include="Server\PlayerJoinedEventArgs.cs" />
|
||||
<Compile Include="Logic\IBlockRepository.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<ItemGroup />
|
||||
|
@ -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; } }
|
||||
|
||||
|
41
TrueCraft/BlockRepository.cs
Normal file
41
TrueCraft/BlockRepository.cs
Normal file
@ -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<Type>();
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -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<IWorld> Worlds { get; private set; }
|
||||
public IList<IEntityManager> 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<IEntityManager>();
|
||||
LogProviders = new List<ILogProvider>();
|
||||
Scheduler = new EventScheduler(this);
|
||||
var blockRepository = new BlockRepository();
|
||||
blockRepository.DiscoverBlockProviders();
|
||||
BlockRepository = blockRepository;
|
||||
ExecutingTick = false;
|
||||
|
||||
reader.RegisterCorePackets();
|
||||
|
@ -57,6 +57,7 @@
|
||||
<Compile Include="Entities\PlayerEntity.cs" />
|
||||
<Compile Include="Handlers\InteractionHandlers.cs" />
|
||||
<Compile Include="Commands\DebugCommands.cs" />
|
||||
<Compile Include="BlockRepository.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<ItemGroup>
|
||||
|
Loading…
x
Reference in New Issue
Block a user