2026-03-22 15:24:33 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using MinecraftClient.Mapping;
|
|
|
|
|
using MinecraftClient.Mapping.BlockPalettes;
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient.Physics
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Registry of block collision shapes. Maps block state IDs to collision AABBs.
|
|
|
|
|
/// Data sourced from PrismarineJS/minecraft-data blockCollisionShapes.json.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class BlockShapes
|
|
|
|
|
{
|
|
|
|
|
private static readonly Aabb FullBlock = new(0, 0, 0, 1, 1, 1);
|
|
|
|
|
private static readonly Aabb[] FullBlockArray = { FullBlock };
|
|
|
|
|
private static readonly Aabb[] EmptyArray = Array.Empty<Aabb>();
|
|
|
|
|
|
|
|
|
|
private static Dictionary<int, Aabb[]>? stateToShape;
|
|
|
|
|
private static Dictionary<string, object>? prismarineBlocks;
|
|
|
|
|
private static Dictionary<int, Aabb[]>? prismarineShapes;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initialize the shape registry from embedded data + current palette.
|
|
|
|
|
/// Call once after the block palette is set.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void Initialize()
|
|
|
|
|
{
|
|
|
|
|
LoadPrismarineData();
|
|
|
|
|
BuildStateMap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get collision shapes for a block state ID.
|
|
|
|
|
/// Returns empty array for air/passable blocks, single full-block for solid cubes, etc.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static Aabb[] GetShapes(int blockStateId)
|
|
|
|
|
{
|
refactor: convert == null / != null to is null / is not null in 31 files
Replace old-style null comparisons with modern C# pattern matching
syntax across Commands, Protocol, Mapping, ChatBots, Physics,
Inventory, Logger, CommandHandler, Scripting, Crypto, and other modules.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-24 00:42:59 +00:00
|
|
|
if (stateToShape is not null && stateToShape.TryGetValue(blockStateId, out var shapes))
|
2026-03-22 15:24:33 +08:00
|
|
|
return shapes;
|
|
|
|
|
return FallbackShape(blockStateId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get collision shapes for a Block at a specific position (state-aware)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static Aabb[] GetShapes(Block block) => GetShapes(block.BlockId);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Check if a block state is effectively empty (no collision)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool IsEmpty(int blockStateId)
|
|
|
|
|
{
|
|
|
|
|
var shapes = GetShapes(blockStateId);
|
|
|
|
|
return shapes.Length == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Aabb[] FallbackShape(int blockStateId)
|
|
|
|
|
{
|
|
|
|
|
Material mat = Block.Palette.FromId(blockStateId);
|
|
|
|
|
if (mat == Material.Air) return EmptyArray;
|
|
|
|
|
if (mat.IsLiquid()) return EmptyArray;
|
|
|
|
|
if (mat.IsSolid()) return FullBlockArray;
|
|
|
|
|
return EmptyArray;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void LoadPrismarineData()
|
|
|
|
|
{
|
|
|
|
|
prismarineBlocks = new Dictionary<string, object>();
|
|
|
|
|
prismarineShapes = new Dictionary<int, Aabb[]>();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var assembly = Assembly.GetExecutingAssembly();
|
|
|
|
|
using var stream = assembly.GetManifestResourceStream("BlockShapeData.json");
|
refactor: convert == null / != null to is null / is not null in 31 files
Replace old-style null comparisons with modern C# pattern matching
syntax across Commands, Protocol, Mapping, ChatBots, Physics,
Inventory, Logger, CommandHandler, Scripting, Crypto, and other modules.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-24 00:42:59 +00:00
|
|
|
if (stream is null)
|
2026-03-22 15:24:33 +08:00
|
|
|
{
|
TUI for MCC (#2976)
* feat: implement interactive TUI inventory viewer
- Added InventoryTui command to open an interactive terminal user interface for inventory management.
- Introduced InventoryApp and InventoryMainView classes for TUI layout and functionality.
- Created InventoryViewModel and SlotViewModel to manage inventory data and display.
- Integrated Consolonia for enhanced console UI experience.
- Updated McClient to support console message handling during TUI operation.
These changes enhance user interaction with inventory management in Minecraft Console Client.
* feat: enhance debugging workflow with mcc-debug.sh and TUI support
- Introduced mcc-debug.sh for streamlined one-step build, server start, and MCC launch.
- Added TUI mode for improved user experience during debugging sessions.
- Updated mcc-env.sh to include new debug helpers and TUI mode functionality.
- Enhanced SKILL.md with detailed instructions for using the new debugging tools and console modes.
These changes significantly improve the debugging process for Minecraft Console Client, making it more efficient and user-friendly.
* feat: introduce TUI console backend and related enhancements
- Added ClassicConsoleBackend and TuiConsoleBackend to support different console I/O modes.
- Implemented IConsoleBackend interface for better abstraction of console operations.
- Enhanced ConsoleIO to utilize the new backend structure for input and output handling.
- Introduced MainTuiView and MccTuiApp for a full-screen TUI experience using Avalonia.
- Updated InventoryTuiHost to manage TUI lifecycle and interactions.
These changes significantly improve the console experience in Minecraft Console Client, providing a more flexible and user-friendly interface.
* refactor: remove InventoryTui command implementation
- Deleted the InventoryTui class, which provided an interactive terminal user interface for inventory management.
- This change simplifies the command structure as part of ongoing improvements to the console experience in Minecraft Console Client.
The removal of this command is aligned with recent enhancements to the console backend and user interface.
* refactor: update InventoryMainView and MainTuiView for improved UI handling
- Changed several fields from readonly to mutable in InventoryMainView to allow for dynamic updates.
- Introduced a new McColorParser class for parsing Minecraft color codes and creating colored text blocks.
- Enhanced MainTuiView to support formatted log lines and improved notification handling.
- Updated chat scrolling behavior to ensure better user experience during text input and log display.
These changes streamline the UI components and enhance the overall console experience in Minecraft Console Client.
* feat: enhance command input handling in MainTuiView
- Updated command input to use event routing for better key handling.
- Added support for Ctrl key shortcuts to improve text manipulation (e.g., word deletion, caret movement).
- Implemented text cleaning on input change to prevent newline characters.
- Improved health and food status bar rendering with a new method for building bar text.
These changes significantly enhance the user experience in the TUI by providing more intuitive command input functionality.
* feat: enhance TUI command suggestion functionality
- Introduced a new CommandSuggestion struct for backend-independent suggestion handling.
- Updated ConsoleIO to streamline suggestion updates for both Classic and TUI backends.
- Enhanced MainTuiView to display command suggestions with improved visibility and interaction.
- Increased the maximum number of displayed suggestions from 6 to 10 for better user experience.
These changes significantly improve the command input experience in the TUI, making it more intuitive and user-friendly.
* feat: enhance offline command autocomplete functionality
- Introduced an OfflineAutocompleteHandler to provide command suggestions for offline commands.
- Added support for tab cycling through suggestions in the TUI.
- Improved command input handling to clear suggestions when necessary and manage user input more effectively.
- Updated TuiConsoleBackend to integrate with the new autocomplete feature.
These changes significantly enhance the user experience by making command input more intuitive and responsive in offline scenarios.
* feat: enhance localization and user feedback in TUI
- Updated various TUI components to utilize localized strings for improved user experience.
- Enhanced debug state output with translated labels for better clarity.
- Improved inventory command help messages with localized text.
- Added new translations for console mode descriptions and inventory viewer prompts.
These changes significantly enhance the usability and accessibility of the TUI in Minecraft Console Client, making it more user-friendly and informative.
* feat: enforce localization for user-facing strings in AGENTS.md
- Added guidelines to ensure all user-facing text, including log messages and error messages, is managed through the translation system.
- Specified the use of `Translations.resx` and `Translations.Designer.cs` for localization, emphasizing the importance of avoiding hardcoded strings in source code.
These changes improve the consistency and accessibility of user-facing content across the application.
2026-03-26 02:08:18 +08:00
|
|
|
ConsoleIO.WriteLineFormatted("§e[Physics] BlockShapeData.json not found as embedded resource");
|
2026-03-22 15:24:33 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
using var doc = JsonDocument.Parse(stream);
|
|
|
|
|
var root = doc.RootElement;
|
|
|
|
|
|
|
|
|
|
// Parse shapes: shapeId -> list of AABB boxes
|
|
|
|
|
if (root.TryGetProperty("shapes", out var shapesEl))
|
|
|
|
|
{
|
|
|
|
|
foreach (var prop in shapesEl.EnumerateObject())
|
|
|
|
|
{
|
|
|
|
|
if (int.TryParse(prop.Name, out int shapeId))
|
|
|
|
|
{
|
|
|
|
|
var boxes = new List<Aabb>();
|
|
|
|
|
foreach (var boxEl in prop.Value.EnumerateArray())
|
|
|
|
|
{
|
|
|
|
|
var coords = new double[6];
|
|
|
|
|
int idx = 0;
|
|
|
|
|
foreach (var c in boxEl.EnumerateArray())
|
|
|
|
|
{
|
|
|
|
|
if (idx < 6) coords[idx++] = c.GetDouble();
|
|
|
|
|
}
|
|
|
|
|
if (idx == 6)
|
|
|
|
|
boxes.Add(new Aabb(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]));
|
|
|
|
|
}
|
|
|
|
|
prismarineShapes[shapeId] = boxes.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse blocks: blockName -> shapeId (int) or list of shapeIds
|
|
|
|
|
if (root.TryGetProperty("blocks", out var blocksEl))
|
|
|
|
|
{
|
|
|
|
|
foreach (var prop in blocksEl.EnumerateObject())
|
|
|
|
|
{
|
|
|
|
|
string blockName = prop.Name;
|
|
|
|
|
if (prop.Value.ValueKind == JsonValueKind.Number)
|
|
|
|
|
{
|
|
|
|
|
prismarineBlocks[blockName] = prop.Value.GetInt32();
|
|
|
|
|
}
|
|
|
|
|
else if (prop.Value.ValueKind == JsonValueKind.Array)
|
|
|
|
|
{
|
|
|
|
|
var ids = new List<int>();
|
|
|
|
|
foreach (var el in prop.Value.EnumerateArray())
|
|
|
|
|
ids.Add(el.GetInt32());
|
|
|
|
|
prismarineBlocks[blockName] = ids;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
TUI for MCC (#2976)
* feat: implement interactive TUI inventory viewer
- Added InventoryTui command to open an interactive terminal user interface for inventory management.
- Introduced InventoryApp and InventoryMainView classes for TUI layout and functionality.
- Created InventoryViewModel and SlotViewModel to manage inventory data and display.
- Integrated Consolonia for enhanced console UI experience.
- Updated McClient to support console message handling during TUI operation.
These changes enhance user interaction with inventory management in Minecraft Console Client.
* feat: enhance debugging workflow with mcc-debug.sh and TUI support
- Introduced mcc-debug.sh for streamlined one-step build, server start, and MCC launch.
- Added TUI mode for improved user experience during debugging sessions.
- Updated mcc-env.sh to include new debug helpers and TUI mode functionality.
- Enhanced SKILL.md with detailed instructions for using the new debugging tools and console modes.
These changes significantly improve the debugging process for Minecraft Console Client, making it more efficient and user-friendly.
* feat: introduce TUI console backend and related enhancements
- Added ClassicConsoleBackend and TuiConsoleBackend to support different console I/O modes.
- Implemented IConsoleBackend interface for better abstraction of console operations.
- Enhanced ConsoleIO to utilize the new backend structure for input and output handling.
- Introduced MainTuiView and MccTuiApp for a full-screen TUI experience using Avalonia.
- Updated InventoryTuiHost to manage TUI lifecycle and interactions.
These changes significantly improve the console experience in Minecraft Console Client, providing a more flexible and user-friendly interface.
* refactor: remove InventoryTui command implementation
- Deleted the InventoryTui class, which provided an interactive terminal user interface for inventory management.
- This change simplifies the command structure as part of ongoing improvements to the console experience in Minecraft Console Client.
The removal of this command is aligned with recent enhancements to the console backend and user interface.
* refactor: update InventoryMainView and MainTuiView for improved UI handling
- Changed several fields from readonly to mutable in InventoryMainView to allow for dynamic updates.
- Introduced a new McColorParser class for parsing Minecraft color codes and creating colored text blocks.
- Enhanced MainTuiView to support formatted log lines and improved notification handling.
- Updated chat scrolling behavior to ensure better user experience during text input and log display.
These changes streamline the UI components and enhance the overall console experience in Minecraft Console Client.
* feat: enhance command input handling in MainTuiView
- Updated command input to use event routing for better key handling.
- Added support for Ctrl key shortcuts to improve text manipulation (e.g., word deletion, caret movement).
- Implemented text cleaning on input change to prevent newline characters.
- Improved health and food status bar rendering with a new method for building bar text.
These changes significantly enhance the user experience in the TUI by providing more intuitive command input functionality.
* feat: enhance TUI command suggestion functionality
- Introduced a new CommandSuggestion struct for backend-independent suggestion handling.
- Updated ConsoleIO to streamline suggestion updates for both Classic and TUI backends.
- Enhanced MainTuiView to display command suggestions with improved visibility and interaction.
- Increased the maximum number of displayed suggestions from 6 to 10 for better user experience.
These changes significantly improve the command input experience in the TUI, making it more intuitive and user-friendly.
* feat: enhance offline command autocomplete functionality
- Introduced an OfflineAutocompleteHandler to provide command suggestions for offline commands.
- Added support for tab cycling through suggestions in the TUI.
- Improved command input handling to clear suggestions when necessary and manage user input more effectively.
- Updated TuiConsoleBackend to integrate with the new autocomplete feature.
These changes significantly enhance the user experience by making command input more intuitive and responsive in offline scenarios.
* feat: enhance localization and user feedback in TUI
- Updated various TUI components to utilize localized strings for improved user experience.
- Enhanced debug state output with translated labels for better clarity.
- Improved inventory command help messages with localized text.
- Added new translations for console mode descriptions and inventory viewer prompts.
These changes significantly enhance the usability and accessibility of the TUI in Minecraft Console Client, making it more user-friendly and informative.
* feat: enforce localization for user-facing strings in AGENTS.md
- Added guidelines to ensure all user-facing text, including log messages and error messages, is managed through the translation system.
- Specified the use of `Translations.resx` and `Translations.Designer.cs` for localization, emphasizing the importance of avoiding hardcoded strings in source code.
These changes improve the consistency and accessibility of user-facing content across the application.
2026-03-26 02:08:18 +08:00
|
|
|
ConsoleIO.WriteLineFormatted($"§e[Physics] Failed to load BlockShapeData.json: {ex.Message}");
|
2026-03-22 15:24:33 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void BuildStateMap()
|
|
|
|
|
{
|
|
|
|
|
stateToShape = new Dictionary<int, Aabb[]>();
|
|
|
|
|
|
refactor: convert == null / != null to is null / is not null in 31 files
Replace old-style null comparisons with modern C# pattern matching
syntax across Commands, Protocol, Mapping, ChatBots, Physics,
Inventory, Logger, CommandHandler, Scripting, Crypto, and other modules.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-24 00:42:59 +00:00
|
|
|
if (prismarineBlocks is null || prismarineShapes is null)
|
2026-03-22 15:24:33 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var palette = Block.Palette;
|
|
|
|
|
var dict = GetPaletteDict(palette);
|
refactor: convert == null / != null to is null / is not null in 31 files
Replace old-style null comparisons with modern C# pattern matching
syntax across Commands, Protocol, Mapping, ChatBots, Physics,
Inventory, Logger, CommandHandler, Scripting, Crypto, and other modules.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-24 00:42:59 +00:00
|
|
|
if (dict is null) return;
|
2026-03-22 15:24:33 +08:00
|
|
|
|
|
|
|
|
// Group consecutive state IDs by Material to find state ranges per block
|
|
|
|
|
var materialRanges = new Dictionary<Material, List<(int start, int end)>>();
|
|
|
|
|
int? rangeStart = null;
|
|
|
|
|
Material? currentMat = null;
|
|
|
|
|
|
|
|
|
|
foreach (var kvp in dict.OrderBy(k => k.Key))
|
|
|
|
|
{
|
|
|
|
|
if (currentMat == kvp.Value && rangeStart.HasValue && kvp.Key == (materialRanges[currentMat.Value].Last().end + 1))
|
|
|
|
|
{
|
|
|
|
|
var ranges = materialRanges[currentMat.Value];
|
|
|
|
|
ranges[ranges.Count - 1] = (ranges.Last().start, kvp.Key);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
currentMat = kvp.Value;
|
|
|
|
|
if (!materialRanges.ContainsKey(currentMat.Value))
|
|
|
|
|
materialRanges[currentMat.Value] = new List<(int, int)>();
|
|
|
|
|
materialRanges[currentMat.Value].Add((kvp.Key, kvp.Key));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Map each Material to PrismarineJS block name
|
|
|
|
|
foreach (var kvp in materialRanges)
|
|
|
|
|
{
|
|
|
|
|
string snakeName = MaterialToSnakeCase(kvp.Key);
|
|
|
|
|
if (!prismarineBlocks.TryGetValue(snakeName, out var blockShapeData))
|
|
|
|
|
continue;
|
|
|
|
|
|
2026-03-22 15:49:14 +08:00
|
|
|
int globalStateOffset = 0;
|
2026-03-22 15:24:33 +08:00
|
|
|
foreach (var (start, end) in kvp.Value)
|
|
|
|
|
{
|
|
|
|
|
int stateCount = end - start + 1;
|
|
|
|
|
|
|
|
|
|
if (blockShapeData is int singleShapeId)
|
|
|
|
|
{
|
|
|
|
|
var shapes = prismarineShapes.GetValueOrDefault(singleShapeId, EmptyArray);
|
|
|
|
|
for (int sid = start; sid <= end; sid++)
|
|
|
|
|
stateToShape[sid] = shapes;
|
|
|
|
|
}
|
|
|
|
|
else if (blockShapeData is List<int> shapeIdList)
|
|
|
|
|
{
|
2026-03-22 15:49:14 +08:00
|
|
|
for (int i = 0; i < stateCount && (globalStateOffset + i) < shapeIdList.Count; i++)
|
2026-03-22 15:24:33 +08:00
|
|
|
{
|
2026-03-22 15:49:14 +08:00
|
|
|
int shapeId = shapeIdList[globalStateOffset + i];
|
2026-03-22 15:24:33 +08:00
|
|
|
stateToShape[start + i] = prismarineShapes.GetValueOrDefault(shapeId, EmptyArray);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-22 15:49:14 +08:00
|
|
|
globalStateOffset += stateCount;
|
2026-03-22 15:24:33 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Convert Material enum name (PascalCase) to snake_case block name
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static string MaterialToSnakeCase(Material mat)
|
|
|
|
|
{
|
|
|
|
|
string name = mat.ToString();
|
|
|
|
|
var sb = new System.Text.StringBuilder(name.Length + 5);
|
|
|
|
|
for (int i = 0; i < name.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
char c = name[i];
|
|
|
|
|
if (char.IsUpper(c) && i > 0)
|
|
|
|
|
sb.Append('_');
|
|
|
|
|
sb.Append(char.ToLowerInvariant(c));
|
|
|
|
|
}
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Access the internal dictionary of a palette via reflection (all palettes store it the same way)
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static Dictionary<int, Material>? GetPaletteDict(BlockPalette palette)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var method = palette.GetType().GetMethod("GetDict",
|
|
|
|
|
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
|
|
|
|
|
return method?.Invoke(palette, null) as Dictionary<int, Material>;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|