Skip to content

Commit

Permalink
Fixed issue #12 | Updated PlayerWarps version
Browse files Browse the repository at this point in the history
  • Loading branch information
myzticbean committed Mar 24, 2024
1 parent c5a9252 commit 061de9b
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
### Bug fixes
- Fixed a code refactoring bug where search all shops would return no shops for Quickshop Reremake
- Added a INFO level logger to log time taken for a search process
- Fix the lag issue caused due to new QS-Hikari cache feature impl ([#12](https://github.com/myzticbean/QSFindItemAddOn/issues/12))

## Release 2.0.5.5
### Bug fixes
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@
<dependency>
<groupId>com.olziedev</groupId>
<artifactId>playerwarps-api</artifactId>
<version>6.28.0</version>
<version>6.30.0</version>
<scope>provided</scope>
<exclusions>
<exclusion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,26 @@ public void handleShopSearch(String buySellSubCommand, CommandSender commandSend
}

if(itemArg.equalsIgnoreCase("*") && !FindItemAddOn.getConfigProvider().FIND_ITEM_CMD_DISABLE_SEARCH_ALL_SHOPS) {
List<FoundShopItemModel> searchResultList = FindItemAddOn.getQsApiInstance().fetchAllItemsFromAllShops(isBuying, player);
if(searchResultList.size() > 0) {
// If QS Hikari installed and Shop Cache feature available (>6), then run in async thread (Fix for Issue #12)
if(!FindItemAddOn.isQSReremakeInstalled() && FindItemAddOn.getQsApiInstance().isQSShopCacheImplemented()) {
LoggerUtils.logWarning("Should run in async thread...");
Bukkit.getScheduler().runTaskAsynchronously(FindItemAddOn.getInstance(), () -> {
FoundShopsMenu menu = new FoundShopsMenu(FindItemAddOn.getPlayerMenuUtility(player), searchResultList);
Bukkit.getScheduler().runTask(FindItemAddOn.getInstance(), () -> menu.open(searchResultList));
List<FoundShopItemModel> searchResultList = FindItemAddOn.getQsApiInstance().fetchAllItemsFromAllShops(isBuying, player);
if(!searchResultList.isEmpty()) {
FoundShopsMenu menu = new FoundShopsMenu(FindItemAddOn.getPlayerMenuUtility(player), searchResultList);
Bukkit.getScheduler().runTask(FindItemAddOn.getInstance(), () -> menu.open(searchResultList));
}
else {
if(!StringUtils.isEmpty(FindItemAddOn.getConfigProvider().NO_SHOP_FOUND_MSG)) {
player.sendMessage(ColorTranslator.translateColorCodes(
FindItemAddOn.getConfigProvider().PLUGIN_PREFIX + FindItemAddOn.getConfigProvider().NO_SHOP_FOUND_MSG));
}
}
});
}
else {
if(!StringUtils.isEmpty(FindItemAddOn.getConfigProvider().NO_SHOP_FOUND_MSG)) {
player.sendMessage(ColorTranslator.translateColorCodes(
FindItemAddOn.getConfigProvider().PLUGIN_PREFIX + FindItemAddOn.getConfigProvider().NO_SHOP_FOUND_MSG));
}
}
}
else {
Material mat = Material.getMaterial(itemArg.toUpperCase());
if(mat != null) {
LoggerUtils.logDebugInfo("Material found: " + mat.toString());
List<FoundShopItemModel> searchResultList = FindItemAddOn.getQsApiInstance().findItemBasedOnTypeFromAllShops(new ItemStack(mat), isBuying, player);
if(searchResultList.size() > 0) {
} else {
// Else run in MAIN thread
List<FoundShopItemModel> searchResultList = FindItemAddOn.getQsApiInstance().fetchAllItemsFromAllShops(isBuying, player);
if(!searchResultList.isEmpty()) {
Bukkit.getScheduler().runTaskAsynchronously(FindItemAddOn.getInstance(), () -> {
FoundShopsMenu menu = new FoundShopsMenu(FindItemAddOn.getPlayerMenuUtility(player), searchResultList);
Bukkit.getScheduler().runTask(FindItemAddOn.getInstance(), () -> menu.open(searchResultList));
Expand All @@ -89,20 +89,74 @@ public void handleShopSearch(String buySellSubCommand, CommandSender commandSend
}
}
}
}
else {
Material mat = Material.getMaterial(itemArg.toUpperCase());
if(mat != null) {
LoggerUtils.logDebugInfo("Material found: " + mat);
// If QS Hikari installed and Shop Cache feature available (>6), then run in async thread (Fix for Issue #12)
if(!FindItemAddOn.isQSReremakeInstalled() && FindItemAddOn.getQsApiInstance().isQSShopCacheImplemented()) {
Bukkit.getScheduler().runTaskAsynchronously(FindItemAddOn.getInstance(), () -> {
List<FoundShopItemModel> searchResultList = FindItemAddOn.getQsApiInstance().findItemBasedOnTypeFromAllShops(new ItemStack(mat), isBuying, player);
if(!searchResultList.isEmpty()) {
FoundShopsMenu menu = new FoundShopsMenu(FindItemAddOn.getPlayerMenuUtility(player), searchResultList);
Bukkit.getScheduler().runTask(FindItemAddOn.getInstance(), () -> menu.open(searchResultList));
}
else {
if(!StringUtils.isEmpty(FindItemAddOn.getConfigProvider().NO_SHOP_FOUND_MSG)) {
player.sendMessage(ColorTranslator.translateColorCodes(
FindItemAddOn.getConfigProvider().PLUGIN_PREFIX + FindItemAddOn.getConfigProvider().NO_SHOP_FOUND_MSG));
}
}
});
} else {
List<FoundShopItemModel> searchResultList = FindItemAddOn.getQsApiInstance().findItemBasedOnTypeFromAllShops(new ItemStack(mat), isBuying, player);
if(!searchResultList.isEmpty()) {
Bukkit.getScheduler().runTaskAsynchronously(FindItemAddOn.getInstance(), () -> {
FoundShopsMenu menu = new FoundShopsMenu(FindItemAddOn.getPlayerMenuUtility(player), searchResultList);
Bukkit.getScheduler().runTask(FindItemAddOn.getInstance(), () -> menu.open(searchResultList));
});
}
else {
if(!StringUtils.isEmpty(FindItemAddOn.getConfigProvider().NO_SHOP_FOUND_MSG)) {
player.sendMessage(ColorTranslator.translateColorCodes(
FindItemAddOn.getConfigProvider().PLUGIN_PREFIX + FindItemAddOn.getConfigProvider().NO_SHOP_FOUND_MSG));
}
}
}
}
else {
LoggerUtils.logDebugInfo("Material not found! Performing query based search..");
List<FoundShopItemModel> searchResultList = FindItemAddOn.getQsApiInstance().findItemBasedOnDisplayNameFromAllShops(itemArg, isBuying, player);
if(searchResultList.size() > 0) {
// If QS Hikari installed and Shop Cache feature available (>6), then run in async thread (Fix for Issue #12)
if(!FindItemAddOn.isQSReremakeInstalled() && FindItemAddOn.getQsApiInstance().isQSShopCacheImplemented()) {
Bukkit.getScheduler().runTaskAsynchronously(FindItemAddOn.getInstance(), () -> {
FoundShopsMenu menu = new FoundShopsMenu(FindItemAddOn.getPlayerMenuUtility(player), searchResultList);
Bukkit.getScheduler().runTask(FindItemAddOn.getInstance(), () -> menu.open(searchResultList));
List<FoundShopItemModel> searchResultList = FindItemAddOn.getQsApiInstance().findItemBasedOnDisplayNameFromAllShops(itemArg, isBuying, player);
if(!searchResultList.isEmpty()) {
FoundShopsMenu menu = new FoundShopsMenu(FindItemAddOn.getPlayerMenuUtility(player), searchResultList);
Bukkit.getScheduler().runTask(FindItemAddOn.getInstance(), () -> menu.open(searchResultList));
}
else {
// Invalid Material
if(!StringUtils.isEmpty(FindItemAddOn.getConfigProvider().FIND_ITEM_CMD_INVALID_MATERIAL_MSG)) {
player.sendMessage(ColorTranslator.translateColorCodes(
FindItemAddOn.getConfigProvider().PLUGIN_PREFIX + FindItemAddOn.getConfigProvider().FIND_ITEM_CMD_INVALID_MATERIAL_MSG));
}
}
});
}
else {
// Invalid Material
if(!StringUtils.isEmpty(FindItemAddOn.getConfigProvider().FIND_ITEM_CMD_INVALID_MATERIAL_MSG)) {
player.sendMessage(ColorTranslator.translateColorCodes(
FindItemAddOn.getConfigProvider().PLUGIN_PREFIX + FindItemAddOn.getConfigProvider().FIND_ITEM_CMD_INVALID_MATERIAL_MSG));
} else {
List<FoundShopItemModel> searchResultList = FindItemAddOn.getQsApiInstance().findItemBasedOnDisplayNameFromAllShops(itemArg, isBuying, player);
if(!searchResultList.isEmpty()) {
Bukkit.getScheduler().runTaskAsynchronously(FindItemAddOn.getInstance(), () -> {
FoundShopsMenu menu = new FoundShopsMenu(FindItemAddOn.getPlayerMenuUtility(player), searchResultList);
Bukkit.getScheduler().runTask(FindItemAddOn.getInstance(), () -> menu.open(searchResultList));
});
}
else {
// Invalid Material
if(!StringUtils.isEmpty(FindItemAddOn.getConfigProvider().FIND_ITEM_CMD_INVALID_MATERIAL_MSG)) {
player.sendMessage(ColorTranslator.translateColorCodes(
FindItemAddOn.getConfigProvider().PLUGIN_PREFIX + FindItemAddOn.getConfigProvider().FIND_ITEM_CMD_INVALID_MATERIAL_MSG));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public interface QSApi<QSType, Shop> {
void registerSubCommand();
UUID convertNameToUuid(String playerName);

boolean isQSShopCacheImplemented();

static List<FoundShopItemModel> sortShops(int sortingMethod, List<FoundShopItemModel> shopsFoundList, boolean toBuy) {
switch (sortingMethod) {
// Random
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.ghostchu.quickshop.api.shop.Shop;
import com.ghostchu.quickshop.api.shop.permission.BuiltInShopPermission;
import com.ghostchu.quickshop.database.DataTables;
import com.vdurmont.semver4j.Semver;
import io.myzticbean.finditemaddon.Commands.QSSubCommands.FindItemCmdHikariImpl;
import io.myzticbean.finditemaddon.FindItemAddOn;
import io.myzticbean.finditemaddon.Models.CachedShop;
Expand All @@ -29,8 +28,6 @@
import java.nio.charset.StandardCharsets;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand All @@ -50,14 +47,18 @@ public class QSHikariAPIHandler implements QSApi<QuickShop, Shop> {

private final int SHOP_CACHE_TIMEOUT_SECONDS = 5*60;

private final boolean isQSHikariShopCacheImplemented;

public QSHikariAPIHandler() {
api = QuickShopAPI.getInstance();
pluginVersion = Bukkit.getPluginManager().getPlugin("QuickShop-Hikari").getDescription().getVersion();
LoggerUtils.logInfo("Initializing Shop caching");
shopCache = new ConcurrentHashMap<>();
isQSHikariShopCacheImplemented = checkIfQSHikariShopCacheImplemented();
}

public List<FoundShopItemModel> findItemBasedOnTypeFromAllShops(ItemStack item, boolean toBuy, Player searchingPlayer) {
LoggerUtils.logDebugInfo("Is MAIN Thread?" + Bukkit.isPrimaryThread());
long begin = System.currentTimeMillis();
List<FoundShopItemModel> shopsFoundList = new ArrayList<>();
List<Shop> allShops;
Expand Down Expand Up @@ -117,6 +118,7 @@ static List<FoundShopItemModel> handleShopSorting(boolean toBuy, List<FoundShopI
}

public List<FoundShopItemModel> findItemBasedOnDisplayNameFromAllShops(String displayName, boolean toBuy, Player searchingPlayer) {
LoggerUtils.logDebugInfo("Is MAIN Thread?" + Bukkit.isPrimaryThread());
long begin = System.currentTimeMillis();
List<FoundShopItemModel> shopsFoundList = new ArrayList<>();
List<Shop> allShops;
Expand Down Expand Up @@ -157,6 +159,7 @@ public List<FoundShopItemModel> findItemBasedOnDisplayNameFromAllShops(String di
}

public List<FoundShopItemModel> fetchAllItemsFromAllShops(boolean toBuy, Player searchingPlayer) {
LoggerUtils.logDebugInfo("Is MAIN Thread?" + Bukkit.isPrimaryThread());
long begin = System.currentTimeMillis();
List<FoundShopItemModel> shopsFoundList = new ArrayList<>();
List<Shop> allShops;
Expand Down Expand Up @@ -270,10 +273,6 @@ public void registerSubCommand() {
}
}
LoggerUtils.logInfo("Registered finditem sub-command for /qs");
/*
final TextComponent textComponent = Component.text("Search for items from all shops using an interactive GUI");
final Function<String, Component> func = x -> Component.text("Search for items from all shops using an interactive GUI");
*/
api.getCommandManager().registerCmd(
CommandContainer.builder()
.prefix("finditem")
Expand Down Expand Up @@ -391,4 +390,15 @@ private void testQuickShopHikariExternalCache(Shop shop) throws RuntimeException
throw new RuntimeException(e);
}
}

private boolean checkIfQSHikariShopCacheImplemented() {
String mainVersionStr = pluginVersion.split("\\.")[0];
int mainVersion = Integer.parseInt(mainVersionStr);
return mainVersion >= 6;
}

@Override
public boolean isQSShopCacheImplemented() {
return isQSHikariShopCacheImplemented;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,10 @@ private static void logTotalShopsOnServer(int allShopsCount) {
LoggerUtils.logDebugInfo("Total shops on server: " + allShopsCount);
}


@Override
public boolean isQSShopCacheImplemented() {
return false;
}


}
2 changes: 1 addition & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ blacklisted-worlds:
- "world_number_1"
- "world_number_2"
debug-mode: false
config-version: 13
config-version: 14
2 changes: 1 addition & 1 deletion src/main/resources/sample-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,4 @@ blacklisted-worlds:
debug-mode: false

# Do not change the below value
config-version: 13
config-version: 14

0 comments on commit 061de9b

Please sign in to comment.