Search in sources :

Example 6 with DimensionType

use of net.minecraft.world.DimensionType in project Cavern2 by kegare.

the class RegenerationMessage method process.

@Override
public IMessage process() {
    MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
    File rootDir = DimensionManager.getCurrentSaveRootDirectory();
    if (rootDir == null || !rootDir.exists()) {
        return new RegenerationGuiMessage(EnumType.FAILED);
    }
    server.getPlayerList().saveAllPlayerData();
    for (DimensionType type : dimensions) {
        WorldServer world = DimensionManager.getWorld(type.getId());
        if (world != null) {
            for (EntityPlayer player : world.playerEntities) {
                if (player != null && player instanceof EntityPlayerMP) {
                    EntityPlayerMP playerMP = (EntityPlayerMP) player;
                    if (playerMP.connection != null) {
                        playerMP.connection.disconnect(new TextComponentTranslation("cavern.message.disconnect.unload"));
                    }
                }
            }
            try {
                world.saveAllChunks(true, null);
            } catch (MinecraftException e) {
                return new RegenerationGuiMessage(EnumType.FAILED);
            }
            world.flush();
            MinecraftForge.EVENT_BUS.post(new WorldEvent.Unload(world));
            DimensionManager.setWorld(world.provider.getDimension(), null, server);
            world.getWorldInfo().setDimensionData(world.provider.getDimension(), null);
        }
        for (EntityPlayerMP player : server.getPlayerList().getPlayers()) {
            if (player != null) {
                PortalCache.get(player).clearLastPos(null, type);
            }
        }
        File dimDir = new File(rootDir, "DIM" + type.getId());
        if (!dimDir.exists()) {
            return new RegenerationGuiMessage(EnumType.FAILED);
        }
        ITextComponent name = new TextComponentString(CaveDimensions.getLocalizedName(type));
        name.getStyle().setBold(true);
        sendProgress(EnumType.START);
        ITextComponent message = new TextComponentTranslation("cavern.regeneration.regenerating", name);
        message.getStyle().setColor(TextFormatting.GRAY);
        server.getPlayerList().sendMessage(message);
        if (backup) {
            message = new TextComponentTranslation("cavern.regeneration.backup", name);
            message.getStyle().setColor(TextFormatting.GRAY);
            server.getPlayerList().sendMessage(message);
            sendProgress(EnumType.BACKUP);
            Calendar calendar = Calendar.getInstance();
            String year = Integer.toString(calendar.get(Calendar.YEAR));
            String month = String.format("%02d", calendar.get(Calendar.MONTH) + 1);
            String day = String.format("%02d", calendar.get(Calendar.DATE));
            String hour = String.format("%02d", calendar.get(Calendar.HOUR_OF_DAY));
            String minute = String.format("%02d", calendar.get(Calendar.MINUTE));
            String second = String.format("%02d", calendar.get(Calendar.SECOND));
            File bak = new File(rootDir, type.getName() + "_bak-" + String.join("", year, month, day) + "-" + String.join("", hour, minute, second) + ".zip");
            if (CaveUtils.archiveDirectory(dimDir, bak)) {
                message = new TextComponentTranslation("cavern.regeneration.backup.success", name);
                message.getStyle().setColor(TextFormatting.GRAY);
                server.getPlayerList().sendMessage(message);
            } else {
                message = new TextComponentTranslation("cavern.regeneration.backup.failed", name);
                message.getStyle().setColor(TextFormatting.RED);
                server.getPlayerList().sendMessage(message);
                return new RegenerationGuiMessage(EnumType.FAILED);
            }
        }
        try {
            FileUtils.deleteDirectory(dimDir);
        } catch (IOException e) {
            return new RegenerationGuiMessage(EnumType.FAILED);
        }
        message = new TextComponentTranslation("cavern.regeneration.regenerated", name);
        message.getStyle().setColor(TextFormatting.GRAY);
        server.getPlayerList().sendMessage(message);
        if (type.shouldLoadSpawn()) {
            world = server.getWorld(type.getId());
            try {
                world.saveAllChunks(true, null);
            } catch (MinecraftException e) {
            }
            world.flush();
        }
    }
    return new RegenerationGuiMessage(EnumType.REGENERATED);
}
Also used : DimensionType(net.minecraft.world.DimensionType) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) MinecraftException(net.minecraft.world.MinecraftException) Calendar(java.util.Calendar) ITextComponent(net.minecraft.util.text.ITextComponent) WorldServer(net.minecraft.world.WorldServer) RegenerationGuiMessage(cavern.network.client.RegenerationGuiMessage) TextComponentString(net.minecraft.util.text.TextComponentString) IOException(java.io.IOException) MinecraftServer(net.minecraft.server.MinecraftServer) TextComponentString(net.minecraft.util.text.TextComponentString) WorldEvent(net.minecraftforge.event.world.WorldEvent) EntityPlayer(net.minecraft.entity.player.EntityPlayer) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) File(java.io.File)

