]> git.seodisparate.com - TurnBasedMinecraftMod/commitdiff
1.17.2.1: CustomNPCs related fix attempt 1.17.2.1
authorStephen Seo <seo.disparate@gmail.com>
Thu, 1 Sep 2022 08:55:21 +0000 (17:55 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 1 Sep 2022 08:55:21 +0000 (17:55 +0900)
Attempts to fix players not being damaged by CustomNPC entities.

Changelog.md
build.gradle
src/main/java/com/burnedkirby/TurnBasedMinecraft/common/CommonProxy.java
src/main/java/com/burnedkirby/TurnBasedMinecraft/common/OtherModHandler.java [new file with mode: 0644]
src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java
src/main/resources/META-INF/mods.toml
src/main/resources/mcmod.info

index 5b1b23de5a86aa2cd189f31980491cb568d801fb..640c4152bced70fc8e1a0610a708ad75534dd278 100644 (file)
@@ -1,5 +1,9 @@
 # Upcoming changes
 
+# Version 1.17.2.1
+
+Attempt to fix CustomNPCs mods not damaging players in TurnBased combat.
+
 # Version 1.17.2
 
 (try to) Fix potential freeze bug when an entity leaves battle.
index 9f1ee806faf37ed8b4a35f82d5c2c12f4f9f10f9..4a08ba6e8a7649de2d2e3399785fd03eb10b5dea 100644 (file)
@@ -14,7 +14,7 @@ apply plugin: 'eclipse'
 //apply plugin: 'maven-publish'\r
 apply plugin: 'com.github.johnrengelman.shadow'\r
 \r
-version = "1.17.2"\r
+version = "1.17.2.1"\r
 group = "com.burnedkirby.TurnBasedMinecraft"\r
 archivesBaseName = "TurnBasedMinecraft"\r
 \r
index 3074f6914902a092d70cba8a2dbacd8d2a425b31..910968fc2947ad59d98a87868049ec51c43ff07b 100644 (file)
@@ -25,12 +25,15 @@ public class CommonProxy
     private Config config = null;
     protected Logger logger = null;
     private Map<Integer, EditingInfo> editingPlayers;
+
+    private OtherModHandler otherModHandler;
     
     public final void initialize()
     {
         attackerViaBow = new HashSet<AttackerViaBow>();
         editingPlayers = new Hashtable<Integer, EditingInfo>();
         initializeClient();
+        otherModHandler = new OtherModHandler();
         logger.debug("Init proxy for com_burnedkirby_turnbasedminecraft");
     }
     
@@ -77,6 +80,8 @@ public class CommonProxy
         postInitClient();
         pamsFoodIntegrationLoading();
         logger.debug("postInit proxy for com_burnedkirby_turnbasedminecraft");
+
+        otherModHandler.postInit();
     }
     
     protected void postInitClient() {}
