Add inventory select block support
This commit is contained in:
parent
5654c041c5
commit
2149458d96
6 changed files with 130 additions and 2 deletions
|
@ -36,8 +36,6 @@ namespace GamecraftModdingAPI.Blocks
|
|||
|
||||
public bool IsInGame = false;
|
||||
|
||||
private System.Random random = new System.Random();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
IsInGame = false;
|
||||
|
|
|
@ -542,4 +542,7 @@
|
|||
</ItemGroup>
|
||||
<!--End Dependencies-->
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Inventory\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
38
GamecraftModdingAPI/Inventory/Hotbar.cs
Normal file
38
GamecraftModdingAPI/Inventory/Hotbar.cs
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
55
GamecraftModdingAPI/Inventory/HotbarEngine.cs
Normal file
55
GamecraftModdingAPI/Inventory/HotbarEngine.cs
Normal file
|
@ -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<InputEntityStruct>(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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue