Skip to content

Commit a0b6868

Browse files
committed
Closed Ghost-chu#718 Ghost-chu#873; Impl MySQL logs feature
1 parent 5ed6071 commit a0b6868

35 files changed

+398
-163
lines changed

src/main/java/org/maxgamer/quickshop/QuickShop.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -505,17 +505,21 @@ public boolean loadEcon() {
505505
}
506506
}
507507

508-
/**
509-
* Logs the given string to qs.log, if QuickShop is configured to do so.
510-
*
511-
* @param s The string to log. It will be prefixed with the date and time.
512-
*/
513-
public void log(@NotNull String s) {
514-
Util.debugLog("[SHOP LOG] " + s);
515-
if (this.getLogWatcher() == null) {
516-
return;
517-
}
518-
this.getLogWatcher().log(s);
508+
// /**
509+
// * Logs the given string to qs.log, if QuickShop is configured to do so.
510+
// *
511+
// * @param s The string to log. It will be prefixed with the date and time.
512+
// */
513+
// public void log(@NotNull String s) {
514+
// Util.debugLog("[SHOP LOG] " + s);
515+
// if (this.getLogWatcher() == null) {
516+
// return;
517+
// }
518+
// this.getLogWatcher().log(s);
519+
// }
520+
521+
public void logEvent(@NotNull Object eventObject){
522+
getDatabaseHelper().insertHistoryRecord(eventObject);
519523
}
520524

521525
@Override
@@ -955,6 +959,14 @@ public void run() {
955959
shopPurger.runTaskAsynchronously(this);
956960
Util.debugLog("Now using display-type: " + DisplayItem.getNowUsing().name());
957961
getLogger().info("QuickShop Loaded! " + enableTimer.stopAndGetTimePassed() + " ms.");
962+
963+
// TODO: Test code
964+
getDatabaseHelper().insertHistoryRecord(new TestRecord());
965+
}
966+
967+
static class TestRecord{
968+
private String test = "Hello World!";
969+
private ShopChunk chunk = new ShopChunk("hello!",2,3);
958970
}
959971

960972
private void loadItemMatcher() {

src/main/java/org/maxgamer/quickshop/command/subcommand/SubCommand_Clean.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.maxgamer.quickshop.shop.Shop;
2929
import org.maxgamer.quickshop.util.MsgUtil;
3030
import org.maxgamer.quickshop.util.Util;
31+
import org.maxgamer.quickshop.util.logging.container.ShopRemoveLog;
3132

3233
import java.util.ArrayList;
3334
import java.util.List;
@@ -65,7 +66,7 @@ public void onCommand(@NotNull CommandSender sender, @NotNull String commandLabe
6566
}
6667

6768
for (Shop shop : pendingRemoval) {
68-
plugin.log("Deleting shop " + shop + " as requested by the /qs clean command.");
69+
plugin.logEvent(new ShopRemoveLog(Util.getSenderUniqueId(sender),"/qs clean",shop.saveToInfoStorage()));
6970
shop.delete();
7071
}
7172

src/main/java/org/maxgamer/quickshop/command/subcommand/SubCommand_CleanGhost.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.maxgamer.quickshop.shop.Shop;
3030
import org.maxgamer.quickshop.util.MsgUtil;
3131
import org.maxgamer.quickshop.util.Util;
32+
import org.maxgamer.quickshop.util.logging.container.ShopRemoveLog;
3233

3334
@AllArgsConstructor
3435
public class SubCommand_CleanGhost implements CommandHandler<CommandSender> {
@@ -70,23 +71,23 @@ public void onCommand(@NotNull CommandSender sender, @NotNull String commandLabe
7071
if (shop.getItem().getType() == Material.AIR) {
7172
MsgUtil.sendDirectMessage(sender,
7273
ChatColor.YELLOW + "Deleting shop " + shop + " because of corrupted item data.");
73-
plugin.log("Deleting shop " + shop + " as requested by the /qs cleanghost command.");
74+
plugin.logEvent(new ShopRemoveLog(Util.getSenderUniqueId(sender),"/qs cleanghost command",shop.saveToInfoStorage()));
7475
Util.mainThreadRun(shop::delete);
7576
continue;
7677
}
7778
if (!shop.getLocation().isWorldLoaded()) {
7879
MsgUtil.sendDirectMessage(sender,
7980
ChatColor.YELLOW + "Deleting shop " + shop + " because the its world is not loaded.");
8081
Util.mainThreadRun(shop::delete);
81-
plugin.log("Deleting shop " + shop + " as requested by the /qs cleanghost command.");
82+
plugin.logEvent(new ShopRemoveLog(Util.getSenderUniqueId(sender),"/qs cleanghost command",shop.saveToInfoStorage()));
8283
continue;
8384
}
8485
//noinspection ConstantConditions
8586
if (shop.getOwner() == null) {
8687
MsgUtil.sendDirectMessage(sender,
8788
ChatColor.YELLOW + "Deleting shop " + shop + " because of corrupted owner data.");
8889
Util.mainThreadRun(shop::delete);
89-
plugin.log("Deleting shop " + shop + " as requested by the /qs cleanghost command.");
90+
plugin.logEvent(new ShopRemoveLog(Util.getSenderUniqueId(sender),"/qs cleanghost command",shop.saveToInfoStorage()));
9091
continue;
9192
}
9293
// Shop exist check

src/main/java/org/maxgamer/quickshop/command/subcommand/SubCommand_Paste.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ private boolean pasteToPastebin(@NotNull CommandSender sender) {
7373
String pasteResult = paste.paste(pasteText);
7474
if (pasteResult != null) {
7575
sender.sendMessage(pasteResult);
76-
plugin.log(pasteResult);
7776
return true;
7877
}
7978
return false;

src/main/java/org/maxgamer/quickshop/command/subcommand/SubCommand_Remove.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.maxgamer.quickshop.QuickShop;
2828
import org.maxgamer.quickshop.command.CommandHandler;
2929
import org.maxgamer.quickshop.shop.Shop;
30+
import org.maxgamer.quickshop.util.logging.container.ShopRemoveLog;
3031

3132
@AllArgsConstructor
3233
public class SubCommand_Remove implements CommandHandler<Player> {
@@ -52,7 +53,7 @@ public void onCommand(@NotNull Player sender, @NotNull String commandLabel, @Not
5253
if (shop.getModerator().isModerator(sender.getUniqueId())
5354
|| QuickShop.getPermissionManager().hasPermission(sender, "quickshop.other.destroy")) {
5455
shop.delete();
55-
plugin.log("Deleting shop " + shop + " as requested by the /qs remove command.");
56+
plugin.logEvent(new ShopRemoveLog(sender.getUniqueId(),"/qs remove command",shop.saveToInfoStorage()));
5657
} else {
5758
plugin.text().of(sender, "no-permission").send();
5859
}

src/main/java/org/maxgamer/quickshop/command/subcommand/SubCommand_RemoveAll.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.maxgamer.quickshop.QuickShop;
2929
import org.maxgamer.quickshop.command.CommandHandler;
3030
import org.maxgamer.quickshop.shop.Shop;
31+
import org.maxgamer.quickshop.util.Util;
32+
import org.maxgamer.quickshop.util.logging.container.ShopRemoveLog;
3133

3234
import java.util.ArrayList;
3335
import java.util.Collections;
@@ -65,7 +67,7 @@ public void onCommand(@NotNull CommandSender sender, @NotNull String commandLabe
6567
}
6668
for (Shop shop : tempList) {
6769
if (shop.getOwner().equals(shopOwner.getUniqueId())) {
68-
plugin.log("Deleting shop " + shop + " as requested by the /qs removeall command.");
70+
plugin.logEvent(new ShopRemoveLog(Util.getSenderUniqueId(sender),"Deleting shop " + shop + " as requested by the /qs removeall command.",shop.saveToInfoStorage()));
6971
shop.delete();
7072
i++;
7173
}
@@ -81,7 +83,7 @@ public void onCommand(@NotNull CommandSender sender, @NotNull String commandLabe
8183
}
8284
for (Shop shop : tempList) {
8385
if (shop.getOwner().equals(((OfflinePlayer) sender).getUniqueId())) {
84-
plugin.log("Deleting shop " + shop + " as requested by the /qs removeall command.");
86+
plugin.logEvent(new ShopRemoveLog(Util.getSenderUniqueId(sender),"Deleting shop " + shop + " as requested by the /qs removeall command.",shop.saveToInfoStorage()));
8587
shop.delete();
8688
i++;
8789
}

src/main/java/org/maxgamer/quickshop/command/subcommand/SubCommand_SilentRemove.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.maxgamer.quickshop.command.CommandHandler;
2727
import org.maxgamer.quickshop.shop.Shop;
2828
import org.maxgamer.quickshop.util.Util;
29+
import org.maxgamer.quickshop.util.logging.container.ShopRemoveLog;
2930

3031
import java.util.UUID;
3132

@@ -54,7 +55,7 @@ public void onCommand(@NotNull Player sender, @NotNull String commandLabel, @Not
5455
return;
5556
}
5657

57-
plugin.log("Deleting shop " + shop + " as requested by the /qs silentremove command from the control panel.");
58+
plugin.logEvent(new ShopRemoveLog(sender.getUniqueId(),"/qs silentremove command",shop.saveToInfoStorage()));
5859
shop.delete();
5960
}
6061
}

src/main/java/org/maxgamer/quickshop/database/DataTypeMapping.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,12 @@
33
import lombok.Getter;
44

55
public enum DataTypeMapping {
6-
TINYINT("tinyint", "integer"),
7-
SMALLINT("smallint", "integer"),
8-
MEDIUMINT("mediumint", "integer"),
96
INT("int", "integer"),
107
BIGINT("bigint", "integer"),
118
FLOAT("float", "float"),
129
DOUBLE("double", "float"),
1310
VARCHAR("varchar", "varchar"),
14-
TINYBLOB("tinyblob", "blob"),
15-
TINYTEXT("tinytext", "tinytext"),
16-
BLOB("blob", "blob"),
1711
TEXT("text", "text"),
18-
MEDIUMBLOB("mediumblob", "blob"),
19-
MEDIUMTEXT("mediumtext", "text"),
2012
LONGBLOB("longblob", "blob"),
2113
LONGTEXT("longtext", "text");
2214

src/main/java/org/maxgamer/quickshop/database/DatabaseHelper.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.maxgamer.quickshop.QuickShop;
2727
import org.maxgamer.quickshop.shop.Shop;
2828
import org.maxgamer.quickshop.shop.ShopModerator;
29+
import org.maxgamer.quickshop.util.JsonUtil;
2930
import org.maxgamer.quickshop.util.Util;
3031
import org.maxgamer.quickshop.util.reload.ReloadResult;
3132
import org.maxgamer.quickshop.util.reload.ReloadStatus;
@@ -65,6 +66,9 @@ private void init() throws SQLException {
6566
if (!manager.hasTable(plugin.getDbPrefix() + "messages")) {
6667
createMessagesTable();
6768
}
69+
if(!manager.hasTable(plugin.getDbPrefix() + "logs")){
70+
createLogsTable();
71+
}
6872
checkColumns();
6973
}
7074

@@ -93,6 +97,14 @@ private void createMessagesTable() {
9397
manager.runInstantTask(new DatabaseTask(createTable));
9498
}
9599

100+
private void createLogsTable(){
101+
String createTable = "CREATE TABLE " + plugin.getDbPrefix()
102+
+ "logs (time BIGINT(32) NOT NULL);";
103+
manager.runInstantTask(new DatabaseTask(createTable));
104+
createColumn("logs", "classname", new DataType(DataTypeMapping.TEXT, null, ""));
105+
createColumn("logs", "data", new DataType(DataTypeMapping.LONGTEXT, null, ""));
106+
}
107+
96108

97109
/**
98110
* Verifies that all required columns exist.
@@ -128,6 +140,12 @@ public void onFailed(SQLException e) {
128140
.getDbPrefix() + "messages MODIFY COLUMN message text CHARACTER SET utf8mb4 NOT NULL AFTER owner", checkTask));
129141
manager.runInstantTask(new DatabaseTask("ALTER TABLE " + plugin
130142
.getDbPrefix() + "shops MODIFY COLUMN itemConfig text CHARACTER SET utf8mb4 NOT NULL AFTER price", checkTask));
143+
manager.runInstantTask(new DatabaseTask("ALTER TABLE " + plugin
144+
.getDbPrefix() + "shops TO CHARACTER SET uft8mb4 COLLATE utf8mb4_general_ci", checkTask));
145+
manager.runInstantTask(new DatabaseTask("ALTER TABLE " + plugin
146+
.getDbPrefix() + "messages TO CHARACTER SET uft8mb4 COLLATE utf8mb4_general_ci", checkTask));
147+
manager.runInstantTask(new DatabaseTask("ALTER TABLE " + plugin
148+
.getDbPrefix() + "history TO CHARACTER SET uft8mb4 COLLATE utf8mb4_general_ci", checkTask));
131149
}
132150
plugin.getLogger().info("Finished!");
133151
}
@@ -220,8 +238,6 @@ public void onFailed(SQLException e) {
220238
}
221239

222240
public void removeShop(Shop shop) {
223-
plugin.log("[DATABASE HELPER] Removing shop in the database: " + shop.toString());
224-
bakeTraceIfNeeded();
225241
String sqlString = "DELETE FROM "
226242
+ plugin.getDbPrefix()
227243
+ "shops WHERE x = ? AND y = ? AND z = ? AND world = ?"
@@ -237,8 +253,6 @@ public void removeShop(Shop shop) {
237253
}
238254

239255
public void removeShop(String world, int x, int y, int z) {
240-
plugin.log("[DATABASE HELPER] Removing shop in the database: " + world + "/" + x + "/" + y + "z" + z);
241-
bakeTraceIfNeeded();
242256
String sqlString = "DELETE FROM "
243257
+ plugin.getDbPrefix()
244258
+ "shops WHERE x = ? AND y = ? AND z = ? AND world = ?"
@@ -321,14 +335,16 @@ public void updateShop(@NotNull String owner, @NotNull ItemStack item, int unlim
321335

322336
}
323337

324-
private void bakeTraceIfNeeded() {
325-
if (plugin.getConfig().getBoolean("debug.shop-deletion")) {
326-
for (StackTraceElement stackTraceElement : new Exception().getStackTrace()) {
327-
plugin.log("at [" + stackTraceElement.getClassName() + "]" +
328-
"[" + stackTraceElement.getMethodName() + "] (" + stackTraceElement.getLineNumber() + ") - "
329-
+ stackTraceElement.getFileName());
330-
}
331-
}
338+
public void insertHistoryRecord(Object record){
339+
String sqlString = "INSERT INTO " + plugin.getDbPrefix() + "logs (time, classname, data) VALUES (?, ?, ?)";
340+
manager.addDelayTask(
341+
new DatabaseTask(
342+
sqlString,
343+
(ps) -> {
344+
ps.setLong(1,System.currentTimeMillis());
345+
ps.setString(2, record.getClass().getName());
346+
ps.setString(3, JsonUtil.getGson().toJson(record));
347+
}));
332348
}
333349

334350
/**

src/main/java/org/maxgamer/quickshop/economy/EconomyTransaction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.maxgamer.quickshop.event.EconomyCommitEvent;
2929
import org.maxgamer.quickshop.util.CalculateUtil;
3030
import org.maxgamer.quickshop.util.Util;
31+
import org.maxgamer.quickshop.util.logging.container.EconomyTransactionLog;
3132

3233
import java.util.ArrayList;
3334
import java.util.List;
@@ -261,6 +262,7 @@ default boolean onCommit(@NotNull EconomyTransaction economyTransaction) {
261262
*/
262263
default void onSuccess(@NotNull EconomyTransaction economyTransaction) {
263264
Util.debugLog("Transaction succeed.");
265+
QuickShop.getInstance().logEvent(new EconomyTransactionLog(true,economyTransaction));
264266
}
265267

266268
/**
@@ -272,6 +274,7 @@ default void onSuccess(@NotNull EconomyTransaction economyTransaction) {
272274
*/
273275
default void onFailed(@NotNull EconomyTransaction economyTransaction) {
274276
Util.debugLog("Transaction failed: " + economyTransaction.getLastError() + ".");
277+
QuickShop.getInstance().logEvent(new EconomyTransactionLog(false,economyTransaction));
275278
}
276279

277280
/**
@@ -283,6 +286,7 @@ default void onFailed(@NotNull EconomyTransaction economyTransaction) {
283286
*/
284287
default void onTaxFailed(@NotNull EconomyTransaction economyTransaction) {
285288
Util.debugLog("Tax Transaction failed: " + economyTransaction.getLastError() + ".");
289+
QuickShop.getInstance().logEvent(new EconomyTransactionLog(false,economyTransaction));
286290
}
287291

288292
}

0 commit comments

Comments
 (0)