]> git.seodisparate.com - TurnBasedMinecraftMod/commitdiff
Impl. CustomNPC support with "custom names" CustomNPCsSupport_1.17.2 1.17.2.6
authorStephen Seo <seo.disparate@gmail.com>
Fri, 2 Sep 2022 10:20:06 +0000 (19:20 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Fri, 2 Sep 2022 10:30:56 +0000 (19:30 +0900)
Version 1.17.2.6 .

Now, if a CustomNPC has a specific name, and an entry exist in the config with
the same custom name (as when one is set up with a name-tag and added with
"/tbm-edit custom"), then the custom config will be applied to that CustomNPC
entity.

Changelog.md
build.gradle
src/main/java/com/burnedkirby/TurnBasedMinecraft/client/BattleGui.java
src/main/java/com/burnedkirby/TurnBasedMinecraft/common/Battle.java
src/main/java/com/burnedkirby/TurnBasedMinecraft/common/BattleManager.java
src/main/java/com/burnedkirby/TurnBasedMinecraft/common/OtherModHandler.java
src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java
src/main/resources/META-INF/mods.toml
src/main/resources/mcmod.info

index 8063f2bcabc328459259ea2cf3f0b9b2c3dc2e47..f5733a332b9e5608009df8e255df78344e145374 100644 (file)
@@ -1,5 +1,10 @@
 # Upcoming changes
 
+# Version 1.17.2.6
+
+Implemented getting EntityInfo for CustomNPCs that have the same name as a
+"custom entry" in the "server\_config.entity" array in the config.
+
 # Version 1.17.2.5
 
 Refactored OtherModHandling.java to be more efficient when handling CustomNPCs
index 2e7ea9fdb31fccf6199a602b32a64d24c30eea46..902897324ba34350b2814e5ead2c18358660d8c2 100644 (file)
@@ -14,7 +14,7 @@ apply plugin: 'eclipse'
 //apply plugin: 'maven-publish'\r
 apply plugin: 'com.github.johnrengelman.shadow'\r
 \r
-version = "1.17.2.5"\r
+version = "1.17.2.6"\r
 group = "com.burnedkirby.TurnBasedMinecraft"\r
 archivesBaseName = "TurnBasedMinecraft"\r
 \r
index fa3368306c3f4142da6569bd3de4c9357d88fd4b..be7eed04ce77f52be5809a4a4b13503873286fe9 100644 (file)
@@ -151,7 +151,11 @@ public class BattleGui extends Screen {
                                for (Map.Entry<Integer, Combatant> e : TurnBasedMinecraftMod.proxy.getLocalBattle()
                                                .getSideAEntrySet()) {
                                        if (e.getValue().entity != null) {
-                                               addButton(new EntitySelectionButton(width / 4 - 60, y, 120, 20, e.getValue().entity.getName().getString(), e.getKey(), true, (button) -> {
+                                               String name = e.getValue().entity.getName().getString();
+                                               if (name.isEmpty()) {
+                                                       name = "Unknown";
+                                               }
+                                               addButton(new EntitySelectionButton(width / 4 - 60, y, 120, 20, name, e.getKey(), true, (button) -> {
                                                        buttonActionEvent(button, ButtonAction.ATTACK_TARGET);
                                                }));
                                        } else {
@@ -169,7 +173,11 @@ public class BattleGui extends Screen {
                                for (Map.Entry<Integer, Combatant> e : TurnBasedMinecraftMod.proxy.getLocalBattle()
                                                .getSideBEntrySet()) {
                                        if (e.getValue().entity != null) {
-                                               addButton(new EntitySelectionButton(width * 3 / 4 - 60, y, 120, 20, e.getValue().entity.getName().getString(), e.getKey(), false, (button) -> {
+                                               String name = e.getValue().entity.getName().getString();
+                                               if (name.isEmpty()) {
+                                                       name = "Unknown";
+                                               }
+                                               addButton(new EntitySelectionButton(width * 3 / 4 - 60, y, 120, 20, name, e.getKey(), false, (button) -> {
                                                        buttonActionEvent(button, ButtonAction.ATTACK_TARGET);
                                                }));
                                        } else {
index 1bacc908bf640ca4104bcb14dd19ea0387299158..ac34036477ebd14d8d5b5270acf74ef46f070601 100644 (file)
@@ -142,6 +142,14 @@ public class Battle
                 } catch(NullPointerException exception) {
                     entityInfo = null;
                 }
+                if (entityInfo == null) {
+                    // Check if is CustomNPC with matching name entry.
+                    try {
+                        entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getCustomEntityInfoReference(OtherModHandler.getCustomNPCName(e));
+                    } catch(NullPointerException exception) {
+                        entityInfo = null;
+                    }
+                }
                 if(entityInfo == null)
                 {
                     entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getMatchingEntityInfo(e);
@@ -180,6 +188,14 @@ public class Battle
                 } catch(NullPointerException exception) {
                     entityInfo = null;
                 }
+                if (entityInfo == null) {
+                    // Check if is CustomNPC with matching name entry.
+                    try {
+                        entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getCustomEntityInfoReference(OtherModHandler.getCustomNPCName(e));
+                    } catch(NullPointerException exception) {
+                        entityInfo = null;
+                    }
+                }
                 if(entityInfo == null)
                 {
                     entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getMatchingEntityInfo(e);
@@ -311,12 +327,20 @@ public class Battle
         } catch(NullPointerException exception) {
             entityInfo = null;
         }
+        if (entityInfo == null) {
+            // Check if entity is CustomNPC entity.
+            try {
+                entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getCustomEntityInfoReference(OtherModHandler.getCustomNPCName(e));
+            } catch(NullPointerException exception) {
+                entityInfo = null;
+            }
+        }
         if(entityInfo == null)
         {
             entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getMatchingEntityInfo(e);
         }
 
-        if(entityInfo == null && !(e instanceof PlayerEntity) && TurnBasedMinecraftMod.proxy.isServerRunning())
+        if(isServer && entityInfo == null && !(e instanceof PlayerEntity) && TurnBasedMinecraftMod.proxy.isServerRunning())
         {
             return;
         }
@@ -374,12 +398,20 @@ public class Battle
         } catch(NullPointerException exception) {
             entityInfo = null;
         }
+        if (entityInfo == null) {
+            // Check if entity is CustomNPC entity.
+            try {
+                entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getCustomEntityInfoReference(OtherModHandler.getCustomNPCName(e));
+            } catch(NullPointerException exception) {
+                entityInfo = null;
+            }
+        }
         if(entityInfo == null)
         {
             entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getMatchingEntityInfo(e);
         }
 
-        if(entityInfo == null && !(e instanceof PlayerEntity) && TurnBasedMinecraftMod.proxy.isServerRunning())
+        if(isServer && entityInfo == null && !(e instanceof PlayerEntity) && TurnBasedMinecraftMod.proxy.isServerRunning())
         {
             return;
         }
index d76a4303c77ce2a7308672feebc3c76ef286b4c6..de171c1b144f6cea472ab410ddbf59188f284396 100644 (file)
@@ -61,6 +61,11 @@ public class BattleManager
         } catch (NullPointerException e) {
             receiverCustomName = null;
         }
+        if (receiverCustomName == null) {
+            // If entity does not have a "custom name", see if it is a CustomNPC and get its name.
+            receiverCustomName = OtherModHandler.getCustomNPCName(event.getEntity());
+        }
+
         String attackerClassName;
         try {
             attackerClassName = event.getSource().getEntity().getClass().getName();
@@ -73,6 +78,10 @@ public class BattleManager
         } catch (NullPointerException e) {
             attackerCustomName = null;
         }
+        if (attackerCustomName == null) {
+            // If entity does not have a "custom name", see if it is a CustomNPC and get its name.
+            attackerCustomName = OtherModHandler.getCustomNPCName(event.getSource().getEntity());
+        }
 
         // verify that both entities are EntityPlayer and not in creative or has a corresponding EntityInfo
         if(!((event.getEntity() instanceof PlayerEntity && !((PlayerEntity)event.getEntity()).isCreative())
@@ -190,12 +199,20 @@ public class BattleManager
         } catch (NullPointerException e) {
             targetedCustomName = null;
         }
+        if (targetedCustomName == null) {
+            // If entity does not have a "custom name", see if it is a CustomNPC and get its name.
+            targetedCustomName = OtherModHandler.getCustomNPCName(event.getEntity());
+        }
         String attackerCustomName;
         try {
             attackerCustomName = event.getEntity().getCustomName().getString();
         } catch (NullPointerException e) {
             attackerCustomName = null;
         }
+        if (attackerCustomName == null) {
+            // If entity does not have a "custom name", see if it is a CustomNPC and get its name.
+            attackerCustomName = OtherModHandler.getCustomNPCName(event.getEntity());
+        }
 
         EntityInfo attackerInfo = TurnBasedMinecraftMod.proxy.getConfig().getCustomEntityInfoReference(attackerCustomName);
         if(attackerInfo == null)
@@ -366,4 +383,4 @@ public class BattleManager
     public static Collection<ItemGroup> getOtherFoodItemGroups() {
         return otherFoodItemGroups;
     }
-}
\ No newline at end of file
+}
index 5670b6ad42a66c7e234d517b681dc67c2b788e22..d6cc8ce0ce431e439ba2006e897ddd38677a8d47 100644 (file)
@@ -4,10 +4,21 @@ import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 
+import net.minecraft.entity.Entity;
 import net.minecraftforge.eventbus.api.EventPriority;
 import net.minecraftforge.eventbus.api.IEventBus;
 
 public class OtherModHandler {
+
+    private static boolean customNPCsExists = false;
+    private static Object NpcAPIObject = null;
+    private static Class<?> NpcAPIClass = null;
+    private static Method NpcAPI_getEntity = null;
+    private static Class<?> ICustomNPCClass = null;
+    private static Method ICustomNPC_getDisplayMethod = null;
+    private static Class<?> INPCDisplayClass = null;
+    private static Method INPCDisplay_getNameMethod = null;
+
     public OtherModHandler() {
     }
 
@@ -15,13 +26,12 @@ public class OtherModHandler {
         // 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.api.NpcAPI");
+                NpcAPIClass = Class.forName("noppes.npcs.api.NpcAPI");
             } catch (ClassNotFoundException e) {
                 TurnBasedMinecraftMod.logger.info("NpcAPI not found, not handling it.");
             }
-            if (customNPCsAPI == null) {
+            if (NpcAPIClass == null) {
                 break;
             }
 
@@ -96,12 +106,11 @@ public class OtherModHandler {
             }
 
             // Check if available
-            Object instance = null;
             try {
-                Method instanceMethod = customNPCsAPI.getMethod("Instance");
-                instance = instanceMethod.invoke(null);
-                if (!customNPCsAPI.isInstance(instance)) {
-                    instance = null;
+                Method instanceMethod = NpcAPIClass.getMethod("Instance");
+                NpcAPIObject = instanceMethod.invoke(null);
+                if (!NpcAPIClass.isInstance(NpcAPIObject)) {
+                    NpcAPIObject = null;
                     TurnBasedMinecraftMod.logger.error("NpcAPI.Instance() is not NpcAPI!");
                 }
             } catch (NoSuchMethodException e) {
@@ -111,14 +120,14 @@ public class OtherModHandler {
             } catch (IllegalAccessException e) {
                 TurnBasedMinecraftMod.logger.error("Failed to call NpcAPI.Instance(), IllegalAccessException!");
             }
-            if (instance == null) {
+            if (NpcAPIObject == null) {
                 break;
             }
 
             Boolean isAvailable = false;
             try {
-                Method isAvailableMethod = customNPCsAPI.getMethod("IsAvailable");
-                isAvailable = (Boolean)isAvailableMethod.invoke(instance);
+                Method isAvailableMethod = NpcAPIClass.getMethod("IsAvailable");
+                isAvailable = (Boolean)isAvailableMethod.invoke(NpcAPIObject);
             } catch (NoSuchMethodException e) {
                 TurnBasedMinecraftMod.logger.warn("NpcAPI.IsAvailable() does not exist!");
             } catch (InvocationTargetException e) {
@@ -137,7 +146,7 @@ public class OtherModHandler {
 
             Method getNPCsEventBusMethod = null;
             try {
-                getNPCsEventBusMethod = customNPCsAPI.getMethod("events");
+                getNPCsEventBusMethod = NpcAPIClass.getMethod("events");
             } catch (NoSuchMethodException e) {
                 TurnBasedMinecraftMod.logger.warn("NpcAPI.events() could not be found!");
             }
@@ -147,7 +156,7 @@ public class OtherModHandler {
 
             IEventBus customNPCsEventBus = null;
             try {
-                customNPCsEventBus = (IEventBus) getNPCsEventBusMethod.invoke(instance);
+                customNPCsEventBus = (IEventBus) getNPCsEventBusMethod.invoke(NpcAPIObject);
             } catch (InvocationTargetException e) {
                 TurnBasedMinecraftMod.logger.warn("Failed to invoke NpcAPI.events(), InvocationTargetException!");
             } catch (IllegalAccessException e) {
@@ -227,6 +236,105 @@ public class OtherModHandler {
                 }
             });
             TurnBasedMinecraftMod.logger.info("Enabled NpcAPI handling of Player damaged event");
+
+            try {
+                NpcAPI_getEntity = NpcAPIClass.getMethod("getIEntity", Entity.class);
+            } catch (NoSuchMethodException e) {
+                TurnBasedMinecraftMod.logger.warn("Failed to reflect NpcAPI.getIEntity() method");
+            }
+            if (NpcAPI_getEntity == null) {
+                break;
+            }
+
+            try {
+                ICustomNPCClass = Class.forName("noppes.npcs.api.entity.ICustomNpc");
+            } catch (ClassNotFoundException e) {
+                TurnBasedMinecraftMod.logger.warn("Failed to reflect ICustomNPC class");
+            }
+            if (ICustomNPCClass == null) {
+                break;
+            }
+
+            try {
+                ICustomNPC_getDisplayMethod = ICustomNPCClass.getMethod("getDisplay");
+            } catch (NoSuchMethodException e) {
+                TurnBasedMinecraftMod.logger.warn("Failed to reflect ICustomNPC.getDisplay() method");
+            }
+            if (ICustomNPC_getDisplayMethod == null) {
+                break;
+            }
+
+            try {
+                INPCDisplayClass = Class.forName("noppes.npcs.api.entity.data.INPCDisplay");
+            } catch (ClassNotFoundException e) {
+                TurnBasedMinecraftMod.logger.warn("Failed to reflect INPCDisplay class");
+            }
+            if (INPCDisplayClass == null) {
+                break;
+            }
+
+            try {
+                INPCDisplay_getNameMethod = INPCDisplayClass.getMethod("getName");
+            } catch (NoSuchMethodException e) {
+                TurnBasedMinecraftMod.logger.warn("Failed to reflect INPCDisplay.getName() method");
+            }
+            if (INPCDisplay_getNameMethod == null) {
+                break;
+            }
+
+            customNPCsExists = true;
+        }
+    }
+
+    public static String getCustomNPCName(Entity entity) {
+        if (customNPCsExists) {
+            Object ientity = null;
+            try {
+                ientity = NpcAPI_getEntity.invoke(NpcAPIObject, entity);
+            } catch (InvocationTargetException e) {
+                TurnBasedMinecraftMod.logger.debug("Cannot getCustomNPCName, NpcAPI.getEntity(...) InvocationTargetException");
+            } catch (IllegalAccessException e) {
+                TurnBasedMinecraftMod.logger.debug("Cannot getCustomNPCName, NpcAPI.getEntity(...) IllegalAccessException");
+            }
+            if (ientity == null) {
+                return null;
+            }
+
+            if (!ICustomNPCClass.isInstance(ientity)) {
+                TurnBasedMinecraftMod.logger.debug("Cannot getCustomNPCName, entity is not ICustomNPC!");
+                return null;
+            }
+            Object objINPCDisplay = null;
+            try {
+                objINPCDisplay = ICustomNPC_getDisplayMethod.invoke(ientity);
+            } catch (InvocationTargetException e) {
+                TurnBasedMinecraftMod.logger.error("Failed to get INPCDisplay object, InvocationTargetException!");
+            } catch (IllegalAccessException e) {
+                TurnBasedMinecraftMod.logger.error("Failed to get INPCDisplay object, IllegalAccessException!");
+            }
+            if (!INPCDisplayClass.isInstance(objINPCDisplay)) {
+                TurnBasedMinecraftMod.logger.debug("Cannot getCustomNPCName, ientity object is not ICustomNPC!");
+                return null;
+            }
+
+            String name = null;
+            try {
+                name = (String)INPCDisplay_getNameMethod.invoke(objINPCDisplay);
+            } catch (InvocationTargetException e) {
+                TurnBasedMinecraftMod.logger.error("Failed to get INPCDisplay name, InvocationTargetException!");
+            } catch (IllegalAccessException e) {
+                TurnBasedMinecraftMod.logger.error("Failed to get INPCDisplay name, IllegalAccessException!");
+            } catch (ClassCastException e) {
+                TurnBasedMinecraftMod.logger.error("Failed to get INPCDisplay name, ClassCastException!");
+            }
+
+            if (name == null) {
+                TurnBasedMinecraftMod.logger.debug("Cannot getCustomNPCName, got null name!");
+            }
+            return name;
+        } else {
+            TurnBasedMinecraftMod.logger.debug("Cannot getCustomNPCName, reflected classes/methods not loaded!");
+            return null;
         }
     }
 }
index aeb25a59331784957f2220bebe909ca7d8fa932e..77d8abb8fbc593b3b736f08e355daf2dc8328f7a 100644 (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.5";
+    public static final String VERSION = "1.17.2.6";
     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/";
index 282a11c628e83f0713c2b0e2eef77f4f364a85a9..3d2a272072eb28dbf23c71a7d6ea000524136110 100644 (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.5" #mandatory
+version="1.17.2.6" #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>
index b1e252e40c9ba2458a30383e2f1c30baaac18858..49f6767ae8955587dfd0a8ad72e015efeaf57cb3 100644 (file)
@@ -3,7 +3,7 @@
   "modid": "com_burnedkirby_turnbasedminecraft",
   "name": "Turn Based Minecraft",
   "description": "Changes battles to be turn-based.",
-  "version": "1.17.2.5",
+  "version": "1.17.2.6",
   "mcversion": "1.16.3",
   "url": "",
   "updateUrl": "",