ZipLibrary static abuse removal

This commit is contained in:
nossr50 2019-09-23 20:03:16 -07:00
parent 1c2d1fa56f
commit 2735cb1666
2 changed files with 33 additions and 25 deletions

View File

@ -99,6 +99,7 @@ public class mcMMO extends JavaPlugin {
private ItemTools itemTools; private ItemTools itemTools;
private PermissionTools permissionTools; private PermissionTools permissionTools;
private WorldGuardUtils worldGuardUtils; private WorldGuardUtils worldGuardUtils;
private ZipLibrary zipLibrary;
/* Never-Ending tasks */ /* Never-Ending tasks */
private BleedTimerTask bleedTimerTask; private BleedTimerTask bleedTimerTask;
@ -316,7 +317,8 @@ public class mcMMO extends JavaPlugin {
if (getConfigManager().getConfigAutomatedBackups().isZipBackupsEnabled()) { if (getConfigManager().getConfigAutomatedBackups().isZipBackupsEnabled()) {
// Remove other tasks BEFORE starting the Backup, or we just cancel it straight away. // Remove other tasks BEFORE starting the Backup, or we just cancel it straight away.
try { try {
ZipLibrary.mcMMOBackup(); zipLibrary = new ZipLibrary(this);
zipLibrary.mcMMOBackup();
} catch (IOException e) { } catch (IOException e) {
getLogger().severe(e.toString()); getLogger().severe(e.toString());
} catch (Throwable e) { } catch (Throwable e) {

View File

@ -15,28 +15,34 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
public class ZipLibrary { public class ZipLibrary {
private static String BACKUP_DIRECTORY = mcMMO.getMainDirectory() + "backup" + File.separator; private final mcMMO pluginRef;
private static File BACKUP_DIR = new File(BACKUP_DIRECTORY);
private static File FLAT_FILE_DIRECTORY = new File(mcMMO.getFlatFileDirectory());
private static File MOD_FILE_DIRECTORY = new File(mcMMO.getModDirectory());
/* private static File CONFIG_FILE = new File(mcMMO.getMainDirectory() + "config.yml");
private static File EXPERIENCE_FILE = new File(mcMMO.getMainDirectory() + "experience.yml");
//private static File TREASURE_FILE = new File(mcMMO.getMainDirectory() + "treasures.yml");
private static File ADVANCED_FILE = new File(mcMMO.getMainDirectory() + "advanced.yml");
private static File REPAIR_FILE = new File(mcMMO.getMainDirectory() + "repair.vanilla.yml");*/
public static void mcMMOBackup() throws IOException { private String BACKUP_DIRECTORY;
if (mcMMO.getMySQLConfigSettings().isMySQLEnabled()) { private File BACKUP_DIR;
mcMMO.p.debug("This server is running in SQL Mode."); private File FLAT_FILE_DIRECTORY;
mcMMO.p.debug("Only config files will be backed up."); private File MOD_FILE_DIRECTORY;
public ZipLibrary(mcMMO pluginRef) {
this.pluginRef = pluginRef;
BACKUP_DIRECTORY = pluginRef.getMainDirectory() + "backup" + File.separator;
BACKUP_DIR = new File(BACKUP_DIRECTORY);
FLAT_FILE_DIRECTORY = new File(pluginRef.getFlatFileDirectory());
MOD_FILE_DIRECTORY = new File(pluginRef.getModDirectory());
}
public void mcMMOBackup() throws IOException {
if (pluginRef.getMySQLConfigSettings().isMySQLEnabled()) {
pluginRef.debug("This server is running in SQL Mode.");
pluginRef.debug("Only config files will be backed up.");
} }
try { try {
if (BACKUP_DIR.mkdir()) { if (BACKUP_DIR.mkdir()) {
mcMMO.p.debug("Created Backup Directory."); pluginRef.debug("Created Backup Directory.");
} }
} catch (Exception e) { } catch (Exception e) {
mcMMO.p.getLogger().severe(e.toString()); pluginRef.getLogger().severe(e.toString());
} }
// Generate the proper date for the backup filename // Generate the proper date for the backup filename
@ -48,19 +54,19 @@ public class ZipLibrary {
List<File> sources = new ArrayList<>(); List<File> sources = new ArrayList<>();
sources.add(FLAT_FILE_DIRECTORY); sources.add(FLAT_FILE_DIRECTORY);
sources.addAll(mcMMO.getConfigManager().getConfigFiles()); //Config File Backups sources.addAll(pluginRef.getConfigManager().getConfigFiles()); //Config File Backups
if (MOD_FILE_DIRECTORY.exists()) { if (MOD_FILE_DIRECTORY.exists()) {
sources.add(MOD_FILE_DIRECTORY); sources.add(MOD_FILE_DIRECTORY);
} }
// Actually do something // Actually do something
mcMMO.p.debug("Backing up your mcMMO Configuration... "); pluginRef.debug("Backing up your mcMMO Configuration... ");
packZip(fileZip, sources); packZip(fileZip, sources);
} }
private static void packZip(File output, List<File> sources) throws IOException { private void packZip(File output, List<File> sources) throws IOException {
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(output)); ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(output));
zipOut.setLevel(Deflater.DEFAULT_COMPRESSION); zipOut.setLevel(Deflater.DEFAULT_COMPRESSION);
@ -74,10 +80,10 @@ public class ZipLibrary {
zipOut.flush(); zipOut.flush();
zipOut.close(); zipOut.close();
mcMMO.p.debug("Backup Completed."); pluginRef.debug("Backup Completed.");
} }
private static String buildPath(String path, String file) { private String buildPath(String path, String file) {
if (path == null || path.isEmpty()) { if (path == null || path.isEmpty()) {
return file; return file;
} }
@ -85,9 +91,9 @@ public class ZipLibrary {
return path + File.separator + file; return path + File.separator + file;
} }
private static void zipDir(ZipOutputStream zos, String path, File dir) throws IOException { private void zipDir(ZipOutputStream zos, String path, File dir) throws IOException {
if (!dir.canRead()) { if (!dir.canRead()) {
mcMMO.p.getLogger().severe("Cannot read " + dir.getCanonicalPath() + " (Maybe because of permissions?)"); pluginRef.getLogger().severe("Cannot read " + dir.getCanonicalPath() + " (Maybe because of permissions?)");
return; return;
} }
@ -103,9 +109,9 @@ public class ZipLibrary {
} }
} }
private static void zipFile(ZipOutputStream zos, String path, File file) throws IOException { private void zipFile(ZipOutputStream zos, String path, File file) throws IOException {
if (!file.canRead()) { if (!file.canRead()) {
mcMMO.p.getLogger().severe("Cannot read " + file.getCanonicalPath() + "(File Permissions?)"); pluginRef.getLogger().severe("Cannot read " + file.getCanonicalPath() + "(File Permissions?)");
return; return;
} }