Fixed scheduling bug, if the gui was refresh / not closed using esc the task would not cancel...

This commit is contained in:
Kiran Hart 2021-03-23 19:00:54 -04:00
parent f3cabe8710
commit 8125aa46f6
4 changed files with 54 additions and 29 deletions

View File

@ -18,7 +18,6 @@ import ca.tweetzy.core.configuration.Config;
import ca.tweetzy.core.core.PluginID;
import ca.tweetzy.core.gui.GuiManager;
import ca.tweetzy.core.utils.Metrics;
import ca.tweetzy.core.utils.TextUtils;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;

View File

@ -37,13 +37,8 @@ public class GUIActiveAuctions extends Gui {
draw();
if (Settings.AUTO_REFRESH_AUCTION_PAGES.getBoolean()) {
setOnOpen(e -> {
taskId = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(AuctionHouse.getInstance(), this::draw, 0L, Settings.TICK_UPDATE_TIME.getInt());
});
setOnClose(e -> {
Bukkit.getServer().getScheduler().cancelTask(taskId);
});
setOnOpen(e -> startTask());
setOnClose(e -> killTask());
}
}
@ -61,7 +56,10 @@ public class GUIActiveAuctions extends Gui {
});
// Other Buttons
setButton(5, 0, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_CLOSE_BTN_ITEM.getString(), Settings.GUI_CLOSE_BTN_NAME.getString(), Settings.GUI_CLOSE_BTN_LORE.getStringList(), null), e -> e.manager.showGUI(e.player, new GUIAuctionHouse(this.auctionPlayer)));
setButton(5, 0, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_CLOSE_BTN_ITEM.getString(), Settings.GUI_CLOSE_BTN_NAME.getString(), Settings.GUI_CLOSE_BTN_LORE.getStringList(), null), e -> {
killTask();
e.manager.showGUI(e.player, new GUIAuctionHouse(this.auctionPlayer));
});
setButton(5, 1, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_ACTIVE_AUCTIONS_ITEM.getString(), Settings.GUI_ACTIVE_AUCTIONS_NAME.getString(), Settings.GUI_ACTIVE_AUCTIONS_LORE.getStringList(), null), e -> {
this.auctionPlayer.getItems(false).forEach(item -> item.setExpired(true));
draw();
@ -76,4 +74,12 @@ public class GUIActiveAuctions extends Gui {
});
}
}
private void startTask() {
taskId = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(AuctionHouse.getInstance(), this::draw, 0L, Settings.TICK_UPDATE_TIME.getInt());
}
private void killTask() {
Bukkit.getServer().getScheduler().cancelTask(taskId);
}
}

View File

