From c565eb24f4a801fa8fc369751b966a6f4fc746d2 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 1 Sep 2022 17:55:21 +0900 Subject: [PATCH] 1.17.2.1: CustomNPCs related fix attempt Attempts to fix players not being damaged by CustomNPC entities. --- Changelog.md | 4 + build.gradle | 2 +- .../common/CommonProxy.java | 5 + .../common/OtherModHandler.java | 172 ++++++++++++++++++ .../common/TurnBasedMinecraftMod.java | 2 +- src/main/resources/META-INF/mods.toml | 2 +- src/main/resources/mcmod.info | 2 +- 7 files changed, 185 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/burnedkirby/TurnBasedMinecraft/common/OtherModHandler.java diff --git a/Changelog.md b/Changelog.md index 5b1b23d..640c415 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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. diff --git a/build.gradle b/build.gradle index 9f1ee80..4a08ba6 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" +version = "1.17.2.1" group = "com.burnedkirby.TurnBasedMinecraft" archivesBaseName = "TurnBasedMinecraft" diff --git a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/CommonProxy.java b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/CommonProxy.java index 3074f69..910968f 100644 --- a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/CommonProxy.java +++ b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/CommonProxy.java @@ -25,12 +25,15 @@ public class CommonProxy private Config config = null; protected Logger logger = null; private Map editingPlayers; + + private OtherModHandler otherModHandler; public final void initialize() { attackerViaBow = new HashSet(); editingPlayers = new Hashtable(); 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 index 0000000..bfc8d05 --- /dev/null +++ b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/OtherModHandler.java @@ -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!"); + } + } + }); + } + } +} diff --git a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java index 2deee8f..6961c96 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"; + 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/"; diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index 99b8c78..0cc41ec 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" #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 diff --git a/src/main/resources/mcmod.info b/src/main/resources/mcmod.info index b61db10..9080c15 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", + "version": "1.17.2.1", "mcversion": "1.16.3", "url": "", "updateUrl": "", -- 2.49.0