diff --git a/GamecraftModdingAPI/Blocks/SignalEngine.cs b/GamecraftModdingAPI/Blocks/SignalEngine.cs index 2f1d413..d84eefe 100644 --- a/GamecraftModdingAPI/Blocks/SignalEngine.cs +++ b/GamecraftModdingAPI/Blocks/SignalEngine.cs @@ -36,8 +36,6 @@ namespace GamecraftModdingAPI.Blocks public bool IsInGame = false; - private System.Random random = new System.Random(); - public void Dispose() { IsInGame = false; diff --git a/GamecraftModdingAPI/GamecraftModdingAPI.csproj b/GamecraftModdingAPI/GamecraftModdingAPI.csproj index 6e9f5f3..d4f95bc 100644 --- a/GamecraftModdingAPI/GamecraftModdingAPI.csproj +++ b/GamecraftModdingAPI/GamecraftModdingAPI.csproj @@ -542,4 +542,7 @@ + + + diff --git a/GamecraftModdingAPI/Inventory/Hotbar.cs b/GamecraftModdingAPI/Inventory/Hotbar.cs new file mode 100644 index 0000000..a4fcf22 --- /dev/null +++ b/GamecraftModdingAPI/Inventory/Hotbar.cs @@ -0,0 +1,38 @@ +using System; + +using RobocraftX.Common.Input; +using RobocraftX.Multiplayer.Input; + +using GamecraftModdingAPI.Blocks; +using GamecraftModdingAPI.Utility; +using Harmony; + +namespace GamecraftModdingAPI.Inventory +{ + public static class Hotbar + { + private static HotbarEngine hotbarEngine = new HotbarEngine(); + + public static void EquipBlock(BlockIDs block, uint playerID = uint.MaxValue) + { + if (playerID == uint.MaxValue) + { + playerID = hotbarEngine.GetLocalPlayerID(); + } + hotbarEngine.SelectBlock((int) block, playerID); + // cubeSelectedByPick = true will crash the game + // (this would be equivalent to mouse middle click pick block action) + // reason: the game expects a Dictionary entry for the tweaked stats + } + + public static BlockIDs GetEquippedBlock() + { + return HotbarSlotSelectionHandlerEnginePatch.EquippedPartID; + } + + public static void Init() + { + GameEngineManager.AddGameEngine(hotbarEngine); + } + } +} diff --git a/GamecraftModdingAPI/Inventory/HotbarEngine.cs b/GamecraftModdingAPI/Inventory/HotbarEngine.cs new file mode 100644 index 0000000..065298b --- /dev/null +++ b/GamecraftModdingAPI/Inventory/HotbarEngine.cs @@ -0,0 +1,55 @@ +using System; + +using RobocraftX.Character; +using RobocraftX.GUI.Hotbar; +using RobocraftX.Players; +using RobocraftX.Common; +using RobocraftX.Common.Input; +using RobocraftX.Common.Players; +using Svelto.ECS; + +using GamecraftModdingAPI.Blocks; +using GamecraftModdingAPI.Utility; + +namespace GamecraftModdingAPI.Inventory +{ + public class HotbarEngine : IApiEngine + { + public string Name { get; } = "GamecraftModdingAPIHotbarGameEngine"; + + public EntitiesDB entitiesDB { set; private get; } + + public bool IsInGame = false; + + public void Dispose() + { + IsInGame = false; + } + + public void Ready() + { + IsInGame = true; + } + + public bool SelectBlock(int block, uint playerID, bool cubeSelectedByPick = false) + { + InputEntityStruct[] inputs = entitiesDB.QueryEntities(InputExclusiveGroups.LocalPlayers).ToFastAccess(out uint count); + if (count == 0) return false; + for (int i = 0; i < count; i++) + { + if (inputs[i].ID.entityID == playerID) { + inputs[i].cubeSelectedByPick = cubeSelectedByPick; + inputs[i].selectedCube = block; + return true; + } + } + // TODO: expose the rest of the input functionality + return false; + } + + public uint GetLocalPlayerID() + { + return LocalPlayerIDUtility.GetLocalPlayerID(entitiesDB); + } + } +} diff --git a/GamecraftModdingAPI/Inventory/HotbarSlotSelectionHandlerEnginePatch.cs b/GamecraftModdingAPI/Inventory/HotbarSlotSelectionHandlerEnginePatch.cs new file mode 100644 index 0000000..db50e4d --- /dev/null +++ b/GamecraftModdingAPI/Inventory/HotbarSlotSelectionHandlerEnginePatch.cs @@ -0,0 +1,32 @@ +using System; +using System.Reflection; + +using RobocraftX.GUI; +using RobocraftX.GUI.Hotbar; +using Svelto.ECS; + +using Harmony; +using GamecraftModdingAPI.Blocks; + +namespace GamecraftModdingAPI.Inventory +{ + [HarmonyPatch] + public class HotbarSlotSelectionHandlerEnginePatch + { + private static int selectedBlockInt = 0; + + public static BlockIDs EquippedPartID { get => (BlockIDs)selectedBlockInt; } + + private static MethodInfo PatchedMethod { get; } = AccessTools.Method(AccessTools.TypeByName("RobocraftX.GUI.Hotbar.HotbarSlotSelectionHandlerEngine"), "HandleEquippedCubeChanged", parameters: new Type[] { typeof(uint), typeof(int), typeof(ExclusiveGroupStruct) }); + + public static void Prefix(uint playerID, int selectedDBPartID, ExclusiveGroupStruct groupID) + { + selectedBlockInt = selectedDBPartID; + } + + public static MethodBase TargetMethod(HarmonyInstance instance) + { + return PatchedMethod; + } + } +} diff --git a/GamecraftModdingAPI/Main.cs b/GamecraftModdingAPI/Main.cs index 5294c75..70f59e8 100644 --- a/GamecraftModdingAPI/Main.cs +++ b/GamecraftModdingAPI/Main.cs @@ -65,6 +65,8 @@ namespace GamecraftModdingAPI Blocks.Signals.Init(); Blocks.Placement.Init(); Blocks.Tweakable.Init(); + // init inventory + Inventory.Hotbar.Init(); Logging.MetaLog($"{currentAssembly.GetName().Name} v{currentAssembly.GetName().Version} initialized"); }