Browse Source

Fixed payouts not clearing after a new day begins.

MasterCake 4 years ago
parent
commit
614c88668d
1 changed files with 13 additions and 36 deletions
  1. 13 36
      Plugin/src/main/java/de/Linus122/TimeIsMoney/Main.java

+ 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());