use of cavern.network.client.RegenerationGuiMessage 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);
}
Aggregations