Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/datav2 #37

Merged
merged 7 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Components/MineSharp.Protocol/MinecraftClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public sealed class MinecraftClient : IDisposable
private IPacketHandler _internalPacketHandler;
private MinecraftStream? _stream;
private Task? _streamLoop;
public IPAddress ip;
private IPAddress ip;

/// <summary>
/// Fires whenever the client received a known Packet
Expand Down Expand Up @@ -342,7 +342,7 @@ private async Task ReceivePackets()
{
var buffer = this._stream!.ReadPacket();
var packetId = buffer.ReadVarInt();
var packetType = this.Data.Protocol.FromPacketId(PacketFlow.Clientbound, this.gameState, packetId);
var packetType = this.Data.Protocol.GetPacketType(PacketFlow.Clientbound, this.gameState, packetId);

if (this._bundlePackets)
this._bundledPackets.Enqueue((packetType, buffer));
Expand Down Expand Up @@ -491,7 +491,7 @@ public static async Task<ServerStatus> RequestServerStatus(
int timeout = 10000,
ITcpClientFactory? tcpFactory = null)
{
var latest = MinecraftData.FromVersion(LATEST_SUPPORTED_VERSION);
var latest = await MinecraftData.FromVersion(LATEST_SUPPORTED_VERSION);
var client = new MinecraftClient(
latest,
Session.OfflineSession("RequestStatus"),
Expand Down Expand Up @@ -536,10 +536,10 @@ public static async Task<ServerStatus> RequestServerStatus(
/// <param name="hostname"></param>
/// <param name="port"></param>
/// <returns></returns>
public static Task<MinecraftData> AutodetectServerVersion(string hostname, ushort port)
public static async Task<MinecraftData> AutodetectServerVersion(string hostname, ushort port)
{
return RequestServerStatus(hostname, port)
.ContinueWith(
prev => MinecraftData.FromVersion(prev.Result.Version));
var status = await RequestServerStatus(hostname, port);

return await MinecraftData.FromVersion(status.Version);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void WriteOptionalItem(this PacketBuffer buffer, Item? item)
return null;

return new Item(
data.Items.GetById(buffer.ReadVarInt()),
data.Items.ById(buffer.ReadVarInt())!,
buffer.ReadByte(),
null,
buffer.ReadNbtCompound());
Expand Down
6 changes: 3 additions & 3 deletions Components/MineSharp.World/AbstractWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
/// </summary>
public readonly MinecraftData Data = data;

private readonly BlockInfo OutOfMapBlock = data.Blocks.GetByType(BlockType.Air);
private readonly BlockInfo OutOfMapBlock = data.Blocks.ByType(BlockType.Air)!;

/// <inheritdoc />
[Pure]
Expand Down Expand Up @@ -161,7 +161,7 @@
var relative = this.ToChunkPosition(position);
var blockState = chunk.GetBlockAt(relative);
var block = new Block(
this.Data.Blocks.GetByState(blockState),
this.Data.Blocks.ByState(blockState)!,
blockState,
position);
return block;
Expand Down Expand Up @@ -215,7 +215,7 @@
int found = 0;
foreach (var chunk in this.Chunks.Values)
{
foreach (var block in chunk.FindBlocks(type, maxCount - found))

Check warning on line 218 in Components/MineSharp.World/AbstractWorld.cs

View workflow job for this annotation

GitHub Actions / build

'IChunk.FindBlocks(BlockType, int?)' is obsolete
{
block.Position = this.ToWorldPosition(chunk.Coordinates, block.Position);
yield return block;
Expand All @@ -233,7 +233,7 @@
{
var worldPosition = this.ToWorldPosition(chunk.Coordinates, position);
this.OnBlockUpdated?.Invoke(this, new Block( // TODO: Hier jedes mal ein neuen block zu erstellen ist quatsch
this.Data.Blocks.GetByState(state), state, worldPosition));
this.Data.Blocks.ByState(state)!, state, worldPosition));
}

private int NonNegativeMod(int x, int m)
Expand Down
10 changes: 5 additions & 5 deletions Components/MineSharp.World/V1_18/ChunkSection_1_18.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public int GetBlockAt(Position position)
public void SetBlockAt(int state, Position position)
{
var index = GetBlockIndex(position);
var old = this._data.Blocks.GetByState(
this._blockContainer.GetAt(index));
var old = this._data.Blocks.ByState(
this._blockContainer.GetAt(index))!;

bool wasSolid = old.IsSolid();
bool isSolid = this._data.Blocks.GetByState(state).IsSolid();
bool isSolid = this._data.Blocks.ByState(state)!.IsSolid();

if (wasSolid != isSolid)
{
Expand All @@ -54,7 +54,7 @@ public Biome GetBiomeAt(Position position)
{
var index = GetBiomeIndex(position);
var state = this._biomeContainer.GetAt(index);
return new Biome(this._data.Biomes.GetById(state));
return new Biome(this._data.Biomes.ById(state)!);
}

public void SetBiomeAt(Position position, Biome biome)
Expand All @@ -65,7 +65,7 @@ public void SetBiomeAt(Position position, Biome biome)

public IEnumerable<Block> FindBlocks(BlockType type, int? maxCount = null)
{
var info = this._data.Blocks.GetByType(type);
var info = this._data.Blocks.ByType(type)!;
if (!this._blockContainer.Palette.ContainsState(info.MinState, info.MaxState) || maxCount == 0)
{
yield break;
Expand Down
8 changes: 4 additions & 4 deletions Components/Tests/MineSharp.Windows.Tests/WindowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public class WindowTests
[SetUp]
public void Setup()
{
this._data = MinecraftData.FromVersion("1.19.3");
this._data = MinecraftData.FromVersion("1.19.3").Result;
this._mainInventory = new Window(255, "", 4 * 9, null, null);
this._inventory = new Window(0, "Inventory", 9, this._mainInventory, null);

this._oakLog = this._data.Items.GetByName("oak_log");
this._netherStar = this._data.Items.GetByName("nether_star");
this._diamond = this._data.Items.GetByName("diamond");
this._oakLog = this._data.Items.ByName("oak_log")!;
this._netherStar = this._data.Items.ByName("nether_star")!;
this._diamond = this._data.Items.ByName("diamond")!;
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion Data/MineSharp.Data.Tests/MineSharp.Data.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<TargetFrameworks>net8.0;net7.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
24 changes: 23 additions & 1 deletion Data/MineSharp.Data.Tests/TestVersions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
using MineSharp.Core.Common.Biomes;
using MineSharp.Core.Common.Blocks;
using MineSharp.Core.Common.Effects;
using MineSharp.Core.Common.Enchantments;
using MineSharp.Core.Common.Entities;
using MineSharp.Core.Common.Items;
using MineSharp.Data.Protocol;

namespace MineSharp.Data.Tests;

public class Tests
Expand All @@ -14,7 +22,21 @@ public void TestLoadData()
{
foreach (var version in Versions)
{
MinecraftData.FromVersion(version).Wait();
var data = MinecraftData.FromVersion(version).Result;

// load all data
data.Biomes.ByType(BiomeType.Beach);
data.BlockCollisionShapes.GetShapes(BlockType.Stone, 0);
data.Blocks.ByType(BlockType.Air);
data.Effects.ByType(EffectType.Absorption);
data.Enchantments.ByType(EnchantmentType.Channeling);
data.Entities.ByType(EntityType.Allay);
data.Items.ByType(ItemType.Bamboo);
data.Language.GetTranslation("menu.quit");
data.Materials.GetMultiplier(Material.Shovel, ItemType.StoneSword);
data.Protocol.GetPacketId(PacketType.CB_Play_Login);
data.Recipes.ByItem(ItemType.DiamondShovel);
data.Windows.ById(0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace MineSharp.Data.BlockCollisionShapes;

public record BlockCollisionShapeDataBlob(
internal record BlockCollisionShapeDataBlob(
Dictionary<BlockType, int[]> BlockToIndicesMap,
Dictionary<int, float[][]> IndexToShapeMap);
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace MineSharp.Data.BlockCollisionShapes;

internal class BlockCollisionShapesProvider(JToken token, IBlockData blocks) : IDataProvider<BlockCollisionShapeDataBlob>
internal class BlockCollisionShapesProvider(JToken token) : IDataProvider<BlockCollisionShapeDataBlob>
{
private static readonly EnumNameLookup<BlockType> BlockTypeLookup = new();

Expand Down
36 changes: 35 additions & 1 deletion Data/MineSharp.Data/Blocks/BlockData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,38 @@
namespace MineSharp.Data.Blocks;

internal class BlockData(IDataProvider<BlockInfo[]> provider)
: TypeIdNameIndexedData<BlockType, BlockInfo>(provider), IBlockData;
: TypeIdNameIndexedData<BlockType, BlockInfo>(provider), IBlockData
{

private BlockInfo[]? sortedByState;

public int TotalBlockStateCount { get; private set; } = -1;

public BlockInfo? ByState(int state)
{
if (!this.Loaded)
this.Load();

var half = this.sortedByState!.Length / 2;
var start = state < sortedByState![half].MaxState
? 0
: half;

for (var i = start; i < sortedByState.Length; i++)
{
if (state <= this.sortedByState[i].MaxState)
return this.sortedByState[i];
}

return null;
}

protected override void InitializeData(BlockInfo[] data)
{
Array.Sort(data, (a, b) => a.MinState.CompareTo(b.MinState));
this.sortedByState = data;
this.TotalBlockStateCount = this.sortedByState[^1].MaxState + 1;

base.InitializeData(data);
}
}
5 changes: 3 additions & 2 deletions Data/MineSharp.Data/Blocks/BlockProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private static ItemType[] GetHarvestTools(JObject? array, IItemData items)
.Select(x => x.Name)
.Select(x => Convert.ToInt32(x))
.Select(items.ById)
.Select(x => x.Type)
.Select(x => x!.Type)
.ToArray();
}

Expand All @@ -120,7 +120,8 @@ private static IBlockProperty GetBlockProperty(JObject obj)
return type switch {
"bool" => new BoolProperty(name),
"int" => new IntProperty(name, numValues),
"enum" => new EnumProperty(name, obj.SelectToken("values")!.ToObject<string[]>()!)
"enum" => new EnumProperty(name, obj.SelectToken("values")!.ToObject<string[]>()!),
_ => throw new NotSupportedException($"Property of type '{type}' is not supported.")
};
}
}
41 changes: 0 additions & 41 deletions Data/MineSharp.Data/DataProvider.cs

This file was deleted.

6 changes: 0 additions & 6 deletions Data/MineSharp.Data/DataVersion.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Data/MineSharp.Data/Enchantments/EnchantmentProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static EnchantmentInfo FromToken(JToken token)
treasureOnly,
curse,
GetExclusions(exclude),
EnchantmentCategoryLookup.FromName(NameUtils.GetEnchantmentName(category)),
EnchantmentCategoryLookup.FromName(NameUtils.GetEnchantmentCategory(category)),
weight,
tradeable,
discoverable);
Expand Down
2 changes: 1 addition & 1 deletion Data/MineSharp.Data/Entities/EntityProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private static EntityInfo FromToken(JToken token)
width,
height,
MobTypeLookup.FromName(NameUtils.GetEntityName(mobType)),
EntityCategoryLookup.FromName(NameUtils.GetEntityName(category))
EntityCategoryLookup.FromName(NameUtils.GetEntityCategory(category))
);
}
}
28 changes: 23 additions & 5 deletions Data/MineSharp.Data/Framework/DataInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,20 @@ public interface IBiomeData : ITypeIdNameIndexedData<BiomeType, BiomeInfo>;
/// <summary>
/// Interface for implementing indexed block data
/// </summary>
public interface IBlockData : ITypeIdNameIndexedData<BlockType, BlockInfo>;
public interface IBlockData : ITypeIdNameIndexedData<BlockType, BlockInfo>
{
/// <summary>
/// The total number of block states
/// </summary>
public int TotalBlockStateCount { get; }

/// <summary>
/// Get a block info by state
/// </summary>
/// <param name="state"></param>
/// <returns></returns>
public BlockInfo? ByState(int state);
}

/// <summary>
/// Interface for implementing indexed effect data
Expand Down Expand Up @@ -70,7 +83,7 @@ public interface IBlockCollisionShapeData
public AABB[] GetShapes(BlockType type, int index)
{
var indices = GetShapeIndices(type);
var entry = indices.Length > 0 ? indices[index] : indices[0];
var entry = indices.Length > 1 ? indices[index] : indices[0];
return GetShapes(entry);
}

Expand All @@ -80,7 +93,7 @@ public AABB[] GetShapes(BlockType type, int index)
/// <param name="block"></param>
/// <returns></returns>
public AABB[] GetForBlock(Block block)
=> GetShapes(block.Info.Type, block.State);
=> GetShapes(block.Info.Type, block.Metadata);
}

/// <summary>
Expand All @@ -93,7 +106,7 @@ public interface ILanguageData
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public string GetTranslation(string name);
public string? GetTranslation(string name);
}

/// <summary>
Expand Down Expand Up @@ -129,7 +142,7 @@ public interface IProtocolData
/// <param name="state"></param>
/// <param name="id"></param>
/// <returns></returns>
public PacketType FromPacketType(PacketFlow flow, GameState state, int id);
public PacketType GetPacketType(PacketFlow flow, GameState state, int id);
}

/// <summary>
Expand All @@ -150,6 +163,11 @@ public interface IRecipeData
/// </summary>
public interface IWindowData
{
/// <summary>
/// A list of blocks that can be opened
/// </summary>
public IList<BlockType> AllowedBlocksToOpen { get; }

/// <summary>
/// Get a window info by id
/// </summary>
Expand Down
Loading
Loading