diff --git a/Changelog.md b/Changelog.md index 40aeca3..8063f2b 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,10 @@ # Upcoming changes +# Version 1.17.2.5 + +Refactored OtherModHandling.java to be more efficient when handling CustomNPCs +DamagedEvent. + # Version 1.17.2.4 Fix usage of NpcAPI. diff --git a/build.gradle b/build.gradle index 4020761..2e7ea9f 100644 --- a/build.gradle +++ b/build.gradle @@ -14,7 +14,7 @@ apply plugin: 'eclipse' //apply plugin: 'maven-publish' apply plugin: 'com.github.johnrengelman.shadow' -version = "1.17.2.4" +version = "1.17.2.5" group = "com.burnedkirby.TurnBasedMinecraft" archivesBaseName = "TurnBasedMinecraft" diff --git a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/OtherModHandler.java b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/OtherModHandler.java index e47c78e..5670b6a 100644 --- a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/OtherModHandler.java +++ b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/OtherModHandler.java @@ -35,6 +35,16 @@ public class OtherModHandler { break; } + Field damageSourceField = null; + try { + damageSourceField = customNPCsPlayerHurtEvent.getField("damageSource"); + } catch (NoSuchFieldException e) { + TurnBasedMinecraftMod.logger.error("CustomNPCs PlayerHurtEvent does not have \".damageSource\"!"); + } + if (damageSourceField == null) { + break; + } + Class customNPCsIDamageSource = null; try { customNPCsIDamageSource = Class.forName("noppes.npcs.api.IDamageSource"); @@ -45,6 +55,16 @@ public class OtherModHandler { break; } + Method trueSourceMethod = null; + try { + trueSourceMethod = customNPCsIDamageSource.getMethod("getTrueSource"); + } catch (NoSuchMethodException e) { + TurnBasedMinecraftMod.logger.error("CustomNPCs IDamageSource does not have \".getTrueSource()\"!"); + } + if (trueSourceMethod == null) { + break; + } + Class customNPCsIEntity = null; try { customNPCsIEntity = Class.forName("noppes.npcs.api.entity.IEntity"); @@ -55,6 +75,26 @@ public class OtherModHandler { break; } + Method getEntityUUIDMethod = null; + try { + getEntityUUIDMethod = customNPCsIEntity.getMethod("getUUID"); + } catch (NoSuchMethodException e) { + TurnBasedMinecraftMod.logger.error("Failed to get CustomNPCs \".getEntityId()\"!"); + } + if (getEntityUUIDMethod == null) { + break; + } + + Method getCanceledMethod = null; + try { + getCanceledMethod = customNPCsPlayerHurtEvent.getMethod("setCanceled", boolean.class); + } catch (NoSuchMethodException e) { + TurnBasedMinecraftMod.logger.error("CustomNPCs PlayerHurtEvent does not have setCanceled(...)!"); + } + if (getCanceledMethod == null) { + break; + } + // Check if available Object instance = null; try { @@ -120,23 +160,19 @@ public class OtherModHandler { } final Class finalCustomNPCsPlayerHurtEvent = customNPCsPlayerHurtEvent; + final Field finalDamageSourceField = damageSourceField; final Class finalCustomNPCsIDamageSource = customNPCsIDamageSource; + final Method finalTrueSourceMethod = trueSourceMethod; final Class finalCustomNPCsIEntity = customNPCsIEntity; + final Method finalGetEntityUUIDMethod = getEntityUUIDMethod; + final Method finalGetCanceledMethod = getCanceledMethod; customNPCsEventBus.addListener(EventPriority.LOWEST, true, (event) -> { if (finalCustomNPCsPlayerHurtEvent.isInstance(event) && TurnBasedMinecraftMod.proxy.getAttackingEntity() != null) { - 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); + damageSourceObject = finalDamageSourceField.get(event); } catch (IllegalAccessException e) { TurnBasedMinecraftMod.logger.error("CustomNPCs PlayerHurtEvent failed to get \".damageSource\"!"); return; @@ -147,17 +183,9 @@ public class OtherModHandler { 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); + iEntityObject = finalTrueSourceMethod.invoke(damageSourceObject); } catch (IllegalAccessException e) { TurnBasedMinecraftMod.logger.error("Failed to get CustomNPCs IEntity from IDamageSource, IllegalAccessException!"); return; @@ -171,17 +199,9 @@ public class OtherModHandler { return; } - Method getEntityUUIDMethod; - try { - getEntityUUIDMethod = finalCustomNPCsIEntity.getMethod("getUUID"); - } catch (NoSuchMethodException e) { - TurnBasedMinecraftMod.logger.error("Failed to get CustomNPCs \".getEntityId()\"!"); - return; - } - String entityUUID; try { - entityUUID = (String)getEntityUUIDMethod.invoke(iEntityObject); + entityUUID = (String)finalGetEntityUUIDMethod.invoke(iEntityObject); } catch (InvocationTargetException e) { TurnBasedMinecraftMod.logger.error("Failed to get CustomNPCs IEntity ID, InvocationTargetException!"); return; @@ -197,16 +217,8 @@ public class OtherModHandler { 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); + finalGetCanceledMethod.invoke(event, false); } catch (IllegalAccessException e) { TurnBasedMinecraftMod.logger.error("Failed to un-cancel Player hurt event, IllegalAccessException!"); } catch (InvocationTargetException e) { diff --git a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java index 754c941..aeb25a5 100644 --- a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java +++ b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java @@ -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.4"; + public static final String VERSION = "1.17.2.5"; 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/"; diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index 509d24d..282a11c 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -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.4" #mandatory +version="1.17.2.5" #mandatory # A display name for the mod displayName="TurnBasedMinecraftMod" #mandatory # A URL to query for updates for this mod. See the JSON update specification diff --git a/src/main/resources/mcmod.info b/src/main/resources/mcmod.info index 7bc5388..b1e252e 100644 --- a/src/main/resources/mcmod.info +++ b/src/main/resources/mcmod.info @@ -3,7 +3,7 @@ "modid": "com_burnedkirby_turnbasedminecraft", "name": "Turn Based Minecraft", "description": "Changes battles to be turn-based.", - "version": "1.17.2.4", + "version": "1.17.2.5", "mcversion": "1.16.3", "url": "", "updateUrl": "",