Search in sources :

Example 1 with DimensionType

use of net.minecraft.world.DimensionType in project SpongeCommon by SpongePowered.

the class WorldManager method registerExistingSpongeDimensions.

/**
 * Handles registering existing Sponge dimensions that are not the root dimension (known as overworld).
 */
private static void registerExistingSpongeDimensions(Path rootPath) {
    try (final DirectoryStream<Path> stream = Files.newDirectoryStream(rootPath, LEVEL_AND_SPONGE)) {
        for (Path worldPath : stream) {
            final Path spongeLevelPath = worldPath.resolve("level_sponge.dat");
            final String worldFolderName = worldPath.getFileName().toString();
            NBTTagCompound compound;
            try {
                compound = CompressedStreamTools.readCompressed(Files.newInputStream(spongeLevelPath));
            } catch (IOException e) {
                SpongeImpl.getLogger().error("Failed loading Sponge data for World [{}]}. Report to Sponge ASAP.", worldFolderName, e);
                continue;
            }
            NBTTagCompound spongeDataCompound = compound.getCompoundTag(NbtDataUtil.SPONGE_DATA);
            if (!compound.hasKey(NbtDataUtil.SPONGE_DATA)) {
                SpongeImpl.getLogger().error("World [{}] has Sponge related data in the form of [level-sponge.dat] but the structure is not proper." + " Generally, the data is within a [{}] tag but it is not for this world. Report to Sponge ASAP.", worldFolderName, NbtDataUtil.SPONGE_DATA);
                continue;
            }
            if (!spongeDataCompound.hasKey(NbtDataUtil.DIMENSION_ID)) {
                SpongeImpl.getLogger().error("World [{}] has no dimension id. Report this to Sponge ASAP.", worldFolderName);
                continue;
            }
            int dimensionId = spongeDataCompound.getInteger(NbtDataUtil.DIMENSION_ID);
            if (dimensionId == Integer.MIN_VALUE) {
                // temporary fix for existing worlds created with wrong dimension id
                dimensionId = WorldManager.getNextFreeDimensionId();
            }
            // We do not handle Vanilla dimensions, skip them
            if (dimensionId == 0 || dimensionId == -1 || dimensionId == 1) {
                continue;
            }
            spongeDataCompound = DataUtil.spongeDataFixer.process(FixTypes.LEVEL, spongeDataCompound);
            String dimensionTypeId = "overworld";
            if (spongeDataCompound.hasKey(NbtDataUtil.DIMENSION_TYPE)) {
                dimensionTypeId = spongeDataCompound.getString(NbtDataUtil.DIMENSION_TYPE);
            } else {
                SpongeImpl.getLogger().warn("World [{}] (DIM{}) has no specified dimension type. Defaulting to [{}}]...", worldFolderName, dimensionId, DimensionTypes.OVERWORLD.getName());
            }
            dimensionTypeId = fixDimensionTypeId(dimensionTypeId);
            org.spongepowered.api.world.DimensionType dimensionType = Sponge.getRegistry().getType(org.spongepowered.api.world.DimensionType.class, dimensionTypeId).orElse(null);
            if (dimensionType == null) {
                SpongeImpl.getLogger().warn("World [{}] (DIM{}) has specified dimension type that is not registered. Skipping...", worldFolderName, dimensionId);
                continue;
            }
            spongeDataCompound.setString(NbtDataUtil.DIMENSION_TYPE, dimensionTypeId);
            if (!spongeDataCompound.hasUniqueId(NbtDataUtil.UUID)) {
                SpongeImpl.getLogger().error("World [{}] (DIM{}) has no valid unique identifier. This is a critical error and should be reported" + " to Sponge ASAP.", worldFolderName, dimensionId);
                continue;
            }
            worldFolderByDimensionId.put(dimensionId, worldFolderName);
            registerDimensionPath(dimensionId, rootPath.resolve(worldFolderName));
            registerDimension(dimensionId, (DimensionType) (Object) dimensionType);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : Path(java.nio.file.Path) IMixinDimensionType(org.spongepowered.common.interfaces.world.IMixinDimensionType) DimensionType(net.minecraft.world.DimensionType) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IOException(java.io.IOException)

Example 2 with DimensionType

use of net.minecraft.world.DimensionType in project EnderIO by SleepyTrousers.

the class TelepadTarget method getDimenionName.

@Nonnull
public static String getDimenionName(int dim) {
    if (!DimensionManager.isDimensionRegistered(dim)) {
        return Integer.toString(dim);
    }
    DimensionType type = DimensionManager.getProviderType(dim);
    if (type == null) {
        return Integer.toString(dim);
    }
    String name = type.getName();
    int[] dims = DimensionManager.getDimensions(type);
    if (dims != null && dims.length > 1) {
        name += " " + dim;
    }
    return name;
}
Also used : DimensionType(net.minecraft.world.DimensionType) Nonnull(javax.annotation.Nonnull)

Example 3 with DimensionType

use of net.minecraft.world.DimensionType in project BiomeTweaker by superckl.

the class BiomeTweaker method generateOutputFiles.

public void generateOutputFiles() throws IOException {
    LogHelper.info("Generating Biome status report...");
    final JsonArray array = new JsonArray();
    final Iterator<Biome> it = Biome.REGISTRY.iterator();
    while (it.hasNext()) {
        final Biome gen = it.next();
        if (gen == null)
            continue;
        array.add(BiomeHelper.fillJsonObject(gen));
    }
    final Gson gson = new GsonBuilder().setPrettyPrinting().create();
    final File baseDir = new File(this.config.getBtConfigFolder(), "output/");
    final File biomeDir = new File(baseDir, "/biome/");
    biomeDir.mkdirs();
    for (final File file : biomeDir.listFiles()) if (file.getName().endsWith(".json"))
        file.delete();
    if (this.config.isOutputSeperateFiles())
        for (final JsonElement element : array) {
            final JsonObject obj = (JsonObject) element;
            final StringBuilder fileName = new StringBuilder(obj.get("Name").getAsString().replaceAll("[^a-zA-Z0-9.-]", "_")).append(" (").append(obj.get("Resource Location").getAsString().replaceAll("[^a-zA-Z0-9.-]", "_")).append(")").append(".json");
            final File biomeOutput = new File(biomeDir, fileName.toString());
            if (biomeOutput.exists())
                biomeOutput.delete();
            biomeOutput.createNewFile();
            @Cleanup final BufferedWriter writer = new BufferedWriter(new FileWriter(biomeOutput));
            writer.newLine();
            writer.write(gson.toJson(obj));
        }
    else {
        final File biomeOutput = new File(biomeDir, "BiomeTweaker - Biome Status Report.json");
        if (biomeOutput.exists())
            biomeOutput.delete();
        biomeOutput.createNewFile();
        @Cleanup final BufferedWriter writer = new BufferedWriter(new FileWriter(biomeOutput));
        writer.write("//Yeah, it's a doozy.");
        writer.newLine();
        writer.write(gson.toJson(array));
    }
    LogHelper.info("Generating LivingEntity status report...");
    final File entityDir = new File(baseDir, "/entity/");
    entityDir.mkdirs();
    for (final File file : entityDir.listFiles()) if (file.getName().endsWith(".json"))
        file.delete();
    final Iterator<EntityEntry> entityIt = ForgeRegistries.ENTITIES.getValues().iterator();
    final JsonArray entityArray = new JsonArray();
    while (entityIt.hasNext()) {
        final EntityEntry entry = entityIt.next();
        if (!EntityLiving.class.isAssignableFrom(entry.getEntityClass()))
            continue;
        entityArray.add(EntityHelper.populateObject(entry));
    }
    if (this.config.isOutputSeperateFiles())
        for (final JsonElement ele : entityArray) {
            final JsonObject obj = (JsonObject) ele;
            final StringBuilder fileName = new StringBuilder(obj.get("Name").getAsString().replaceAll("[^a-zA-Z0-9.-]", "_")).append(" (").append(obj.get("Registry ID").getAsString().replaceAll("[^a-zA-Z0-9.-]", "_")).append(")").append(".json");
            final File entityOutput = new File(entityDir, fileName.toString());
            if (entityOutput.exists())
                entityOutput.delete();
            entityOutput.createNewFile();
            @Cleanup final BufferedWriter writer = new BufferedWriter(new FileWriter(entityOutput));
            writer.newLine();
            writer.write(gson.toJson(obj));
        }
    else {
        final File entityOutput = new File(entityDir, "BiomeTweaker - EntityLiving Status Report.json");
        if (entityOutput.exists())
            entityOutput.delete();
        entityOutput.createNewFile();
        @Cleanup final BufferedWriter writer = new BufferedWriter(new FileWriter(entityOutput));
        writer.write("//Yeah, it's a doozy.");
        writer.newLine();
        writer.write(gson.toJson(entityArray));
    }
    LogHelper.info("Generating Dimension status report...");
    final File dimDir = new File(baseDir, "/dimension/");
    dimDir.mkdirs();
    for (final File file : dimDir.listFiles()) if (file.getName().endsWith(".json"))
        file.delete();
    final DimensionType[] dimTypes = DimensionType.values();
    final JsonArray dimArray = new JsonArray();
    for (final DimensionType dimType : dimTypes) dimArray.add(DimensionHelper.populateObject(dimType));
    if (this.config.isOutputSeperateFiles())
        for (final JsonElement ele : dimArray) {
            final JsonObject obj = (JsonObject) ele;
            final StringBuilder fileName = new StringBuilder(obj.get("Name").getAsString().replaceAll("[^a-zA-Z0-9.-]", "_")).append(" (").append(obj.get("Suffix").getAsString().replaceAll("[^a-zA-Z0-9.-]", "_")).append(")").append(".json");
            final File dimOutput = new File(dimDir, fileName.toString());
            if (dimOutput.exists())
                dimOutput.delete();
            dimOutput.createNewFile();
            @Cleanup final BufferedWriter writer = new BufferedWriter(new FileWriter(dimOutput));
            writer.newLine();
            writer.write(gson.toJson(obj));
        }
    else {
        final File dimOutput = new File(entityDir, "BiomeTweaker - Dimension Status Report.json");
        if (dimOutput.exists())
            dimOutput.delete();
        dimOutput.createNewFile();
        @Cleanup final BufferedWriter writer = new BufferedWriter(new FileWriter(dimOutput));
        writer.write("//Yeah, it's a doozy.");
        writer.newLine();
        writer.write(gson.toJson(entityArray));
    }
}
Also used : DimensionType(net.minecraft.world.DimensionType) EntityLiving(net.minecraft.entity.EntityLiving) GsonBuilder(com.google.gson.GsonBuilder) FileWriter(java.io.FileWriter) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) Cleanup(lombok.Cleanup) BufferedWriter(java.io.BufferedWriter) JsonArray(com.google.gson.JsonArray) EntityEntry(net.minecraftforge.fml.common.registry.EntityEntry) CommandSetBiome(me.superckl.biometweaker.server.command.CommandSetBiome) Biome(net.minecraft.world.biome.Biome) JsonElement(com.google.gson.JsonElement) File(java.io.File)

Example 4 with DimensionType

use of net.minecraft.world.DimensionType in project SpongeForge by SpongePowered.

the class MixinDimensionManager method createProviderFor.

@Overwrite
public static WorldProvider createProviderFor(int dim) {
    final DimensionType dimensionType = WorldManager.getDimensionType(dim).orElseThrow(() -> new RuntimeException("Attempt to create " + "provider for dimension id [" + dim + "] failed because it has not been registered!"));
    try {
        final WorldProvider provider = dimensionType.createDimension();
        provider.setDimension(dim);
        return provider;
    } catch (Exception e) {
        throw new RuntimeException("Failed to create provider for dimension id [" + dim + "]!");
    }
}
Also used : DimensionType(net.minecraft.world.DimensionType) WorldProvider(net.minecraft.world.WorldProvider) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 5 with DimensionType

use of net.minecraft.world.DimensionType in project SpongeForge by SpongePowered.

the class MixinDimensionManager method initDimension.

/**
 * @author Zidane - June 2nd, 2016
 * @reason Forge's initDimension is very different from Sponge's multi-world. We basically rig it into our system so mods work.
 * @param dim The dimension to load
 */
@Overwrite
public static void initDimension(int dim) {
    // World is already loaded, bail
    if (WorldManager.getWorldByDimensionId(dim).isPresent()) {
        return;
    }
    if (dim == 0) {
        throw new RuntimeException("Attempt made to initialize overworld!");
    }
    WorldManager.getWorldByDimensionId(0).orElseThrow(() -> new RuntimeException("Attempt made to initialize " + "dimension before overworld is loaded!"));
    DimensionType dimensionType = WorldManager.getDimensionType(dim).orElse(null);
    if (dimensionType == null) {
        SpongeImpl.getLogger().warn("Attempt made to initialize dimension id {} which isn't registered!" + ", falling back to overworld.", dim);
        return;
    }
    final WorldProvider provider = dimensionType.createDimension();
    // make sure to set the dimension id to avoid getting a null save folder
    provider.setDimension(dim);
    String worldFolder = WorldManager.getWorldFolderByDimensionId(dim).orElse(provider.getSaveFolder());
    WorldProperties properties = WorldManager.getWorldProperties(worldFolder).orElse(null);
    final Path worldPath = WorldManager.getCurrentSavesDirectory().get().resolve(worldFolder);
    if (properties == null || !Files.isDirectory(worldPath)) {
        if (properties != null) {
            WorldManager.unregisterWorldProperties(properties, false);
        }
        String modId = StaticMixinForgeHelper.getModIdFromClass(provider.getClass());
        WorldArchetype archetype = Sponge.getRegistry().getType(WorldArchetype.class, modId + ":" + dimensionType.getName().toLowerCase()).orElse(null);
        if (archetype == null) {
            final WorldArchetype.Builder builder = WorldArchetype.builder().dimension((org.spongepowered.api.world.DimensionType) (Object) dimensionType).keepsSpawnLoaded(dimensionType.shouldLoadSpawn());
            archetype = builder.build(modId + ":" + dimensionType.getName().toLowerCase(), dimensionType.getName());
        }
        IMixinWorldSettings worldSettings = (IMixinWorldSettings) archetype;
        worldSettings.setDimensionType((org.spongepowered.api.world.DimensionType) (Object) dimensionType);
        worldSettings.setLoadOnStartup(false);
        properties = WorldManager.createWorldProperties(worldFolder, archetype, dim);
        ((IMixinWorldInfo) properties).setDimensionId(dim);
        ((IMixinWorldInfo) properties).setIsMod(true);
    }
    if (!properties.isEnabled()) {
        return;
    }
    Optional<WorldServer> optWorld = WorldManager.loadWorld(properties);
    if (!optWorld.isPresent()) {
        SpongeImpl.getLogger().error("Could not load world [{}]!", properties.getWorldName());
    }
}
Also used : Path(java.nio.file.Path) DimensionType(net.minecraft.world.DimensionType) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) WorldServer(net.minecraft.world.WorldServer) WorldArchetype(org.spongepowered.api.world.WorldArchetype) WorldProvider(net.minecraft.world.WorldProvider) IMixinWorldSettings(org.spongepowered.common.interfaces.world.IMixinWorldSettings) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Aggregations

DimensionType (net.minecraft.world.DimensionType)28 BlockPos (net.minecraft.util.math.BlockPos)8 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)6 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)6 ResourceLocation (net.minecraft.util.ResourceLocation)6 WorldServer (net.minecraft.world.WorldServer)6 IPortalCache (cavern.api.IPortalCache)5 ItemStack (net.minecraft.item.ItemStack)5 TextComponentTranslation (net.minecraft.util.text.TextComponentTranslation)5 NBTTagList (net.minecraft.nbt.NBTTagList)4 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 MinecraftServer (net.minecraft.server.MinecraftServer)3 File (java.io.File)2 PatternHelper (net.minecraft.block.state.pattern.BlockPattern.PatternHelper)2 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 ActionResult (net.minecraft.util.ActionResult)2 EnumActionResult (net.minecraft.util.EnumActionResult)2 Vec3d (net.minecraft.util.math.Vec3d)2 WorldProvider (net.minecraft.world.WorldProvider)2