Browse Source

Fixed total payouts not clearing after a new day begins

MasterCake 4 years ago
parent
commit
1f73ce8875

+ 13 - 36
Plugin/src/main/java/de/Linus122/TimeIsMoney/Main.java

@@ -24,6 +24,7 @@ import org.bukkit.scheduler.BukkitWorker;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.FileReader;
 import java.io.IOException;
@@ -81,10 +82,6 @@ public class Main extends JavaPlugin {
 	 * The payouts listed in the config.
 	 */
 	private final List<Payout> payouts = new ArrayList<>();
-	/**
-	 * The payouts for the day.
-	 */
-	private HashMap<String, Double> payedMoney = new HashMap<>();
 	/**
 	 * The time online in seconds of each player by UUID.
 	 */
@@ -105,10 +102,6 @@ public class Main extends JavaPlugin {
 	 * The console logger.
 	 */
 	private final ConsoleCommandSender clogger = this.getServer().getConsoleSender();
-	/**
-	 * The current day.
-	 */
-	private int currentDay = 0;
 	/**
 	 * If actionbars are supported for the server's version.
 	 */
@@ -122,7 +115,6 @@ public class Main extends JavaPlugin {
 	public void onEnable() {
 		this.getCommand("timeismoney").setExecutor(new Cmd(this));
 		PL_VERSION = this.getDescription().getVersion();
-		currentDay = (new Date()).getDay();
 		this.reloadConfig();
 		
 		File config = new File("plugins/TimeIsMoney/config.yml");
@@ -153,7 +145,6 @@ public class Main extends JavaPlugin {
 		
 		if (getConfig().getBoolean("enable_atm")) new ATM(this);
 		
-		final int seconds = getConfig().getInt("give_money_every_second");
 		Bukkit.getScheduler().runTaskTimer(this, () -> {
 			try {
 				for (Player p : Bukkit.getOnlinePlayers()) {
@@ -165,7 +156,7 @@ public class Main extends JavaPlugin {
 					} else {
 						onlineSeconds.put(p.getUniqueId(), 1);
 					}
-					if (onlineSeconds.get(p.getUniqueId()) >= seconds) {
+					if (onlineSeconds.get(p.getUniqueId()) >=  getConfig().getInt("give_money_every_second")) {
 						pay(p);
 						onlineSeconds.remove(p.getUniqueId());
 					}
@@ -181,10 +172,10 @@ public class Main extends JavaPlugin {
         }
 		
 		Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
-			if (currentDay != new Date().getDay()) { //Next day, clear payouts!
-				log("Cleared all payouts");
-				payedMoney.clear();
-				currentDay = new Date().getDay();
+			if (PluginData.getLastRefreshDay() != new Date().getDay() && PluginData.getPayedMoney().size() > 0) { //Next day, clear payouts!
+				log("Cleared all payouts for last day");
+				PluginData.getPayedMoney().clear();
+				PluginData.setLastRefreshDay(new Date().getDay());
 			}
 		}, 20L * 60, 20L * 60 * 15);
 		setupEconomy();
@@ -194,14 +185,7 @@ public class Main extends JavaPlugin {
 		messageActionbar = finalconfig.getString("message_actionbar");
 		messageActionbar = CC(messageActionbar);
 		
-		try {
-			FileInputStream fis = new FileInputStream(new File("plugins/TimeIsMoney/payed_today.data"));
-			ObjectInputStream ois = new ObjectInputStream(fis);
-			payedMoney = (HashMap<String, Double>) ((HashMap<String, Double>) ois.readObject()).clone();
-			
-			ois.close();
-		} catch (Exception ignored) {
-		}
+		PluginData.loadData();
 		
 		loadPayouts();
 		
@@ -235,14 +219,7 @@ public class Main extends JavaPlugin {
 	 */
 	@Override
 	public void onDisable() {
-		FileOutputStream fos;
-		try {
-			fos = new FileOutputStream(new File("plugins/TimeIsMoney/payed_today.data"));
-			ObjectOutputStream oos = new ObjectOutputStream(fos);
-			oos.writeObject(payedMoney);
-			oos.close();
-		} catch (Exception ignored) {
-		}
+		PluginData.saveData();
 	}
 
 	/**
@@ -333,8 +310,8 @@ public class Main extends JavaPlugin {
 		
 		//REACHED MAX PAYOUT CHECK
 		double payed = 0;
-		if (payedMoney.containsKey(p.getName())) {
-			payed = payedMoney.get(p.getName());
+		if (PluginData.getPayedMoney().containsKey(p.getName())) {
+			payed = PluginData.getPayedMoney().get(p.getName());
 		}
 		
 		List<Payout> applicablePayouts = this.getApplicablePayoutsForPlayer(p);
@@ -460,10 +437,10 @@ public class Main extends JavaPlugin {
 		}
 		
 		//ADD PAYED MONEY
-		if (payedMoney.containsKey(p.getName())) {
-			payedMoney.put(p.getName(), payedMoney.get(p.getName()) + payout_amt);
+		if (PluginData.getPayedMoney().containsKey(p.getName())) {
+			PluginData.getPayedMoney().put(p.getName(), PluginData.getPayedMoney().get(p.getName()) + payout_amt);
 		} else {
-			payedMoney.put(p.getName(), payout_amt);
+			PluginData.getPayedMoney().put(p.getName(), payout_amt);
 		}
 		
 		lastLocation.put(p.getUniqueId(), p.getLocation());

+ 90 - 0
Plugin/src/main/java/de/Linus122/TimeIsMoney/PluginData.java

@@ -0,0 +1,90 @@
+package de.Linus122.TimeIsMoney;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.HashMap;
+
+public class PluginData {
+	/**
+	 * The payouts for the day.
+	 */
+	private static HashMap<String, Double> payedMoney = new HashMap<>();
+	/**
+	 * Day since last refresh
+	 */
+	private static int lastRefreshDay = 0;
+	
+
+	private final static String filePath = "plugins/TimeIsMoney/data/";
+	private final static File dataFile = new File(filePath + "payed_today.data");
+	
+	/**
+	 * Loads data from file if {@link #dataFile} exists.
+	 */
+	public static void loadData() {
+		if(!dataFile.exists()) return;
+		try {
+			FileInputStream fis = new FileInputStream(dataFile);
+			ObjectInputStream ois = new ObjectInputStream(fis);
+			Object payedMoneyObj = ois.readObject();
+			payedMoney = (HashMap<String, Double>) ((HashMap<String, Double>) payedMoneyObj).clone();
+			lastRefreshDay = ois.readInt();
+			ois.close();
+		} catch (IOException e1) {
+			// TODO Auto-generated catch block
+			e1.printStackTrace();
+		} catch (ClassNotFoundException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+
+	}
+	
+	/**
+	 * Saves the data on disc to file {@link #dataFile}
+	 */
+	public static void saveData() {
+		(new File(filePath)).mkdirs();
+		
+		try {
+			FileOutputStream fos = new FileOutputStream(dataFile);
+			ObjectOutputStream oos = new ObjectOutputStream(fos);
+			oos.writeObject(payedMoney);
+			oos.writeInt(lastRefreshDay);
+			oos.close();
+		} catch (FileNotFoundException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+	}
+	
+	/**
+	 * @return the lastRefreshDay
+	 */
+	public static int getLastRefreshDay() {
+		return lastRefreshDay;
+	}
+	
+	/**
+	 * @param lastRefreshDay the lastRefreshDay to set
+	 */
+	public static void setLastRefreshDay(int lastRefreshDay) {
+		PluginData.lastRefreshDay = lastRefreshDay;
+	}
+	
+	/**
+	 * @return the payedMoney
+	 */
+	public static HashMap<String, Double> getPayedMoney() {
+		return payedMoney;
+	}
+
+}