@ -26,7 +26,7 @@ import java.util.stream.Collectors;
public class GUIAuctionHouse extends Gui {
final AuctionPlayer auctionPlayer;
final List<AuctionItem> items;
List<AuctionItem> items;
private int taskId;
private AuctionItemCategory filterCategory = AuctionItemCategory.ALL;
@ -41,13 +41,8 @@ public class GUIAuctionHouse extends Gui {
draw();
if (Settings.AUTO_REFRESH_AUCTION_PAGES.getBoolean()) {
setOnOpen(e -> {
taskId = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(AuctionHouse.getInstance(), this::draw, 0L, Settings.TICK_UPDATE_TIME.getInt());
});
setOnClose(e -> {
Bukkit.getServer().getScheduler().cancelTask(taskId);
});
setOnOpen(e -> startTask());
setOnClose(e -> killTask());
}
}
@ -55,6 +50,12 @@ public class GUIAuctionHouse extends Gui {
this(auctionPlayer);
this.filterCategory = filterCategory;
this.filterAuctionType = filterAuctionType;
// Apply any filtering, there is probably a cleaner way of doing this, but I'm blanking
if (this.filterCategory != AuctionItemCategory.ALL)
items = items.stream().filter(item -> item.getCategory() == this.filterCategory).collect(Collectors.toList());
if (this.filterAuctionType != AuctionSaleType.BOTH)
items = items.stream().filter(item -> this.filterAuctionType == AuctionSaleType.USED_BIDDING_SYSTEM ? item.getBidStartPrice() >= Settings.MIN_AUCTION_START_PRICE.getDouble() : item.getBidStartPrice() <= 0).collect(Collectors.toList());
}
public void draw() {
@ -63,7 +64,10 @@ public class GUIAuctionHouse extends Gui {
// Pagination
pages = (int) Math.max(1, Math.ceil(this.items.size() / (double) 45));
setPrevPage(5, 3, new TItemBuilder(Objects.requireNonNull(Settings.GUI_BACK_BTN_ITEM.getMaterial().parseMaterial())).setName(Settings.GUI_BACK_BTN_NAME.getString()).setLore(Settings.GUI_BACK_BTN_LORE.getStringList()).toItemStack());
setButton(5, 4, new TItemBuilder(Objects.requireNonNull(Settings.GUI_REFRESH_BTN_ITEM.getMaterial().parseMaterial())).setName(Settings.GUI_REFRESH_BTN_NAME.getString()).setLore(Settings.GUI_REFRESH_BTN_LORE.getStringList()).toItemStack(), e -> e.manager.showGUI(e.player, new GUIAuctionHouse(this.auctionPlayer, this.filterCategory, this.filterAuctionType)));
setButton(5, 4, new TItemBuilder(Objects.requireNonNull(Settings.GUI_REFRESH_BTN_ITEM.getMaterial().parseMaterial())).setName(Settings.GUI_REFRESH_BTN_NAME.getString()).setLore(Settings.GUI_REFRESH_BTN_LORE.getStringList()).toItemStack(), e -> {
killTask();
e.manager.showGUI(e.player, new GUIAuctionHouse(this.auctionPlayer, this.filterCategory, this.filterAuctionType));
});
setNextPage(5, 5, new TItemBuilder(Objects.requireNonNull(Settings.GUI_NEXT_BTN_ITEM.getMaterial().parseMaterial())).setName(Settings.GUI_NEXT_BTN_NAME.getString()).setLore(Settings.GUI_NEXT_BTN_LORE.getStringList()).toItemStack());
setOnPage(e -> {
draw();
@ -73,11 +77,17 @@ public class GUIAuctionHouse extends Gui {
// Other Buttons
setButton(5, 0, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_AUCTION_HOUSE_ITEMS_YOUR_AUCTIONS_ITEM.getString(), Settings.GUI_AUCTION_HOUSE_ITEMS_YOUR_AUCTIONS_NAME.getString(), Settings.GUI_AUCTION_HOUSE_ITEMS_YOUR_AUCTIONS_LORE.getStringList(), new HashMap<String, Object>() {{
put("%active_player_auctions%", auctionPlayer.getItems(false).size());
}}), e -> e.manager.showGUI(e.player, new GUIActiveAuctions(this.auctionPlayer)));
}}), e -> {
killTask();
e.manager.showGUI(e.player, new GUIActiveAuctions(this.auctionPlayer));
});
setButton(5, 1, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_AUCTION_HOUSE_ITEMS_COLLECTION_BIN_ITEM.getString(), Settings.GUI_AUCTION_HOUSE_ITEMS_COLLECTION_BIN_NAME.getString(), Settings.GUI_AUCTION_HOUSE_ITEMS_COLLECTION_BIN_LORE.getStringList(), new HashMap<String, Object>() {{
put("%expired_player_auctions%", auctionPlayer.getItems(true).size());
}}), e -> e.manager.showGUI(e.player, new GUIExpiredItems(this.auctionPlayer)));
}}), e -> {
killTask();
e.manager.showGUI(e.player, new GUIExpiredItems(this.auctionPlayer));
});
setButton(5, 2, ConfigurationItemHelper.createConfigurationItem(Settings.GUI_AUCTION_HOUSE_ITEMS_FILTER_ITEM.getString(), Settings.GUI_AUCTION_HOUSE_ITEMS_FILTER_NAME.getString(), Settings.GUI_AUCTION_HOUSE_ITEMS_FILTER_LORE.getStringList(), new HashMap<String, Object>() {{
put("%filter_category%", filterCategory.getType());
@ -87,7 +97,8 @@ public class GUIAuctionHouse extends Gui {
case LEFT:
this.filterCategory = this.filterCategory.next();
if (Settings.REFRESH_GUI_ON_FILTER_CHANGE.getBoolean()) {
e.manager.showGUI(e.player, new GUIAuctionHouse(this.auctionPlayer));
killTask();
e.manager.showGUI(e.player, new GUIAuctionHouse(this.auctionPlayer, this.filterCategory, this.filterAuctionType));
} else {
draw();
}
@ -95,7 +106,8 @@ public class GUIAuctionHouse extends Gui {
case RIGHT:
this.filterAuctionType = this.filterAuctionType.next();
if (Settings.REFRESH_GUI_ON_FILTER_CHANGE.getBoolean()) {
e.manager.showGUI(e.player, new GUIAuctionHouse(this.auctionPlayer));
killTask();
e.manager.showGUI(e.player, new GUIAuctionHouse(this.auctionPlayer, this.filterCategory, this.filterAuctionType));
} else {
draw();
}
@ -110,11 +122,6 @@ public class GUIAuctionHouse extends Gui {
// Items
int slot = 0;
List<AuctionItem> data = this.items.stream().sorted(Comparator.comparingInt(AuctionItem::getRemainingTime).reversed()).skip((page - 1) * 45L).limit(45).collect(Collectors.toList());
// Apply any filtering, there is probably a cleaner way of doing this, but I'm blanking
if (this.filterCategory != AuctionItemCategory.ALL)
data = data.stream().filter(item -> item.getCategory() == this.filterCategory).collect(Collectors.toList());
if (this.filterAuctionType != AuctionSaleType.BOTH)
data = data.stream().filter(item -> this.filterAuctionType == AuctionSaleType.USED_BIDDING_SYSTEM ? item.getBidStartPrice() >= Settings.MIN_AUCTION_START_PRICE.getDouble() : item.getBidStartPrice() <= 0).collect(Collectors.toList());
for (AuctionItem auctionItem : data) {
setButton(slot++, auctionItem.getDisplayStack(AuctionStackType.MAIN_AUCTION_HOUSE), e -> {
@ -125,6 +132,8 @@ public class GUIAuctionHouse extends Gui {
AuctionHouse.getInstance().getLocale().getMessage("general.cantbuyown").sendPrefixedMessage(e.player);
return;
}
killTask();
e.manager.showGUI(e.player, new GUIConfirmPurchase(this.auctionPlayer, auctionItem));
} else {
if (e.player.getUniqueId().equals(auctionItem.getOwner()) && !Settings.OWNER_CAN_BID_OWN_ITEM.getBoolean()) {
@ -140,6 +149,7 @@ public class GUIAuctionHouse extends Gui {
}
if (Settings.REFRESH_GUI_WHEN_BID.getBoolean()) {
killTask();
e.manager.showGUI(e.player, new GUIAuctionHouse(this.auctionPlayer));
}
}
@ -147,6 +157,7 @@ public class GUIAuctionHouse extends Gui {
case MIDDLE:
if (e.player.isOp() || e.player.hasPermission("auctionhouse.admin")) {
AuctionHouse.getInstance().getAuctionItemManager().removeItem(auctionItem.getKey());
killTask();
e.manager.showGUI(e.player, new GUIAuctionHouse(this.auctionPlayer));
}
break;
@ -156,6 +167,7 @@ public class GUIAuctionHouse extends Gui {
AuctionHouse.getInstance().getLocale().getMessage("general.cantbuyown").sendPrefixedMessage(e.player);
return;
}
killTask();
e.manager.showGUI(e.player, new GUIConfirmPurchase(this.auctionPlayer, auctionItem));
}
break;
@ -163,4 +175,12 @@ public class GUIAuctionHouse extends Gui {
});
}
}
private void startTask() {
taskId = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(AuctionHouse.getInstance(), this::draw, 0L, Settings.TICK_UPDATE_TIME.getInt());
}
private void killTask() {
Bukkit.getServer().getScheduler().cancelTask(taskId);
}
}

View File

@ -25,10 +25,10 @@ public class Settings {
* ===============================*/
public static final ConfigSetting DEFAULT_AUCTION_TIME = new ConfigSetting(config, "auction setting.default auction house", 60, "The default auction time before an item expires (in seconds)");
public static final ConfigSetting MAX_AUCTION_PRICE = new ConfigSetting(config, "auction setting.pricing.max auction price", 1000000000, "The max price for buy only / buy now items");
public static final ConfigSetting MAX_AUCTION_START_PRICE = new ConfigSetting(config, "auction setting.max auction start price", 1000000000, "The max price starting a bidding auction");
public static final ConfigSetting MAX_AUCTION_START_PRICE = new ConfigSetting(config, "auction setting.pricing.max auction start price", 1000000000, "The max price starting a bidding auction");
public static final ConfigSetting MAX_AUCTION_INCREMENT_PRICE = new ConfigSetting(config, "auction setting.pricing.max auction increment price", 1000000000, "The max amount for incrementing a bid.");
public static final ConfigSetting MIN_AUCTION_PRICE = new ConfigSetting(config, "auction setting.pricing.min auction price", 1, "The min price for buy only / buy now items");
public static final ConfigSetting MIN_AUCTION_START_PRICE = new ConfigSetting(config, "auction setting.min auction start price", 1, "The min price starting a bidding auction");
public static final ConfigSetting MIN_AUCTION_START_PRICE = new ConfigSetting(config, "auction setting.pricing.min auction start price", 1, "The min price starting a bidding auction");
public static final ConfigSetting MIN_AUCTION_INCREMENT_PRICE = new ConfigSetting(config, "auction setting.pricing.min auction increment price", 1, "The min amount for incrementing a bid.");
public static final ConfigSetting OWNER_CAN_PURCHASE_OWN_ITEM = new ConfigSetting(config, "auction setting.purchase.owner can purchase own item", false, "Should the owner of an auction be able to purchase it?", "This probably should be set to false...");
public static final ConfigSetting OWNER_CAN_BID_OWN_ITEM = new ConfigSetting(config, "auction setting.purchase.owner can bid on own item", false, "Should the owner of an auction be able to bid on it?", "This probably should be set to false...");