ModDimensions.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 WorldServer getWorldFromName(String s)
  57. {
  58. WorldServer ws = DimensionManager.getWorld(0);
  59. String normal = ws.getWorldInfo().getWorldName();
  60. if(s.equals(normal))
  61. {
  62. return ws;
  63. }
  64. else if(s.equals(normal + "_nether"))
  65. {
  66. return DimensionManager.getWorld(-1);
  67. }
  68. else if(s.equals(normal + "_end"))
  69. {
  70. return DimensionManager.getWorld(1);
  71. }
  72. return Arrays.stream(DimensionManager.getWorlds()).filter(w -> w.getWorldInfo().getWorldName().equals(s)).findAny().orElse(null);
  73. }
  74. /** Unloads a world by its name.
  75. *
  76. * @param name the name of the world
  77. * @return false, if world does not exist or cannot be unloaded
  78. */
  79. public static boolean unloadWorld(String name)
  80. {
  81. WorldServer ws = getWorldFromName(name);
  82. if(ws == null)
  83. {
  84. return false;
  85. }
  86. int id = ws.provider.getDimension();
  87. if(id >= -1 && id <= 1)
  88. {
  89. return false;
  90. }
  91. DimensionManager.unloadWorld(id);
  92. // doing this on World.Unload Event, otherwise server crashes because unloadWorld is delayed
  93. // DimensionManager.unregisterDimension(id);
  94. return true;
  95. }
  96. /** Loads a world by its name.
  97. *
  98. * @param name the name of the world
  99. * @param type a custom dimension type
  100. * @return false, if the world is already loaded
  101. */
  102. public static boolean loadWorld(String name, DimensionType type)
  103. {
  104. WorldServer ws = getWorldFromName(name);
  105. if(ws != null)
  106. {
  107. return false;
  108. }
  109. int i = 10;
  110. for(int dim : LOADED_DIMS)
  111. {
  112. if(dim == i)
  113. {
  114. i++;
  115. }
  116. else if(dim > i)
  117. {
  118. break;
  119. }
  120. }
  121. loadWorld(type, i, name);
  122. LOADED_DIMS.add(i);
  123. return true;
  124. }
  125. private static void loadWorld(DimensionType type, int dim, String name)
  126. {
  127. if(!DimensionManager.isDimensionRegistered(dim))
  128. {
  129. DimensionManager.registerDimension(dim, type);
  130. }
  131. MinecraftServer ms = KajetansMod.server;
  132. ISaveHandler isavehandler = ms.getActiveAnvilConverter().getSaveLoader(name, true);
  133. WorldInfo worldinfo = isavehandler.loadWorldInfo();
  134. WorldSettings worldsettings;
  135. if (worldinfo == null)
  136. {
  137. worldsettings = new WorldSettings((new Random()).nextLong(), ms.getGameType(), ms.canStructuresSpawn(), ms.isHardcore(), WorldType.DEFAULT);
  138. worldsettings.setGeneratorOptions("");
  139. worldinfo = new WorldInfo(worldsettings, name);
  140. }
  141. else
  142. {
  143. worldinfo.setWorldName(name);
  144. worldsettings = new WorldSettings(worldinfo);
  145. }
  146. WorldServer overWorld = (WorldServer) new WorldServer(ms, isavehandler, worldinfo, dim, ms.profiler).init();
  147. overWorld.initialize(worldsettings);
  148. overWorld.addEventListener(new ServerWorldEventHandler(ms, overWorld));
  149. overWorld.getWorldInfo().setGameType(ms.getGameType());
  150. //net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.world.WorldEvent.Load(world));
  151. // Copied from com.forgeessentials.multiworld.MultiworldManager; Thank you guys.
  152. // Tell everyone about the new dim
  153. sendNotificationForDimension(dim);
  154. }
  155. private static void sendNotificationForDimension(int dim)
  156. {
  157. FMLEmbeddedChannel channel = NetworkRegistry.INSTANCE.getChannel("FORGE", Side.SERVER);
  158. DimensionRegisterMessage msg = new DimensionRegisterMessage(dim, "OVERWORLD");
  159. channel.attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALL);
  160. channel.writeOutbound(msg);
  161. }
  162. // used on player connection in ModDedicatedPlayerList
  163. public static void sendNotificationsForDimensions(EntityPlayerMP p)
  164. {
  165. FMLEmbeddedChannel channel = NetworkRegistry.INSTANCE.getChannel("FORGE", Side.SERVER);
  166. LOADED_DIMS.forEach(i ->
  167. {
  168. DimensionRegisterMessage msg = new DimensionRegisterMessage(i, "OVERWORLD");
  169. channel.attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.PLAYER);
  170. channel.attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(p);
  171. channel.writeOutbound(msg);
  172. });
  173. }
  174. }