WorldCommands.java 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package me.km.snuviscript.commands;
  2. import java.util.ArrayList;
  3. import java.util.stream.Collectors;
  4. import me.hammerle.snuviscript.code.ScriptManager;
  5. import me.km.utils.Location;
  6. import me.km.utils.Mapper;
  7. import me.km.utils.ReflectionUtils;
  8. import me.km.world.WorldManager;
  9. import net.minecraft.server.MinecraftServer;
  10. import net.minecraft.world.Difficulty;
  11. import net.minecraft.world.GameRules;
  12. import net.minecraft.world.World;
  13. import net.minecraft.world.server.ServerWorld;
  14. import net.minecraft.world.storage.WorldInfo;
  15. public class WorldCommands {
  16. @SuppressWarnings("")
  17. public static void registerFunctions(ScriptManager sm, MinecraftServer server) {
  18. sm.registerAlias("players.toworldlist", "world.getplayers");
  19. sm.registerFunction("world.register", (sc, in) -> WorldManager.register(in[0].getString(sc)));
  20. sm.registerFunction("world.unregister", (sc, in) -> WorldManager.unregister(server, in[0].getString(sc)));
  21. sm.registerFunction("world.get", (sc, in) -> WorldManager.get(server, in[0].getString(sc)));
  22. sm.registerFunction("world.getname", (sc, in) -> WorldManager.getName((World) in[0].get(sc)));
  23. sm.registerConsumer("world.setdiffi", (sc, in) -> {
  24. Difficulty diffi = Difficulty.valueOf(in[1].getString(sc).toUpperCase());
  25. ((World) in[0].get(sc)).getWorldInfo().setDifficulty(diffi);
  26. });
  27. sm.registerConsumer("world.setgamerule", (sc, in) -> {
  28. Object o = ((World) in[0].get(sc)).getGameRules().get(Mapper.getGameRule(in[1].getString(sc)));
  29. if(o instanceof GameRules.BooleanValue) {
  30. ((GameRules.BooleanValue) o).set(in[2].getBoolean(sc), server);
  31. } else if(o instanceof GameRules.IntegerValue) {
  32. ReflectionUtils.setIntegerValue((GameRules.IntegerValue) o, in[2].getInt(sc));
  33. }
  34. });
  35. sm.registerFunction("world.getgamerule", (sc, in) -> {
  36. Object o = ((World) in[0].get(sc)).getGameRules().get(Mapper.getGameRule(in[1].getString(sc)));
  37. if(o instanceof GameRules.BooleanValue) {
  38. return ((GameRules.BooleanValue) o).get();
  39. } else if(o instanceof GameRules.IntegerValue) {
  40. return (double) ((GameRules.IntegerValue) o).get();
  41. }
  42. return null;
  43. });
  44. sm.registerConsumer("world.setspawn", (sc, in) -> {
  45. Location l = ((Location) in[0].get(sc));
  46. l.getWorld().getWorldInfo().setSpawn(l.getBlockPos());
  47. });
  48. sm.registerFunction("world.getspawn", (sc, in) -> {
  49. World w = (World) in[0].get(sc);
  50. WorldInfo info = w.getWorldInfo();
  51. return new Location(w, info.getSpawnX(), info.getSpawnY(), info.getSpawnZ(), 0.0f, 0.0f);
  52. });
  53. sm.registerFunction("world.getall", (sc, in) -> {
  54. ArrayList<World> worlds = new ArrayList<>();
  55. for(World w : server.getWorlds()) {
  56. worlds.add(w);
  57. }
  58. return worlds;
  59. });
  60. sm.registerConsumer("world.settime", (sc, in) -> ((World) in[0].get(sc)).setDayTime(in[1].getLong(sc)));
  61. sm.registerFunction("world.gettime", (sc, in) -> (double) ((World) in[0].get(sc)).getDayTime());
  62. sm.registerFunction("world.hasstorm", (sc, in) -> ((World) in[0].get(sc)).isRaining());
  63. sm.registerConsumer("world.clearweather", (sc, in) -> {
  64. WorldInfo wi = ((World) in[0].get(sc)).getWorldInfo();
  65. wi.setClearWeatherTime(in[1].getInt(sc));
  66. wi.setRainTime(0);
  67. wi.setThunderTime(0);
  68. wi.setRaining(false);
  69. wi.setThundering(false);
  70. });
  71. sm.registerConsumer("world.setrain", (sc, in) -> {
  72. WorldInfo wi = ((World) in[0].get(sc)).getWorldInfo();
  73. wi.setClearWeatherTime(0);
  74. int i = in[1].getInt(sc);
  75. wi.setRainTime(i);
  76. wi.setThunderTime(i);
  77. wi.setRaining(true);
  78. wi.setThundering(false);
  79. });
  80. sm.registerConsumer("world.setthunder", (sc, in) -> {
  81. WorldInfo wi = ((World) in[0].get(sc)).getWorldInfo();
  82. wi.setClearWeatherTime(0);
  83. int i = in[1].getInt(sc);
  84. wi.setRainTime(i);
  85. wi.setThunderTime(i);
  86. wi.setRaining(true);
  87. wi.setThundering(true);
  88. });
  89. sm.registerFunction("world.getentities", (sc, in) -> {
  90. return ((ServerWorld) in[0].get(sc)).getEntities().collect(Collectors.toList());
  91. });
  92. }
  93. }