Skip to content

Commit 6617a79

Browse files
Added transaction logging
1 parent a632467 commit 6617a79

8 files changed

Lines changed: 119 additions & 13 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,4 @@ nb-configuration.xml
8989
##############################
9090
~$*
9191
*.tmp
92+
/libs/

src/main/java/net/mackenziemolloy/shopguiplus/sellgui/SellGUI.java

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.io.InputStream;
6-
import java.util.function.Supplier;
6+
import java.util.logging.FileHandler;
77
import java.util.logging.Level;
88
import java.util.logging.Logger;
99

10+
import net.mackenziemolloy.shopguiplus.sellgui.utility.FileUtils;
11+
import net.mackenziemolloy.shopguiplus.sellgui.utility.LogFormatter;
1012
import org.bukkit.Bukkit;
1113
import org.bukkit.ChatColor;
1214
import org.bukkit.command.CommandSender;
1315
import org.bukkit.configuration.InvalidConfigurationException;
14-
import org.bukkit.plugin.Plugin;
1516
import org.bukkit.plugin.PluginDescriptionFile;
16-
import org.bukkit.plugin.PluginManager;
1717
import org.bukkit.plugin.java.JavaPlugin;
1818

1919
import net.mackenziemolloy.shopguiplus.sellgui.command.CommandSellGUI;
@@ -24,6 +24,9 @@
2424

