Search in sources :

Example 1 with MinecraftException

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

the class WorldManager method unloadWorld.

// TODO Result
public static boolean unloadWorld(WorldServer worldServer, boolean checkConfig) {
    checkNotNull(worldServer);
    final MinecraftServer server = SpongeImpl.getServer();
    // Likely leaked, don't want to drop leaked world data
    if (!worldByDimensionId.containsValue(worldServer)) {
        return false;
    }
    // Vanilla sometimes doesn't remove player entities from world first
    if (server.isServerRunning()) {
        if (!worldServer.playerEntities.isEmpty()) {
            return false;
        }
        // We only check config if base game wants to unload world. If mods/plugins say unload, we unload
        if (checkConfig) {
            if (((WorldProperties) worldServer.getWorldInfo()).doesKeepSpawnLoaded()) {
                return false;
            }
        }
    }
    try (PhaseContext<?> context = GeneralPhase.State.WORLD_UNLOAD.createPhaseContext().source(worldServer).buildAndSwitch()) {
        if (SpongeImpl.postEvent(SpongeEventFactory.createUnloadWorldEvent(Sponge.getCauseStackManager().getCurrentCause(), (org.spongepowered.api.world.World) worldServer))) {
            return false;
        }
        final IMixinWorldServer mixinWorldServer = (IMixinWorldServer) worldServer;
        final int dimensionId = mixinWorldServer.getDimensionId();
        try {
            // Don't save if server is stopping to avoid duplicate saving.
            if (server.isServerRunning()) {
                saveWorld(worldServer, true);
            }
            mixinWorldServer.getActiveConfig().save();
        } catch (MinecraftException e) {
            e.printStackTrace();
        } finally {
            worldByDimensionId.remove(dimensionId);
            weakWorldByWorld.remove(worldServer);
            ((IMixinMinecraftServer) server).removeWorldTickTimes(dimensionId);
            SpongeImpl.getLogger().info("Unloading world [{}] (DIM{})", worldServer.getWorldInfo().getWorldName(), dimensionId);
            reorderWorldsVanillaFirst();
        }
        if (!server.isServerRunning()) {
            unregisterDimension(dimensionId);
        }
    }
    return true;
}
Also used : IMixinMinecraftServer(org.spongepowered.common.interfaces.IMixinMinecraftServer) MinecraftException(net.minecraft.world.MinecraftException) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) World(net.minecraft.world.World) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) IMixinMinecraftServer(org.spongepowered.common.interfaces.IMixinMinecraftServer) MinecraftServer(net.minecraft.server.MinecraftServer)

Example 2 with MinecraftException

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

the class WorldManager method copyWorld.

public static CompletableFuture<Optional<WorldProperties>> copyWorld(WorldProperties worldProperties, String copyName) {
    checkArgument(worldPropertiesByFolderName.containsKey(worldProperties.getWorldName()), "World properties not registered!");
    checkArgument(!worldPropertiesByFolderName.containsKey(copyName), "Destination world name already is registered!");
    final WorldInfo info = (WorldInfo) worldProperties;
    final WorldServer worldServer = worldByDimensionId.get(((IMixinWorldInfo) info).getDimensionId().intValue());
    if (worldServer != null) {
        try {
            saveWorld(worldServer, true);
        } catch (MinecraftException e) {
            throw new RuntimeException(e);
        }
        ((IMixinMinecraftServer) SpongeImpl.getServer()).setSaveEnabled(false);
    }
    final CompletableFuture<Optional<WorldProperties>> future = SpongeImpl.getScheduler().submitAsyncTask(new CopyWorldTask(info, copyName));
    if (worldServer != null) {
        // World was loaded
        future.thenRun(() -> ((IMixinMinecraftServer) SpongeImpl.getServer()).setSaveEnabled(true));
    }
    return future;
}
Also used : IMixinMinecraftServer(org.spongepowered.common.interfaces.IMixinMinecraftServer) MinecraftException(net.minecraft.world.MinecraftException) Optional(java.util.Optional) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) WorldInfo(net.minecraft.world.storage.WorldInfo) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) WorldServer(net.minecraft.world.WorldServer)

Example 3 with MinecraftException

use of net.minecraft.world.MinecraftException 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 4 with MinecraftException

use of net.minecraft.world.MinecraftException in project MinecraftForge by MinecraftForge.

