]> git.seodisparate.com - TurnBasedMinecraftMod/commitdiff
Fixes, add maximum-distance to aggro-start-battle
authorStephen Seo <seo.disparate@gmail.com>
Thu, 18 Oct 2018 07:26:09 +0000 (16:26 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 18 Oct 2018 07:26:09 +0000 (16:26 +0900)
Ugly fix for still not found freeze bug in BattleUpdater.
Added maximum-distance for monsters initiating battle when targeting a
player or entity in battle.
Updated config version to 6.

src/main/java/com/seodisparate/TurnBasedMinecraft/common/AttackEventHandler.java
src/main/java/com/seodisparate/TurnBasedMinecraft/common/Battle.java
src/main/java/com/seodisparate/TurnBasedMinecraft/common/BattleUpdater.java
src/main/java/com/seodisparate/TurnBasedMinecraft/common/Config.java
src/main/java/com/seodisparate/TurnBasedMinecraft/common/Utility.java
src/main/resources/assets/TurnBasedMinecraft/TBM_Config.xml

index 014f81adeff443ff7946e7af0ac532fd77727c7d..29f08835b9bf27884af2c43e51a4ad91f7880165 100644 (file)
@@ -95,7 +95,8 @@ public class AttackEventHandler
         if(event.getEntity().world.isRemote
                 || config.isOldBattleBehaviorEnabled()
                 || (event.getEntity() != null && battleManager.isRecentlyLeftBattle(event.getEntity().getEntityId()))
-                || (event.getTarget() != null && battleManager.isRecentlyLeftBattle(event.getTarget().getEntityId())))
+                || (event.getTarget() != null && battleManager.isRecentlyLeftBattle(event.getTarget().getEntityId()))
+                || (event.getEntity() != null && event.getTarget() != null && Utility.distanceBetweenEntities(event.getEntity(), event.getTarget()) > (double)config.getAggroStartBattleDistance()))
         {
             return;
         }
index 065bcf22e9261a48080c3f6e1308dbb13cdb0046..e40671819a82cd080a25f2aa990905bc8755b8d0 100644 (file)
@@ -839,17 +839,14 @@ public class Battle
                         Combatant target = null;
                         if(next.entity instanceof EntityPlayer)
                         {
-                            if(next.isSideA)
+                            target = sideA.get(next.targetEntityID);
+                            if(target == null)
                             {
                                 target = sideB.get(next.targetEntityID);
                             }
-                            else
-                            {
-                                target = sideA.get(next.targetEntityID);
-                            }
-                            if(target == null || !target.entity.isEntityAlive())
+                            if(target == null || !target.entity.isEntityAlive() || target == next)
                             {
-                                break;
+                                continue;
                             }
                             ItemStack heldItemStack = ((EntityPlayer)next.entity).getHeldItemMainhand();
                             if(heldItemStack.getItem() instanceof ItemBow)
@@ -980,7 +977,7 @@ public class Battle
                                     }
                                 }
                             }
-                            if(target == null || !target.entity.isEntityAlive())
+                            if(target == null || !target.entity.isEntityAlive() || target == next)
                             {
                                 continue;
                             }
index 7cec495b674354c19f57be2a6eebd8e8cd74104a..6a497ecf176449424594f239a414f8b1a0bd96d8 100644 (file)
@@ -9,6 +9,35 @@ public class BattleUpdater implements Runnable
     private BattleManager manager;
     private AtomicBoolean isRunning;
     
