refactor(creative): prepend support and logging

This commit is contained in:
Jacobwasbeast
2026-03-10 19:44:17 -05:00
parent 15c02e14ae
commit 86cbf225dc
12 changed files with 338 additions and 59 deletions

View File

@@ -64,6 +64,7 @@ public class BlockProperties
internal float LightEmissionValue = 0.0f;
internal int LightBlockValue = 255;
internal CreativeTab CreativeTabValue = CreativeTab.None;
internal CreativePlacement? CreativePlacementValue;
internal Text? NameValue;
internal int RequiredHarvestLevelValue = -1;
internal ToolType RequiredToolValue = ToolType.None;
@@ -82,6 +83,8 @@ public class BlockProperties
public BlockProperties LightBlocking(int level) { LightBlockValue = level; return this; }
public BlockProperties Indestructible() { HardnessValue = -1.0f; ResistanceValue = 6000000f; return this; }
public BlockProperties InCreativeTab(CreativeTab tab) { CreativeTabValue = tab; return this; }
public BlockProperties CreativePlacement(CreativePlacement placement) { CreativePlacementValue = placement; return this; }
public BlockProperties Prepend() { CreativePlacementValue = global::WeaveLoader.API.CreativePlacement.Prepend(); return this; }
/// <summary>Display name shown in-game (e.g. "Ruby Ore"). Used for localization.</summary>
public BlockProperties Name(string displayName) { NameValue = Text.Literal(displayName); return this; }
/// <summary>Localized display name using a language key (e.g. "block.examplemod.ruby_ore").</summary>

View File

@@ -65,14 +65,13 @@ public static class BlockRegistry
properties.AcceptsRedstonePowerValue ? 1 : 0);
if (numericId < 0)
throw new InvalidOperationException($"Failed to register block '{id}'. No free IDs or invalid parameters.");
if (properties.CreativeTabValue != CreativeTab.None)
{
NativeInterop.native_add_to_creative(numericId, 1, 0, (int)properties.CreativeTabValue);
Logger.Debug($"Block '{id}' added to creative tab {properties.CreativeTabValue}");
Logger.Error($"Failed to register block '{id}'. No free IDs or invalid parameters.");
throw new InvalidOperationException($"Failed to register block '{id}'. No free IDs or invalid parameters.");
}
AddToCreative(id, numericId, properties);
Logger.Debug($"Registered block '{id}' -> numeric ID {numericId}");
lock (s_lock)
{
@@ -104,13 +103,13 @@ public static class BlockRegistry
properties.AcceptsRedstonePowerValue ? 1 : 0);
if (numericId < 0)
throw new InvalidOperationException($"Failed to register managed block '{id}'.");
if (properties.CreativeTabValue != CreativeTab.None)
{
NativeInterop.native_add_to_creative(numericId, 1, 0, (int)properties.CreativeTabValue);
Logger.Error($"Failed to register managed block '{id}'.");
throw new InvalidOperationException($"Failed to register managed block '{id}'.");
}
AddToCreative(id, numericId, properties);
ManagedBlockDispatcher.RegisterBlock(id, numericId, managedBlock);
int dropNumericId = -1;
@@ -150,13 +149,13 @@ public static class BlockRegistry
properties.AcceptsRedstonePowerValue ? 1 : 0);
if (numericId < 0)
throw new InvalidOperationException($"Failed to register falling block '{id}'.");
if (properties.CreativeTabValue != CreativeTab.None)
{
NativeInterop.native_add_to_creative(numericId, 1, 0, (int)properties.CreativeTabValue);
Logger.Error($"Failed to register falling block '{id}'.");
throw new InvalidOperationException($"Failed to register falling block '{id}'.");
}
AddToCreative(id, numericId, properties);
if (managedBlock != null)
{
ManagedBlockDispatcher.RegisterBlock(id, numericId, managedBlock);
@@ -198,14 +197,17 @@ public static class BlockRegistry
out int doubleNumericId);
if (numericId < 0)
throw new InvalidOperationException($"Failed to register slab block '{id}'.");
if (doubleNumericId < 0)
throw new InvalidOperationException($"Failed to resolve generated slab pair '{doubleId}'.");
if (properties.CreativeTabValue != CreativeTab.None)
{
NativeInterop.native_add_to_creative(numericId, 1, 0, (int)properties.CreativeTabValue);
Logger.Error($"Failed to register slab block '{id}'.");
throw new InvalidOperationException($"Failed to register slab block '{id}'.");
}
if (doubleNumericId < 0)
{
Logger.Error($"Failed to resolve generated slab pair '{doubleId}'.");
throw new InvalidOperationException($"Failed to resolve generated slab pair '{doubleId}'.");
}
AddToCreative(id, numericId, properties);
lock (s_lock)
{
@@ -222,4 +224,59 @@ public static class BlockRegistry
return s_idByNumeric.TryGetValue(numericId, out id);
}
}
private static void AddToCreative(Identifier id, int numericId, BlockProperties properties)
{
if (properties.CreativeTabValue == CreativeTab.None)
{
Logger.Debug($"Block '{id}' not added to creative (CreativeTab.None)");
return;
}
bool added = false;
if (properties.CreativePlacementValue.HasValue)
{
CreativePlacement placement = properties.CreativePlacementValue.Value;
if (placement.Insert == CreativeInsert.Prepend)
{
try
{
NativeInterop.native_add_to_creative_ex(
numericId, 1, 0, (int)properties.CreativeTabValue,
(int)placement.Insert, -1, -1);
}
catch (DllNotFoundException e)
{
Logger.Error($"Creative add failed for block '{id}': {e.Message}. Check that WeaveLoaderRuntime is present.");
throw;
}
catch (EntryPointNotFoundException e)
{
Logger.Error($"Creative add failed for block '{id}': {e.Message}. API/runtime mismatch.");
throw;
}
added = true;
}
}
if (!added)
{
try
{
NativeInterop.native_add_to_creative(numericId, 1, 0, (int)properties.CreativeTabValue);
}
catch (DllNotFoundException e)
{
Logger.Error($"Creative add failed for block '{id}': {e.Message}. Check that WeaveLoaderRuntime is present.");
throw;
}
catch (EntryPointNotFoundException e)
{
Logger.Error($"Creative add failed for block '{id}': {e.Message}. API/runtime mismatch.");
throw;
}
}
Logger.Debug($"Block '{id}' added to creative tab {properties.CreativeTabValue}");
}
}