Example 7 with DimensionType

use of net.minecraft.world.DimensionType in project Cavern2 by kegare.

the class RegenerationMessage method toBytes.

@Override
public void toBytes(ByteBuf buf) {
    buf.writeBoolean(backup);
    buf.writeInt(dimensions.size());
    for (DimensionType type : dimensions) {
        if (type != null) {
            buf.writeInt(type.getId());
        }
    }
}
Also used : DimensionType(net.minecraft.world.DimensionType)

Example 8 with DimensionType

use of net.minecraft.world.DimensionType in project Cavern2 by kegare.

the class PlayerData method readFromNBT.

@Override
public void readFromNBT(NBTTagCompound nbt) {
    NBTTagList tagList = nbt.getTagList("LastTeleportTime", NBT.TAG_COMPOUND);
    if (tagList != null && tagList.tagCount() > 0) {
        for (int i = 0; i < tagList.tagCount(); ++i) {
            NBTTagCompound tag = tagList.getCompoundTagAt(i);
            DimensionType type = null;
            try {
                type = DimensionType.getById(tag.getInteger("Dim"));
            } catch (IllegalArgumentException e) {
                continue;
            }
            if (type != null) {
                lastTeleportTimes.put(type, tag.getLong("Time"));
            }
        }
    }
    lastSleepTime = nbt.getLong("LastSleepTime");
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) DimensionType(net.minecraft.world.DimensionType) NBTTagCompound(net.minecraft.nbt.NBTTagCompound)

Example 9 with DimensionType

use of net.minecraft.world.DimensionType in project Cavern2 by kegare.

the class PlayerData method writeToNBT.

@Override
public void writeToNBT(NBTTagCompound nbt) {
    NBTTagList tagList = new NBTTagList();
    for (Entry<DimensionType, Long> entry : lastTeleportTimes.entrySet()) {
        NBTTagCompound tag = new NBTTagCompound();
        tag.setInteger("Dim", entry.getKey().getId());
        tag.setLong("Time", entry.getValue().longValue());
    }
    nbt.setTag("LastTeleportTimes", tagList);
    nbt.setLong("LastSleepTime", lastSleepTime);
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) DimensionType(net.minecraft.world.DimensionType) NBTTagCompound(net.minecraft.nbt.NBTTagCompound)

Example 10 with DimensionType

use of net.minecraft.world.DimensionType in project Cavern2 by kegare.

the class CaveDimensions method register.

public static DimensionType register(String name, int id, Class<? extends WorldProvider> provider) {
    if (Strings.isNullOrEmpty(name) || id == 0 || DimensionManager.isDimensionRegistered(id)) {
        return null;
    }
    DimensionType type = DimensionType.register(name, "_" + name.replace(" ", "_").toLowerCase(), id, provider, false);
    DimensionManager.registerDimension(id, type);
    return type;
}
Also used : DimensionType(net.minecraft.world.DimensionType)

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