Added logging for total amount of money added/removed from econ per material using VillagerUI
This commit is contained in:
parent
647fcd389a
commit
af5fb054cc
|
|
@ -93,7 +93,11 @@ public class SellGUI extends GUIMerchant {
|
|||
return;
|
||||
}
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
if (!inventory.containsAtLeast(new ItemStack(purchase.material()), purchase.amount())) {
|
||||
if (Arrays.stream(inventory.getContents())
|
||||
.filter(Objects::nonNull)
|
||||
.filter(itemStack -> itemStack.getType().equals(purchase.material()))
|
||||
.mapToInt(ItemStack::getAmount).sum()
|
||||
< purchase.amount()) {
|
||||
player.sendMiniMessage(Config.NOT_ENOUGH_ITEMS, List.of(
|
||||
Template.template("type", purchase.material().name()),
|
||||
Template.template("amount", String.valueOf(purchase.amount()))));
|
||||
|
|
|
|||
|
|
@ -7,10 +7,8 @@ import com.alttd.config.Config;
|
|||
import com.alttd.config.VillagerConfig;
|
||||
import com.alttd.config.WorthConfig;
|
||||
import com.alttd.database.Queries;
|
||||
import com.alttd.events.LoginEvent;
|
||||
import com.alttd.events.LogoutEvent;
|
||||
import com.alttd.events.VehicleEvent;
|
||||
import com.alttd.events.VillagerEvents;
|
||||
import com.alttd.events.*;
|
||||
import com.alttd.logging.LogInOut;
|
||||
import com.alttd.objects.EconUser;
|
||||
import com.alttd.util.Logger;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
|
|
@ -23,6 +21,7 @@ public class VillagerUI extends JavaPlugin {
|
|||
|
||||
public static VillagerUI instance;
|
||||
private Economy economy = null;
|
||||
private LogInOut logInOut;
|
||||
|
||||
public static VillagerUI getInstance() {
|
||||
return instance;
|
||||
|
|
@ -35,6 +34,7 @@ public class VillagerUI extends JavaPlugin {
|
|||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
logInOut = new LogInOut();
|
||||
registerEvents();
|
||||
new CommandManager();
|
||||
Config.reload();
|
||||
|
|
@ -56,6 +56,7 @@ public class VillagerUI extends JavaPlugin {
|
|||
Logger.info("Syncing %", econUser.getUuid().toString());
|
||||
Queries.updateUserPoints(econUser.getUuid(), econUser.getPointsMap());
|
||||
});
|
||||
logInOut.run();
|
||||
}
|
||||
|
||||
private void scheduleTasks() {
|
||||
|
|
@ -74,6 +75,7 @@ public class VillagerUI extends JavaPlugin {
|
|||
});
|
||||
}
|
||||
}.runTaskTimerAsynchronously(getInstance(), 0L, 10 * 60 * 20L);
|
||||
logInOut.runTaskTimerAsynchronously(this, 20 * 60 * 5, 20 * 60 * 10);
|
||||
}
|
||||
|
||||
private void registerEvents() {
|
||||
|
|
@ -82,6 +84,7 @@ public class VillagerUI extends JavaPlugin {
|
|||
getServer().getPluginManager().registerEvents(new LogoutEvent(), this);
|
||||
getServer().getPluginManager().registerEvents(new LoginEvent(), this);
|
||||
getServer().getPluginManager().registerEvents(new VehicleEvent(), this);
|
||||
getServer().getPluginManager().registerEvents(new SpawnShopListener(logInOut), this);
|
||||
}
|
||||
|
||||
public Economy getEconomy() {
|
||||
|
|
|
|||
20
src/main/java/com/alttd/events/SpawnShopListener.java
Normal file
20
src/main/java/com/alttd/events/SpawnShopListener.java
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package com.alttd.events;
|
||||
|
||||
import com.alttd.logging.LogInOut;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class SpawnShopListener implements Listener {
|
||||
|
||||
private final LogInOut log;
|
||||
public SpawnShopListener(LogInOut log) {
|
||||
this.log = log;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onSpawnShopEvent(SpawnShopEvent event) {
|
||||
log.log(event.item().name(), event.price());
|
||||
}
|
||||
|
||||
}
|
||||
130
src/main/java/com/alttd/logging/LogInOut.java
Normal file
130
src/main/java/com/alttd/logging/LogInOut.java
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
package com.alttd.logging;
|
||||
|
||||
import com.alttd.VillagerUI;
|
||||
import com.alttd.config.Config;
|
||||
import com.alttd.util.Logger;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class LogInOut extends BukkitRunnable {
|
||||
|
||||
private HashMap<String, Double> map;
|
||||
private int day;
|
||||
private File file;
|
||||
|
||||
public LogInOut() {
|
||||
day = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
|
||||
file = new File(getPath());
|
||||
if (file.exists())
|
||||
map = loadFile(file);
|
||||
}
|
||||
|
||||
public void log(String material, double cost) {
|
||||
if (map.containsKey(material))
|
||||
map.put(material, map.get(material) + cost);
|
||||
else
|
||||
map.put(material, cost);
|
||||
}
|
||||
|
||||
private HashMap<String, Double> loadFile(File file) {
|
||||
HashMap<String, Double> map = new HashMap<>();
|
||||
try {
|
||||
Scanner scanner = new Scanner(file);
|
||||
while (scanner.hasNextLine()) {
|
||||
String line = scanner.nextLine();
|
||||
String[] split = line.split(": ");
|
||||
if (split.length != 2) {
|
||||
Logger.warning("Invalid entry in money-used log: %", line);
|
||||
continue;
|
||||
}
|
||||
map.put(split[0], Double.parseDouble(split[1]));
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (this.isCancelled())
|
||||
return;
|
||||
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()) {
|
||||
boolean success = false;
|
||||
try {
|
||||
success = file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (!success) {
|
||||
Logger.warning("Unable to log money used because the file couldn't be created");
|
||||
return;
|
||||
}
|
||||
}
|
||||
try {
|
||||
FileWriter fileWriter = new FileWriter(file.getPath(), false);
|
||||
String text = getText();
|
||||
if (text != null)
|
||||
fileWriter.write(text);
|
||||
else if (Config.DEBUG)
|
||||
Logger.info("Didn't write to used money log as map was empty");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (new_day != day) {
|
||||
day = new_day;
|
||||
map.clear();
|
||||
file = new File(getPath());
|
||||
}
|
||||
}
|
||||
|
||||
private String getText() {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (String key : map.keySet())
|
||||
stringBuilder.append(key).append(map.get(key)).append("\n");
|
||||
return stringBuilder.length() > 2 ? stringBuilder.substring(0, stringBuilder.length() - 1) : null;
|
||||
}
|
||||
|
||||
private String getPath() {
|
||||
return VillagerUI.getInstance().getDataFolder() + File.separator + "logs" + File.separator + getDateStringYYYYMMDD() + "-money-used-log.txt";
|
||||
}
|
||||
|
||||
private String getDateStringYYYYMMDD() {
|
||||
|
||||
int day = LocalDate.now().getDayOfMonth();
|
||||
int month = LocalDate.now().getMonthValue();
|
||||
int year = LocalDate.now().getYear();
|
||||
|
||||
String date = "";
|
||||
|
||||
date = date.concat(String.valueOf(year));
|
||||
date = date.concat("-");
|
||||
|
||||
if (month < 10) {
|
||||
date = date.concat("0");
|
||||
}
|
||||
date = date.concat(String.valueOf(month));
|
||||
date = date.concat("-");
|
||||
|
||||
if (day < 10) {
|
||||
date = date.concat("0");
|
||||
}
|
||||
date = date.concat(String.valueOf(day));
|
||||
|
||||
return date;
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user