mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2026-08-15 13:04:36 +00:00
feat: expose block state properties through MCP
This commit is contained in:
parent
1fd4b0244d
commit
f1d844afbe
8 changed files with 4733 additions and 10 deletions
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using MinecraftClient.Mapping.BlockPalettes;
|
||||
using MinecraftClient.Protocol.Message;
|
||||
|
|
@ -78,6 +79,19 @@ namespace MinecraftClient.Mapping
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Exact raw block state ID. For Minecraft 1.12 and older this contains the packed block ID and metadata.
|
||||
/// </summary>
|
||||
public int StateId => blockIdAndMeta;
|
||||
|
||||
/// <summary>
|
||||
/// Get the properties associated with this block's exact state ID.
|
||||
/// </summary>
|
||||
public IReadOnlyDictionary<string, string> GetStateProperties()
|
||||
{
|
||||
return Palette.GetStateProperties(StateId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Material of the block
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MinecraftClient.Mapping.BlockPalettes
|
||||
{
|
||||
|
|
@ -23,6 +24,46 @@ namespace MinecraftClient.Mapping.BlockPalettes
|
|||
return Material.Air;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get block-state properties for a modern block state ID.
|
||||
/// </summary>
|
||||
/// <param name="stateId">Raw block state ID.</param>
|
||||
/// <returns>Block-state property names and values, or an empty map when unavailable.</returns>
|
||||
public IReadOnlyDictionary<string, string> GetStateProperties(int stateId)
|
||||
{
|
||||
BlockStateDefinition[] definitions = GetStateDefinitions();
|
||||
int low = 0;
|
||||
int high = definitions.Length - 1;
|
||||
|
||||
while (low <= high)
|
||||
{
|
||||
int middle = low + ((high - low) / 2);
|
||||
BlockStateDefinition definition = definitions[middle];
|
||||
if (stateId < definition.FirstStateId)
|
||||
{
|
||||
high = middle - 1;
|
||||
}
|
||||
else if (stateId > definition.LastStateId)
|
||||
{
|
||||
low = middle + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return definition.GetProperties(stateId);
|
||||
}
|
||||
}
|
||||
|
||||
return BlockStateDefinition.EmptyProperties;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get compact block-state definitions sorted by their first state ID.
|
||||
/// </summary>
|
||||
protected virtual BlockStateDefinition[] GetStateDefinitions()
|
||||
{
|
||||
return Array.Empty<BlockStateDefinition>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns TRUE if block ID uses old metadata encoding with ID and Meta inside one ushort
|
||||
/// Only Palette112 should override this.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,71 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace MinecraftClient.Mapping.BlockPalettes
|
||||
{
|
||||
/// <summary>
|
||||
/// Compact description of the property combinations in a contiguous block-state range.
|
||||
/// </summary>
|
||||
public sealed class BlockStateDefinition
|
||||
{
|
||||
private static readonly IReadOnlyDictionary<string, string> s_emptyProperties =
|
||||
new ReadOnlyDictionary<string, string>(new Dictionary<string, string>());
|
||||
|
||||
private readonly BlockStatePropertyDefinition[] _properties;
|
||||
public int FirstStateId { get; }
|
||||
public int LastStateId { get; }
|
||||
public static IReadOnlyDictionary<string, string> EmptyProperties => s_emptyProperties;
|
||||
|
||||
public BlockStateDefinition(int firstStateId, int stateCount, BlockStatePropertyDefinition[] properties)
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(firstStateId);
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(stateCount);
|
||||
ArgumentNullException.ThrowIfNull(properties);
|
||||
|
||||
FirstStateId = firstStateId;
|
||||
LastStateId = checked(firstStateId + stateCount - 1);
|
||||
_properties = properties;
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<string, string> GetProperties(int stateId)
|
||||
{
|
||||
if (stateId < FirstStateId || stateId > LastStateId)
|
||||
return EmptyProperties;
|
||||
|
||||
int offset = stateId - FirstStateId;
|
||||
Dictionary<string, string> result = new(_properties.Length, StringComparer.Ordinal);
|
||||
for (int i = 0; i < _properties.Length; i++)
|
||||
{
|
||||
BlockStatePropertyDefinition property = _properties[i];
|
||||
int valueIndex = (offset / property.Stride) % property.Values.Length;
|
||||
result[property.Name] = property.Values[valueIndex];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Property name and its values in Minecraft's block-state iteration order.
|
||||
/// </summary>
|
||||
public sealed class BlockStatePropertyDefinition
|
||||
{
|
||||
public string Name { get; }
|
||||
public string[] Values { get; }
|
||||
public int Stride { get; }
|
||||
|
||||
public BlockStatePropertyDefinition(string name, string[] values, int stride)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrEmpty(name);
|
||||
ArgumentNullException.ThrowIfNull(values);
|
||||
if (values.Length == 0)
|
||||
throw new ArgumentException("A block-state property must define at least one value.", nameof(values));
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(stride);
|
||||
|
||||
Name = name;
|
||||
Values = values;
|
||||
Stride = stride;
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -1090,6 +1090,8 @@ public sealed class MccMcpCapabilities : IMccMcpCapabilities
|
|||
typeLabel,
|
||||
blockId = block.BlockId,
|
||||
blockMeta = block.BlockMeta,
|
||||
stateId = block.StateId,
|
||||
properties = block.GetStateProperties(),
|
||||
distance = Math.Sqrt(dx * dx + dy * dy + dz * dz)
|
||||
});
|
||||
}
|
||||
|
|
@ -1139,7 +1141,7 @@ public sealed class MccMcpCapabilities : IMccMcpCapabilities
|
|||
int cy = (int)Math.Floor(playerLocation.Y) - 1;
|
||||
int cz = (int)Math.Floor(playerLocation.Z);
|
||||
|
||||
List<(int x, int y, int z, string material, string typeLabel, int blockId, byte blockMeta, double distance)> found = new();
|
||||
List<(int x, int y, int z, string material, string typeLabel, int blockId, byte blockMeta, int stateId, IReadOnlyDictionary<string, string> properties, double distance)> found = new();
|
||||
World world = client.GetWorld();
|
||||
|
||||
for (int y = cy - radius; y <= cy + radius && found.Count < limit; y++)
|
||||
|
|
@ -1167,6 +1169,8 @@ public sealed class MccMcpCapabilities : IMccMcpCapabilities
|
|||
block.GetTypeString(),
|
||||
block.BlockId,
|
||||
block.BlockMeta,
|
||||
block.StateId,
|
||||
block.GetStateProperties(),
|
||||
Math.Sqrt(dx * dx + dy * dy + dz * dz)));
|
||||
}
|
||||
}
|
||||
|
|
@ -1190,6 +1194,8 @@ public sealed class MccMcpCapabilities : IMccMcpCapabilities
|
|||
entry.typeLabel,
|
||||
entry.blockId,
|
||||
entry.blockMeta,
|
||||
entry.stateId,
|
||||
entry.properties,
|
||||
entry.distance
|
||||
})
|
||||
.ToArray()
|
||||
|
|
@ -1853,7 +1859,9 @@ public sealed class MccMcpCapabilities : IMccMcpCapabilities
|
|||
z,
|
||||
material = block.Type.ToString(),
|
||||
blockId = block.BlockId,
|
||||
blockMeta = block.BlockMeta
|
||||
blockMeta = block.BlockMeta,
|
||||
stateId = block.StateId,
|
||||
properties = block.GetStateProperties()
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
@ -3121,7 +3129,9 @@ public sealed class MccMcpCapabilities : IMccMcpCapabilities
|
|||
material = block.Type.ToString(),
|
||||
typeLabel = block.GetTypeString(),
|
||||
blockId = block.BlockId,
|
||||
blockMeta = block.BlockMeta
|
||||
blockMeta = block.BlockMeta,
|
||||
stateId = block.StateId,
|
||||
properties = block.GetStateProperties()
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -26,6 +26,8 @@ public sealed class MccBlockStateSnapshot
|
|||
public required string TypeLabel { get; init; }
|
||||
public required int BlockId { get; init; }
|
||||
public required int BlockMeta { get; init; }
|
||||
public required int StateId { get; init; }
|
||||
public required IReadOnlyDictionary<string, string> Properties { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -143,7 +145,9 @@ public static class MccGameCommon
|
|||
Material = block.Type.ToString(),
|
||||
TypeLabel = block.GetTypeString(),
|
||||
BlockId = block.BlockId,
|
||||
BlockMeta = block.BlockMeta
|
||||
BlockMeta = block.BlockMeta,
|
||||
StateId = block.StateId,
|
||||
Properties = block.GetStateProperties()
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue