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
# 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.

View file

@ -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"

View file

@ -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) {

View 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.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/";

View 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.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 <here>

View file

@ -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": "",