LocationCommands.java 7.1 KB

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