Refactored CustomNPCs handling, v1.17.2.5

This commit is contained in:
Stephen Seo 2022-09-02 15:16:53 +09:00
parent a89adbc624
commit f58a1942ca
6 changed files with 57 additions and 40 deletions

View file

@ -1,5 +1,10 @@
# Upcoming changes # Upcoming changes
# Version 1.17.2.5
Refactored OtherModHandling.java to be more efficient when handling CustomNPCs
DamagedEvent.
# Version 1.17.2.4 # Version 1.17.2.4
Fix usage of NpcAPI. Fix usage of NpcAPI.

View file

@ -14,7 +14,7 @@ apply plugin: 'eclipse'
//apply plugin: 'maven-publish' //apply plugin: 'maven-publish'
apply plugin: 'com.github.johnrengelman.shadow' apply plugin: 'com.github.johnrengelman.shadow'
version = "1.17.2.4" version = "1.17.2.5"
group = "com.burnedkirby.TurnBasedMinecraft" group = "com.burnedkirby.TurnBasedMinecraft"
archivesBaseName = "TurnBasedMinecraft" archivesBaseName = "TurnBasedMinecraft"

View file

@ -35,6 +35,16 @@ public class OtherModHandler {
break; 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; Class<?> customNPCsIDamageSource = null;
try { try {
customNPCsIDamageSource = Class.forName("noppes.npcs.api.IDamageSource"); customNPCsIDamageSource = Class.forName("noppes.npcs.api.IDamageSource");
@ -45,6 +55,16 @@ public class OtherModHandler {
break; 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; Class<?> customNPCsIEntity = null;
try { try {
customNPCsIEntity = Class.forName("noppes.npcs.api.entity.IEntity"); customNPCsIEntity = Class.forName("noppes.npcs.api.entity.IEntity");
@ -55,6 +75,26 @@ public class OtherModHandler {
break; 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 // Check if available
Object instance = null; Object instance = null;
try { try {
@ -120,23 +160,19 @@ public class OtherModHandler {
} }
final Class<?> finalCustomNPCsPlayerHurtEvent = customNPCsPlayerHurtEvent; final Class<?> finalCustomNPCsPlayerHurtEvent = customNPCsPlayerHurtEvent;
final Field finalDamageSourceField = damageSourceField;
final Class<?> finalCustomNPCsIDamageSource = customNPCsIDamageSource; final Class<?> finalCustomNPCsIDamageSource = customNPCsIDamageSource;
final Method finalTrueSourceMethod = trueSourceMethod;
final Class<?> finalCustomNPCsIEntity = customNPCsIEntity; final Class<?> finalCustomNPCsIEntity = customNPCsIEntity;
final Method finalGetEntityUUIDMethod = getEntityUUIDMethod;
final Method finalGetCanceledMethod = getCanceledMethod;
customNPCsEventBus.addListener(EventPriority.LOWEST, true, (event) -> { customNPCsEventBus.addListener(EventPriority.LOWEST, true, (event) -> {
if (finalCustomNPCsPlayerHurtEvent.isInstance(event) if (finalCustomNPCsPlayerHurtEvent.isInstance(event)
&& TurnBasedMinecraftMod.proxy.getAttackingEntity() != null) { && 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; Object damageSourceObject;
try { try {
damageSourceObject = damageSourceField.get(event); damageSourceObject = finalDamageSourceField.get(event);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
TurnBasedMinecraftMod.logger.error("CustomNPCs PlayerHurtEvent failed to get \".damageSource\"!"); TurnBasedMinecraftMod.logger.error("CustomNPCs PlayerHurtEvent failed to get \".damageSource\"!");
return; return;
@ -147,17 +183,9 @@ public class OtherModHandler {
return; return;
} }
Method trueSourceMethod;
try {
trueSourceMethod = finalCustomNPCsIDamageSource.getMethod("getTrueSource");
} catch (NoSuchMethodException e) {
TurnBasedMinecraftMod.logger.error("CustomNPCs IDamageSource does not have \".getTrueSource()\"!");
return;
}
Object iEntityObject; Object iEntityObject;
try { try {
iEntityObject = trueSourceMethod.invoke(damageSourceObject); iEntityObject = finalTrueSourceMethod.invoke(damageSourceObject);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
TurnBasedMinecraftMod.logger.error("Failed to get CustomNPCs IEntity from IDamageSource, IllegalAccessException!"); TurnBasedMinecraftMod.logger.error("Failed to get CustomNPCs IEntity from IDamageSource, IllegalAccessException!");
return; return;
@ -171,17 +199,9 @@ public class OtherModHandler {
return; return;
} }
Method getEntityUUIDMethod;
try {
getEntityUUIDMethod = finalCustomNPCsIEntity.getMethod("getUUID");
} catch (NoSuchMethodException e) {
TurnBasedMinecraftMod.logger.error("Failed to get CustomNPCs \".getEntityId()\"!");
return;
}
String entityUUID; String entityUUID;
try { try {
entityUUID = (String)getEntityUUIDMethod.invoke(iEntityObject); entityUUID = (String)finalGetEntityUUIDMethod.invoke(iEntityObject);
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
TurnBasedMinecraftMod.logger.error("Failed to get CustomNPCs IEntity ID, InvocationTargetException!"); TurnBasedMinecraftMod.logger.error("Failed to get CustomNPCs IEntity ID, InvocationTargetException!");
return; return;
@ -197,16 +217,8 @@ public class OtherModHandler {
return; return;
} }
Method getCanceledMethod;
try { try {
getCanceledMethod = finalCustomNPCsPlayerHurtEvent.getMethod("setCanceled", boolean.class); finalGetCanceledMethod.invoke(event, false);
} catch (NoSuchMethodException e) {
TurnBasedMinecraftMod.logger.error("CustomNPCs PlayerHurtEvent does not have setCanceled(...)!");
return;
}
try {
getCanceledMethod.invoke(event, false);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
TurnBasedMinecraftMod.logger.error("Failed to un-cancel Player hurt event, IllegalAccessException!"); TurnBasedMinecraftMod.logger.error("Failed to un-cancel Player hurt event, IllegalAccessException!");
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {

View file

@ -35,7 +35,7 @@ public class TurnBasedMinecraftMod
{ {
public static final String MODID = "com_burnedkirby_turnbasedminecraft"; public static final String MODID = "com_burnedkirby_turnbasedminecraft";
public static final String NAME = "Turn Based Minecraft Mod"; 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 CONFIG_FILENAME = "TBM_Config.toml";
public static final String DEFAULT_CONFIG_FILENAME = "TBM_Config_DEFAULT.toml"; public static final String DEFAULT_CONFIG_FILENAME = "TBM_Config_DEFAULT.toml";
public static final String CONFIG_DIRECTORY = "config/TurnBasedMinecraft/"; public static final String CONFIG_DIRECTORY = "config/TurnBasedMinecraft/";

View file

@ -15,7 +15,7 @@ license="MIT"
# The modid of the mod # The modid of the mod
modId="com_burnedkirby_turnbasedminecraft" #mandatory modId="com_burnedkirby_turnbasedminecraft" #mandatory
# The version number of the mod - there's a few well known ${} variables useable here or just hardcode it # 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 # A display name for the mod
displayName="TurnBasedMinecraftMod" #mandatory displayName="TurnBasedMinecraftMod" #mandatory
# A URL to query for updates for this mod. See the JSON update specification <here> # A URL to query for updates for this mod. See the JSON update specification <here>

View file

@ -3,7 +3,7 @@
"modid": "com_burnedkirby_turnbasedminecraft", "modid": "com_burnedkirby_turnbasedminecraft",
"name": "Turn Based Minecraft", "name": "Turn Based Minecraft",
"description": "Changes battles to be turn-based.", "description": "Changes battles to be turn-based.",
"version": "1.17.2.4", "version": "1.17.2.5",
"mcversion": "1.16.3", "mcversion": "1.16.3",
"url": "", "url": "",
"updateUrl": "", "updateUrl": "",