synchronized items

This commit is contained in:
Kiran Hart 2021-06-18 13:37:53 -04:00
parent c560e8d0d9
commit e0163fe147
5 changed files with 70 additions and 31 deletions

View File

@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>ca.tweetzy</groupId>
<artifactId>auctionhouse</artifactId>
<version>2.13.1</version>
<version>2.13.2</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -5,7 +5,9 @@ import lombok.Getter;
import lombok.Setter;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
@ -27,7 +29,21 @@ public class AuctionPlayer {
}
public List<AuctionItem> getItems(boolean getExpired) {
return Collections.unmodifiableList(AuctionHouse.getInstance().getAuctionItemManager().getAuctionItems().stream().filter(item -> item.getOwner().equals(this.player.getUniqueId()) && item.isExpired() == getExpired).collect(Collectors.toList()));
List<AuctionItem> auctionItems = AuctionHouse.getInstance().getAuctionItemManager().getAuctionItems();
List<AuctionItem> items = new ArrayList<>();
synchronized (auctionItems) {
Iterator<AuctionItem> iterator = auctionItems.iterator();
while(iterator.hasNext()) {
AuctionItem item = iterator.next();
if (item.getOwner().equals(this.player.getUniqueId()) && item.isExpired() == getExpired) {
items.add(item);
}
}
}
// return Collections.unmodifiableList(AuctionHouse.getInstance().getAuctionItemManager().getAuctionItems().stream().filter(item -> item.getOwner().equals(this.player.getUniqueId()) && item.isExpired() == getExpired).collect(Collectors.toList()));
return items;
}
public int getSellLimit() {

View File

@ -21,10 +21,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.scheduler.BukkitTask;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
/**
@ -38,7 +35,7 @@ import java.util.stream.Collectors;
public class GUIAuctionHouse extends Gui {
final AuctionPlayer auctionPlayer;
List<AuctionItem> items;
private List<AuctionItem> items;
private int taskId;
private BukkitTask task;
@ -51,6 +48,7 @@ public class GUIAuctionHouse extends Gui {
setTitle(TextUtils.formatText(Settings.GUI_AUCTION_HOUSE_TITLE.getString()));
setRows(6);
setAcceptsItems(false);
setAllowShiftClick(false);
draw();
if (Settings.AUTO_REFRESH_AUCTION_PAGES.getBoolean()) {
@ -78,19 +76,39 @@ public class GUIAuctionHouse extends Gui {
private void drawItems() {
AuctionHouse.newChain().asyncFirst(() -> {
this.items = AuctionHouse.getInstance().getAuctionItemManager().getAuctionItems().stream().filter(item -> !item.isExpired() && item.getRemainingTime() >= 1).collect(Collectors.toList());
List<AuctionItem> filteredItems = new ArrayList<>();
this.items = AuctionHouse.getInstance().getAuctionItemManager().getAuctionItems().stream().filter(auctionItem -> !auctionItem.isExpired() && auctionItem.getRemainingTime() >= 1).collect(Collectors.toList());
if (this.searchPhrase != null && this.searchPhrase.length() != 0) {
this.items = this.items.stream().filter(auctionItem -> AuctionAPI.getInstance().match(this.searchPhrase, auctionItem.getItemName()) || AuctionAPI.getInstance().match(this.searchPhrase, auctionItem.getCategory().getTranslatedType()) || AuctionAPI.getInstance().match(this.searchPhrase, Bukkit.getOfflinePlayer(auctionItem.getOwner()).getName())).collect(Collectors.toList());
synchronized (this.items) {
Iterator<AuctionItem> iterator = this.items.iterator();
while (iterator.hasNext()) {
AuctionItem item = iterator.next();
if (!item.isExpired() && item.getRemainingTime() >= 1) {
filteredItems.add(item);
}
// do the filter here now
if (this.searchPhrase != null && this.searchPhrase.length() != 0) {
filteredItems = filteredItems.stream().filter(auctionItem ->
AuctionAPI.getInstance().match(this.searchPhrase, auctionItem.getItemName()) ||
AuctionAPI.getInstance().match(this.searchPhrase, auctionItem.getCategory().getTranslatedType()) ||
AuctionAPI.getInstance().match(this.searchPhrase, Bukkit.getOfflinePlayer(auctionItem.getOwner()).getName())) // TODO add enchantment searching
.collect(Collectors.toList());
}
if (this.filterCategory != AuctionItemCategory.ALL) {
filteredItems = filteredItems.stream().filter(auctionItem -> auctionItem.getCategory() == this.filterCategory).collect(Collectors.toList());
}
if (this.filterAuctionType != AuctionSaleType.BOTH) {
filteredItems = filteredItems.stream().filter(auctionItem -> this.filterAuctionType == AuctionSaleType.USED_BIDDING_SYSTEM ? auctionItem.getBidStartPrice() >= Settings.MIN_AUCTION_START_PRICE.getDouble() : auctionItem.getBidStartPrice() <= 0).collect(Collectors.toList());
}
}
}
if (this.filterCategory != AuctionItemCategory.ALL)
this.items = items.stream().filter(item -> item.getCategory() == this.filterCategory).collect(Collectors.toList());
if (this.filterAuctionType != AuctionSaleType.BOTH)
this.items = this.items.stream().filter(item -> this.filterAuctionType == AuctionSaleType.USED_BIDDING_SYSTEM ? item.getBidStartPrice() >= Settings.MIN_AUCTION_START_PRICE.getDouble() : item.getBidStartPrice() <= 0).collect(Collectors.toList());
return this.items.stream().sorted(Comparator.comparingInt(AuctionItem::getRemainingTime).reversed()).skip((page - 1) * 45L).limit(45).collect(Collectors.toList());
filteredItems = filteredItems.stream().skip((page - 1) * 45L).limit(45).sorted(Comparator.comparingInt(AuctionItem::getRemainingTime).reversed()).collect(Collectors.toList());
return filteredItems;
}).asyncLast((data) -> {
pages = (int) Math.max(1, Math.ceil(this.items.size() / (double) 45L));
drawPaginationButtons();
@ -293,7 +311,7 @@ public class GUIAuctionHouse extends Gui {
AuctionHouse.getInstance().getAuctionPlayerManager().addCooldown(this.auctionPlayer.getPlayer().getUniqueId());
}
cleanup();
e.manager.showGUI(e.player, new GUIAuctionHouse(this.auctionPlayer, this.filterCategory, this.filterAuctionType));
e.manager.showGUI(e.player, new GUIAuctionHouse(this.auctionPlayer));
});
}

View File

@ -40,7 +40,7 @@ public class AuctionItemManager {
}
public List<AuctionItem> getAuctionItems() {
return Collections.unmodifiableList(this.auctionItems);
return Collections.synchronizedList(this.auctionItems);
}
public void loadItems(boolean useDatabase) {

View File

@ -2,10 +2,12 @@ package ca.tweetzy.auctionhouse.tasks;
import ca.tweetzy.auctionhouse.AuctionHouse;
import ca.tweetzy.auctionhouse.api.AuctionAPI;
import ca.tweetzy.auctionhouse.auction.AuctionItem;
import ca.tweetzy.auctionhouse.settings.Settings;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.stream.Collectors;
import java.util.Iterator;
import java.util.List;
/**
* The current file has been created by Kiran Hart
@ -28,24 +30,27 @@ public class TickAuctionsTask extends BukkitRunnable {
@Override
public void run() {
try {
// check if the auction stack even has items
if (AuctionHouse.getInstance().getAuctionItemManager().getAuctionItems().size() == 0) {
// check if the auction stack even has items
List<AuctionItem> auctionItems = AuctionHouse.getInstance().getAuctionItemManager().getAuctionItems();
synchronized (auctionItems) {
if (auctionItems.size() == 0) {
return;
}
// tick all the auction items
AuctionHouse.getInstance().getAuctionItemManager().getAuctionItems().forEach(item -> {
Iterator<AuctionItem> iterator = auctionItems.iterator();
while (iterator.hasNext()) {
AuctionItem item = iterator.next();
if (!item.isExpired()) {
item.updateRemainingTime(Settings.TICK_UPDATE_TIME.getInt());
}
});
// filter items where the time is less than or equal to 0
AuctionHouse.getInstance().getAuctionItemManager().getAuctionItems().stream().filter(item -> item.getRemainingTime() <= 0).collect(Collectors.toList()).iterator().forEachRemaining(AuctionAPI.getInstance()::endAuction);
} catch (Exception e) {
e.printStackTrace();
if (item.getRemainingTime() <= 0) {
AuctionAPI.getInstance().endAuction(item);
}
}
}
}
}