Skip to content

Commit cdae04a

Browse files
committed
Closed Ghost-chu#912, Impl the shop purger
1 parent df72109 commit cdae04a

File tree

8 files changed

+160
-2
lines changed

8 files changed

+160
-2
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ public class QuickShop extends JavaPlugin {
263263
private WorldEditAdapter worldEditAdapter;
264264
@Getter
265265
private TextManager textManager;
266+
@Getter
267+
private ShopPurger shopPurger;
266268

267269
/**
268270
* Use for mock bukkit
@@ -949,6 +951,8 @@ public void run() {
949951
}
950952
calendarWatcher = new CalendarWatcher(this);
951953
calendarWatcher.start();
954+
this.shopPurger = new ShopPurger(this,false);
955+
shopPurger.runTaskAsynchronously(this);
952956
Util.debugLog("Now using display-type: " + DisplayItem.getNowUsing().name());
953957
getLogger().info("QuickShop Loaded! " + enableTimer.stopAndGetTimePassed() + " ms.");
954958
}

src/main/java/org/maxgamer/quickshop/command/CommandManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ public CommandManager(QuickShop plugin) {
326326
.permission("quickshop.toggledisplay")
327327
.executor(new SubCommand_ToggleDisplay(plugin))
328328
.build());
329+
registerCmd(CommandContainer.builder()
330+
.prefix("purge")
331+
.permission("quickshop.purge")
332+
.executor(new SubCommand_ToggleDisplay(plugin))
333+
.build());
329334
// registerCmd(CommandContainer.builder()
330335
// .prefix("backup")
331336
// .permission("quickshop.backup")
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is a part of project QuickShop, the name is SubCommand_Info.java
3+
* Copyright (C) PotatoCraft Studio and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License as published by the
7+
* Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
* for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*
18+
*/
19+
20+
package org.maxgamer.quickshop.command.subcommand;
21+
22+
import lombok.AllArgsConstructor;
23+
import org.bukkit.command.CommandSender;
24+
import org.jetbrains.annotations.NotNull;
25+
import org.maxgamer.quickshop.QuickShop;
26+
import org.maxgamer.quickshop.command.CommandHandler;
27+
28+
@AllArgsConstructor
29+
public class SubCommand_Purge implements CommandHandler<CommandSender> {
30+
31+
private final QuickShop plugin;
32+
33+
@Override
34+
public void onCommand(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] cmdArg) {
35+
plugin.getShopPurger().runTaskAsynchronously(plugin);
36+
plugin.text().of(sender,"shop-purged-start").send();
37+
}
38+
39+
40+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.maxgamer.quickshop.shop;
2+
3+
import lombok.AllArgsConstructor;
4+
import org.bukkit.Bukkit;
5+
import org.bukkit.OfflinePlayer;
6+
import org.bukkit.scheduler.BukkitRunnable;
7+
import org.maxgamer.quickshop.QuickShop;
8+
import org.maxgamer.quickshop.economy.EconomyTransaction;
9+
import org.maxgamer.quickshop.util.Util;
10+
11+
import java.time.temporal.ChronoUnit;
12+
13+
@AllArgsConstructor
14+
public class ShopPurger extends BukkitRunnable {
15+
private QuickShop plugin;
16+
private volatile boolean executing;
17+
18+
@Override
19+
public void run() {
20+
Util.ensureThread(true);
21+
if (executing) {
22+
plugin.getLogger().info("[Shop Purger] Another purge task still running!");
23+
return;
24+
}
25+
executing = true;
26+
if (!plugin.getConfig().getBoolean("purge.enabled")) return;
27+
Util.debugLog("[Shop Purger] Scanning and removing shops");
28+
int days = plugin.getConfig().getInt("purge.days", 360);
29+
boolean deleteBanned = plugin.getConfig().getBoolean("purge.banned");
30+
boolean skipOp = plugin.getConfig().getBoolean("purge.skip-op");
31+
boolean returnCreationFee = plugin.getConfig().getBoolean("purge.return-create-fee");
32+
for (Shop shop : plugin.getShopManager().getAllShops()) {
33+
OfflinePlayer player = Bukkit.getOfflinePlayer(shop.getOwner());
34+
if (!player.hasPlayedBefore()) {
35+
Util.debugLog("Shop " + shop + " detection skipped: Owner never played before.");
36+
continue;
37+
}
38+
long lastPlayed = player.getLastPlayed();
39+
if (lastPlayed == 0) {
40+
continue;
41+
}
42+
if (player.isOnline()) {
43+
continue;
44+
}
45+
if (player.isOp() && skipOp) {
46+
continue;
47+
}
48+
boolean markDeletion = player.isBanned() && deleteBanned;
49+
//noinspection ConstantConditions
50+
long noOfDaysBetween = ChronoUnit.DAYS.between(Util.getDateTimeFromTimestamp(lastPlayed), Util.getDateTimeFromTimestamp(System.currentTimeMillis()));
51+
if (noOfDaysBetween > days)
52+
markDeletion = true;
53+
if (!markDeletion)
54+
continue;
55+
plugin.getLogger().info("[Shop Purger] Shop " + shop + " has been purged.");
56+
shop.delete(false);
57+
if (returnCreationFee) {
58+
EconomyTransaction transaction =
59+
EconomyTransaction.builder()
60+
.amount(plugin.getConfig().getDouble("shop.cost"))
61+
.allowLoan(false)
62+
.core(QuickShop.getInstance().getEconomy())
63+
.currency(shop.getCurrency())
64+
.world(shop.getLocation().getWorld())
65+
.to(shop.getOwner())
66+
.build();
67+
transaction.failSafeCommit();
68+
}
69+
executing = false;
70+
plugin.getLogger().info("[Shop Purger] Task completed");
71+
}
72+
73+
}
74+
}

src/main/java/org/maxgamer/quickshop/util/Util.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
import java.nio.charset.StandardCharsets;
6666
import java.nio.file.Files;
6767
import java.text.DecimalFormat;
68+
import java.time.Instant;
69+
import java.time.LocalDate;
70+
import java.time.LocalDateTime;
6871
import java.util.AbstractMap.SimpleEntry;
6972
import java.util.*;
7073
import java.util.Map.Entry;
@@ -1385,6 +1388,18 @@ public static void mainThreadRun(@NotNull Runnable runnable) {
13851388
Bukkit.getScheduler().runTask(QuickShop.getInstance(), runnable);
13861389
}
13871390
}
1391+
// http://www.java2s.com/Tutorials/Java/Data_Type_How_to/Date_Convert/Convert_long_type_timestamp_to_LocalDate_and_LocalDateTime.htm
1392+
public static LocalDateTime getDateTimeFromTimestamp(long timestamp) {
1393+
if (timestamp == 0)
1394+
return null;
1395+
return LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), TimeZone
1396+
.getDefault().toZoneId());
1397+
}
1398+
// http://www.java2s.com/Tutorials/Java/Data_Type_How_to/Date_Convert/Convert_long_type_timestamp_to_LocalDate_and_LocalDateTime.htm
1399+
public static LocalDate getDateFromTimestamp(long timestamp) {
1400+
LocalDateTime date = getDateTimeFromTimestamp(timestamp);
1401+
return date == null ? null : date.toLocalDate();
1402+
}
13881403

