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.
This commit is contained in:
Stephen Seo 2022-09-02 11:20:27 +09:00
parent 6aebe3eb82
commit a89adbc624
6 changed files with 63 additions and 15 deletions

View file

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

View file

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

View file

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

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

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.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 <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.3",
"version": "1.17.2.4",
"mcversion": "1.16.3",
"url": "",
"updateUrl": "",