Fixed file permission, and not creating folders. Fixed rounding
This commit is contained in:
parent
95204f9daf
commit
55b675086a
|
|
@ -3,6 +3,7 @@ package com.alttd.logging;
|
||||||
import com.alttd.VillagerUI;
|
import com.alttd.VillagerUI;
|
||||||
import com.alttd.config.Config;
|
import com.alttd.config.Config;
|
||||||
import com.alttd.util.Logger;
|
import com.alttd.util.Logger;
|
||||||
|
import com.alttd.util.Utilities;
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
@ -16,15 +17,18 @@ import java.util.Scanner;
|
||||||
|
|
||||||
public class LogInOut extends BukkitRunnable {
|
public class LogInOut extends BukkitRunnable {
|
||||||
|
|
||||||
private HashMap<String, Double> map;
|
private final HashMap<String, Double> map;
|
||||||
private int day;
|
private int day;
|
||||||
private File file;
|
private File file;
|
||||||
|
|
||||||
public LogInOut() {
|
public LogInOut() {
|
||||||
|
createLogsDir();
|
||||||
day = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
|
day = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
|
||||||
file = new File(getPath());
|
file = new File(getPath());
|
||||||
if (file.exists())
|
if (file.exists())
|
||||||
map = loadFile(file);
|
map = loadFile(file);
|
||||||
|
else
|
||||||
|
map = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void log(String material, double cost) {
|
public void log(String material, double cost) {
|
||||||
|
|
@ -58,10 +62,6 @@ public class LogInOut extends BukkitRunnable {
|
||||||
if (this.isCancelled())
|
if (this.isCancelled())
|
||||||
return;
|
return;
|
||||||
int new_day = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
|
int new_day = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
|
||||||
if (!file.canRead() || !file.canWrite()) {
|
|
||||||
Logger.warning("Unable to log money used due to incorrect file permissions");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
boolean success = false;
|
boolean success = false;
|
||||||
try {
|
try {
|
||||||
|
|
@ -75,12 +75,13 @@ public class LogInOut extends BukkitRunnable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
FileWriter fileWriter = new FileWriter(file.getPath(), false);
|
FileWriter fileWriter = new FileWriter(file, false);
|
||||||
String text = getText();
|
String text = getText();
|
||||||
if (text != null)
|
if (text != null)
|
||||||
fileWriter.write(text);
|
fileWriter.write(text);
|
||||||
else if (Config.DEBUG)
|
else if (Config.DEBUG)
|
||||||
Logger.info("Didn't write to used money log as map was empty");
|
Logger.info("Didn't write to used money log as map was empty");
|
||||||
|
fileWriter.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
@ -94,10 +95,16 @@ public class LogInOut extends BukkitRunnable {
|
||||||
private String getText() {
|
private String getText() {
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
for (String key : map.keySet())
|
for (String key : map.keySet())
|
||||||
stringBuilder.append(key).append(map.get(key)).append("\n");
|
stringBuilder.append(key).append(": ").append(Utilities.round(map.get(key), 2)).append("\n");
|
||||||
return stringBuilder.length() > 2 ? stringBuilder.substring(0, stringBuilder.length() - 1) : null;
|
return stringBuilder.length() > 2 ? stringBuilder.substring(0, stringBuilder.length() - 1) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void createLogsDir() {
|
||||||
|
File file = new File(VillagerUI.getInstance().getDataFolder() + File.separator + "logs");
|
||||||
|
if (!file.exists()) if (!file.mkdir())
|
||||||
|
Logger.warning("Unable to create logs folder");
|
||||||
|
}
|
||||||
|
|
||||||
private String getPath() {
|
private String getPath() {
|
||||||
return VillagerUI.getInstance().getDataFolder() + File.separator + "logs" + File.separator + getDateStringYYYYMMDD() + "-money-used-log.txt";
|
return VillagerUI.getInstance().getDataFolder() + File.separator + "logs" + File.separator + getDateStringYYYYMMDD() + "-money-used-log.txt";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user