13891404

13901405
}

src/main/resources/config.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,17 @@ protect:
768768
#
769769
custom-item-stacksize: [ ]
770770

771+
# Shops purger
772+
# Will purge all shops that owner offline x days while use command or server startup
773+
purge:
774+
enabled: false
775+
days: 60
776+
# At this moment, QuickShop only compatible with Vanilla banning system (e.g Essentials using)
777+
banned: true
778+
# At this moment, QuickShop only compatible with Vanilla OP system, LuckPerms may need add in future.
779+
skip-op: true
780+
return-create-fee: true
781+
771782
debug:
772783
#Should we disable the debug logger?
773784
#This will save a lot of execution time but will make reporting bugs

src/main/resources/lang-original/messages.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@
178178
"freeze": "&eDisable or Enable shop trading",
179179
"lock": "&eSwitch the shop's lock status",
180180
"taxaccount": "&eSet the tax account that shop using",
181-
"toggledisplay": "&eToggle the shop display item status"
181+
"toggledisplay": "&eToggle the shop display item status",
182+
"purge": "&aStart the shop purge task in background"
182183
},
183184
"disabled": "&cThis command is disabled: &e{0}",
184185
"feature-not-enabled": "This feature is not enabled in the config file."
@@ -340,5 +341,6 @@
340341
"taxaccount-unset": "&aThis shop's tax account now following server global setting.",
341342
"taxaccount-invalid": "&cTarget account not invalid, please enter a valid player name or uuid(with dashes).",
342343
"display-turn-on": "&aSuccessfully turn on the shop display.",
343-
"display-turn-off": "&aSuccessfully turn off the shop display."
344+
"display-turn-off": "&aSuccessfully turn off the shop display.",
345+
"shop-purged-start": "&aShop purge started, check the console for details."
344346
}

src/main/resources/plugin.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ permissions:
8080
quickshop.other.currency: true
8181
quickshop.other.taxaccount: true
8282
quickshop.other.toggledisplay: true
83+
quickshop.purge: true
84+
quickshop.other.setowner: true
85+
86+
8387
quickshop.create.sell:
8488
description: Allows a player to sell from a shop.
8589
default: op
@@ -224,3 +228,6 @@ permissions:
224228
quickshop.other.toggledisplay:
225229
description: Permission to use /qs toggledisplay on others shops
226230
default: op
231+
quickshop.purge:
232+
description: Permission to use /qs purge
233+
default: op

0 commit comments

Comments
 (0)