1.17.2.1: CustomNPCs related fix attempt

Attempts to fix players not being damaged by CustomNPC entities.
This commit is contained in:
Stephen Seo 2022-09-01 17:55:21 +09:00
parent 89ca942c31
commit c565eb24f4
7 changed files with 185 additions and 4 deletions

View file

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

View file

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

View file

@ -25,12 +25,15 @@ public class CommonProxy
private Config config = null;
protected Logger logger = null;
private Map<Integer, EditingInfo> editingPlayers;
private OtherModHandler otherModHandler;
public final void initialize()
{
attackerViaBow = new HashSet<AttackerViaBow>();
editingPlayers = new Hashtable<Integer, EditingInfo>();
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() {}

View file

@ -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!");
}
}
});
}
}
}

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

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" #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 <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",
"version": "1.17.2.1",
"mcversion": "1.16.3",
"url": "",
"updateUrl": "",