From 4ec548cb8d548f35a70b5c57ea765d85d23f683b Mon Sep 17 00:00:00 2001 From: DNx5 Date: Sat, 19 Sep 2015 13:27:31 +0700 Subject: [PATCH] added null check, #209 #210 --- src/main/java/fr/xephi/authme/api/NewAPI.java | 4 +- .../xephi/authme/cache/backup/JsonCache.java | 47 ++++++++++++++----- .../authme/process/join/AsyncronousJoin.java | 3 +- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/main/java/fr/xephi/authme/api/NewAPI.java b/src/main/java/fr/xephi/authme/api/NewAPI.java index 604d9d2d..b31f9ab2 100644 --- a/src/main/java/fr/xephi/authme/api/NewAPI.java +++ b/src/main/java/fr/xephi/authme/api/NewAPI.java @@ -34,9 +34,7 @@ public class NewAPI { /** * Hook into AuthMe - * - * @return - * + * * @return AuthMe plugin */ public static NewAPI getInstance() { diff --git a/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java b/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java index 3203fae5..dee7b743 100644 --- a/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java +++ b/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java @@ -1,6 +1,8 @@ package fr.xephi.authme.cache.backup; +import com.google.common.base.Charsets; import com.google.common.io.BaseEncoding; +import com.google.common.io.Files; import com.google.gson.*; import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; @@ -18,7 +20,6 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.lang.reflect.Type; -import java.nio.file.Files; public class JsonCache { @@ -61,7 +62,8 @@ public class JsonCache { try { String data = gson.toJson(playerData); - Files.write(file.toPath(), data.getBytes()); + Files.touch(file); + Files.write(data, file, Charsets.UTF_8); } catch (IOException e) { e.printStackTrace(); } @@ -76,9 +78,12 @@ public class JsonCache { } File file = new File(cacheDir, path + File.separator + "cache.json"); + if (!file.exists()) { + return null; + } + try { - byte[] bytes = Files.readAllBytes(file.toPath()); - String str = new String(bytes); + String str = Files.toString(file, Charsets.UTF_8); return gson.fromJson(str, DataFileCache.class); } catch (Exception e) { e.printStackTrace(); @@ -142,17 +147,37 @@ public class JsonCache { @Override public DataFileCache deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { JsonObject jsonObject = jsonElement.getAsJsonObject(); - String group = jsonObject.get("group").getAsString(); - boolean operator = jsonObject.get("operator").getAsBoolean(); - boolean flying = jsonObject.get("flying").getAsBoolean(); + if (jsonObject == null) { + return null; + } + JsonElement e; + String group = null; + boolean operator = false; + boolean flying = false; + + if ((e = jsonObject.get("group")) != null) { + group = e.getAsString(); + } + if ((e = jsonObject.get("operator")) != null) { + operator = e.getAsBoolean(); + } + if ((e = jsonObject.get("flying")) != null) { + flying = e.getAsBoolean(); + } JsonArray arr; + ItemStack[] inv = null; + ItemStack[] armour = null; - arr = jsonObject.get("inventory").getAsJsonArray(); - ItemStack[] inv = getItems(arr); + if (jsonObject.has("inventory")) { + arr = jsonObject.get("inventory").getAsJsonArray(); + inv = getItems(arr); + } - arr = jsonObject.get("armour").getAsJsonArray(); - ItemStack[] armour = getItems(arr); + if (jsonObject.has("armour")) { + arr = jsonObject.get("armour").getAsJsonArray(); + armour = getItems(arr); + } return new DataFileCache(inv, armour, group, operator, flying); } diff --git a/src/main/java/fr/xephi/authme/process/join/AsyncronousJoin.java b/src/main/java/fr/xephi/authme/process/join/AsyncronousJoin.java index a9e7d603..a75b91b4 100644 --- a/src/main/java/fr/xephi/authme/process/join/AsyncronousJoin.java +++ b/src/main/java/fr/xephi/authme/process/join/AsyncronousJoin.java @@ -195,7 +195,8 @@ public class AsyncronousJoin { }); } - } catch (NullPointerException ex) { + } catch (Exception ex) { + ex.printStackTrace(); } } String[] msg;