package me.km.dimensions; import java.util.Arrays; import java.util.Random; import me.km.KajetansMod; import net.minecraft.server.MinecraftServer; import net.minecraft.world.DimensionType; import net.minecraft.world.ServerWorldEventHandler; import net.minecraft.world.World; import net.minecraft.world.WorldServer; import net.minecraft.world.WorldSettings; import net.minecraft.world.WorldType; import net.minecraft.world.storage.ISaveHandler; import net.minecraft.world.storage.WorldInfo; import net.minecraftforge.common.DimensionManager; public class ModDimensions { private static int counter = 2; public static final DimensionType CUSTOM_OVERWORLD = DimensionType.register("CUSTOM_OVERWORLD", "_coverworld", 3, CustomWorldProviderSurface.class, false); public static final DimensionType CUSTOM_NETHER = DimensionType.register("CUSTOM_NETHER", "_cnether", 2, CustomWorldProviderHell.class, false); public static final DimensionType CUSTOM_THE_END = DimensionType.register("CUSTOM_THE_END", "_cend", 4, CustomWorldProviderEnd.class, false); public static String getWorldName(World ws) { int dim = ws.provider.getDimension(); switch(dim) { case -1: return DimensionManager.getWorld(-1).getWorldInfo().getWorldName() + "_nether"; case 1: return DimensionManager.getWorld(-1).getWorldInfo().getWorldName() + "_end"; } return ws.getWorldInfo().getWorldName(); } public static WorldServer getWorldFromName(String s) { WorldServer ws = DimensionManager.getWorld(0); String normal = DimensionManager.getWorld(0).getWorldInfo().getWorldName(); if(s.equals(normal)) { return ws; } else if(s.equals(normal + "_nether")) { return DimensionManager.getWorld(-1); } else if(s.equals(normal + "_end")) { return DimensionManager.getWorld(1); } return Arrays.stream(DimensionManager.getWorlds()).filter(w -> w.getWorldInfo().getWorldName().equals(s)).findAny().orElse(null); } /** Unloads a world by its name. * * @param name the name of the world * @return false, if world does not exist or cannot be unloaded */ public static boolean unloadWorld(String name) { WorldServer ws = getWorldFromName(name); if(ws == null) { return false; } int id = ws.provider.getDimension(); if(id >= -1 && id <= 1) { return false; } DimensionManager.unregisterDimension(id); DimensionManager.unloadWorld(id); return true; } /** Loads a world by its name. * * @param name the name of the world * @param type a custom dimension type * @return false, if the world is already loaded */ public static boolean loadWorld(String name, DimensionType type) { WorldServer ws = getWorldFromName(name); if(ws != null) { return false; } loadWorld(type, counter, name); counter++; if(counter == Integer.MAX_VALUE) { counter = 2; } return true; } private static void loadWorld(DimensionType type2, int dim, String name) { if(!DimensionManager.isDimensionRegistered(dim)) { DimensionManager.registerDimension(dim, type2); } MinecraftServer ms = KajetansMod.server; ISaveHandler isavehandler = ms.getActiveAnvilConverter().getSaveLoader(name, true); WorldInfo worldinfo = isavehandler.loadWorldInfo(); WorldSettings worldsettings; if (worldinfo == null) { worldsettings = new WorldSettings((new Random()).nextLong(), ms.getGameType(), ms.canStructuresSpawn(), ms.isHardcore(), WorldType.DEFAULT); worldsettings.setGeneratorOptions(""); worldinfo = new WorldInfo(worldsettings, name); } else { worldinfo.setWorldName(name); worldsettings = new WorldSettings(worldinfo); } WorldServer overWorld = (WorldServer) new WorldServer(ms, isavehandler, worldinfo, dim, ms.profiler).init(); overWorld.initialize(worldsettings); overWorld.addEventListener(new ServerWorldEventHandler(ms, overWorld)); overWorld.getWorldInfo().setGameType(ms.getGameType()); //net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.event.world.WorldEvent.Load(world)); } }