ModWorldInfo.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. package me.km.world;
  2. import net.minecraft.world.Difficulty;
  3. import net.minecraft.world.storage.DerivedWorldInfo;
  4. import net.minecraft.world.storage.WorldInfo;
  5. import me.hammerle.snuviscript.config.SnuviConfig;
  6. import me.km.ObjectRegistry;
  7. import me.km.utils.ReflectionUtils;
  8. import net.minecraft.nbt.CompoundNBT;
  9. import net.minecraft.nbt.ListNBT;
  10. import net.minecraft.server.MinecraftServer;
  11. import net.minecraft.util.math.BlockPos;
  12. import net.minecraft.world.GameRules;
  13. import static net.minecraft.world.GameRules.*;
  14. import net.minecraft.world.WorldType;
  15. import net.minecraft.world.biome.provider.BiomeProviderType;
  16. import net.minecraft.world.biome.provider.OverworldBiomeProviderSettings;
  17. public class ModWorldInfo extends DerivedWorldInfo {
  18. public static enum Type {
  19. VANILLA, VOID, DESERT, SNOW;
  20. public BiomeProviderType<OverworldBiomeProviderSettings, ModBiomeProvider> getProvider() {
  21. switch(this) {
  22. case DESERT:
  23. return ObjectRegistry.DESERT_BIOME_PROVIDER;
  24. case SNOW:
  25. return ObjectRegistry.SNOW_BIOME_PROVIDER;
  26. }
  27. return null;
  28. }
  29. }
  30. private final SnuviConfig config;
  31. private long gameTime;
  32. private long dayTime;
  33. private int clearWeatherTime;
  34. private boolean raining;
  35. private int rainTime;
  36. private boolean thundering;
  37. private int thunderTime;
  38. private Difficulty difficulty;
  39. private final GameRules gameRules = new GameRules();
  40. private int spawnX;
  41. private int spawnY;
  42. private int spawnZ;
  43. private long seed = -1;
  44. private Type type;
  45. public ModWorldInfo(WorldInfo info, String name, MinecraftServer server) {
  46. super(info);
  47. config = new SnuviConfig("worlddata", name);
  48. if(config.exists()) {
  49. config.load(null);
  50. }
  51. onLoad(info, server);
  52. org.apache.logging.log4j.LogManager.getLogger().warn("status: " + type);
  53. }
  54. private void onLoad(WorldInfo info, MinecraftServer server) {
  55. String configSeed = config.getString(null, "seed", null);
  56. if(configSeed != null) {
  57. seed = configSeed.hashCode();
  58. }
  59. spawnX = config.getInt(null, "spawnX", info.getSpawnX());
  60. spawnY = config.getInt(null, "spawnY", info.getSpawnY());
  61. spawnZ = config.getInt(null, "spawnZ", info.getSpawnZ());
  62. gameTime = config.getLong(null, "gameTime", info.getGameTime());
  63. dayTime = config.getLong(null, "dayTime", info.getDayTime());
  64. clearWeatherTime = config.getInt(null, "clearWeatherTime", info.getClearWeatherTime());
  65. raining = config.getBoolean(null, "raining", info.isRaining());
  66. rainTime = config.getInt(null, "rainTime", info.getRainTime());
  67. thundering = config.getBoolean(null, "thundering", info.isThundering());
  68. thunderTime = config.getInt(null, "thunderTime", info.getThunderTime());
  69. String diffi = config.getString(null, "difficulty");
  70. if(diffi == null) {
  71. difficulty = info.getDifficulty();
  72. } else {
  73. try {
  74. difficulty = Difficulty.valueOf(diffi);
  75. } catch(Exception ex) {
  76. difficulty = info.getDifficulty();
  77. }
  78. }
  79. GameRules rules = info.getGameRulesInstance();
  80. gameRules.get(DO_FIRE_TICK).set(config.getBoolean(null, "doFireTick", rules.getBoolean(DO_FIRE_TICK)), server);
  81. gameRules.get(MOB_GRIEFING).set(config.getBoolean(null, "mobGriefing", rules.getBoolean(MOB_GRIEFING)), server);
  82. gameRules.get(KEEP_INVENTORY).set(config.getBoolean(null, "keepInventory", rules.getBoolean(KEEP_INVENTORY)), server);
  83. gameRules.get(DO_MOB_SPAWNING).set(config.getBoolean(null, "doMobSpawning", rules.getBoolean(DO_MOB_SPAWNING)), server);
  84. gameRules.get(DO_MOB_LOOT).set(config.getBoolean(null, "doMobLoot", rules.getBoolean(DO_MOB_LOOT)), server);
  85. gameRules.get(DO_TILE_DROPS).set(config.getBoolean(null, "doTileDrops", rules.getBoolean(DO_TILE_DROPS)), server);
  86. gameRules.get(DO_ENTITY_DROPS).set(config.getBoolean(null, "doEntityDrops", rules.getBoolean(DO_ENTITY_DROPS)), server);
  87. gameRules.get(COMMAND_BLOCK_OUTPUT).set(config.getBoolean(null, "commandBlockOutput", rules.getBoolean(COMMAND_BLOCK_OUTPUT)), server);
  88. gameRules.get(NATURAL_REGENERATION).set(config.getBoolean(null, "naturalRegeneration", rules.getBoolean(NATURAL_REGENERATION)), server);
  89. gameRules.get(DO_DAYLIGHT_CYCLE).set(config.getBoolean(null, "doDaylightCycle", rules.getBoolean(DO_DAYLIGHT_CYCLE)), server);
  90. gameRules.get(LOG_ADMIN_COMMANDS).set(config.getBoolean(null, "logAdminCommands", rules.getBoolean(LOG_ADMIN_COMMANDS)), server);
  91. gameRules.get(SHOW_DEATH_MESSAGES).set(config.getBoolean(null, "showDeathMessages", rules.getBoolean(SHOW_DEATH_MESSAGES)), server);
  92. ReflectionUtils.setIntegerValue(gameRules.get(RANDOM_TICK_SPEED), config.getInt(null, "randomTickSpeed", rules.getInt(RANDOM_TICK_SPEED)));
  93. gameRules.get(SEND_COMMAND_FEEDBACK).set(config.getBoolean(null, "sendCommandFeedback", rules.getBoolean(SEND_COMMAND_FEEDBACK)), server);
  94. gameRules.get(REDUCED_DEBUG_INFO).set(config.getBoolean(null, "reducedDebugInfo", rules.getBoolean(REDUCED_DEBUG_INFO)), server);
  95. gameRules.get(SPECTATORS_GENERATE_CHUNKS).set(config.getBoolean(null, "spectatorsGenerateChunks", rules.getBoolean(SPECTATORS_GENERATE_CHUNKS)), server);
  96. ReflectionUtils.setIntegerValue(gameRules.get(SPAWN_RADIUS), config.getInt(null, "spawnRadius", rules.getInt(SPAWN_RADIUS)));
  97. gameRules.get(DISABLE_ELYTRA_MOVEMENT_CHECK).set(config.getBoolean(null, "disableElytraMovementCheck", rules.getBoolean(DISABLE_ELYTRA_MOVEMENT_CHECK)), server);
  98. ReflectionUtils.setIntegerValue(gameRules.get(MAX_ENTITY_CRAMMING), config.getInt(null, "maxEntityCramming", rules.getInt(MAX_ENTITY_CRAMMING)));
  99. gameRules.get(DO_WEATHER_CYCLE).set(config.getBoolean(null, "doWeatherCycle", rules.getBoolean(DO_WEATHER_CYCLE)), server);
  100. gameRules.get(DO_LIMITED_CRAFTING).set(config.getBoolean(null, "doLimitedCrafting", rules.getBoolean(DO_LIMITED_CRAFTING)), server);
  101. ReflectionUtils.setIntegerValue(gameRules.get(MAX_COMMAND_CHAIN_LENGTH), config.getInt(null, "maxCommandChainLength", rules.getInt(MAX_COMMAND_CHAIN_LENGTH)));
  102. gameRules.get(ANNOUNCE_ADVANCEMENTS).set(config.getBoolean(null, "announceAdvancements", rules.getBoolean(ANNOUNCE_ADVANCEMENTS)), server);
  103. gameRules.get(DISABLE_RAIDS).set(config.getBoolean(null, "disableRaids", rules.getBoolean(DISABLE_RAIDS)), server);
  104. gameRules.get(DO_INSOMNIA).set(config.getBoolean(null, "doInsomnia", rules.getBoolean(DO_INSOMNIA)), server);
  105. gameRules.get(DO_IMMEDIATE_RESPAWN).set(config.getBoolean(null, "doImmediateRespawn", rules.getBoolean(DO_IMMEDIATE_RESPAWN)), server);
  106. try {
  107. type = Type.valueOf(config.getString(null, "type", ""));
  108. } catch(IllegalArgumentException ex) {
  109. type = Type.VOID;
  110. }
  111. }
  112. public void onSave() {
  113. config.set("spawnX", spawnX);
  114. config.set("spawnY", spawnY);
  115. config.set("spawnZ", spawnZ);
  116. config.set("gameTime", gameTime);
  117. config.set("dayTime", dayTime);
  118. config.set("clearWeatherTime", clearWeatherTime);
  119. config.set("raining", raining);
  120. config.set("rainTime", rainTime);
  121. config.set("thundering", thundering);
  122. config.set("thunderTime", thunderTime);
  123. config.set("difficulty", difficulty);
  124. config.set("doFireTick", gameRules.getBoolean(DO_FIRE_TICK));
  125. config.set("mobGriefing", gameRules.getBoolean(MOB_GRIEFING));
  126. config.set("keepInventory", gameRules.getBoolean(KEEP_INVENTORY));
  127. config.set("doMobSpawning", gameRules.getBoolean(DO_MOB_SPAWNING));
  128. config.set("doMobLoot", gameRules.getBoolean(DO_MOB_LOOT));
  129. config.set("doTileDrops", gameRules.getBoolean(DO_TILE_DROPS));
  130. config.set("doEntityDrops", gameRules.getBoolean(DO_ENTITY_DROPS));
  131. config.set("commandBlockOutput", gameRules.getBoolean(COMMAND_BLOCK_OUTPUT));
  132. config.set("naturalRegeneration", gameRules.getBoolean(NATURAL_REGENERATION));
  133. config.set("doDaylightCycle", gameRules.getBoolean(DO_DAYLIGHT_CYCLE));
  134. config.set("logAdminCommands", gameRules.getBoolean(LOG_ADMIN_COMMANDS));
  135. config.set("showDeathMessages", gameRules.getBoolean(SHOW_DEATH_MESSAGES));
  136. config.set("randomTickSpeed", gameRules.getInt(RANDOM_TICK_SPEED));
  137. config.set("sendCommandFeedback", gameRules.getBoolean(SEND_COMMAND_FEEDBACK));
  138. config.set("reducedDebugInfo", gameRules.getBoolean(REDUCED_DEBUG_INFO));
  139. config.set("spectatorsGenerateChunks", gameRules.getBoolean(SPECTATORS_GENERATE_CHUNKS));
  140. config.set("spawnRadius", gameRules.getInt(SPAWN_RADIUS));
  141. config.set("disableElytraMovementCheck", gameRules.getBoolean(DISABLE_ELYTRA_MOVEMENT_CHECK));
  142. config.set("maxEntityCramming", gameRules.getInt(MAX_ENTITY_CRAMMING));
  143. config.set("doWeatherCycle", gameRules.getBoolean(DO_WEATHER_CYCLE));
  144. config.set("doLimitedCrafting", gameRules.getBoolean(DO_LIMITED_CRAFTING));
  145. config.set("maxCommandChainLength", gameRules.getInt(MAX_COMMAND_CHAIN_LENGTH));
  146. config.set("announceAdvancements", gameRules.getBoolean(ANNOUNCE_ADVANCEMENTS));
  147. config.set("disableRaids", gameRules.getBoolean(DISABLE_RAIDS));
  148. config.set("doInsomnia", gameRules.getBoolean(DO_INSOMNIA));
  149. config.set("doImmediateRespawn", gameRules.getBoolean(DO_IMMEDIATE_RESPAWN));
  150. config.set("type", type);
  151. config.save(null);
  152. }
  153. @Override
  154. public long getGameTime() {
  155. return this.gameTime;
  156. }
  157. @Override
  158. public long getDayTime() {
  159. return this.dayTime;
  160. }
  161. @Override
  162. public void setGameTime(long time) {
  163. this.gameTime = time;
  164. }
  165. @Override
  166. public void setDayTime(long time) {
  167. this.dayTime = time;
  168. }
  169. @Override
  170. public int getClearWeatherTime() {
  171. return this.clearWeatherTime;
  172. }
  173. @Override
  174. public void setClearWeatherTime(int cleanWeatherTimeIn) {
  175. this.clearWeatherTime = cleanWeatherTimeIn;
  176. }
  177. @Override
  178. public boolean isThundering() {
  179. return this.thundering;
  180. }
  181. @Override
  182. public void setThundering(boolean thunderingIn) {
  183. this.thundering = thunderingIn;
  184. }
  185. @Override
  186. public int getThunderTime() {
  187. return this.thunderTime;
  188. }
  189. @Override
  190. public void setThunderTime(int time) {
  191. this.thunderTime = time;
  192. }
  193. @Override
  194. public boolean isRaining() {
  195. return this.raining;
  196. }
  197. @Override
  198. public void setRaining(boolean isRaining) {
  199. this.raining = isRaining;
  200. }
  201. @Override
  202. public int getRainTime() {
  203. return this.rainTime;
  204. }
  205. @Override
  206. public void setRainTime(int time) {
  207. this.rainTime = time;
  208. }
  209. @Override
  210. public Difficulty getDifficulty() {
  211. return this.difficulty;
  212. }
  213. @Override
  214. public void setDifficulty(Difficulty newDifficulty) {
  215. this.difficulty = newDifficulty;
  216. }
  217. @Override
  218. public GameRules getGameRulesInstance() {
  219. return gameRules;
  220. }
  221. @Override
  222. public WorldType getGenerator() {
  223. if(type == Type.VOID) {
  224. return WorldType.FLAT;
  225. }
  226. return super.getGenerator();
  227. }
  228. public Type getType() {
  229. return type;
  230. }
  231. @Override
  232. public CompoundNBT getGeneratorOptions() {
  233. if(type == Type.VOID) {
  234. CompoundNBT base = new CompoundNBT();
  235. base.putString("biome", "minecraft:the_void");
  236. ListNBT list = new ListNBT();
  237. base.put("layers", list);
  238. CompoundNBT layer = new CompoundNBT();
  239. layer.putString("block", "minecraft:air");
  240. layer.putByte("height", (byte) 1);
  241. list.add(layer);
  242. return base;
  243. }
  244. return super.getGeneratorOptions();
  245. }
  246. @Override
  247. public void setSpawn(BlockPos spawnPoint) {
  248. spawnX = spawnPoint.getX();
  249. spawnY = spawnPoint.getY();
  250. spawnZ = spawnPoint.getZ();
  251. }
  252. @Override
  253. public int getSpawnX() {
  254. return spawnX;
  255. }
  256. @Override
  257. public int getSpawnY() {
  258. return spawnY;
  259. }
  260. @Override
  261. public int getSpawnZ() {
  262. return spawnZ;
  263. }
  264. @Override
  265. public long getSeed() {
  266. if(seed != -1) {
  267. return seed;
  268. }
  269. return super.getSeed();
  270. }
  271. }