LocationCommands.java 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package me.km.snuviscript.commands;
  2. import me.hammerle.snuviscript.code.ScriptManager;
  3. import me.km.utils.Location;
  4. import me.km.world.WorldManager;
  5. import net.minecraft.util.math.BlockPos;
  6. import net.minecraft.util.math.MathHelper;
  7. import net.minecraft.world.World;
  8. public class LocationCommands {
  9. @SuppressWarnings("")
  10. public static void registerFunctions(ScriptManager sm) {
  11. sm.registerFunction("loc.new", (sc, in) -> {
  12. if(in.length >= 6) {
  13. return new Location((World) in[0].get(sc),
  14. in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc),
  15. in[4].getFloat(sc), in[5].getFloat(sc));
  16. }
  17. return new Location((World) in[0].get(sc), in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc), 0, 0);
  18. });
  19. sm.registerConsumer("loc.setblockpos", (sc, in) -> {
  20. Location l = (Location) in[0].get(sc);
  21. BlockPos pos = (BlockPos) in[1].get(sc);
  22. l.set(pos.getX(), pos.getY(), pos.getZ());
  23. });
  24. sm.registerFunction("loc.getx", (sc, in) -> ((Location) in[0].get(sc)).getX());
  25. sm.registerFunction("loc.gety", (sc, in) -> ((Location) in[0].get(sc)).getY());
  26. sm.registerFunction("loc.getz", (sc, in) -> ((Location) in[0].get(sc)).getZ());
  27. sm.registerConsumer("loc.setx", (sc, in) -> ((Location) in[0].get(sc)).setX(in[1].getDouble(sc)));
  28. sm.registerConsumer("loc.sety", (sc, in) -> ((Location) in[0].get(sc)).setY(in[1].getDouble(sc)));
  29. sm.registerConsumer("loc.setz", (sc, in) -> ((Location) in[0].get(sc)).setZ(in[1].getDouble(sc)));
  30. sm.registerConsumer("loc.addx", (sc, in) -> ((Location) in[0].get(sc)).addX(in[1].getDouble(sc)));
  31. sm.registerConsumer("loc.addy", (sc, in) -> ((Location) in[0].get(sc)).addY(in[1].getDouble(sc)));
  32. sm.registerConsumer("loc.addz", (sc, in) -> ((Location) in[0].get(sc)).addZ(in[1].getDouble(sc)));
  33. sm.registerConsumer("loc.setyaw", (sc, in) -> ((Location) in[0].get(sc)).setYaw(in[1].getFloat(sc)));
  34. sm.registerFunction("loc.getyaw", (sc, in) -> (double) ((Location) in[0].get(sc)).getYaw());
  35. sm.registerConsumer("loc.setpitch", (sc, in) -> ((Location) in[0].get(sc)).setPitch(in[1].getFloat(sc)));
  36. sm.registerFunction("loc.getpitch", (sc, in) -> (double) ((Location) in[0].get(sc)).getPitch());
  37. sm.registerFunction("loc.getworld", (sc, in) -> ((Location) in[0].get(sc)).getWorld());
  38. sm.registerFunction("loc.distance", (sc, in) -> ((Location) in[0].get(sc)).getPos().distanceTo(((Location) in[1].get(sc)).getPos()));
  39. sm.registerFunction("loc.mod", (sc, in) -> ((Location) in[0].get(sc)).copyAdd(in[1].getDouble(sc), in[2].getDouble(sc), in[3].getDouble(sc)));
  40. sm.registerFunction("loc.getcoord", (sc, in) -> {
  41. Location l = (Location) in[0].get(sc);
  42. switch(in[1].getString(sc)) {
  43. case "x":
  44. return l.getX();
  45. case "y":
  46. return l.getY();
  47. case "z":
  48. return l.getZ();
  49. case "bx":
  50. return (double) MathHelper.floor(l.getX());
  51. case "by":
  52. return (double) MathHelper.floor(l.getY());
  53. case "bz":
  54. return (double) MathHelper.floor(l.getZ());
  55. case "w":
  56. return WorldManager.getName(l.getWorld());
  57. default:
  58. return null;
  59. }
  60. });
  61. sm.registerFunction("loc.isbetween", (sc, in) -> {
  62. Location l1 = (Location) in[0].get(sc);
  63. Location l2 = (Location) in[1].get(sc);
  64. Location l3 = (Location) in[2].get(sc);
  65. return l1.getX() >= Math.min(l2.getX(), l3.getX()) && l1.getX() <= Math.max(l2.getX(), l3.getX())
  66. && l1.getY() >= Math.min(l2.getY(), l3.getY()) && l1.getY() <= Math.max(l2.getY(), l3.getY())
  67. && l1.getZ() >= Math.min(l2.getZ(), l3.getZ()) && l1.getZ() <= Math.max(l2.getZ(), l3.getZ());
  68. });
  69. sm.registerConsumer("loc.sort", (sc, in) -> {
  70. Location l1 = (Location) in[0].get(sc);
  71. Location l2 = (Location) in[1].get(sc);
  72. if(l1.getX() > l2.getX()) {
  73. double tmp = l1.getX();
  74. l1.setX(l2.getX());
  75. l2.setX(tmp);
  76. }
  77. if(l1.getY() > l2.getY()) {
  78. double tmp = l1.getY();
  79. l1.setY(l2.getY());
  80. l2.setY(tmp);
  81. }
  82. if(l1.getZ() > l2.getZ()) {
  83. double tmp = l1.getZ();
  84. l1.setZ(l2.getZ());
  85. l2.setZ(tmp);
  86. }
  87. });
  88. }
  89. }