diff options
author | Draknyte1 <Draknyte1@hotmail.com> | 2016-08-26 22:18:56 +1000 |
---|---|---|
committer | Draknyte1 <Draknyte1@hotmail.com> | 2016-08-26 22:18:56 +1000 |
commit | 20f3ef5c6b35554118ad395e477b8d263ffbf936 (patch) | |
tree | 2c054f0b48ef246db39da6100f10e33ca43d2c7b /src/Java/miscutil/core/util | |
parent | e0f72471c5da950378a3102b4f5de781b5442ed8 (diff) | |
download | GT5-Unofficial-20f3ef5c6b35554118ad395e477b8d263ffbf936.tar.gz GT5-Unofficial-20f3ef5c6b35554118ad395e477b8d263ffbf936.tar.bz2 GT5-Unofficial-20f3ef5c6b35554118ad395e477b8d263ffbf936.zip |
% Initial changes to internal handling of PlayerCache.dat
> Cache is now populated upon player login.
Diffstat (limited to 'src/Java/miscutil/core/util')
-rw-r--r-- | src/Java/miscutil/core/util/player/PlayerCache.java | 109 |
1 files changed, 70 insertions, 39 deletions
diff --git a/src/Java/miscutil/core/util/player/PlayerCache.java b/src/Java/miscutil/core/util/player/PlayerCache.java index 5969766556..d1c40ec3fc 100644 --- a/src/Java/miscutil/core/util/player/PlayerCache.java +++ b/src/Java/miscutil/core/util/player/PlayerCache.java @@ -6,12 +6,14 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; +import java.io.ObjectInputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; import java.util.Properties; +import java.util.UUID; import miscutil.core.lib.CORE; import miscutil.core.util.Utils; @@ -23,16 +25,21 @@ public class PlayerCache { public static final void initCache() { if (CORE.PlayerCache == null || CORE.PlayerCache.equals(null)){ try { - CORE.PlayerCache = PlayerCache.readPropertiesFileAsMap(); - Utils.LOG_INFO("Loaded PlayerCache.dat"); + + if (cache != null){ + CORE.PlayerCache = PlayerCache.readPropertiesFileAsMap(); + Utils.LOG_INFO("Loaded PlayerCache.dat"); + } + + } catch (Exception e) { Utils.LOG_INFO("Failed to initialise PlayerCache.dat"); - PlayerCache.createPropertiesFile("CACHE_FILE_=", "THIS_CONTAINS_PLAYERDATA"); + PlayerCache.createPropertiesFile("PLAYER_", "DATA"); //e.printStackTrace(); } } } - + public static void createPropertiesFile(String playerName, String playerUUIDasString) { try { Properties props = new Properties(); @@ -47,25 +54,25 @@ public class PlayerCache { } public static void appendParamChanges(String playerName, String playerUUIDasString) { - Properties properties = new Properties(); - try { - Utils.LOG_WARNING("Attempting to load "+cache.getName()); - properties.load(new FileInputStream(cache)); - if (properties == null || properties.equals(null)){ - Utils.LOG_WARNING("Null properties"); - } - else { - Utils.LOG_WARNING("Loaded PlayerCache.dat"); - properties.setProperty(playerName+"_", playerUUIDasString); - FileOutputStream fr=new FileOutputStream(cache); - properties.store(fr, "Player Cache."); - fr.close(); - } - - } catch (IOException e) { - Utils.LOG_WARNING("No PlayerCache file found, creating one."); - createPropertiesFile(playerName, playerUUIDasString); - } + Properties properties = new Properties(); + try { + Utils.LOG_INFO("Attempting to load "+cache.getName()); + properties.load(new FileInputStream(cache)); + if (properties == null || properties.equals(null)){ + Utils.LOG_WARNING("Null properties"); + } + else { + Utils.LOG_INFO("Loaded PlayerCache.dat"); + properties.setProperty(playerName+"_", playerUUIDasString); + FileOutputStream fr=new FileOutputStream(cache); + properties.store(fr, "Player Cache."); + fr.close(); + } + + } catch (IOException e) { + Utils.LOG_INFO("No PlayerCache file found, creating one."); + createPropertiesFile(playerName, playerUUIDasString); + } } /** @@ -81,7 +88,8 @@ public class PlayerCache { * @return The Map that contains the key/value pairs. * @throws Exception */ - public static Map<String, String> readPropertiesFileAsMap() throws Exception { + @Deprecated + public static Map<String, String> readPropertiesFileAsMapOld() throws Exception { String delimiter = "="; @SuppressWarnings({ "rawtypes", "unchecked" }) Map<String, String> map = new HashMap(); @@ -103,25 +111,48 @@ public class PlayerCache { return map; } - public static String lookupPlayerByUUID(String UUID){ - try { - Map<String, String> map = null; + public static HashMap<String, UUID> readPropertiesFileAsMap() { + HashMap<String, UUID> map = null; + try + { + FileInputStream fis = new FileInputStream(cache); + ObjectInputStream ois = new ObjectInputStream(fis); + map = (HashMap) ois.readObject(); + ois.close(); + fis.close(); + }catch(IOException ioe) + { + ioe.printStackTrace(); + return null; + }catch(ClassNotFoundException c) + { + System.out.println("Class not found"); + c.printStackTrace(); + return null; + } + System.out.println("Deserialized HashMap.."); + return map; + } + + public static String lookupPlayerByUUID(UUID UUID){ try { - map = readPropertiesFileAsMap(); - } catch (Exception e) { + Map<String, UUID> map = null; + try { + map = readPropertiesFileAsMap(); + } catch (Exception e) { + Utils.LOG_ERROR("With "+e.getCause()+" as cause, Caught Exception: "+e.toString()); + //e.printStackTrace(); + } + for (Entry<String, UUID> entry : map.entrySet()) { + if (Objects.equals(UUID, entry.getValue())) { + return entry.getKey(); + } + } + return null; + } catch (NullPointerException e) { Utils.LOG_ERROR("With "+e.getCause()+" as cause, Caught Exception: "+e.toString()); //e.printStackTrace(); } - for (Entry<String, String> entry : map.entrySet()) { - if (Objects.equals(UUID, entry.getValue())) { - return entry.getKey(); - } - } return null; - } catch (NullPointerException e) { - Utils.LOG_ERROR("With "+e.getCause()+" as cause, Caught Exception: "+e.toString()); - //e.printStackTrace(); } - return null; -} } |