+    private class UpdateRunnable implements Runnable
+    {
+        private Battle battle;
+        private AtomicBoolean finished = new AtomicBoolean(false);
+        private AtomicBoolean battleFinished = new AtomicBoolean(false);
+        
+        public UpdateRunnable(Battle battle)
+        {
+            this.battle = battle;
+        }
+        
+        public boolean isFinished()
+        {
+            return finished.get();
+        }
+        
+        public boolean isBattleFinished()
+        {
+            return battleFinished.get();
+        }
+
+        @Override
+        public void run()
+        {
+            battleFinished.set(battle.update());
+            finished.set(true);
+        }
+    }
+    
     public BattleUpdater(BattleManager manager)
     {
         this.manager = manager;
@@ -25,7 +54,20 @@ public class BattleUpdater implements Runnable
                 for(Iterator<Map.Entry<Integer, Battle>> iter = manager.battleMap.entrySet().iterator(); iter.hasNext();)
                 {
                     Map.Entry<Integer, Battle> entry = iter.next();
-                    if(entry.getValue().update())
+                    UpdateRunnable updateRunnable = new UpdateRunnable(entry.getValue());
+                    Thread updateThread = new Thread(updateRunnable);
+                    updateThread.start();
+                    try
+                    {
+                        updateThread.join(3000);
+                    } catch(InterruptedException e){ /* exception ignored */ }
+                    if(!updateRunnable.isFinished())
+                    {
+                        // TODO this is an ugly fix to a still-not-found freeze bug in Battle.update()
+                        TurnBasedMinecraftMod.logger.error("Battle (" + entry.getValue().getId() + ") update timed out!");
+                        updateThread.stop();
+                    }
+                    else if(updateRunnable.isBattleFinished())
                     {
                         iter.remove();
                     }
index 143694b09095b6b72738fe47d725ebdc46c7a685..057153d9924c7ae37a642dba5fd27349505d0083 100644 (file)
@@ -49,6 +49,7 @@ public class Config
     private boolean battleDisabledForAll = false;
     private boolean oldBattleBehaviorEnabled = false;
     private int leaveBattleCooldownSeconds = 5;
+    private int aggroStartBattleDistance = 8;
     
     public Config(Logger logger)
     {
@@ -187,6 +188,18 @@ public class Config
                         leaveBattleCooldownSeconds = 10;
                     }
                 }
+                else if(xmlReader.getLocalName().equals("AggroStartBattleDistance"))
+                {
+                    aggroStartBattleDistance = Integer.parseInt(xmlReader.getElementText());
+                    if(aggroStartBattleDistance < 5)
+                    {
+                        aggroStartBattleDistance = 5;
+                    }
+                    else if(aggroStartBattleDistance > 50)
+                    {
+                        aggroStartBattleDistance = 50;
+                    }
+                }
                 else if(xmlReader.getLocalName().equals("OldBattleBehavior"))
                 {
                     if(xmlReader.getElementText().toLowerCase().equals("false"))
@@ -656,4 +669,9 @@ public class Config
     {
         return (long)leaveBattleCooldownSeconds * 1000000000L;
     }
+    
+    public int getAggroStartBattleDistance()
+    {
+        return aggroStartBattleDistance;
+    }
 }
index 73a66d9660cb481f4fe30a0a97193177715d0597..b54b853a7cc90ddb4f145efe68866b51846ba0da 100644 (file)
@@ -1,5 +1,6 @@
 package com.seodisparate.TurnBasedMinecraft.common;
 
+import net.minecraft.entity.Entity;
 import net.minecraft.entity.player.EntityPlayer;
 import net.minecraft.item.ItemArrow;
 
@@ -43,4 +44,9 @@ public class Utility
         }
         return false;
     }
+    
+    public static double distanceBetweenEntities(Entity a, Entity b)
+    {
+        return Math.sqrt(Math.pow(a.posX - b.posX, 2.0) + Math.pow(a.posY - b.posY, 2.0) + Math.pow(a.posZ - b.posZ, 2.0));
+    }
 }
index 17c08380ddd69981eb6e48e3ba6b574c19049c8c..582c1c1a2ec33cd2d35a430261bd12e09db58e33 100644 (file)
@@ -1,8 +1,11 @@
 <TurnBasedMinecraftConfig>
        <!-- If the mod has a newer version config, it will rename the existing config and place the new config -->
-       <Version>5</Version>
+       <Version>6</Version>
        <!-- Number of seconds that an entity cannot enter battle after having just left one. Minimum 1, maximum 10-->
        <LeaveBattleCooldown>5</LeaveBattleCooldown>
+       <!-- Maximum distance for a monster to start battle by targeting a player or other entity in turn-based-battle.
+       Minimum 5, maximum 50. -->
+       <AggroStartBattleDistance>8</AggroStartBattleDistance>
        <!-- If not "false", uses old battle behavior where battles only start on attack/hit. Otherwise, battles can
        start when a hostile mob targets a player or another entity in battle. -->
        <OldBattleBehavior>false</OldBattleBehavior>