ModDimensions.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package me.km.dimensions;
  2. import java.util.Arrays;
  3. import java.util.Random;
  4. import java.util.TreeSet;
  5. import me.km.KajetansMod;
  6. import net.minecraft.entity.player.EntityPlayerMP;
  7. import net.minecraft.server.MinecraftServer;
  8. import net.minecraft.world.DimensionType;
  9. import net.minecraft.world.ServerWorldEventHandler;
  10. import net.minecraft.world.World;
  11. import net.minecraft.world.WorldServer;
  12. import net.minecraft.world.WorldSettings;
  13. import net.minecraft.world.WorldType;
  14. import net.minecraft.world.storage.ISaveHandler;
  15. import net.minecraft.world.storage.WorldInfo;
  16. import net.minecraftforge.common.DimensionManager;
  17. import net.minecraftforge.common.network.ForgeMessage.DimensionRegisterMessage;
  18. import net.minecraftforge.fml.common.network.FMLEmbeddedChannel;
  19. import net.minecraftforge.fml.common.network.FMLOutboundHandler;
  20. import net.minecraftforge.fml.common.network.NetworkRegistry;
  21. import net.minecraftforge.fml.relauncher.Side;
  22. public class ModDimensions
  23. {
  24. public static final TreeSet<Integer> LOADED_DIMS = new TreeSet<>();
  25. public static final DimensionType CUSTOM_WORLD = DimensionType.register("CUSTOM_WORLD", "_coverworld", 3, ModWorldProvider.class, false);
  26. public static String getWorldName(World ws)
  27. {
  28. if(ws == null)
  29. {
  30. return "null";
  31. }
  32. int dim = ws.provider.getDimension();
  33. switch(dim)
  34. {
  35. case -1:
  36. {
  37. World w = DimensionManager.getWorld(-1);
  38. if(w == null)
  39. {
  40. return "null_nether";
  41. }
  42. return w.getWorldInfo().getWorldName() + "_nether";
  43. }
  44. case 1:
  45. {
  46. World w = DimensionManager.getWorld(1);
  47. if(w == null)
  48. {
  49. return "null_end";
  50. }
  51. return w.getWorldInfo().getWorldName() + "_end";
  52. }
  53. }
  54. return ws.getWorldInfo().getWorldName();
  55. }
  56. public static String getWorldName(int dim)
  57. {
  58. return getWorldName(DimensionManager.getWorld(dim));
  59. }
  60. public static WorldServer getWorldFromName(String s)
  61. {
  62. WorldServer ws = DimensionManager.getWorld(0);
  63. String normal = ws.getWorldInfo().getWorldName();
  64. if(s.equals(normal))
  65. {
  66. return ws;
  67. }
  68. else if(s.equals(normal + "_nether"))
  69. {
  70. return DimensionManager.getWorld(-1);
  71. }
  72. else if(s.equals(normal + "_end"))
  73. {
  74. return DimensionManager.getWorld(1);
  75. }
  76. return Arrays.stream(DimensionManager.getWorlds()).filter(w -> w.getWorldInfo().getWorldName().equals(s)).findAny().orElse(null);
  77. }
  78. /** Unloads a world by its name.
  79. *
  80. * @param name the name of the world
  81. * @return false, if world does not exist or cannot be unloaded
  82. */
  83. public static boolean unloadWorld(String name)
  84. {
  85. WorldServer ws = getWorldFromName(name);
  86. if(ws == null)
  87. {
  88. return false;
  89. }
  90. int id = ws.provider.getDimension();
  91. if(id >= -1 && id <= 1)
  92. {
  93. return false;
  94. }
  95. DimensionManager.unloadWorld(id);
  96. // doing this on World.Unload Event, otherwise server crashes because unloadWorld is delayed
  97. // DimensionManager.unregisterDimension(id);
  98. return true;
  99. }
  100. /** Loads a world by its name.
  101. *
  102. * @param name the name of the world
  103. * @param type a custom dimension type
  104. * @return false, if the world is already loaded
  105. */
  106. public static boolean loadWorld(String name, DimensionType type)
  107. {
  108. WorldServer ws = getWorldFromName(name);
  109. if(ws != null)
  110. {
  111. return false;
  112. }
  113. int i = 10;
  114. for(int dim : LOADED_DIMS)
  115. {
  116. if(dim == i)
  117. {
  118. i++;
  119. }
  120. else if(dim > i)
  121. {
  122. break;
  123. }
  124. }
  125. loadWorld(type, i, name);
  126. LOADED_DIMS.add(i);
  127. return true;
  128. }
  129. private static void loadWorld(DimensionType type, int dim, String name)
  130. {
  131. if(!DimensionManager.isDimensionRegistered(dim))
  132. {
  133. DimensionManager.registerDimension(dim, type);
  134. }
  135. MinecraftServer ms = KajetansMod.server;
  136. ISaveHandler isavehandler = ms.getActiveAnvilConverter().getSaveLoader(name, true);
  137. WorldInfo worldinfo = isavehandler.loadWorldInfo();
  138. WorldSettings worldsettings;
  139. if (worldinfo == null)
  140. {
  141. worldsettings = new WorldSettings((new Random()).nextLong(), ms.getGameType(), ms.canStructuresSpawn(), ms.isHardcore(), WorldType.DEFAULT);
  142. worldsettings.setGeneratorOptions("");
  143. worldinfo = new WorldInfo(worldsettings, name);
  144. }
  145. else
  146. {
  147. worldinfo.setWorldName(name);
  148. worldsettings = new WorldSettings(worldinfo);
  149. }
  150. WorldServer overWorld = (WorldServer) new WorldServer(ms, isavehandler, worldinfo, dim, ms.profiler).init();
  151. overWorld.initialize(worldsettings);
  152. overWorld.addEventListener(new ServerWorldEventHandler(ms, overWorld));
  153. overWorld.getWorldInfo().setGameType(ms.getGameType());
  154. //net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.world.WorldEvent.Load(world));
  155. // Copied from com.forgeessentials.multiworld.MultiworldManager; Thank you guys.
  156. // Tell everyone about the new dim
  157. sendNotificationForDimension(dim);
  158. }
  159. private static void sendNotificationForDimension(int dim)
  160. {
  161. FMLEmbeddedChannel channel = NetworkRegistry.INSTANCE.getChannel("FORGE", Side.SERVER);
  162. DimensionRegisterMessage msg = new DimensionRegisterMessage(dim, "OVERWORLD");
  163. channel.attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALL);
  164. channel.writeOutbound(msg);
  165. }
  166. // used on player connection in ModDedicatedPlayerList
  167. public static void sendNotificationsForDimensions(EntityPlayerMP p)
  168. {
  169. FMLEmbeddedChannel channel = NetworkRegistry.INSTANCE.getChannel("FORGE", Side.SERVER);
  170. LOADED_DIMS.forEach(i ->
  171. {
  172. DimensionRegisterMessage msg = new DimensionRegisterMessage(i, "OVERWORLD");
  173. channel.attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.PLAYER);
  174. channel.attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(p);
  175. channel.writeOutbound(msg);
  176. });
  177. }
  178. }