This commit is contained in:
huangyuhui
2016-05-08 09:41:27 +08:00
parent 8d5088d2e4
commit b232a255e7
26 changed files with 118 additions and 260 deletions

View File

@@ -20,12 +20,12 @@ package org.jackhuang.hellominecraft.svrmgr.server;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import org.jackhuang.hellominecraft.util.logging.HMCLog;
import org.jackhuang.hellominecraft.util.system.Compressor;
import org.jackhuang.hellominecraft.svrmgr.setting.SettingsManager;
import org.jackhuang.hellominecraft.svrmgr.util.Utilities;
import org.jackhuang.hellominecraft.util.func.Consumer;
import org.jackhuang.hellominecraft.util.system.FileUtils;
import org.jackhuang.hellominecraft.util.system.IOUtils;
@@ -39,9 +39,8 @@ public class BackupManager {
return Utilities.getGameDir() + "backups-HMCSM" + File.separator;
}
public static ArrayList<String> getBackupList() {
String gameDir = backupDir();
return IOUtils.findAllFile(new File(gameDir));
public static void getBackupList(Consumer<String> c) {
IOUtils.findAllFile(new File(backupDir()), c);
}
public static void addWorldBackup(final String folder) {
@@ -61,17 +60,15 @@ public class BackupManager {
t.start();
}
public static ArrayList<String> findAllWorlds() {
public static void findAllWorlds(Consumer<String> callback) {
String gameDir = Utilities.getGameDir();
ArrayList<String> folders = IOUtils.findAllDir(new File(gameDir));
ArrayList<String> result = new ArrayList<>();
for (String folder : folders) {
String worldPath = gameDir + folder + File.separator;
ArrayList<String> files = IOUtils.findAllFile(new File(worldPath));
if (files.contains("level.dat"))
result.add(folder);
}
return result;
IOUtils.findAllDir(new File(gameDir), folder -> {
String worldPath = gameDir + folder + File.separator;
IOUtils.findAllFile(new File(worldPath), f -> {
if ("level.dat".equals(f))
callback.accept(folder);
});
});
}
public static void restoreBackup(File backupFile) {
@@ -89,10 +86,10 @@ public class BackupManager {
}
public static void backupAllWorlds() {
ArrayList<String> al = findAllWorlds();
for (String world : al)
findAllWorlds(world -> {
if (!SettingsManager.settings.inactiveWorlds.contains(world))
addWorldBackup(world);
});
}
public static void backupAllPlugins() {

View File

@@ -90,7 +90,7 @@ public abstract class PlayerList<T extends BasePlayer> {
op = null;
if (txt.exists())
try {
initByText(FileUtils.readFileToStringIgnoreFileNotFound(txt));
initByText(FileUtils.readIgnoreFileNotFound(txt));
if (op != null)
player.addAll(op);
} catch (IOException e) {

View File

@@ -26,7 +26,6 @@ import java.io.File;
import java.io.IOException;
import org.jackhuang.hellominecraft.util.logging.HMCLog;
import org.jackhuang.hellominecraft.util.system.FileUtils;
import org.jackhuang.hellominecraft.util.system.IOUtils;
/**
*
@@ -37,13 +36,13 @@ public class SettingsManager {
public static Settings settings;
public static boolean isFirstLoad = false;
static Gson gson;
private static final File file = new File("hmcsm.json");
public static void load() {
gson = new Gson();
File file = new File(IOUtils.currentDir(), "hmcsm.json");
if (file.exists())
try {
String str = FileUtils.readFileToString(file);
String str = FileUtils.read(file);
if (str == null || str.trim().equals(""))
init();
else
@@ -64,9 +63,8 @@ public class SettingsManager {
}
public static void save() {
File f = new File(IOUtils.currentDir(), "hmcsm.json");
try {
FileUtils.write(f, gson.toJson(settings));
FileUtils.write(file, gson.toJson(settings));
} catch (IOException ex) {
HMCLog.err("Failed to save settings.", ex);
}

View File

@@ -2781,13 +2781,12 @@ public final class MainWindow extends javax.swing.JFrame
String path = Utilities.getPath("mods");
if (path == null)
return;
ArrayList<String> sl = IOUtils.findAllFile(new File(path));
DefaultTableModel model = (DefaultTableModel) lstExternalMods.getModel();
while (model.getRowCount() > 0)
model.removeRow(0);
for (String s : sl)
model.addRow(new Object[] { !SettingsManager.settings.inactiveExtMods.contains(s), s, ModType.getModTypeShowName(ModType.getModType(IOUtils.addSeparator(path) + s)) });
IOUtils.findAllFile(new File(path), s
-> model.addRow(new Object[] { !SettingsManager.settings.inactiveExtMods.contains(s), s, ModType.getModTypeShowName(ModType.getModType(IOUtils.addSeparator(path) + s)) })
);
lstExternalMods.updateUI();
}
@@ -2795,19 +2794,18 @@ public final class MainWindow extends javax.swing.JFrame
String path = Utilities.getPath("plugins");
if (path == null)
return;
ArrayList<String> sl = IOUtils.findAllFile(new File(path));
DefaultTableModel model = (DefaultTableModel) lstPlugins.getModel();
while (model.getRowCount() > 0)
model.removeRow(0);
for (String s : sl) {
PluginInformation p = PluginManager.getPluginYML(new File(Utilities.getGameDir() + "plugins" + File.separator + s));
if (p == null)
model.addRow(new Object[] { !SettingsManager.settings.inactivePlugins.contains(s), s,
"", "", "", "" });
else
model.addRow(new Object[] { !SettingsManager.settings.inactivePlugins.contains(s), s,
p.name, p.version, p.author, p.description });
}
IOUtils.findAllFile(new File(path), s -> {
PluginInformation p = PluginManager.getPluginYML(new File(Utilities.getGameDir() + "plugins" + File.separator + s));
if (p == null)
model.addRow(new Object[] { !SettingsManager.settings.inactivePlugins.contains(s), s,
"", "", "", "" });
else
model.addRow(new Object[] { !SettingsManager.settings.inactivePlugins.contains(s), s,
p.name, p.version, p.author, p.description });
});
lstPlugins.updateUI();
}
@@ -2816,37 +2814,35 @@ public final class MainWindow extends javax.swing.JFrame
String path = Utilities.getPath("coremods");
if (path == null)
return;
ArrayList<String> sl = IOUtils.findAllFile(new File(path));
DefaultTableModel model = (DefaultTableModel) lstCoreMods.getModel();
while (model.getRowCount() > 0)
model.removeRow(0);
for (String s : sl)
model.addRow(new Object[] { !SettingsManager.settings.inactiveCoreMods.contains(s), s, ModType.getModTypeShowName(ModType.getModType(IOUtils.addSeparator(path) + s)) });
IOUtils.findAllFile(new File(path), s
-> model.addRow(new Object[] { !SettingsManager.settings.inactiveCoreMods.contains(s), s, ModType.getModTypeShowName(ModType.getModType(IOUtils.addSeparator(path) + s)) }));
lstCoreMods.updateUI();
}
void loadWorlds() {
ArrayList<String> s = BackupManager.findAllWorlds();
DefaultTableModel model = (DefaultTableModel) lstWorlds.getModel();
if (SettingsManager.settings.inactiveWorlds == null)
SettingsManager.settings.inactiveWorlds = new ArrayList<>();
for (String world : s)
BackupManager.findAllWorlds(world -> {
model.addRow(new Object[] {
world, Utilities.getGameDir() + world, !SettingsManager.settings.inactiveWorlds.contains(world)
});
});
lstWorlds.updateUI();
}
void loadBackups() {
ArrayList<String> al = BackupManager.getBackupList();
DefaultTableModel model = (DefaultTableModel) lstBackups.getModel();
for (String backup : al) {
BackupManager.getBackupList(backup -> {
String[] names = FileUtils.getExtension(backup).split("\\+");
model.addRow(new Object[] {
names[0], names[1], names[2]
});
}
});
lstBackups.updateUI();
}
@@ -3007,17 +3003,16 @@ public final class MainWindow extends javax.swing.JFrame
}
void refreshInfos() {
ArrayList<String> al = IOUtils.findAllFile(new File(Utilities.getGameDir() + "infos-HMCSM"));
DefaultTableModel model = (DefaultTableModel) lstInfos.getModel();
for (String s : al)
model.addRow(new Object[] { s, FileUtils.getExtension(s) });
while (model.getRowCount() > 0)
model.removeRow(0);
IOUtils.findAllFile(new File(Utilities.getGameDir() + "infos-HMCSM"), s -> model.addRow(new Object[] { s, FileUtils.getExtension(s) }));
lstInfos.updateUI();
}
void refreshReports() {
ArrayList<String> al = IOUtils.findAllFile(new File(Utilities.getGameDir() + "crash-reports"));
for (String s : al)
lstCrashReportsModel.addElement(s);
lstCrashReportsModel.clear();
IOUtils.findAllFile(new File(Utilities.getGameDir() + "crash-reports"), s -> lstCrashReportsModel.addElement(s));
lstReports.setModel(lstCrashReportsModel);
}
@@ -3721,7 +3716,7 @@ public final class MainWindow extends javax.swing.JFrame
String id = (String) lstDownloads.getModel().getValueAt(lstDownloads.getSelectedRow(), 0);
final String MC_DOWNLOAD_URL = "https://s3.amazonaws.com/Minecraft.Download/versions/";
String url = MC_DOWNLOAD_URL + id + "/";
File serverjar = new File(IOUtils.currentDir(), "minecraft_server." + id + ".jar");
File serverjar = new File("minecraft_server." + id + ".jar");
serverjar.delete();
String downloadURL = url + "minecraft_server." + id + ".jar";
@@ -3739,7 +3734,7 @@ public final class MainWindow extends javax.swing.JFrame
if (index == -1)
return;
String path = Utilities.getGameDir() + "infos-HMCSM" + File.separator + model.getValueAt(index, 0);
String content = FileUtils.readFileToString(new File(path));
String content = FileUtils.read(new File(path));
txtInfo.setText(content);
} catch (IOException ex) {
HMCLog.warn("Failed to read info.", ex);
@@ -3747,14 +3742,14 @@ public final class MainWindow extends javax.swing.JFrame
}//GEN-LAST:event_btnShowInfoActionPerformed
private void btnAutoSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAutoSearchActionPerformed
ArrayList<String> al = IOUtils.findAllFile(IOUtils.currentDir());
for (String s : al)
if (ServerChecker.isServerJar(new File(s))) {
String path = IOUtils.tryGetCanonicalFilePath(new File(IOUtils.currentDir(), s));
txtMainJar.setText(path);
SettingsManager.settings.mainjar = path;
SettingsManager.save();
}
IOUtils.findAllFile(new File("."), s -> {
if (ServerChecker.isServerJar(new File(s))) {
String path = IOUtils.tryGetCanonicalFilePath(new File(s));
txtMainJar.setText(path);
SettingsManager.settings.mainjar = path;
SettingsManager.save();
}
});
}//GEN-LAST:event_btnAutoSearchActionPerformed
private void cboCategoryItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboCategoryItemStateChanged
@@ -3784,7 +3779,7 @@ public final class MainWindow extends javax.swing.JFrame
if (index == -1)
return;
String path = Utilities.getGameDir() + "crash-reports" + File.separator + lstCrashReportsModel.get(index);
String content = FileUtils.readFileToString(new File(path));
String content = FileUtils.read(new File(path));
txtCrashReport.setText(content);
} catch (IOException ex) {
HMCLog.warn("Failed to get crash-report.", ex);
@@ -3858,7 +3853,7 @@ public final class MainWindow extends javax.swing.JFrame
return;
}
BukkitVersion v = cb.get(idx);
File file = new File(IOUtils.currentDir(), "craftbukkit-" + ext + "-" + v.version + ".jar");
File file = new File("craftbukkit-" + ext + "-" + v.version + ".jar");
TaskWindow.factory().append(new FileDownloadTask(v.downloadLink, IOUtils.tryGetCanonicalFile(file)).setTag("bukkit-" + ext + "-" + v.version))
.create();
}//GEN-LAST:event_btnDownloadCraftbukkitActionPerformed
@@ -3869,7 +3864,7 @@ public final class MainWindow extends javax.swing.JFrame
return;
ForgeVersion v = mcpcPackages.get(cboCauldronMinecraft.getSelectedItem().toString()).get(idx);
String url;
File filepath = new File(IOUtils.currentDir(), "forge-installer.jar");
File filepath = new File("forge-installer.jar");
url = v.installer[1];
if (!TaskWindow.factory().append(new FileDownloadTask(url, filepath).setTag("cauldron-" + v.ver)).create())
MessageBox.Show(C.i18n("install.failed_download_forge"));
@@ -3879,7 +3874,7 @@ public final class MainWindow extends javax.swing.JFrame
private void installMCPC(final File filepath) {
try {
ForgeInstaller installer = new ForgeInstaller(IOUtils.currentDir(), filepath);
ForgeInstaller installer = new ForgeInstaller(new File("."), filepath);
installer.install();
MessageBox.Show(C.i18n("install.success"));
} catch (Exception e) {
@@ -3910,7 +3905,7 @@ public final class MainWindow extends javax.swing.JFrame
}//GEN-LAST:event_cboCauldronMinecraftItemStateChanged
private void btnInstallMCPCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInstallMCPCActionPerformed
File filepath = new File(IOUtils.currentDir(), "forge-installer.jar");
File filepath = new File("forge-installer.jar");
if (!filepath.exists()) {
MessageBox.Show("您还未下载Cauldron请点击下载按钮下载并自动安装");
return;