From 4f0be27f0a7d56c49d13f2c64c8404e99ce9f21b Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sat, 29 Aug 2020 08:35:19 +1200 Subject: [PATCH] Try avoid issues with preferences.json --- .../me/libraryaddict/disguise/DisguiseConfig.java | 4 +++- .../disguise/utilities/DisguiseUtilities.java | 12 +++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java b/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java index df822a02..9dc422cd 100644 --- a/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java +++ b/src/main/java/me/libraryaddict/disguise/DisguiseConfig.java @@ -286,7 +286,9 @@ public class DisguiseConfig { } private static void doUpdaterTask() { - boolean startTask = isAutoUpdate() || isNotifyUpdate(); + boolean startTask = isAutoUpdate() || isNotifyUpdate() || "1592".equals( + (LibsPremium.getPaidInformation() == null ? LibsPremium.getPluginInformation() : + LibsPremium.getPaidInformation()).getUserID()); // Don't ever run the auto updater on a custom build.. if (!LibsDisguises.getInstance().isNumberedBuild()) { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index 41c6c052..b576bc63 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -62,7 +62,9 @@ import org.bukkit.util.Vector; import java.io.*; import java.lang.reflect.*; import java.nio.charset.StandardCharsets; +import java.nio.file.CopyOption; import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.nio.file.StandardOpenOption; import java.util.*; import java.util.concurrent.TimeUnit; @@ -232,6 +234,7 @@ public class DisguiseUtilities { } File viewPreferences = new File(LibsDisguises.getInstance().getDataFolder(), "preferences.json"); + File viewPreferencesTemp = new File(LibsDisguises.getInstance().getDataFolder(), "preferences-temp.json"); HashMap> map = new HashMap<>(); map.put("selfdisguise", getViewSelf()); @@ -240,8 +243,9 @@ public class DisguiseUtilities { String json = getGson().toJson(map); try { - viewPreferences.delete(); - Files.write(viewPreferences.toPath(), json.getBytes(), StandardOpenOption.CREATE); + Files.write(viewPreferencesTemp.toPath(), json.getBytes()); + Files.move(viewPreferencesTemp.toPath(), viewPreferences.toPath(), StandardCopyOption.REPLACE_EXISTING, + StandardCopyOption.ATOMIC_MOVE); } catch (IOException e) { e.printStackTrace(); } @@ -274,8 +278,10 @@ public class DisguiseUtilities { getViewBar().clear(); map.get("notifybar").forEach(uuid -> getViewBar().add(UUID.fromString(uuid))); } - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); + getLogger().warning("preferences.json has been deleted as its corrupt"); + viewPreferences.delete(); } }