mirror of
https://github.com/Jacobwasbeast/LegacyWeaveLoader.git
synced 2026-06-02 02:54:31 +00:00
refactor(creative): prepend support and logging
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -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}");
|
||||
}
|
||||
}
|
||||
|
||||
20
WeaveLoader.API/CreativePlacement.cs
Normal file
20
WeaveLoader.API/CreativePlacement.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
namespace WeaveLoader.API;
|
||||
|
||||
public enum CreativeInsert
|
||||
{
|
||||
Append = 0,
|
||||
Prepend = 1
|
||||
}
|
||||
|
||||
public readonly struct CreativePlacement
|
||||
{
|
||||
public CreativeInsert Insert { get; }
|
||||
|
||||
private CreativePlacement(CreativeInsert insert)
|
||||
{
|
||||
Insert = insert;
|
||||
}
|
||||
|
||||
public static CreativePlacement Append() => new(CreativeInsert.Append);
|
||||
public static CreativePlacement Prepend() => new(CreativeInsert.Prepend);
|
||||
}
|
||||
@@ -12,6 +12,7 @@ public class ItemProperties
|
||||
internal float AttackDamageValue = 0.0f;
|
||||
internal string IconValue = "";
|
||||
internal CreativeTab CreativeTabValue = CreativeTab.None;
|
||||
internal CreativePlacement? CreativePlacementValue;
|
||||
internal Text? NameValue;
|
||||
|
||||
public ItemProperties MaxStackSize(int size) { MaxStackSizeValue = size; return this; }
|
||||
@@ -29,6 +30,8 @@ public class ItemProperties
|
||||
/// <summary>Override the native attack damage value for tool items.</summary>
|
||||
public ItemProperties AttackDamage(float damage) { AttackDamageValue = damage; return this; }
|
||||
public ItemProperties InCreativeTab(CreativeTab tab) { CreativeTabValue = tab; return this; }
|
||||
public ItemProperties CreativePlacement(CreativePlacement placement) { CreativePlacementValue = placement; return this; }
|
||||
public ItemProperties Prepend() { CreativePlacementValue = global::WeaveLoader.API.CreativePlacement.Prepend(); return this; }
|
||||
/// <summary>Display name shown in-game (e.g. "Ruby"). Used for localization.</summary>
|
||||
public ItemProperties Name(string displayName) { NameValue = Text.Literal(displayName); return this; }
|
||||
/// <summary>Localized display name using a language key (e.g. "item.examplemod.ruby").</summary>
|
||||
|
||||
@@ -184,13 +184,63 @@ public static class ItemRegistry
|
||||
}
|
||||
|
||||
if (numericId < 0)
|
||||
{
|
||||
Logger.Error($"Failed to register item '{id}'. No free IDs or invalid parameters.");
|
||||
throw new InvalidOperationException($"Failed to register item '{id}'. No free IDs or invalid parameters.");
|
||||
}
|
||||
|
||||
if (properties.CreativeTabValue != CreativeTab.None)
|
||||
{
|
||||
NativeInterop.native_add_to_creative(numericId, 1, 0, (int)properties.CreativeTabValue);
|
||||
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 item '{id}': {e.Message}. Check that WeaveLoaderRuntime is present.");
|
||||
throw;
|
||||
}
|
||||
catch (EntryPointNotFoundException e)
|
||||
{
|
||||
Logger.Error($"Creative add failed for item '{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 item '{id}': {e.Message}. Check that WeaveLoaderRuntime is present.");
|
||||
throw;
|
||||
}
|
||||
catch (EntryPointNotFoundException e)
|
||||
{
|
||||
Logger.Error($"Creative add failed for item '{id}': {e.Message}. API/runtime mismatch.");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
Logger.Debug($"Item '{id}' added to creative tab {properties.CreativeTabValue}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Debug($"Item '{id}' not added to creative (CreativeTab.None)");
|
||||
}
|
||||
|
||||
if (managedItem != null)
|
||||
{
|
||||
|
||||
@@ -225,6 +225,9 @@ internal static class NativeInterop
|
||||
[DllImport(RuntimeDll, CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void native_add_to_creative(int numericId, int count, int auxValue, int groupIndex);
|
||||
|
||||
[DllImport(RuntimeDll, CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void native_add_to_creative_ex(int numericId, int count, int auxValue, int groupIndex, int insertMode, int anchorId, int anchorAux);
|
||||
|
||||
[DllImport(RuntimeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
|
||||
internal static extern nint native_find_symbol(string fullName);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user