diff --git a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/OtherModHandler.java b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/OtherModHandler.java
new file mode 100644 (file)
index 0000000..bfc8d05
--- /dev/null
@@ -0,0 +1,172 @@
+package com.burnedkirby.TurnBasedMinecraft.common;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import net.minecraftforge.eventbus.api.EventPriority;
+import net.minecraftforge.eventbus.api.IEventBus;
+
+public class OtherModHandler {
+    public OtherModHandler() {
+    }
+
+    public void postInit() {
+        // Check if CustomNPCs is available, and handle player damage events if it is.
+        for (int i = 0; i < 1; ++i) {
+            Class<?> customNPCsAPI = null;
+            try {
+                customNPCsAPI = Class.forName("noppes.npcs.scripted.NpcAPI");
+            } catch (ClassNotFoundException e) {
+                TurnBasedMinecraftMod.logger.info("NpcAPI not found, not handling it.");
+            }
+            if (customNPCsAPI == null) {
+                break;
+            }
+
+            Class<?> customNPCsPlayerHurtEvent = null;
+            try {
+                customNPCsPlayerHurtEvent = Class.forName("noppes.npcs.scripted.event.PlayerEvent.DamagedEvent");
+            } catch (ClassNotFoundException e) {
+                TurnBasedMinecraftMod.logger.info("CustomNPCs Player Hurt Event class not found, not handling it.");
+            }
+            if (customNPCsPlayerHurtEvent == null) {
+                break;
+            }
+
+            Class<?> customNPCsIDamageSource = null;
+            try {
+                customNPCsIDamageSource = Class.forName("noppes.npcs.scripted.interfaces.IDamageSource");
+            } catch (ClassNotFoundException e) {
+                TurnBasedMinecraftMod.logger.info("CustomNPCs IDamageSource not found, not handling it.");
+            }
+            if (customNPCsIDamageSource == null) {
+                break;
+            }
+
+            Class<?> customNPCsIEntity = null;
+            try {
+                customNPCsIEntity = Class.forName("noppes.npcs.scripted.interfaces.entity.IEntity");
+            } catch (ClassNotFoundException e) {
+                TurnBasedMinecraftMod.logger.info("CustomNPCs IEntity not found, not handling it.");
+            }
+            if (customNPCsIEntity == null) {
+                break;
+            }
+
+            TurnBasedMinecraftMod.logger.info("NpcAPI found, setting up player-not-getting-hurt workaround...");
+
+            Method getNPCsEventBusMethod = null;
+            try {
+                getNPCsEventBusMethod = customNPCsAPI.getMethod("events");
+            } catch (NoSuchMethodException e) {
+                TurnBasedMinecraftMod.logger.warn("NpcAPI.events() could not be found!");
+            }
+            if (getNPCsEventBusMethod == null) {
+                break;
+            }
+
+            IEventBus customNPCsEventBus = null;
+            try {
+                customNPCsEventBus = (IEventBus) getNPCsEventBusMethod.invoke(customNPCsAPI);
+            } catch (InvocationTargetException e) {
+                TurnBasedMinecraftMod.logger.warn("Failed to invoke NpcAPI.events(), InvocationTargetException!");
+            } catch (IllegalAccessException e) {
+                TurnBasedMinecraftMod.logger.warn("Failed to invoke NpcAPI.events(), IllegalAccessException!");
+            }
+            if (customNPCsEventBus == null) {
+                break;
+            }
+
+            final Class<?> finalCustomNPCsPlayerHurtEvent = customNPCsPlayerHurtEvent;
+            final Class<?> finalCustomNPCsIDamageSource = customNPCsIDamageSource;
+            final Class<?> finalCustomNPCsIEntity = customNPCsIEntity;
+
+            customNPCsEventBus.addListener(EventPriority.LOWEST, true, (event) -> {
+                if (finalCustomNPCsPlayerHurtEvent.isInstance(event)) {
+                    Field damageSourceField;
+                    try {
+                        damageSourceField = finalCustomNPCsPlayerHurtEvent.getField("damageSource");
+                    } catch (NoSuchFieldException e) {
+                        TurnBasedMinecraftMod.logger.error("CustomNPCs PlayerHurtEvent does not have \".damageSource\"!");
+                        return;
+                    }
+
+                    Object damageSourceObject;
+                    try {
+                        damageSourceObject = damageSourceField.get(event);
+                    } catch (IllegalAccessException e) {
+                        TurnBasedMinecraftMod.logger.error("CustomNPCs PlayerHurtEvent failed to get \".damageSource\"!");
+                        return;
+                    }
+
+                    if (!finalCustomNPCsIDamageSource.isInstance(damageSourceObject)) {
+                        TurnBasedMinecraftMod.logger.error("CustomNPCs PlayerHurtEvent damageSource is not IDamageSource!");
+                        return;
+                    }
+
+                    Method trueSourceMethod;
+                    try {
+                        trueSourceMethod = finalCustomNPCsIDamageSource.getMethod("getTrueSource");
+                    } catch (NoSuchMethodException e) {
+                        TurnBasedMinecraftMod.logger.error("CustomNPCs IDamageSource does not have \".getTrueSource()\"!");
+                        return;
+                    }
+
+                    Object iEntityObject;
+                    try {
+                        iEntityObject = trueSourceMethod.invoke(damageSourceObject);
+                    } catch (IllegalAccessException e) {
+                        TurnBasedMinecraftMod.logger.error("Failed to get CustomNPCs IEntity from IDamageSource, IllegalAccessException!");
+                        return;
+                    } catch (InvocationTargetException e) {
+                        TurnBasedMinecraftMod.logger.error("Failed to get CustomNPCs IEntity from IDamageSource, InvocationTargetException!");
+                        return;
+                    }
+
+                    Method getEntityIDMethod;
+                    try {
+                        getEntityIDMethod = finalCustomNPCsIEntity.getMethod("getEntityId");
+                    } catch (NoSuchMethodException e) {
+                        TurnBasedMinecraftMod.logger.error("Failed to get CustomNPCs \".getEntityId()\"!");
+                        return;
+                    }
+
+                    Integer entityId;
+                    try {
+                        entityId = (Integer)getEntityIDMethod.invoke(iEntityObject);
+                    } catch (InvocationTargetException e) {
+                        TurnBasedMinecraftMod.logger.error("Failed to get CustomNPCs IEntity ID, InvocationTargetException!");
+                        return;
+                    } catch (IllegalAccessException e) {
+                        TurnBasedMinecraftMod.logger.error("Failed to get CustomNPCs IEntity ID, IllegalAccessException!");
+                        return;
+                    } catch (ClassCastException e) {
+                        TurnBasedMinecraftMod.logger.error("Failed to get CustomNPCs IEntity ID, ClassCastException!");
+                        return;
+                    }
+
+                    if (entityId != TurnBasedMinecraftMod.proxy.getAttackingEntity().getId()) {
+                        return;
+                    }
+
+                    Method getCanceledMethod;
+                    try {
+                        getCanceledMethod = finalCustomNPCsPlayerHurtEvent.getMethod("setCanceled", Boolean.class);
+                    } catch (NoSuchMethodException e) {
+                        TurnBasedMinecraftMod.logger.error("CustomNPCs PlayerHurtEvent does not have setCanceled(...)!");
+                        return;
+                    }
+
+                    try {
+                        getCanceledMethod.invoke(event, false);
+                    } catch (IllegalAccessException e) {
+                        TurnBasedMinecraftMod.logger.error("Failed to un-cancel Player hurt event, IllegalAccessException!");
+                    } catch (InvocationTargetException e) {
+                        TurnBasedMinecraftMod.logger.error("Failed to un-cancel Player hurt event, InvocationTargetException!");
+                    }
+                }
+            });
+        }
+    }
+}
index 2deee8f254606db40ac482dcd8686fe2aaaafd52..6961c96f000b9b275e8442283de1779201623a9e 100644 (file)
@@ -35,7 +35,7 @@ public class TurnBasedMinecraftMod
 {
     public static final String MODID = "com_burnedkirby_turnbasedminecraft";
     public static final String NAME = "Turn Based Minecraft Mod";
-    public static final String VERSION = "1.17.2";
+    public static final String VERSION = "1.17.2.1";
     public static final String CONFIG_FILENAME = "TBM_Config.toml";
     public static final String DEFAULT_CONFIG_FILENAME = "TBM_Config_DEFAULT.toml";
     public static final String CONFIG_DIRECTORY = "config/TurnBasedMinecraft/";
index 99b8c7816fdbbed62a9f235a0c3f59a33b9c1c22..0cc41ecdb92d15b279cff1c6afbd1e91eb1e46a4 100644 (file)
@@ -15,7 +15,7 @@ license="MIT"
 # The modid of the mod
 modId="com_burnedkirby_turnbasedminecraft" #mandatory
 # The version number of the mod - there's a few well known ${} variables useable here or just hardcode it
-version="1.17.2" #mandatory
+version="1.17.2.1" #mandatory
  # A display name for the mod
 displayName="TurnBasedMinecraftMod" #mandatory
 # A URL to query for updates for this mod. See the JSON update specification <here>
index b61db108df121d8a0a45987d6cb003f957459652..9080c1545ddddc7f17187a1bb09c0d5098a7a677 100644 (file)
@@ -3,7 +3,7 @@
   "modid": "com_burnedkirby_turnbasedminecraft",
   "name": "Turn Based Minecraft",
   "description": "Changes battles to be turn-based.",
-  "version": "1.17.2",
+  "version": "1.17.2.1",
   "mcversion": "1.16.3",
   "url": "",
   "updateUrl": "",