From 8d04ec311b0e17f286b239eef3bae20fe08d6d59 Mon Sep 17 00:00:00 2001 From: LordTuxn Date: Tue, 21 Sep 2021 23:11:37 +0200 Subject: [PATCH] Added sftp/ftp schematic files synchronisation --- .../com/alpsbte/plotsystem/PlotSystem.java | 1 + .../plotsystem/core/config/ConfigPaths.java | 4 +++ .../core/system/plot/PlotManager.java | 25 ++++++++++++++++++- src/main/resources/defaultConfig.yml | 8 +++++- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/alpsbte/plotsystem/PlotSystem.java b/src/main/java/com/alpsbte/plotsystem/PlotSystem.java index f7339cd7..fecfc855 100644 --- a/src/main/java/com/alpsbte/plotsystem/PlotSystem.java +++ b/src/main/java/com/alpsbte/plotsystem/PlotSystem.java @@ -171,6 +171,7 @@ public void onEnable() { }); PlotManager.checkPlotsForLastActivity(); + PlotManager.syncPlotSchematicFiles(); pluginEnabled = true; Bukkit.getConsoleSender().sendMessage(""); diff --git a/src/main/java/com/alpsbte/plotsystem/core/config/ConfigPaths.java b/src/main/java/com/alpsbte/plotsystem/core/config/ConfigPaths.java index 2f465cb8..2181134f 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/config/ConfigPaths.java +++ b/src/main/java/com/alpsbte/plotsystem/core/config/ConfigPaths.java @@ -31,6 +31,10 @@ public abstract class ConfigPaths { public static final String ENABLE_SCORE_REQUIREMENT = "enable-score-requirement"; public static final String DEV_MODE = "dev-mode"; + private static final String SYNC_FTP_FILES = "sync-ftp-files."; + public static final String SYNC_FTP_FILES_ENABLED = SYNC_FTP_FILES + "enabled"; + public static final String SYNC_FTP_FILES_INTERVAL = SYNC_FTP_FILES + "sync-interval"; + // Database private static final String DATABASE = "database."; diff --git a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java index b600cb10..7916a953 100644 --- a/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java +++ b/src/main/java/com/alpsbte/plotsystem/core/system/plot/PlotManager.java @@ -25,6 +25,8 @@ package com.alpsbte.plotsystem.core.system.plot; import com.alpsbte.plotsystem.core.config.ConfigPaths; +import com.alpsbte.plotsystem.core.system.CityProject; +import com.alpsbte.plotsystem.core.system.Country; import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.WorldEditException; @@ -44,6 +46,7 @@ import com.alpsbte.plotsystem.utils.ftp.FTPManager; import org.bukkit.Bukkit; import org.bukkit.World; +import org.bukkit.configuration.file.FileConfiguration; import java.io.File; import java.io.FileInputStream; @@ -270,7 +273,7 @@ public static CompletableFuture convertTerraToPlotXZ(Plot plot, double } public static void checkPlotsForLastActivity() { - Bukkit.getScheduler().scheduleAsyncRepeatingTask(PlotSystem.getPlugin(), () -> { + Bukkit.getScheduler().runTaskTimerAsynchronously(PlotSystem.getPlugin(), () -> { try { List plots = getPlots(Status.unfinished); long millisIn14Days = 14L * 24 * 60 * 60 * 1000; // Remove all plots which have no activity for the last 14 days @@ -293,6 +296,26 @@ public static void checkPlotsForLastActivity() { }, 0L, 20 * 60 * 60); // Check every hour } + public static void syncPlotSchematicFiles() { + FileConfiguration config = PlotSystem.getPlugin().getConfigManager().getConfig(); + if (config.getBoolean(ConfigPaths.SYNC_FTP_FILES_ENABLED)) { + long interval = config.getLong(ConfigPaths.SYNC_FTP_FILES_INTERVAL); + + Bukkit.getScheduler().runTaskTimerAsynchronously(PlotSystem.getPlugin(), () -> { + CityProject.getCityProjects(false).forEach(c -> { + try { + if (c.getCountry().getServer().getFTPConfiguration() != null) { + List plots = PlotManager.getPlots(c.getID(), Status.unclaimed); + plots.forEach(Plot::getOutlinesSchematic); + } + } catch (SQLException ex) { + Bukkit.getLogger().log(Level.INFO, "A SQL error occurred!", ex); + } + }); + }, 0L, 20 * interval); + } + } + public static Plot getPlotByWorld(World plotWorld) throws SQLException { return new Plot(Integer.parseInt(plotWorld.getName().substring(2))); } diff --git a/src/main/resources/defaultConfig.yml b/src/main/resources/defaultConfig.yml index f903a07d..8369f920 100644 --- a/src/main/resources/defaultConfig.yml +++ b/src/main/resources/defaultConfig.yml @@ -21,6 +21,12 @@ enable-score-requirement: true # NOTE: Do not change if you do not know what you are doing dev-mode: false +# Automatic synchronisation of schematic files from SFTP/FTP servers +# [interval] -> default: 3600 seconds (1 hour) +sync-ftp-files: + enabled: true + sync-interval: 3600 + # ----------------------------------------------------- # | Supported database: MariaDB @@ -80,4 +86,4 @@ error-colour: §c # NOTE: Do not change -config-version: 1.1 \ No newline at end of file +config-version: 1.2 \ No newline at end of file