mirror of
https://github.com/Jacobwasbeast/LegacyWeaveLoader.git
synced 2026-05-22 05:34:36 +00:00
feat(modloader): add item use and summon APIs
Add managed item use hooks, server-authoritative summon support, and side-aware use item context. Include IdHelper mappings, example ruby wand usage, and related runtime/crash-handler updates.
This commit is contained in:
@@ -12,6 +12,7 @@ public class ExampleMod : IMod
|
||||
public static RegisteredBlock? RubyOre;
|
||||
public static RegisteredItem? Ruby;
|
||||
public static RegisteredItem? RubyPickaxeItem;
|
||||
public static RegisteredItem? RubyWandItem;
|
||||
|
||||
private sealed class RubyPickaxe : PickaxeItem
|
||||
{
|
||||
@@ -22,6 +23,53 @@ public class ExampleMod : IMod
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class RubyWand : Item
|
||||
{
|
||||
private const long CooldownMs = 1500;
|
||||
private long _nextClientUseAtMs;
|
||||
private long _nextServerUseAtMs;
|
||||
|
||||
public override UseItemResult OnUseItem(UseItemContext context)
|
||||
{
|
||||
if (context.IsTestUseOnly)
|
||||
return UseItemResult.CancelVanilla;
|
||||
|
||||
long now = Environment.TickCount64;
|
||||
ref long nextUseAtMs = ref context.IsClientSide ? ref _nextClientUseAtMs : ref _nextServerUseAtMs;
|
||||
if (now < nextUseAtMs)
|
||||
{
|
||||
long remaining = nextUseAtMs - now;
|
||||
Logger.Info($"RubyWand is cooling down ({remaining}ms remaining)");
|
||||
return UseItemResult.CancelVanilla;
|
||||
}
|
||||
|
||||
if (!context.ConsumeInventoryItem("minecraft:gunpowder", 1))
|
||||
{
|
||||
Logger.Info("RubyWand needs gunpowder.");
|
||||
return UseItemResult.CancelVanilla;
|
||||
}
|
||||
|
||||
if (context.IsClientSide)
|
||||
{
|
||||
context.DamageItem(10);
|
||||
nextUseAtMs = now + CooldownMs;
|
||||
return UseItemResult.ContinueVanilla;
|
||||
}
|
||||
|
||||
bool spawned = context.SpawnEntityFromLook("minecraft:wither_skull", speed: 1.4, spawnForward: 1.0, spawnUp: 1.2);
|
||||
if (!spawned)
|
||||
{
|
||||
Logger.Info("RubyWand failed to spawn fireball.");
|
||||
return UseItemResult.CancelVanilla;
|
||||
}
|
||||
|
||||
context.DamageItem(10);
|
||||
Logger.Info($"RubyWand cast fireball! (item={context.ItemId})");
|
||||
nextUseAtMs = now + CooldownMs;
|
||||
return UseItemResult.CancelVanilla;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnInitialize()
|
||||
{
|
||||
RubyOre = Registry.Block.Register("examplemod:ruby_ore",
|
||||
@@ -49,6 +97,13 @@ public class ExampleMod : IMod
|
||||
.Name("Ruby Pickaxe")
|
||||
.InCreativeTab(CreativeTab.ToolsAndWeapons));
|
||||
|
||||
RubyWandItem = Registry.Item.Register("examplemod:ruby_wand", new RubyWand(),
|
||||
new ItemProperties()
|
||||
.MaxStackSize(1)
|
||||
.Icon("examplemod:ruby_wand") // From assets/items/ruby_wand.png
|
||||
.Name("Ruby Wand")
|
||||
.InCreativeTab(CreativeTab.ToolsAndWeapons));
|
||||
|
||||
Registry.Recipe.AddFurnace("examplemod:ruby_ore", "examplemod:ruby", 1.0f);
|
||||
|
||||
GameEvents.OnBlockBreak += OnBlockBroken;
|
||||
|
||||
BIN
ExampleMod/assets/items/ruby_wand.png
Normal file
BIN
ExampleMod/assets/items/ruby_wand.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.9 KiB |
Reference in New Issue
Block a user