From: Stephen Seo Date: Fri, 2 Sep 2022 02:20:27 +0000 (+0900) Subject: Fixes for Custom NPCs, version 1.17.2.4 X-Git-Tag: 1.17.2.4 X-Git-Url: https://git.seodisparate.com/gitweb?a=commitdiff_plain;h=a89adbc62431f81965e5a0f550286da4e00cba84;p=TurnBasedMinecraftMod Fixes for Custom NPCs, version 1.17.2.4 Previous code worked against a different Custom NPCs API. This commit fixes the previous namespaces to the publically available Custom NPCs "NpcAPI" namespaces. --- diff --git a/Changelog.md b/Changelog.md index 6f54b54..40aeca3 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,9 @@ # Upcoming changes +# Version 1.17.2.4 + +Fix usage of NpcAPI. + # Version 1.17.2.3 Fix potential unhandled exception crash bug related to handling CustomNPCs diff --git a/build.gradle b/build.gradle index 7e826b3..4020761 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.3" +version = "1.17.2.4" 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 d0e6396..e47c78e 100644 --- a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/OtherModHandler.java +++ b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/OtherModHandler.java @@ -14,9 +14,10 @@ public class OtherModHandler { public void postInit() { // Check if CustomNPCs is available, and handle player damage events if it is. for (int i = 0; i < 1; ++i) { + // Check if required classes exist Class customNPCsAPI = null; try { - customNPCsAPI = Class.forName("noppes.npcs.scripted.NpcAPI"); + customNPCsAPI = Class.forName("noppes.npcs.api.NpcAPI"); } catch (ClassNotFoundException e) { TurnBasedMinecraftMod.logger.info("NpcAPI not found, not handling it."); } @@ -26,7 +27,7 @@ public class OtherModHandler { Class customNPCsPlayerHurtEvent = null; try { - customNPCsPlayerHurtEvent = Class.forName("noppes.npcs.scripted.event.PlayerEvent.DamagedEvent"); + customNPCsPlayerHurtEvent = Class.forName("noppes.npcs.api.event.PlayerEvent$DamagedEvent"); } catch (ClassNotFoundException e) { TurnBasedMinecraftMod.logger.info("CustomNPCs Player Hurt Event class not found, not handling it."); } @@ -36,7 +37,7 @@ public class OtherModHandler { Class customNPCsIDamageSource = null; try { - customNPCsIDamageSource = Class.forName("noppes.npcs.scripted.interfaces.IDamageSource"); + customNPCsIDamageSource = Class.forName("noppes.npcs.api.IDamageSource"); } catch (ClassNotFoundException e) { TurnBasedMinecraftMod.logger.info("CustomNPCs IDamageSource not found, not handling it."); } @@ -46,7 +47,7 @@ public class OtherModHandler { Class customNPCsIEntity = null; try { - customNPCsIEntity = Class.forName("noppes.npcs.scripted.interfaces.entity.IEntity"); + customNPCsIEntity = Class.forName("noppes.npcs.api.entity.IEntity"); } catch (ClassNotFoundException e) { TurnBasedMinecraftMod.logger.info("CustomNPCs IEntity not found, not handling it."); } @@ -54,6 +55,44 @@ public class OtherModHandler { break; } + // Check if available + Object instance = null; + try { + Method instanceMethod = customNPCsAPI.getMethod("Instance"); + instance = instanceMethod.invoke(null); + if (!customNPCsAPI.isInstance(instance)) { + instance = null; + TurnBasedMinecraftMod.logger.error("NpcAPI.Instance() is not NpcAPI!"); + } + } catch (NoSuchMethodException e) { + TurnBasedMinecraftMod.logger.warn("NpcAPI.Instance() does not exist!"); + } catch (InvocationTargetException e) { + TurnBasedMinecraftMod.logger.error("Failed to call NpcAPI.Instance(), InvocationTargetException!"); + } catch (IllegalAccessException e) { + TurnBasedMinecraftMod.logger.error("Failed to call NpcAPI.Instance(), IllegalAccessException!"); + } + if (instance == null) { + break; + } + + Boolean isAvailable = false; + try { + Method isAvailableMethod = customNPCsAPI.getMethod("IsAvailable"); + isAvailable = (Boolean)isAvailableMethod.invoke(instance); + } catch (NoSuchMethodException e) { + TurnBasedMinecraftMod.logger.warn("NpcAPI.IsAvailable() does not exist!"); + } catch (InvocationTargetException e) { + TurnBasedMinecraftMod.logger.warn("Failed to call NpcAPI.IsAvailable(), InvocationTargetException!"); + } catch (IllegalAccessException e) { + TurnBasedMinecraftMod.logger.warn("Failed to call NpcAPI.IsAvailable(), IllegalAccessException!"); + } catch (ClassCastException e) { + TurnBasedMinecraftMod.logger.warn("Result of NpcAPI.IsAvailable() is not a Boolean!"); + } + if (!isAvailable) { + TurnBasedMinecraftMod.logger.warn("NpcAPI is not available!"); + break; + } + TurnBasedMinecraftMod.logger.info("NpcAPI found, setting up player-not-getting-hurt workaround..."); Method getNPCsEventBusMethod = null; @@ -68,7 +107,7 @@ public class OtherModHandler { IEventBus customNPCsEventBus = null; try { - customNPCsEventBus = (IEventBus) getNPCsEventBusMethod.invoke(customNPCsAPI); + customNPCsEventBus = (IEventBus) getNPCsEventBusMethod.invoke(instance); } catch (InvocationTargetException e) { TurnBasedMinecraftMod.logger.warn("Failed to invoke NpcAPI.events(), InvocationTargetException!"); } catch (IllegalAccessException e) { @@ -127,17 +166,22 @@ public class OtherModHandler { return; } - Method getEntityIDMethod; + if (!finalCustomNPCsIEntity.isInstance(iEntityObject)) { + TurnBasedMinecraftMod.logger.error("IDamageSource.getTrueSource() is not IEntity!"); + return; + } + + Method getEntityUUIDMethod; try { - getEntityIDMethod = finalCustomNPCsIEntity.getMethod("getEntityId"); + getEntityUUIDMethod = finalCustomNPCsIEntity.getMethod("getUUID"); } catch (NoSuchMethodException e) { TurnBasedMinecraftMod.logger.error("Failed to get CustomNPCs \".getEntityId()\"!"); return; } - Integer entityId; + String entityUUID; try { - entityId = (Integer)getEntityIDMethod.invoke(iEntityObject); + entityUUID = (String)getEntityUUIDMethod.invoke(iEntityObject); } catch (InvocationTargetException e) { TurnBasedMinecraftMod.logger.error("Failed to get CustomNPCs IEntity ID, InvocationTargetException!"); return; @@ -149,13 +193,13 @@ public class OtherModHandler { return; } - if (entityId != TurnBasedMinecraftMod.proxy.getAttackingEntity().getId()) { + if (!TurnBasedMinecraftMod.proxy.getAttackingEntity().getStringUUID().equals(entityUUID)) { return; } Method getCanceledMethod; try { - getCanceledMethod = finalCustomNPCsPlayerHurtEvent.getMethod("setCanceled", Boolean.class); + getCanceledMethod = finalCustomNPCsPlayerHurtEvent.getMethod("setCanceled", boolean.class); } catch (NoSuchMethodException e) { TurnBasedMinecraftMod.logger.error("CustomNPCs PlayerHurtEvent does not have setCanceled(...)!"); return; diff --git a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java index 99c4ffe..754c941 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.3"; + public static final String VERSION = "1.17.2.4"; 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 4c77eeb..509d24d 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.3" #mandatory +version="1.17.2.4" #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 b2db4eb..7bc5388 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.3", + "version": "1.17.2.4", "mcversion": "1.16.3", "url": "", "updateUrl": "",