2525
public final class SellGUI extends JavaPlugin {
2626
private CommentedConfiguration configuration;
27+
private static SellGUI instance;
28+
public Logger fileLogger;
29+
private FileHandler handler;
2730
private String version;
2831

2932
public boolean compatible = false;
@@ -38,6 +41,8 @@ public SellGUI() {
3841

3942
@Override
4043
public void onEnable() {
44+
instance = this;
45+
4146
new CommandSellGUI(this).register();
4247
Logger logger = getLogger();
4348

@@ -49,12 +54,14 @@ public void onEnable() {
4954
generateFiles();
5055
setupMetrics();
5156
setupUpdates();
57+
58+
fileLogger = Logger.getLogger("SellGUIFileLogger");
59+
initLogger();
5260

5361
logger.info("*-*");
5462
logger.info("ShopGUIPlus SellGUI");
5563
logger.info("Made by Mackenzie Molloy");
5664
logger.info("*-*");
57-
5865
}
5966

6067
public void checkCompatibility() {
@@ -67,11 +74,21 @@ public void checkCompatibility() {
6774

6875
}
6976

70-
public String getSGPVersion() {
71-
Plugin plugin = getServer().getPluginManager().getPlugin("ShopGUIPlus");
72-
PluginDescriptionFile description = plugin.getDescription();
73-
return description.getVersion();
77+
public void initLogger() {
78+
File log = FileUtils.loadFile("transaction.log");
79+
FileHandler handler = null;
80+
81+
try {
82+
handler = new FileHandler(log.getAbsolutePath(), true);
83+
} catch (IOException ex) {
84+
ex.printStackTrace();
85+
}
86+
87+
handler.setFormatter(new LogFormatter());
88+
this.handler = handler;
7489

90+
fileLogger.addHandler(handler);
91+
fileLogger.setUseParentHandlers(false);
7592
}
7693

7794
public CommentedConfiguration getConfiguration() {
@@ -128,4 +145,9 @@ public void generateFiles() {
128145
logger.log(Level.SEVERE, "Failed to load the 'config.yml' file due to an error:", ex);
129146
}
130147
}
148+
149+
public static SellGUI getInstance() {
150+
return instance;
151+
}
152+
131153
}

src/main/java/net/mackenziemolloy/shopguiplus/sellgui/command/CommandSellGUI.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.logging.Logger;
1919
import java.util.stream.Collectors;
2020

21+
import net.brcdev.shopgui.ShopGuiPlugin;
2122
import net.mackenziemolloy.shopguiplus.sellgui.utility.sirblobman.HexColorUtility;
2223
import org.apache.commons.lang3.text.WordUtils;
2324
import org.bukkit.Bukkit;
@@ -51,7 +52,6 @@
5152

5253
import dev.triumphteam.gui.guis.Gui;
5354
import dev.triumphteam.gui.guis.GuiItem;
54-
import net.brcdev.shopgui.ShopGuiPlugin;
5555
import net.brcdev.shopgui.ShopGuiPlusApi;
5656
import net.brcdev.shopgui.economy.EconomyType;
5757
import net.brcdev.shopgui.event.ShopPostTransactionEvent;
@@ -406,7 +406,7 @@ private void onGuiClose(Player player, InventoryCloseEvent event, Set<Integer> i
406406

407407
Map<ItemStack, Map<Short, Integer>> soldMap2 = new HashMap<>();
408408
Map<EconomyType, Double> moneyMap = new EnumMap<>(EconomyType.class);
409-
409+
410410
double totalPrice = 0;
411411
int itemAmount = 0;
412412
boolean[] excessItems = {false};
@@ -460,7 +460,7 @@ private void onGuiClose(Player player, InventoryCloseEvent event, Set<Integer> i
460460
ShopPostTransactionEvent shopPostTransactionEvent =
461461
(ShopPostTransactionEvent) Class.forName("net.brcdev.shopgui.event.ShopPostTransactionEvent")
462462
.getDeclaredConstructor(ShopTransactionResult.class).newInstance(shopTransactionResult);
463-
463+
464464
Runnable task = () -> {
465465
Bukkit.getPluginManager().callEvent(shopPostTransactionEvent);
466466
};
@@ -550,7 +550,6 @@ private void onGuiClose(Player player, InventoryCloseEvent event, Set<Integer> i
550550
.replace("{price}", profitsFormatted));
551551

552552
receiptList.add(itemLine);
553-
//receiptList.append(itemLine);
554553
itemList.append(itemNameFormatted).append(", ");
555554
}
556555
}
@@ -589,7 +588,11 @@ private void onGuiClose(Player player, InventoryCloseEvent event, Set<Integer> i
589588
.replace("{list}", itemList.substring(0, itemList.length()-2))
590589
.replace("{amount}", itemAmountFormatted));
591590
}
592-
591+
592+
/* Subject to deprecation */
593+
plugin.fileLogger.info(player.getName() + " (" + player.getUniqueId() + ") sold: {" + HexColorUtility.purgeAllColor(receiptList.stream().collect(Collectors.joining(", "))) + "}");
594+
595+
593596
if(configuration.getBoolean("options.sell_titles")) {
594597
sendSellTitles(player, formattedPricing, itemAmountFormatted);
595598
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package net.mackenziemolloy.shopguiplus.sellgui.utility;
2+
3+
import java.io.*;
4+
import net.mackenziemolloy.shopguiplus.sellgui.SellGUI;
5+
6+
public class FileUtils {
7+
8+
public static void copy(InputStream in, File file) {
9+
try {
10+
OutputStream out = new FileOutputStream(file);
11+
byte[] buffer = new byte[1024];
12+
int length;
13+
while ((length = in.read(buffer)) > 0) {
14+
out.write(buffer, 0, length);
15+
}
16+
out.close();
17+
in.close();
18+
} catch (IOException ex) {
19+
ex.printStackTrace();
20+
}
21+
}
22+
23+
public static void mkdir(File file) {
24+
try {
25+
file.mkdir();
26+
} catch (Exception ex) {
27+
ex.printStackTrace();
28+
}
29+
}
30+
31+
public static File loadFile(String name) {
32+
if (!SellGUI.getInstance().getDataFolder().exists()) {
33+
FileUtils.mkdir(SellGUI.getInstance().getDataFolder());
34+
}
35+
36+
File f = new File(SellGUI.getInstance().getDataFolder(), name);
37+
if (!f.exists()) {
38+
FileUtils.copy(SellGUI.getInstance().getResource(name), f);
39+
}
40+
41+
return f;
42+
}
43+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package net.mackenziemolloy.shopguiplus.sellgui.utility;
2+
3+
import net.mackenziemolloy.shopguiplus.sellgui.SellGUI;
4+
5+
import java.text.DateFormat;
6+
import java.text.SimpleDateFormat;
7+
import java.util.Date;
8+
import java.util.logging.Formatter;
9+
import java.util.logging.LogRecord;
10+
11+
public class LogFormatter extends Formatter {
12+
13+
private final DateFormat dateFormat = new SimpleDateFormat(SellGUI.getInstance().getConfiguration().getString("options.date_format"));
14+
15+
@Override
16+
public String format(LogRecord logRecord) {
17+
return getDateAndTime() + " " + logRecord.getMessage() + "\n";
18+
}
19+
20+
private String getDateAndTime() {
21+
Date date = new Date();
22+
return dateFormat.format(date);
23+
}
24+
}

src/main/java/net/mackenziemolloy/shopguiplus/sellgui/utility/sirblobman/HexColorUtility.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,14 @@ private static Pattern getReplaceAllRgbPattern(char colorChar) {
8181

8282
return assembled.toString();
8383
}
84+
85+
public static String purgeAllColor(String input) {
86+
// Remove Minecraft color codes (e.g., &7)
87+
String noMinecraftColorCodes = input.replaceAll("[&§][0-9a-fA-FkKlLnNoOrR]", "");
88+
89+
// Remove hex color codes (e.g., &#FFFF0)
90+
String noHexColorCodes = noMinecraftColorCodes.replaceAll("[&§]#([0-9a-fA-F]){6}", "");
91+
92+
return noHexColorCodes;
93+
}
8494
}

src/main/resources/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ options:
2323
receipt_type: 1
2424
sell_titles: true
2525

26+
# Date format of transactions logging (http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)
27+
date_format: "yyyy/MM/dd HH:mm:ss"
28+
2629
#
2730
# Whether prices should be rounded to 2 decimal places, or not.
2831
# This also formats prices with comma separation. IE: 1800 -> 1,800

src/main/resources/transaction.log

Whitespace-only changes.

0 commit comments

Comments
 (0)