浏览代码

iterator update

Kajetan Johannes Hammerle 5 年之前
父节点
当前提交
e41f50e683

+ 70 - 0
src/main/java/me/km/plots/PlotMap.java

@@ -1,6 +1,7 @@
 package me.km.plots;
 
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 import java.util.UUID;
 import java.util.function.Predicate;
@@ -319,4 +320,73 @@ public class PlotMap
             }
         }
     }
+    
+    public Iterator<Plot> getIterator()
+    {
+        return new Iterator<PlotMap.Plot>()
+        {
+            private Plot current = last;
+            
+            @Override
+            public boolean hasNext()
+            {
+                return current != null;
+            }
+
+            @Override
+            public Plot next()
+            {
+                Plot p = current;
+                current = current.previous;
+                return p;
+            }
+        };
+    }
+    
+    public Iterator<Plot> getIterator(UUID uuid)
+    {
+        return new Iterator<PlotMap.Plot>()
+        {
+            private Plot current = last;
+            
+            private boolean gotoNext()
+            {
+                if(current == null)
+                {
+                    return false;
+                }
+                if(current.getOwners().contains(uuid))
+                {
+                    return true;
+                }
+                while(current.previous != null)
+                {
+                    current = current.previous;
+                    if(current.getOwners().contains(uuid))
+                    {
+                        return true;
+                    }
+                }
+                return false;
+            }
+            
+            @Override
+            public boolean hasNext()
+            {
+                return gotoNext();
+            }
+
+            @Override
+            public Plot next()
+            {
+                if(!gotoNext())
+                {
+                    throw new IllegalStateException("next without a next element");
+                }
+                Plot p = current;
+                current = current.previous;
+                return p;
+            }
+        };
+    }
 }

+ 23 - 1
src/main/java/me/km/plots/WorldPlotMap.java

@@ -1,7 +1,9 @@
 package me.km.plots;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.UUID;
 import me.km.plots.PlotMap.Plot;
@@ -76,7 +78,7 @@ public class WorldPlotMap
         return map.getPlotAt(pos.getX(), pos.getY(), pos.getZ());
     }
     
-    public Plot add(IWorld w, BlockPos pos1, BlockPos pos2)
+    public PlotMap.Plot add(IWorld w, BlockPos pos1, BlockPos pos2)
     {
         PlotMap map = maps.get(w);
         if(map == null)
@@ -95,4 +97,24 @@ public class WorldPlotMap
             map.remove(p);
         }
     }
+    
+    public Iterator<PlotMap.Plot> getIterator(IWorld w)
+    {
+        PlotMap map = maps.get(w);
+        if(map != null)
+        {
+            return map.getIterator();
+        }
+        return Collections.EMPTY_LIST.iterator();
+    }
+    
+    public Iterator<PlotMap.Plot> getIterator(IWorld w, UUID uuid)
+    {
+        PlotMap map = maps.get(w);
+        if(map != null)
+        {
+            return map.getIterator(uuid);
+        }
+        return Collections.EMPTY_LIST.iterator();
+    }
 }

+ 9 - 0
src/main/java/me/km/snuviscript/MinecraftFunctions.java

@@ -1847,6 +1847,15 @@ public class MinecraftFunctions
             return Void.TYPE;
         });
         sm.registerFunction("plot.getid", (sc, in) -> (double) ((Plot) in[0].get(sc)).getId());
+        sm.registerFunction("plot.iterator", (sc, in) -> 
+        {
+            IWorld word = (IWorld) in[0].get(sc);
+            if(in.length >= 2)
+            {
+                plots.getIterator(word, getUUID(in[1].get(sc)));
+            }
+            return plots.getIterator(word);
+        });
         
         // ---------------------------------------------------------------------  
         // block protection library