Browse Source

More efficient acrobatics location memory class

t00thpick1 4 years ago
parent
commit
4aa17e61fc

+ 41 - 0
src/main/java/com/gmail/nossr50/datatypes/BlockLocationHistory.java

@@ -0,0 +1,41 @@
+package com.gmail.nossr50.datatypes;
+
+import com.google.common.collect.HashMultiset;
+import org.bukkit.Location;
+
+import java.util.LinkedList;
+
+/**
+ * This class works with the assumption that you only pass in Block Locations.  If locations have differing pitch/yaw, the logic breaks
+ */
+public class BlockLocationHistory {
+    private final LinkedList<Location> limitedSizeOrderedList = new LinkedList<>();
+    private final HashMultiset<Location> lookup = HashMultiset.create();
+    private final int maxSize;
+
+    public BlockLocationHistory(int maxSize) {
+        this.maxSize = maxSize;
+    }
+
+    /**
+     * Adds a block location to the history.  If the history memory would exceed the max size, it will remove the least recently added block location
+     *
+     * @param newItem
+     */
+    public void add(Location newItem) {
+        limitedSizeOrderedList.addFirst(newItem);
+        lookup.add(newItem);
+        if (limitedSizeOrderedList.size() > maxSize)
+            lookup.remove(limitedSizeOrderedList.removeLast());
+    }
+
+    /**
+     * Returns true if the block location is in the recorded history
+     *
+     * @param targetLoc the block location to search for
+     * @return true if the block location is in the recorded history
+     */
+    public boolean contains(Location targetLoc) {
+        return lookup.contains(targetLoc);
+    }
+}

+ 0 - 57
src/main/java/com/gmail/nossr50/datatypes/LimitedSizeList.java

@@ -1,57 +0,0 @@
-package com.gmail.nossr50.datatypes;
-
-
-import org.bukkit.Location;
-
-public class LimitedSizeList {
-    public Location[] limitedSizeOrderedList;
-    private final int size;
-
-
-    public LimitedSizeList(int size)
-    {
-        this.size = size;
-        limitedSizeOrderedList = new Location[size];
-    }
-
-    /**
-     * Adds objects to our limited size ordered list
-     * New objects are added to the front
-     * @param newItem
-     */
-    public void add(Location newItem)
-    {
-        Location[] newList = new Location[size];
-
-        for(int i = 0; i < size-1; i++)
-        {
-            if(i != 0)
-                newList[i] = limitedSizeOrderedList[i-1];
-            else
-                newList[i] = newItem;
-        }
-
-        limitedSizeOrderedList = newList;
-    }
-
-    /**
-     * Returns true if the object is anywhere in our list
-     * @param targetLoc the object to check for
-     * @return true if the object is in our list
-     */
-    public boolean contains(Location targetLoc)
-    {
-        for(Location iter : limitedSizeOrderedList)
-        {
-            if(iter == null)
-                continue;
-
-            if(iter.getX() == targetLoc.getX()
-                    && iter.getY() == targetLoc.getY()
-                    && iter.getZ() == targetLoc.getZ())
-                return true;
-        }
-
-        return false;
-    }
-}

+ 3 - 3
src/main/java/com/gmail/nossr50/skills/acrobatics/AcrobaticsManager.java

@@ -1,7 +1,7 @@
 package com.gmail.nossr50.skills.acrobatics;
 package com.gmail.nossr50.skills.acrobatics;
 
 
 import com.gmail.nossr50.config.experience.ExperienceConfig;
 import com.gmail.nossr50.config.experience.ExperienceConfig;
-import com.gmail.nossr50.datatypes.LimitedSizeList;
+import com.gmail.nossr50.datatypes.BlockLocationHistory;
 import com.gmail.nossr50.datatypes.experience.XPGainReason;
 import com.gmail.nossr50.datatypes.experience.XPGainReason;
 import com.gmail.nossr50.datatypes.interactions.NotificationType;
 import com.gmail.nossr50.datatypes.interactions.NotificationType;
 import com.gmail.nossr50.datatypes.player.McMMOPlayer;
 import com.gmail.nossr50.datatypes.player.McMMOPlayer;
@@ -28,13 +28,13 @@ public class AcrobaticsManager extends SkillManager {
 
 
     public AcrobaticsManager(McMMOPlayer mcMMOPlayer) {
     public AcrobaticsManager(McMMOPlayer mcMMOPlayer) {
         super(mcMMOPlayer, PrimarySkillType.ACROBATICS);
         super(mcMMOPlayer, PrimarySkillType.ACROBATICS);
-        fallLocationMap = new LimitedSizeList(50);
+        fallLocationMap = new BlockLocationHistory(50);
     }
     }
 
 
     private long rollXPCooldown = 0;
     private long rollXPCooldown = 0;
     private final long rollXPInterval = (1000 * 3); //1 Minute
     private final long rollXPInterval = (1000 * 3); //1 Minute
     private long rollXPIntervalLengthen = (1000 * 10); //10 Seconds
     private long rollXPIntervalLengthen = (1000 * 10); //10 Seconds
-    private final LimitedSizeList fallLocationMap;
+    private final BlockLocationHistory fallLocationMap;
 
 
     public boolean hasFallenInLocationBefore(Location location)
     public boolean hasFallenInLocationBefore(Location location)
     {
     {