LocationCommands.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package me.km.snuviscript.commands;
  2. import me.hammerle.snuviscript.code.ScriptManager;
  3. import me.km.utils.Location;
  4. import me.km.utils.LocationIterator;
  5. import me.km.utils.Utils;
  6. import net.minecraft.util.math.BlockPos;
  7. import net.minecraft.util.math.MathHelper;
  8. import net.minecraft.world.IWorld;
  9. import net.minecraft.world.World;
  10. public class LocationCommands {
  11. @SuppressWarnings("")
  12. public static void registerFunctions(ScriptManager sm) {
  13. sm.registerFunction("loc.new", (sc, in) -> {
  14. if(in.length >= 6) {
  15. return new Location((World) in[0].get(sc),
  16. in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc),
  17. in[4].getFloat(sc), in[5].getFloat(sc));
  18. }
  19. return new Location((World) in[0].get(sc), in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc), 0, 0);
  20. });
  21. sm.registerConsumer("loc.setblockpos", (sc, in) -> {
  22. Location l = (Location) in[0].get(sc);
  23. BlockPos pos = (BlockPos) in[1].get(sc);
  24. l.set(pos.getX(), pos.getY(), pos.getZ());
  25. });
  26. sm.registerFunction("loc.getx", (sc, in) -> ((Location) in[0].get(sc)).getX());
  27. sm.registerFunction("loc.gety", (sc, in) -> ((Location) in[0].get(sc)).getY());
  28. sm.registerFunction("loc.getz", (sc, in) -> ((Location) in[0].get(sc)).getZ());
  29. sm.registerConsumer("loc.set", (sc, in) -> ((Location) in[0].get(sc)).set(in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc)));
  30. sm.registerConsumer("loc.setx", (sc, in) -> ((Location) in[0].get(sc)).setX(in[1].getDouble(sc)));
  31. sm.registerConsumer("loc.sety", (sc, in) -> ((Location) in[0].get(sc)).setY(in[1].getDouble(sc)));
  32. sm.registerConsumer("loc.setz", (sc, in) -> ((Location) in[0].get(sc)).setZ(in[1].getDouble(sc)));
  33. sm.registerConsumer("loc.add", (sc, in) -> ((Location) in[0].get(sc)).add(in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc)));
  34. sm.registerConsumer("loc.addx", (sc, in) -> ((Location) in[0].get(sc)).addX(in[1].getDouble(sc)));
  35. sm.registerConsumer("loc.addy", (sc, in) -> ((Location) in[0].get(sc)).addY(in[1].getDouble(sc)));
  36. sm.registerConsumer("loc.addz", (sc, in) -> ((Location) in[0].get(sc)).addZ(in[1].getDouble(sc)));
  37. sm.registerConsumer("loc.setyaw", (sc, in) -> ((Location) in[0].get(sc)).setYaw(in[1].getFloat(sc)));
  38. sm.registerFunction("loc.getyaw", (sc, in) -> (double) ((Location) in[0].get(sc)).getYaw());
  39. sm.registerConsumer("loc.setpitch", (sc, in) -> ((Location) in[0].get(sc)).setPitch(in[1].getFloat(sc)));
  40. sm.registerFunction("loc.getpitch", (sc, in) -> (double) ((Location) in[0].get(sc)).getPitch());
  41. sm.registerFunction("loc.getworld", (sc, in) -> ((Location) in[0].get(sc)).getWorld());
  42. sm.registerFunction("loc.distance", (sc, in) -> ((Location) in[0].get(sc)).getPos().distanceTo(((Location) in[1].get(sc)).getPos()));
  43. sm.registerFunction("loc.mod", (sc, in) -> ((Location) in[0].get(sc)).copyAdd(in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc)));
  44. sm.registerFunction("loc.getcoord", (sc, in) -> {
  45. Location l = (Location) in[0].get(sc);
  46. switch(in[1].getString(sc)) {
  47. case "x":
  48. return l.getX();
  49. case "y":
  50. return l.getY();
  51. case "z":
  52. return l.getZ();
  53. case "bx":
  54. return (double) MathHelper.floor(l.getX());
  55. case "by":
  56. return (double) MathHelper.floor(l.getY());
  57. case "bz":
  58. return (double) MathHelper.floor(l.getZ());
  59. case "w":
  60. return Utils.getWorldName(l.getWorld());
  61. default:
  62. return null;
  63. }
  64. });
  65. sm.registerFunction("loc.isbetween", (sc, in) -> {
  66. Location l1 = (Location) in[0].get(sc);
  67. Location l2 = (Location) in[1].get(sc);
  68. Location l3 = (Location) in[2].get(sc);
  69. return l1.getX() >= Math.min(l2.getX(), l3.getX()) && l1.getX() <= Math.max(l2.getX(), l3.getX())
  70. && l1.getY() >= Math.min(l2.getY(), l3.getY()) && l1.getY() <= Math.max(l2.getY(), l3.getY())
  71. && l1.getZ() >= Math.min(l2.getZ(), l3.getZ()) && l1.getZ() <= Math.max(l2.getZ(), l3.getZ());
  72. });
  73. sm.registerConsumer("loc.sort", (sc, in) -> {
  74. Location l1 = (Location) in[0].get(sc);
  75. Location l2 = (Location) in[1].get(sc);
  76. if(l1.getX() > l2.getX()) {
  77. double tmp = l1.getX();
  78. l1.setX(l2.getX());
  79. l2.setX(tmp);
  80. }
  81. if(l1.getY() > l2.getY()) {
  82. double tmp = l1.getY();
  83. l1.setY(l2.getY());
  84. l2.setY(tmp);
  85. }
  86. if(l1.getZ() > l2.getZ()) {
  87. double tmp = l1.getZ();
  88. l1.setZ(l2.getZ());
  89. l2.setZ(tmp);
  90. }
  91. });
  92. sm.registerFunction("loc.iterator", (sc, in) -> {
  93. return new LocationIterator((World) in[0].get(sc),
  94. in[1].getInt(sc), in[2].getInt(sc), in[3].getInt(sc),
  95. in[4].getInt(sc), in[5].getInt(sc), in[6].getInt(sc));
  96. });
  97. sm.registerFunction("loc.trace", (sc, in) -> {
  98. Location l = (Location) in[0].get(sc);
  99. IWorld w = l.getWorld();
  100. double x = l.getX();
  101. double y = l.getY();
  102. double z = l.getZ();
  103. BlockPos.Mutable pos = new BlockPos.Mutable(x, y, z);
  104. double ux = in[1].getDouble(sc);
  105. double uy = in[2].getDouble(sc);
  106. double uz = in[3].getDouble(sc);
  107. int steps = in[4].getInt(sc);
  108. boolean last = in[5].getBoolean(sc);
  109. for(int i = 0; i < steps; i++) {
  110. if(!w.isAirBlock(pos)) {
  111. if(last) {
  112. x -= ux;
  113. y -= uy;
  114. z -= uz;
  115. }
  116. l.set(x, y, z);
  117. return true;
  118. }
  119. x += ux;
  120. y += uy;
  121. z += uz;
  122. pos.setPos(x, y, z);
  123. }
  124. return false;
  125. });
  126. }
  127. }