the class DimensionManager method unloadWorlds.

/*
    * To be called by the server at the appropriate time, do not call from mod code.
    */
public static void unloadWorlds(Hashtable<Integer, long[]> worldTickTimes) {
    for (int id : unloadQueue) {
        WorldServer w = worlds.get(id);
        try {
            if (w != null) {
                w.saveAllChunks(true, null);
            } else {
                FMLLog.warning("Unexpected world unload - world %d is already unloaded", id);
            }
        } catch (MinecraftException e) {
            e.printStackTrace();
        } finally {
            if (w != null) {
                MinecraftForge.EVENT_BUS.post(new WorldEvent.Unload(w));
                w.flush();
                setWorld(id, null, w.getMinecraftServer());
            }
        }
    }
    unloadQueue.clear();
}
Also used : MinecraftException(net.minecraft.world.MinecraftException) WorldEvent(net.minecraftforge.event.world.WorldEvent) WorldServer(net.minecraft.world.WorldServer)

Example 5 with MinecraftException

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

the class MixinMinecraftServer method saveAllWorlds.

/**
 * @author blood - June 2nd, 2016
 *
 * @reason To allow per-world auto-save tick intervals or disable auto-saving entirely
 *
 * @param dontLog Whether to log during saving
 */
@Overwrite
public void saveAllWorlds(boolean dontLog) {
    if (!this.enableSaving) {
        return;
    }
    for (WorldServer worldserver : this.worlds) {
        if (worldserver != null && !worldserver.disableLevelSaving) {
            // Sponge start - check auto save interval in world config
            if (this.isDedicatedServer() && this.isServerRunning()) {
                final IMixinWorldServer spongeWorld = (IMixinWorldServer) worldserver;
                final int autoSaveInterval = spongeWorld.getActiveConfig().getConfig().getWorld().getAutoSaveInterval();
                final boolean logAutoSave = spongeWorld.getActiveConfig().getConfig().getLogging().worldAutoSaveLogging();
                if (autoSaveInterval <= 0 || ((WorldProperties) worldserver.getWorldInfo()).getSerializationBehavior() != SerializationBehaviors.AUTOMATIC) {
                    if (logAutoSave) {
                        LOGGER.warn("Auto-saving has been disabled for level \'" + worldserver.getWorldInfo().getWorldName() + "\'/" + worldserver.provider.getDimensionType().getName() + ". " + "No chunk data will be auto-saved - to re-enable auto-saving set 'auto-save-interval' to a value greater than" + " zero in the corresponding world config.");
                    }
                    continue;
                }
                if (this.tickCounter % autoSaveInterval != 0) {
                    continue;
                }
                if (logAutoSave) {
                    LOGGER.info("Auto-saving chunks for level \'" + worldserver.getWorldInfo().getWorldName() + "\'/" + worldserver.provider.getDimensionType().getName());
                }
            } else if (!dontLog) {
                LOGGER.info("Saving chunks for level \'" + worldserver.getWorldInfo().getWorldName() + "\'/" + worldserver.provider.getDimensionType().getName());
            }
            // Sponge end
            try {
                WorldManager.saveWorld(worldserver, false);
            } catch (MinecraftException ex) {
                ex.printStackTrace();
            }
        }
    }
}
Also used : MinecraftException(net.minecraft.world.MinecraftException) WorldServer(net.minecraft.world.WorldServer) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Aggregations

MinecraftException (net.minecraft.world.MinecraftException)6 WorldServer (net.minecraft.world.WorldServer)4 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)3 MinecraftServer (net.minecraft.server.MinecraftServer)2 WorldEvent (net.minecraftforge.event.world.WorldEvent)2 WorldProperties (org.spongepowered.api.world.storage.WorldProperties)2 IMixinMinecraftServer (org.spongepowered.common.interfaces.IMixinMinecraftServer)2 RegenerationGuiMessage (cavern.network.client.RegenerationGuiMessage)1 File (java.io.File)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Optional (java.util.Optional)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)1 ITextComponent (net.minecraft.util.text.ITextComponent)1 TextComponentString (net.minecraft.util.text.TextComponentString)1 TextComponentTranslation (net.minecraft.util.text.TextComponentTranslation)1 DimensionType (net.minecraft.world.DimensionType)1