From d00bdc80ed8f7197a606f98474a3cbd76b2e8543 Mon Sep 17 00:00:00 2001 From: NGnius Date: Thu, 2 Apr 2020 13:08:05 -0400 Subject: [PATCH] Improve type safety of event types and version bump --- .../DeterministicStepComposeEngineGroupsPatch.cs | 6 ------ GamecraftModdingAPI/Events/EventEngineFactory.cs | 6 +++--- .../Events/GameStateBuildEmitterEngine.cs | 2 +- .../Events/GameStateSimulationEmitterEngine.cs | 2 +- GamecraftModdingAPI/Events/IEventEmitterEngine.cs | 2 +- GamecraftModdingAPI/Events/ModEventEntityStruct.cs | 8 +++++--- .../Events/SimpleEventEmitterEngine.cs | 6 +++--- .../Events/SimpleEventHandlerEngine.cs | 12 +++++++++--- GamecraftModdingAPI/GamecraftModdingAPI.csproj | 5 +---- GamecraftModdingAPI/Inventory/Hotbar.cs | 9 +++++++++ doxygen.conf | 2 +- 11 files changed, 34 insertions(+), 26 deletions(-) diff --git a/GamecraftModdingAPI/Events/DeterministicStepComposeEngineGroupsPatch.cs b/GamecraftModdingAPI/Events/DeterministicStepComposeEngineGroupsPatch.cs index b880257..6493b0c 100644 --- a/GamecraftModdingAPI/Events/DeterministicStepComposeEngineGroupsPatch.cs +++ b/GamecraftModdingAPI/Events/DeterministicStepComposeEngineGroupsPatch.cs @@ -29,12 +29,6 @@ namespace GamecraftModdingAPI.Events { stateSyncReg.buildModeInitializationEngines.Add(buildEngine); stateSyncReg.simulationModeInitializationEngines.Add(simEngine); - } - - public static MethodBase NopeTargetMethod() - { - return typeof(DeterministicStepCompositionRoot).GetMethods().First(m => m.Name == "ComposeEnginesGroups") - .MakeGenericMethod(typeof(object)); } } } diff --git a/GamecraftModdingAPI/Events/EventEngineFactory.cs b/GamecraftModdingAPI/Events/EventEngineFactory.cs index 4efd018..f79dfca 100644 --- a/GamecraftModdingAPI/Events/EventEngineFactory.cs +++ b/GamecraftModdingAPI/Events/EventEngineFactory.cs @@ -21,7 +21,7 @@ namespace GamecraftModdingAPI.Events /// The operation to do when the event is created /// The operation to do when the event is destroyed (if applicable) /// The created object - public static SimpleEventHandlerEngine CreateAddSimpleHandler(string name, object type, Action onActivated, Action onDestroyed) + public static SimpleEventHandlerEngine CreateAddSimpleHandler(string name, int type, Action onActivated, Action onDestroyed) { var engine = new SimpleEventHandlerEngine(onActivated, onDestroyed, type, name); EventManager.AddEventHandler(engine); @@ -36,7 +36,7 @@ namespace GamecraftModdingAPI.Events /// The operation to do when the event is created /// The operation to do when the event is destroyed (if applicable) /// The created object - public static SimpleEventHandlerEngine CreateAddSimpleHandler(string name, object type, Action onActivated, Action onDestroyed) + public static SimpleEventHandlerEngine CreateAddSimpleHandler(string name, int type, Action onActivated, Action onDestroyed) { var engine = new SimpleEventHandlerEngine(onActivated, onDestroyed, type, name); EventManager.AddEventHandler(engine); @@ -50,7 +50,7 @@ namespace GamecraftModdingAPI.Events /// The type of event to emit /// Will removing this engine not break your code? /// The created object - public static SimpleEventEmitterEngine CreateAddSimpleEmitter(string name, object type, bool isRemovable = true) + public static SimpleEventEmitterEngine CreateAddSimpleEmitter(string name, int type, bool isRemovable = true) { var engine = new SimpleEventEmitterEngine(type, name, isRemovable); EventManager.AddEventEmitter(engine); diff --git a/GamecraftModdingAPI/Events/GameStateBuildEmitterEngine.cs b/GamecraftModdingAPI/Events/GameStateBuildEmitterEngine.cs index 9905446..5e6f1fe 100644 --- a/GamecraftModdingAPI/Events/GameStateBuildEmitterEngine.cs +++ b/GamecraftModdingAPI/Events/GameStateBuildEmitterEngine.cs @@ -17,7 +17,7 @@ namespace GamecraftModdingAPI.Events public EntitiesDB entitiesDB { set; private get; } - public object type { get; } = EventType.BuildSwitchedTo; + public int type { get; } = (int)EventType.BuildSwitchedTo; public bool isRemovable { get; } = false; diff --git a/GamecraftModdingAPI/Events/GameStateSimulationEmitterEngine.cs b/GamecraftModdingAPI/Events/GameStateSimulationEmitterEngine.cs index 57b1c00..578f320 100644 --- a/GamecraftModdingAPI/Events/GameStateSimulationEmitterEngine.cs +++ b/GamecraftModdingAPI/Events/GameStateSimulationEmitterEngine.cs @@ -17,7 +17,7 @@ namespace GamecraftModdingAPI.Events public EntitiesDB entitiesDB { set; private get; } - public object type { get; } = EventType.SimulationSwitchedTo; + public int type { get; } = (int)EventType.SimulationSwitchedTo; public bool isRemovable { get; } = false; diff --git a/GamecraftModdingAPI/Events/IEventEmitterEngine.cs b/GamecraftModdingAPI/Events/IEventEmitterEngine.cs index ad6ffad..6a303aa 100644 --- a/GamecraftModdingAPI/Events/IEventEmitterEngine.cs +++ b/GamecraftModdingAPI/Events/IEventEmitterEngine.cs @@ -18,7 +18,7 @@ namespace GamecraftModdingAPI.Events /// /// The type of event emitted /// - object type { get; } + int type { get; } /// /// Whether the emitter can be removed with Manager.RemoveEventEmitter(name) diff --git a/GamecraftModdingAPI/Events/ModEventEntityStruct.cs b/GamecraftModdingAPI/Events/ModEventEntityStruct.cs index 3619e69..03bd65f 100644 --- a/GamecraftModdingAPI/Events/ModEventEntityStruct.cs +++ b/GamecraftModdingAPI/Events/ModEventEntityStruct.cs @@ -11,11 +11,13 @@ namespace GamecraftModdingAPI.Events /// /// The event entity struct /// - public struct ModEventEntityStruct : IEntityStruct + public struct ModEventEntityStruct : IEntityStruct, INeedEGID { /// /// The type of event that has been emitted /// - public object type; - } + public int type; + + public EGID ID { get; set; } + } } diff --git a/GamecraftModdingAPI/Events/SimpleEventEmitterEngine.cs b/GamecraftModdingAPI/Events/SimpleEventEmitterEngine.cs index 799cfed..0e51823 100644 --- a/GamecraftModdingAPI/Events/SimpleEventEmitterEngine.cs +++ b/GamecraftModdingAPI/Events/SimpleEventEmitterEngine.cs @@ -15,7 +15,7 @@ namespace GamecraftModdingAPI.Events public class SimpleEventEmitterEngine : IEventEmitterEngine { public string Name { get; set; } - public object type { get; set; } + public int type { get; set; } public bool isRemovable { get; } @@ -44,7 +44,7 @@ namespace GamecraftModdingAPI.Events /// Will removing this engine not break your code? public SimpleEventEmitterEngine(EventType type, string name, bool isRemovable = true) { - this.type = type; + this.type = (int)type; this.Name = name; this.isRemovable = isRemovable; } @@ -55,7 +55,7 @@ namespace GamecraftModdingAPI.Events /// The object to use for ModEventEntityStruct.type /// The name of this engine /// Will removing this engine not break your code? - public SimpleEventEmitterEngine(object type, string name, bool isRemovable = true) + public SimpleEventEmitterEngine(int type, string name, bool isRemovable = true) { this.type = type; this.Name = name; diff --git a/GamecraftModdingAPI/Events/SimpleEventHandlerEngine.cs b/GamecraftModdingAPI/Events/SimpleEventHandlerEngine.cs index 9968cc7..d0bc341 100644 --- a/GamecraftModdingAPI/Events/SimpleEventHandlerEngine.cs +++ b/GamecraftModdingAPI/Events/SimpleEventHandlerEngine.cs @@ -13,7 +13,7 @@ namespace GamecraftModdingAPI.Events /// public class SimpleEventHandlerEngine : IEventHandlerEngine { - public object type { get; set; } + public int type { get; set; } public string Name { get; set; } private bool isActivated = false; @@ -71,7 +71,7 @@ namespace GamecraftModdingAPI.Events /// The type of event to handle /// The name of the engine /// A useless parameter to use to avoid Python overload resolution errors - public SimpleEventHandlerEngine(Action activated, Action removed, object type, string name, bool simple = true) + public SimpleEventHandlerEngine(Action activated, Action removed, int type, string name, bool simple = true) : this((EntitiesDB _) => { activated.Invoke(); }, (EntitiesDB _) => { removed.Invoke(); }, type, name) { } /// @@ -81,12 +81,18 @@ namespace GamecraftModdingAPI.Events /// The operation to do when the event is destroyed (if applicable) /// The type of event to handler /// The name of the engine - public SimpleEventHandlerEngine(Action activated, Action removed, object type, string name) + public SimpleEventHandlerEngine(Action activated, Action removed, int type, string name) { this.type = type; this.Name = name; this.onActivated = activated; this.onDestroyed = removed; } + + public SimpleEventHandlerEngine(Action activated, Action removed, EventType type, string name, bool simple = true) + : this((EntitiesDB _) => { activated.Invoke(); }, (EntitiesDB _) => { removed.Invoke(); }, (int)type, name) { } + + public SimpleEventHandlerEngine(Action activated, Action removed, EventType type, string name, bool simple = true) + : this(activated, removed, (int)type, name) { } } } diff --git a/GamecraftModdingAPI/GamecraftModdingAPI.csproj b/GamecraftModdingAPI/GamecraftModdingAPI.csproj index d4f95bc..d000ff5 100644 --- a/GamecraftModdingAPI/GamecraftModdingAPI.csproj +++ b/GamecraftModdingAPI/GamecraftModdingAPI.csproj @@ -3,7 +3,7 @@ net472 true - 0.1.4.0 + 0.2.0 Exmods GNU General Public Licence 3+ https://git.exmods.org/modtainers/GamecraftModdingAPI @@ -542,7 +542,4 @@ - - - diff --git a/GamecraftModdingAPI/Inventory/Hotbar.cs b/GamecraftModdingAPI/Inventory/Hotbar.cs index a4fcf22..01bd1e0 100644 --- a/GamecraftModdingAPI/Inventory/Hotbar.cs +++ b/GamecraftModdingAPI/Inventory/Hotbar.cs @@ -13,6 +13,11 @@ namespace GamecraftModdingAPI.Inventory { private static HotbarEngine hotbarEngine = new HotbarEngine(); + /// + /// Switch the block in the player's hand + /// + /// The block to switch to. + /// The player. Omit this to use the local player. public static void EquipBlock(BlockIDs block, uint playerID = uint.MaxValue) { if (playerID == uint.MaxValue) @@ -25,6 +30,10 @@ namespace GamecraftModdingAPI.Inventory // reason: the game expects a Dictionary entry for the tweaked stats } + /// + /// Gets the block in the player's hand + /// + /// The equipped block. public static BlockIDs GetEquippedBlock() { return HotbarSlotSelectionHandlerEnginePatch.EquippedPartID; diff --git a/doxygen.conf b/doxygen.conf index 1d0f155..396453c 100644 --- a/doxygen.conf +++ b/doxygen.conf @@ -38,7 +38,7 @@ PROJECT_NAME = "GamecraftModdingAPI" # could be handy for archiving the generated documentation or if some version # control system is used. -PROJECT_NUMBER = "v0.1.4.0" +PROJECT_NUMBER = "v0.2.0" # Using the PROJECT_BRIEF tag one can provide an optional one line description # for a project that appears at the top of each page and should give viewer a