mirror of
https://github.com/Jacobwasbeast/LegacyWeaveLoader.git
synced 2026-06-27 06:45:42 +00:00
feat(modloader): add managed block callbacks and ruby block examples
This commit is contained in:
@@ -65,6 +65,7 @@ public class BlockProperties
|
||||
internal string? NameValue;
|
||||
internal int RequiredHarvestLevelValue = -1;
|
||||
internal ToolType RequiredToolValue = ToolType.None;
|
||||
internal bool AcceptsRedstonePowerValue;
|
||||
|
||||
public BlockProperties Material(MaterialType material) { MaterialValue = material; return this; }
|
||||
public BlockProperties Hardness(float hardness) { HardnessValue = hardness; return this; }
|
||||
@@ -82,4 +83,6 @@ public class BlockProperties
|
||||
public BlockProperties RequiredHarvestLevel(int level) { RequiredHarvestLevelValue = level; return this; }
|
||||
/// <summary>Tool type required to harvest this block (e.g. Pickaxe for stone-like blocks).</summary>
|
||||
public BlockProperties RequiredTool(ToolType tool) { RequiredToolValue = tool; return this; }
|
||||
/// <summary>Marks the block as one that can receive redstone power. Stored for future block callbacks.</summary>
|
||||
public BlockProperties AcceptsRedstonePower(bool accepts = true) { AcceptsRedstonePowerValue = accepts; return this; }
|
||||
}
|
||||
|
||||
@@ -20,6 +20,19 @@ public class RegisteredBlock
|
||||
}
|
||||
}
|
||||
|
||||
public class RegisteredSlabBlock : RegisteredBlock
|
||||
{
|
||||
public Identifier DoubleStringId { get; }
|
||||
public int DoubleNumericId { get; }
|
||||
|
||||
internal RegisteredSlabBlock(Identifier id, Identifier doubleId, int numericId, int doubleNumericId)
|
||||
: base(id, numericId)
|
||||
{
|
||||
DoubleStringId = doubleId;
|
||||
DoubleNumericId = doubleNumericId;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Block registration via the WeaveLoader registry.
|
||||
/// Accessed through <see cref="Registry.Block"/>.
|
||||
@@ -48,7 +61,8 @@ public static class BlockRegistry
|
||||
properties.LightBlockValue,
|
||||
properties.NameValue ?? "",
|
||||
properties.RequiredHarvestLevelValue,
|
||||
(int)properties.RequiredToolValue);
|
||||
(int)properties.RequiredToolValue,
|
||||
properties.AcceptsRedstonePowerValue ? 1 : 0);
|
||||
|
||||
if (numericId < 0)
|
||||
throw new InvalidOperationException($"Failed to register block '{id}'. No free IDs or invalid parameters.");
|
||||
@@ -67,6 +81,140 @@ public static class BlockRegistry
|
||||
return new RegisteredBlock(id, numericId);
|
||||
}
|
||||
|
||||
public static RegisteredBlock Register(Identifier id, Block managedBlock, BlockProperties properties)
|
||||
{
|
||||
if (managedBlock is SlabBlock)
|
||||
return RegisterSlab(id, properties);
|
||||
|
||||
if (managedBlock is FallingBlock)
|
||||
return RegisterFalling(id, managedBlock, properties);
|
||||
|
||||
int numericId = NativeInterop.native_register_managed_block(
|
||||
id.ToString(),
|
||||
(int)properties.MaterialValue,
|
||||
properties.HardnessValue,
|
||||
properties.ResistanceValue,
|
||||
(int)properties.SoundValue,
|
||||
properties.IconValue,
|
||||
properties.LightEmissionValue,
|
||||
properties.LightBlockValue,
|
||||
properties.NameValue ?? "",
|
||||
properties.RequiredHarvestLevelValue,
|
||||
(int)properties.RequiredToolValue,
|
||||
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);
|
||||
}
|
||||
|
||||
ManagedBlockDispatcher.RegisterBlock(id, numericId, managedBlock);
|
||||
|
||||
int dropNumericId = -1;
|
||||
if (managedBlock.DropAsBlockId is Identifier dropId)
|
||||
dropNumericId = IdHelper.GetBlockNumericId(dropId);
|
||||
|
||||
int cloneNumericId = -1;
|
||||
if (managedBlock.CloneAsBlockId is Identifier cloneId)
|
||||
cloneNumericId = IdHelper.GetBlockNumericId(cloneId);
|
||||
NativeInterop.native_configure_managed_block(numericId, dropNumericId, cloneNumericId);
|
||||
|
||||
lock (s_lock)
|
||||
{
|
||||
s_idByNumeric[numericId] = id;
|
||||
}
|
||||
|
||||
return new RegisteredBlock(id, numericId);
|
||||
}
|
||||
|
||||
public static RegisteredBlock RegisterFalling(Identifier id, BlockProperties properties)
|
||||
=> RegisterFalling(id, null, properties);
|
||||
|
||||
private static RegisteredBlock RegisterFalling(Identifier id, Block? managedBlock, BlockProperties properties)
|
||||
{
|
||||
int numericId = NativeInterop.native_register_falling_block(
|
||||
id.ToString(),
|
||||
(int)properties.MaterialValue,
|
||||
properties.HardnessValue,
|
||||
properties.ResistanceValue,
|
||||
(int)properties.SoundValue,
|
||||
properties.IconValue,
|
||||
properties.LightEmissionValue,
|
||||
properties.LightBlockValue,
|
||||
properties.NameValue ?? "",
|
||||
properties.RequiredHarvestLevelValue,
|
||||
(int)properties.RequiredToolValue,
|
||||
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);
|
||||
}
|
||||
|
||||
if (managedBlock != null)
|
||||
{
|
||||
ManagedBlockDispatcher.RegisterBlock(id, numericId, managedBlock);
|
||||
|
||||
int dropNumericId = -1;
|
||||
if (managedBlock.DropAsBlockId is Identifier dropId)
|
||||
dropNumericId = IdHelper.GetBlockNumericId(dropId);
|
||||
|
||||
int cloneNumericId = -1;
|
||||
if (managedBlock.CloneAsBlockId is Identifier cloneId)
|
||||
cloneNumericId = IdHelper.GetBlockNumericId(cloneId);
|
||||
|
||||
NativeInterop.native_configure_managed_block(numericId, dropNumericId, cloneNumericId);
|
||||
}
|
||||
|
||||
lock (s_lock)
|
||||
{
|
||||
s_idByNumeric[numericId] = id;
|
||||
}
|
||||
return new RegisteredBlock(id, numericId);
|
||||
}
|
||||
|
||||
public static RegisteredSlabBlock RegisterSlab(Identifier id, BlockProperties properties)
|
||||
{
|
||||
Identifier doubleId = new($"{id}_double");
|
||||
int numericId = NativeInterop.native_register_slab_block(
|
||||
id.ToString(),
|
||||
(int)properties.MaterialValue,
|
||||
properties.HardnessValue,
|
||||
properties.ResistanceValue,
|
||||
(int)properties.SoundValue,
|
||||
properties.IconValue,
|
||||
properties.LightEmissionValue,
|
||||
properties.LightBlockValue,
|
||||
properties.NameValue ?? "",
|
||||
properties.RequiredHarvestLevelValue,
|
||||
(int)properties.RequiredToolValue,
|
||||
properties.AcceptsRedstonePowerValue ? 1 : 0,
|
||||
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);
|
||||
}
|
||||
|
||||
lock (s_lock)
|
||||
{
|
||||
s_idByNumeric[numericId] = id;
|
||||
s_idByNumeric[doubleNumericId] = doubleId;
|
||||
}
|
||||
|
||||
return new RegisteredSlabBlock(id, doubleId, numericId, doubleNumericId);
|
||||
}
|
||||
internal static bool TryGetIdentifier(int numericId, out Identifier id)
|
||||
{
|
||||
lock (s_lock)
|
||||
|
||||
226
WeaveLoader.API/Block/CustomBlock.cs
Normal file
226
WeaveLoader.API/Block/CustomBlock.cs
Normal file
@@ -0,0 +1,226 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace WeaveLoader.API.Block;
|
||||
|
||||
public abstract class Block
|
||||
{
|
||||
public Identifier? Id { get; internal set; }
|
||||
public int NumericId { get; internal set; } = -1;
|
||||
|
||||
public Identifier? DropAsBlockId { get; init; }
|
||||
public Identifier? CloneAsBlockId { get; init; }
|
||||
|
||||
public virtual void OnPlace(BlockUpdateContext context)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void OnNeighborChanged(BlockNeighborChangedContext context)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void OnScheduledTick(BlockTickContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class FallingBlock : Block
|
||||
{
|
||||
}
|
||||
|
||||
public class SlabBlock : Block
|
||||
{
|
||||
}
|
||||
|
||||
public readonly struct BlockUpdateContext
|
||||
{
|
||||
public int BlockId { get; }
|
||||
public bool IsClientSide { get; }
|
||||
public nint NativeLevelPtr { get; }
|
||||
public int X { get; }
|
||||
public int Y { get; }
|
||||
public int Z { get; }
|
||||
|
||||
internal BlockUpdateContext(int blockId, bool isClientSide, nint nativeLevelPtr, int x, int y, int z)
|
||||
{
|
||||
BlockId = blockId;
|
||||
IsClientSide = isClientSide;
|
||||
NativeLevelPtr = nativeLevelPtr;
|
||||
X = x;
|
||||
Y = y;
|
||||
Z = z;
|
||||
}
|
||||
|
||||
public bool HasNeighborSignal()
|
||||
{
|
||||
if (NativeLevelPtr == 0)
|
||||
return false;
|
||||
|
||||
return NativeInterop.native_level_has_neighbor_signal(NativeLevelPtr, X, Y, Z) != 0;
|
||||
}
|
||||
|
||||
public bool SetBlock(Identifier id, int data = 0, int flags = 2)
|
||||
{
|
||||
int numericId = IdHelper.GetBlockNumericId(id);
|
||||
if (numericId < 0)
|
||||
return false;
|
||||
|
||||
return SetBlock(numericId, data, flags);
|
||||
}
|
||||
|
||||
public bool SetBlock(int numericBlockId, int data = 0, int flags = 2)
|
||||
{
|
||||
if (NativeLevelPtr == 0 || numericBlockId < 0)
|
||||
return false;
|
||||
|
||||
return NativeInterop.native_level_set_tile(NativeLevelPtr, X, Y, Z, numericBlockId, data, flags) != 0;
|
||||
}
|
||||
|
||||
public bool ScheduleTick(int delay)
|
||||
{
|
||||
if (NativeLevelPtr == 0 || delay < 0)
|
||||
return false;
|
||||
|
||||
return NativeInterop.native_level_schedule_tick(NativeLevelPtr, X, Y, Z, BlockId, delay) != 0;
|
||||
}
|
||||
|
||||
public int GetBlockId(int offsetX = 0, int offsetY = 0, int offsetZ = 0)
|
||||
{
|
||||
if (NativeLevelPtr == 0)
|
||||
return -1;
|
||||
|
||||
return NativeInterop.native_level_get_tile(NativeLevelPtr, X + offsetX, Y + offsetY, Z + offsetZ);
|
||||
}
|
||||
}
|
||||
|
||||
public readonly struct BlockNeighborChangedContext
|
||||
{
|
||||
public BlockUpdateContext Block { get; }
|
||||
public int NeighborBlockId { get; }
|
||||
|
||||
internal BlockNeighborChangedContext(BlockUpdateContext block, int neighborBlockId)
|
||||
{
|
||||
Block = block;
|
||||
NeighborBlockId = neighborBlockId;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly struct BlockTickContext
|
||||
{
|
||||
public BlockUpdateContext Block { get; }
|
||||
|
||||
internal BlockTickContext(BlockUpdateContext block)
|
||||
{
|
||||
Block = block;
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct BlockUpdateNativeArgs
|
||||
{
|
||||
public int BlockId;
|
||||
public int IsClientSide;
|
||||
public nint LevelPtr;
|
||||
public int X;
|
||||
public int Y;
|
||||
public int Z;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct BlockNeighborChangedNativeArgs
|
||||
{
|
||||
public BlockUpdateNativeArgs Block;
|
||||
public int NeighborBlockId;
|
||||
}
|
||||
|
||||
internal static class ManagedBlockDispatcher
|
||||
{
|
||||
private static readonly object s_lock = new();
|
||||
private static readonly Dictionary<int, Block> s_blocks = new();
|
||||
|
||||
internal static void RegisterBlock(Identifier id, int numericId, Block block)
|
||||
{
|
||||
block.Id = id;
|
||||
block.NumericId = numericId;
|
||||
|
||||
lock (s_lock)
|
||||
{
|
||||
s_blocks[numericId] = block;
|
||||
}
|
||||
}
|
||||
|
||||
internal static int HandlePlace(IntPtr args, int sizeBytes)
|
||||
{
|
||||
if (args == IntPtr.Zero || sizeBytes < Marshal.SizeOf<BlockUpdateNativeArgs>())
|
||||
return 0;
|
||||
|
||||
BlockUpdateNativeArgs nativeArgs = Marshal.PtrToStructure<BlockUpdateNativeArgs>(args);
|
||||
Block? block;
|
||||
lock (s_lock)
|
||||
{
|
||||
s_blocks.TryGetValue(nativeArgs.BlockId, out block);
|
||||
}
|
||||
|
||||
if (block == null)
|
||||
return 0;
|
||||
|
||||
block.OnPlace(new BlockUpdateContext(
|
||||
nativeArgs.BlockId,
|
||||
nativeArgs.IsClientSide != 0,
|
||||
nativeArgs.LevelPtr,
|
||||
nativeArgs.X,
|
||||
nativeArgs.Y,
|
||||
nativeArgs.Z));
|
||||
return 1;
|
||||
}
|
||||
|
||||
internal static int HandleNeighborChanged(IntPtr args, int sizeBytes)
|
||||
{
|
||||
if (args == IntPtr.Zero || sizeBytes < Marshal.SizeOf<BlockNeighborChangedNativeArgs>())
|
||||
return 0;
|
||||
|
||||
BlockNeighborChangedNativeArgs nativeArgs = Marshal.PtrToStructure<BlockNeighborChangedNativeArgs>(args);
|
||||
Block? block;
|
||||
lock (s_lock)
|
||||
{
|
||||
s_blocks.TryGetValue(nativeArgs.Block.BlockId, out block);
|
||||
}
|
||||
|
||||
if (block == null)
|
||||
return 0;
|
||||
|
||||
var update = new BlockUpdateContext(
|
||||
nativeArgs.Block.BlockId,
|
||||
nativeArgs.Block.IsClientSide != 0,
|
||||
nativeArgs.Block.LevelPtr,
|
||||
nativeArgs.Block.X,
|
||||
nativeArgs.Block.Y,
|
||||
nativeArgs.Block.Z);
|
||||
block.OnNeighborChanged(new BlockNeighborChangedContext(update, nativeArgs.NeighborBlockId));
|
||||
return 1;
|
||||
}
|
||||
|
||||
internal static int HandleScheduledTick(IntPtr args, int sizeBytes)
|
||||
{
|
||||
if (args == IntPtr.Zero || sizeBytes < Marshal.SizeOf<BlockUpdateNativeArgs>())
|
||||
return 0;
|
||||
|
||||
BlockUpdateNativeArgs nativeArgs = Marshal.PtrToStructure<BlockUpdateNativeArgs>(args);
|
||||
Block? block;
|
||||
lock (s_lock)
|
||||
{
|
||||
s_blocks.TryGetValue(nativeArgs.BlockId, out block);
|
||||
}
|
||||
|
||||
if (block == null)
|
||||
return 0;
|
||||
|
||||
block.OnScheduledTick(new BlockTickContext(new BlockUpdateContext(
|
||||
nativeArgs.BlockId,
|
||||
nativeArgs.IsClientSide != 0,
|
||||
nativeArgs.LevelPtr,
|
||||
nativeArgs.X,
|
||||
nativeArgs.Y,
|
||||
nativeArgs.Z)));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user