PluginData.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package de.Linus122.TimeIsMoney;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9. import java.util.HashMap;
  10. public class PluginData {
  11. /**
  12. * The payouts for the day.
  13. */
  14. private static HashMap<String, Double> payedMoney = new HashMap<>();
  15. /**
  16. * Day since last refresh
  17. */
  18. private static int lastRefreshDay = 0;
  19. private final static String filePath = "plugins/TimeIsMoney/data/";
  20. private final static File dataFile = new File(filePath + "payed_today.data");
  21. /**
  22. * Loads data from file if {@link #dataFile} exists.
  23. */
  24. public static void loadData() {
  25. if(!dataFile.exists()) return;
  26. try {
  27. FileInputStream fis = new FileInputStream(dataFile);
  28. ObjectInputStream ois = new ObjectInputStream(fis);
  29. Object payedMoneyObj = ois.readObject();
  30. payedMoney = (HashMap<String, Double>) ((HashMap<String, Double>) payedMoneyObj).clone();
  31. lastRefreshDay = ois.readInt();
  32. ois.close();
  33. } catch (IOException e1) {
  34. // TODO Auto-generated catch block
  35. e1.printStackTrace();
  36. } catch (ClassNotFoundException e) {
  37. // TODO Auto-generated catch block
  38. e.printStackTrace();
  39. }
  40. }
  41. /**
  42. * Saves the data on disc to file {@link #dataFile}
  43. */
  44. public static void saveData() {
  45. (new File(filePath)).mkdirs();
  46. try {
  47. FileOutputStream fos = new FileOutputStream(dataFile);
  48. ObjectOutputStream oos = new ObjectOutputStream(fos);
  49. oos.writeObject(payedMoney);
  50. oos.writeInt(lastRefreshDay);
  51. oos.close();
  52. } catch (FileNotFoundException e) {
  53. // TODO Auto-generated catch block
  54. e.printStackTrace();
  55. } catch (IOException e) {
  56. // TODO Auto-generated catch block
  57. e.printStackTrace();
  58. }
  59. }
  60. /**
  61. * @return the lastRefreshDay
  62. */
  63. public static int getLastRefreshDay() {
  64. return lastRefreshDay;
  65. }
  66. /**
  67. * @param lastRefreshDay the lastRefreshDay to set
  68. */
  69. public static void setLastRefreshDay(int lastRefreshDay) {
  70. PluginData.lastRefreshDay = lastRefreshDay;
  71. }
  72. /**
  73. * @return the payedMoney
  74. */
  75. public static HashMap<String, Double> getPayedMoney() {
  76. return payedMoney;
  77. }
  78. }