diff options
author | matthias-luger <58751503+matthias-luger@users.noreply.github.com> | 2023-10-31 19:15:52 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-31 19:15:52 +0100 |
commit | 921faafb312f19335d71ba9e2938c312d9a1868f (patch) | |
tree | 83f6a734cb0308b6fecfaecc008ab84d658073d8 /src | |
parent | ed5052c23c2e48cd696e3708bbbfc911dcc1718e (diff) | |
download | COFL-921faafb312f19335d71ba9e2938c312d9a1868f.tar.gz COFL-921faafb312f19335d71ba9e2938c312d9a1868f.tar.bz2 COFL-921faafb312f19335d71ba9e2938c312d9a1868f.zip |
reduce description requests (#110)
* reduce description requests
* Update DescriptionHandler.java
fix nullreference
---------
Co-authored-by: Äkwav <16632490+Ekwav@users.noreply.github.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/de/torui/coflsky/handlers/DescriptionHandler.java | 56 |
1 files changed, 36 insertions, 20 deletions
diff --git a/src/main/java/de/torui/coflsky/handlers/DescriptionHandler.java b/src/main/java/de/torui/coflsky/handlers/DescriptionHandler.java index 1c64bfd..3db2027 100644 --- a/src/main/java/de/torui/coflsky/handlers/DescriptionHandler.java +++ b/src/main/java/de/torui/coflsky/handlers/DescriptionHandler.java @@ -35,7 +35,8 @@ public class DescriptionHandler { public int line; } - public static HashMap<ItemStack, DescModification[]> tooltipItemMap = new HashMap<>(); + public static String allItemIds; + public static HashMap<String, DescModification[]> tooltipItemIdMap = new HashMap<>(); public static final DescModification[] EMPTY_ARRAY = new DescModification[0]; @@ -80,9 +81,6 @@ public class DescriptionHandler { } private DescModification[] getTooltipData(ItemStack itemStack) { - if (tooltipItemMap.containsKey(itemStack)) { - return tooltipItemMap.getOrDefault(itemStack, EMPTY_ARRAY); - } String id = ExtractIdFromItemStack(itemStack); if (tooltipItemIdMap.containsKey(id)) { return tooltipItemIdMap.getOrDefault(id, EMPTY_ARRAY); @@ -96,24 +94,24 @@ public class DescriptionHandler { * Called when the inventory is opened * checks for changes every once in a while and updates the description if * there was a change found - * + * * @param event */ public void loadDescriptionAndListenForChanges(GuiOpenEvent event) { GuiContainer gc = (GuiContainer) event.gui; - loadDescriptionForInventory(event, gc, false); + shouldUpdate = loadDescriptionForInventory(event, gc, false); int iteration = 1; while (IsOpen) { try { - Thread.sleep(300 + iteration++); + Thread.sleep(300 * iteration++); + iteration++; } catch (InterruptedException e) { throw new RuntimeException(e); } if (shouldUpdate || hasAnyStackChanged(gc)) { - shouldUpdate = false; - loadDescriptionForInventory(event, gc, true); + shouldUpdate = loadDescriptionForInventory(event, gc, true); // reduce update time since its more likely that more changes occure after one iteration = 5; } @@ -123,16 +121,22 @@ public class DescriptionHandler { } private static boolean hasAnyStackChanged(GuiContainer gc) { + return !allItemIds.equals(getCurrentInventoryIds(gc)); + } + + private static String getCurrentInventoryIds(GuiContainer gc){ + StringBuilder builder = new StringBuilder(); + for (Slot obj : gc.inventorySlots.inventorySlots) { ItemStack stack = obj.getStack(); - if (stack != null && !tooltipItemMap.containsKey(stack)) { - return true; - } + String id = ExtractIdFromItemStack(stack); + builder.append(id); } - return false; + + return builder.toString(); } - private static void loadDescriptionForInventory(GuiOpenEvent event, GuiContainer gc, boolean skipLoadCheck) { + private static boolean loadDescriptionForInventory(GuiOpenEvent event, GuiContainer gc, boolean skipLoadCheck) { InventoryWrapper wrapper = new InventoryWrapper(); if (event.gui instanceof GuiChest) { if (!skipLoadCheck) @@ -146,6 +150,8 @@ public class DescriptionHandler { } } + allItemIds = getCurrentInventoryIds(gc); + NBTTagCompound compound = new NBTTagCompound(); NBTTagList tl = new NBTTagList(); @@ -158,6 +164,7 @@ public class DescriptionHandler { } } + boolean shouldGetRefreshed = false; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { compound.setTag("i", tl); @@ -174,18 +181,29 @@ public class DescriptionHandler { String info = QueryServerCommands.PostRequest(Config.BaseUrl + "/api/mod/description/modifications", data); DescModification[][] arr = WSClient.gson.fromJson(info, DescModification[][].class); - int i = 0; - for (ItemStack stack : stacks) { - tooltipItemMap.put(stack, arr[i]); + for (int i = 0; i < stacks.size(); i++) { + ItemStack stack = stacks.get(i); String id = ExtractIdFromItemStack(stack); if (id.length() > 0) tooltipItemIdMap.put(id, arr[i]); - i++; + + if(stack == null) + continue; + NBTTagList lore = stack.getTagCompound().getCompoundTag("display").getTagList("Lore", 8); + for (int j = 0; j < lore.tagCount(); j++) { + String tag = lore.get(j).toString(); + System.out.println("tag: " + tag); + if(tag.contains("§7Refreshing...")){ + shouldGetRefreshed = true; + } + } } } catch (IOException e) { e.printStackTrace(); } + + return shouldGetRefreshed; } private static void waitForChestContentLoad(GuiOpenEvent event, GuiContainer gc) { @@ -235,8 +253,6 @@ public class DescriptionHandler { * Called when the inventory is closed */ public static void emptyTooltipData() { - tooltipItemMap.clear(); - tooltipItemIdMap.clear(); tooltipItemIdMap.clear(); } } |