ModWorldInfo.java 11 KB

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