@@ -57,79 +57,86 @@ public void purge() {
5757 private void run () {
5858 Util .ensureThread (true );
5959 executing = true ;
60- if (plugin .getConfig ().getBoolean ("purge.backup" )) {
61- String backupFileName = "shop-purge-backup-" + UUID .randomUUID () + ".txt" ;
62- Util .makeExportBackup (backupFileName );
63- plugin .getLogger ().info ("[Shop Purger] We have backup shop data as " + backupFileName + ", if you ran into any trouble, please rename it to recovery.txt then use /qs recovery in console to rollback!" );
64- }
65- plugin .getLogger ().info ("[Shop Purger] Scanning and removing shops...." );
66- List <Shop > pendingRemovalShops = new ArrayList <>();
67- int days = plugin .getConfig ().getInt ("purge.days" , 360 );
68- boolean deleteBanned = plugin .getConfig ().getBoolean ("purge.banned" );
69- boolean skipOp = plugin .getConfig ().getBoolean ("purge.skip-op" );
70- boolean returnCreationFee = plugin .getConfig ().getBoolean ("purge.return-create-fee" );
71- for (Shop shop : plugin .getShopManager ().getAllShops ()) {
72- OfflinePlayer player = PlayerFinder .findOfflinePlayerByUUID (shop .getOwner ());
73- if (!player .hasPlayedBefore ()) {
74- Util .debugLog ("Shop " + shop + " detection skipped: Owner never played before." );
75- continue ;
76- }
77- long lastPlayed = player .getLastPlayed ();
78- if (lastPlayed == 0 ) {
79- continue ;
80- }
81- if (player .isOnline ()) {
82- continue ;
83- }
84- if (player .isOp () && skipOp ) {
85- continue ;
60+ try {
61+ if (plugin .getConfig ().getBoolean ("purge.backup" )) {
62+ String backupFileName = "shop-purge-backup-" + UUID .randomUUID () + ".txt" ;
63+ Util .makeExportBackup (backupFileName );
64+ plugin .getLogger ().info ("[Shop Purger] We have backup shop data as " + backupFileName + ", if you ran into any trouble, please rename it to recovery.txt then use /qs recovery in console to rollback!" );
8665 }
87- boolean markDeletion = player .isBanned () && deleteBanned ;
88- //noinspection ConstantConditions
89- long noOfDaysBetween = ChronoUnit .DAYS .between (Util .getDateTimeFromTimestamp (lastPlayed ), Util .getDateTimeFromTimestamp (System .currentTimeMillis ()));
90- if (noOfDaysBetween > days ) {
91- markDeletion = true ;
92- }
93- if (!markDeletion ) {
94- continue ;
66+ plugin .getLogger ().info ("[Shop Purger] Scanning and removing shops...." );
67+ List <Shop > pendingRemovalShops = new ArrayList <>();
68+ int days = plugin .getConfig ().getInt ("purge.days" , 360 );
69+ boolean deleteBanned = plugin .getConfig ().getBoolean ("purge.banned" );
70+ boolean skipOp = plugin .getConfig ().getBoolean ("purge.skip-op" );
71+ boolean returnCreationFee = plugin .getConfig ().getBoolean ("purge.return-create-fee" );
72+ for (Shop shop : plugin .getShopManager ().getAllShops ()) {
73+ OfflinePlayer player = PlayerFinder .findOfflinePlayerByUUID (shop .getOwner ());
74+ if (!player .hasPlayedBefore ()) {
75+ Util .debugLog ("Shop " + shop + " detection skipped: Owner never played before." );
76+ continue ;
77+ }
78+ long lastPlayed = player .getLastPlayed ();
79+ if (lastPlayed == 0 ) {
80+ continue ;
81+ }
82+ if (player .isOnline ()) {
83+ continue ;
84+ }
85+ if (player .isOp () && skipOp ) {
86+ continue ;
87+ }
88+ boolean markDeletion = player .isBanned () && deleteBanned ;
89+ //noinspection ConstantConditions
90+ long noOfDaysBetween = ChronoUnit .DAYS .between (Util .getDateTimeFromTimestamp (lastPlayed ), Util .getDateTimeFromTimestamp (System .currentTimeMillis ()));
91+ if (noOfDaysBetween > days ) {
92+ markDeletion = true ;
93+ }
94+ if (!markDeletion ) {
95+ continue ;
96+ }
97+ pendingRemovalShops .add (shop );
9598 }
96- pendingRemovalShops .add (shop );
97- }
98- if (pendingRemovalShops .size () > 0 ) {
99- plugin .getLogger ().info ("[Shop Purger] Found " + pendingRemovalShops .size () + " need to removed, will remove in the next tick." );
100- new BukkitRunnable () {
101- @ Override
102- public void run () {
103- Iterator <Shop > shopIterator = pendingRemovalShops .iterator ();
104- long startTimeMs = System .currentTimeMillis ();
105- while (shopIterator .hasNext ()) {
106- if (startTimeMs - System .currentTimeMillis () > 130 ) {
107- plugin .getLogger ().info ("[Shop Purger] it seems taking long time to purge shop (>130ms), do it in the next tick..." );
99+ if (pendingRemovalShops .size () > 0 ) {
100+ plugin .getLogger ().info ("[Shop Purger] Found " + pendingRemovalShops .size () + " need to removed, will remove in the next tick." );
101+ new BukkitRunnable () {
102+ @ Override
103+ public void run () {
104+ try {
105+ Iterator <Shop > shopIterator = pendingRemovalShops .iterator ();
106+ long startTimeMs = System .currentTimeMillis ();
107+ while (shopIterator .hasNext ()) {
108+ if (startTimeMs - System .currentTimeMillis () > 130 ) {
109+ plugin .getLogger ().info ("[Shop Purger] it seems taking long time to purge shop (>130ms), do it in the next tick..." );
110+ }
111+ Shop shop = shopIterator .next ();
112+ shop .delete (false );
113+ if (returnCreationFee ) {
114+ EconomyTransaction transaction =
115+ EconomyTransaction .builder ()
116+ .amount (plugin .getConfig ().getDouble ("shop.cost" ))
117+ .allowLoan (false )
118+ .core (QuickShop .getInstance ().getEconomy ())
119+ .currency (shop .getCurrency ())
120+ .world (shop .getLocation ().getWorld ())
121+ .to (shop .getOwner ())
122+ .build ();
123+ transaction .failSafeCommit ();
124+ }
125+ shopIterator .remove ();
126+ plugin .getLogger ().info ("[Shop Purger] Shop " + shop + " has been purged." );
127+ }
128+ plugin .getLogger ().info ("[Shop Purger] Task completed, " + pendingRemovalShops .size () + " shops was purged" );
129+ } finally {
130+ this .cancel ();
131+ executing = false ;
108132 }
109- Shop shop = shopIterator .next ();
110- shop .delete (false );
111- if (returnCreationFee ) {
112- EconomyTransaction transaction =
113- EconomyTransaction .builder ()
114- .amount (plugin .getConfig ().getDouble ("shop.cost" ))
115- .allowLoan (false )
116- .core (QuickShop .getInstance ().getEconomy ())
117- .currency (shop .getCurrency ())
118- .world (shop .getLocation ().getWorld ())
119- .to (shop .getOwner ())
120- .build ();
121- transaction .failSafeCommit ();
122- }
123- shopIterator .remove ();
124- plugin .getLogger ().info ("[Shop Purger] Shop " + shop + " has been purged." );
125133 }
126- plugin .getLogger ().info ("[Shop Purger] Task completed, " + pendingRemovalShops .size () + " shops was purged" );
127- this .cancel ();
128- executing = false ;
129- }
130- }.runTaskTimer (plugin , 1L , 1L );
131- } else {
132- plugin .getLogger ().info ("[Shop Purger] Task completed, No shops need to purge." );
134+ }.runTaskTimer (plugin , 1L , 1L );
135+ } else {
136+ plugin .getLogger ().info ("[Shop Purger] Task completed, No shops need to purge." );
137+ executing = false ;
138+ }
139+ } finally {
133140 executing = false ;
134141 }
135142 }
0 commit comments