Browse Source

new snuvi commands

Kajetan Johannes Hammerle 4 years ago
parent
commit
9c2680bea6

+ 46 - 8
src/main/java/me/km/snuviscript/commands/BlockCommands.java

@@ -27,6 +27,48 @@ import net.minecraft.world.IWorld;
 import net.minecraft.world.World;
 
 public class BlockCommands {
+    private static class Offset {
+        private final int x;
+        private final int y;
+        private final int z;
+
+        public Offset(int x, int y, int z) {
+            this.x = x;
+            this.y = y;
+            this.z = z;
+        }
+    }
+
+    private static final Offset[] OFFSETS = new Offset[]{
+        //new Offset(-1, -1, -1),
+        new Offset(0, -1, -1),
+        //new Offset(1, -1, -1),
+        new Offset(-1, 0, -1),
+        new Offset(0, 0, -1),
+        new Offset(1, 0, -1),
+        //new Offset(-1, 1, -1),
+        new Offset(0, 1, -1),
+        //new Offset(1, 1, -1),
+
+        new Offset(-1, -1, 0),
+        new Offset(0, -1, 0),
+        new Offset(1, -1, 0),
+        new Offset(-1, 0, 0),
+        new Offset(0, 0, 0),
+        new Offset(1, 0, 0),
+        new Offset(-1, 1, 0),
+        new Offset(0, 1, 0),
+        new Offset(1, 1, 0),
+        //new Offset(-1, -1, 1),
+        new Offset(0, -1, 1),
+        //new Offset(1, -1, 1),
+        new Offset(-1, 0, 1),
+        new Offset(0, 0, 1),
+        new Offset(1, 0, 1),
+        //new Offset(-1, 1, 1),
+        new Offset(0, 1, 1), //new Offset(1, 1, 1),
+    };
+
     public static void registerFunctions(ScriptManager sm) {
         sm.registerFunction("block.gettag", (sc, in) -> BlockTags.getCollection().get(new ResourceLocation(in[0].getString(sc))));
         sm.registerFunction("block.hastag", (sc, in) -> ((Tag<Block>) in[0].get(sc)).contains((Block) in[1].get(sc)));
@@ -46,14 +88,10 @@ public class BlockCommands {
             int oy = pos.getY();
             int oz = pos.getZ();
             double counter = 0;
-            for(int x = -1; x <= 1; x++) {
-                for(int y = -1; y <= 1; y++) {
-                    for(int z = -1; z <= 1; z++) {
-                        pos.setPos(ox + x, oy + y, oz + z);
-                        if(w.isAirBlock(pos)) {
-                            counter++;
-                        }
-                    }
+            for(Offset off : OFFSETS) {
+                pos.setPos(ox + off.x, oy + off.y, oz + off.z);
+                if(w.isAirBlock(pos)) {
+                    counter++;
                 }
             }
             return counter;

+ 38 - 0
src/main/java/me/km/snuviscript/commands/LocationCommands.java

@@ -2,9 +2,11 @@ package me.km.snuviscript.commands;
 
 import me.hammerle.snuviscript.code.ScriptManager;
 import me.km.utils.Location;
+import me.km.utils.LocationIterator;
 import me.km.world.WorldManager;
 import net.minecraft.util.math.BlockPos;
 import net.minecraft.util.math.MathHelper;
+import net.minecraft.world.IWorld;
 import net.minecraft.world.World;
 
 public class LocationCommands {
@@ -26,9 +28,11 @@ public class LocationCommands {
         sm.registerFunction("loc.getx", (sc, in) -> ((Location) in[0].get(sc)).getX());
         sm.registerFunction("loc.gety", (sc, in) -> ((Location) in[0].get(sc)).getY());
         sm.registerFunction("loc.getz", (sc, in) -> ((Location) in[0].get(sc)).getZ());
+        sm.registerConsumer("loc.set", (sc, in) -> ((Location) in[0].get(sc)).set(in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc)));
         sm.registerConsumer("loc.setx", (sc, in) -> ((Location) in[0].get(sc)).setX(in[1].getDouble(sc)));
         sm.registerConsumer("loc.sety", (sc, in) -> ((Location) in[0].get(sc)).setY(in[1].getDouble(sc)));
         sm.registerConsumer("loc.setz", (sc, in) -> ((Location) in[0].get(sc)).setZ(in[1].getDouble(sc)));
+        sm.registerConsumer("loc.add", (sc, in) -> ((Location) in[0].get(sc)).add(in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc)));
         sm.registerConsumer("loc.addx", (sc, in) -> ((Location) in[0].get(sc)).addX(in[1].getDouble(sc)));
         sm.registerConsumer("loc.addy", (sc, in) -> ((Location) in[0].get(sc)).addY(in[1].getDouble(sc)));
         sm.registerConsumer("loc.addz", (sc, in) -> ((Location) in[0].get(sc)).addZ(in[1].getDouble(sc)));
@@ -87,5 +91,39 @@ public class LocationCommands {
                 l2.setZ(tmp);
             }
         });
+        sm.registerFunction("loc.iterator", (sc, in) -> {
+            return new LocationIterator((IWorld) in[0].get(sc),
+                    in[1].getInt(sc), in[2].getInt(sc), in[3].getInt(sc),
+                    in[4].getInt(sc), in[5].getInt(sc), in[6].getInt(sc));
+        });
+        sm.registerFunction("loc.trace", (sc, in) -> {
+            Location l = (Location) in[0].get(sc);
+            IWorld w = l.getWorld();
+            double x = l.getX();
+            double y = l.getY();
+            double z = l.getZ();
+            BlockPos.Mutable pos = new BlockPos.Mutable(x, y, z);
+            double ux = in[1].getDouble(sc);
+            double uy = in[2].getDouble(sc);
+            double uz = in[3].getDouble(sc);
+            int steps = in[4].getInt(sc);
+            boolean last = in[5].getBoolean(sc);
+            for(int i = 0; i < steps; i++) {
+                if(!w.isAirBlock(pos)) {
+                    if(last) {
+                        x -= ux;
+                        y -= uy;
+                        z -= uz;
+                    }
+                    l.set(x, y, z);
+                    return true;
+                }
+                x += ux;
+                y += uy;
+                z += uz;
+                pos.setPos(x, y, z);
+            }
+            return false;
+        });
     }
 }

+ 50 - 0
src/main/java/me/km/utils/LocationIterator.java

@@ -0,0 +1,50 @@
+package me.km.utils;
+
+import java.util.Iterator;
+import net.minecraft.world.IWorld;
+
+public class LocationIterator implements Iterator<Location> {
+    private final int minX;
+    private final int minY;
+    private final int maxX;
+    private final int maxY;
+    private final int maxZ;
+
+    private final Location current;
+    private int x;
+    private int y;
+    private int z;
+
+    public LocationIterator(IWorld world, int minX, int minY, int minZ, int maxX, int maxY, int maxZ) {
+        this.minX = minX;
+        this.minY = minY;
+        this.maxX = maxX;
+        this.maxY = maxY;
+        this.maxZ = maxZ;
+
+        current = new Location(world, minX, minY, minZ, 0, 0);
+        x = minX;
+        y = minY;
+        z = minZ;
+    }
+
+    @Override
+    public boolean hasNext() {
+        return z <= maxZ;
+    }
+
+    @Override
+    public Location next() {
+        current.set(x, y, z);
+        x++;
+        if(x > maxX) {
+            x = minX;
+            y++;
+            if(y > maxY) {
+                y = minY;
+                z++;
+            }
+        }
+        return current;
+    }
+}