Skip to content

Commit

Permalink
fix description updating in trade
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekwav committed May 17, 2023
1 parent afb1844 commit 72ba24f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "de.torui.coflmod"
version = "1.5.2-alpha"
version = "1.5.3-alpha"

// Toolchains:
java {
Expand Down
34 changes: 22 additions & 12 deletions src/main/java/de/torui/coflsky/handlers/DescriptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ public void Close() {
public static String ExtractStackableIdFromItemStack(ItemStack stack) {
if (stack != null) {
try {
String uuid = stack.serializeNBT().getCompoundTag("tag").getCompoundTag("ExtraAttributes")
.getString("id") + ":" + stack.stackSize;
if (uuid.length() == 0) {
throw new Exception();
}
return uuid;
NBTTagCompound serialized = stack.serializeNBT();
String itemTag = serialized.getCompoundTag("tag").getCompoundTag("ExtraAttributes")
.getString("id");
if (itemTag != null && itemTag.length() > 1)
return itemTag + ":" + stack.stackSize;
return serialized.getCompoundTag("tag").getCompoundTag("display")
.getString("Name");
} catch (Exception e) {
}
}
Expand Down Expand Up @@ -91,25 +92,33 @@ private DescModification[] getTooltipData(ItemStack itemStack) {
return EMPTY_ARRAY;
}

/**
* 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);
int iteration = 0;
int iteration = 1;
while (IsOpen) {
iteration++;
try {
Thread.sleep(300 + iteration);
Thread.sleep(300 + iteration++);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (shouldUpdate || iteration % 10 == 0 && hasAnyStackChanged(gc)) {
if (shouldUpdate || hasAnyStackChanged(gc)) {
shouldUpdate = false;
loadDescriptionForInventory(event, gc, true);
// reduce update time since its more likely that more changes occure after one
iteration = 5;
}
if (iteration >= 30)
iteration = 30; // cap at 9 second update interval
iteration = 29; // cap at 9 second update interval
}
}

Expand Down Expand Up @@ -201,7 +210,8 @@ public void setTooltips(ItemTooltipEvent event) {

for (DescModification datum : data) {
if (event.toolTip.size() <= datum.line) {
System.out.println("Skipped line modification " + datum.line + " for " + event.itemStack.getDisplayName());
System.out.println(
"Skipped line modification " + datum.line + " for " + event.itemStack.getDisplayName());
continue;
}
switch (datum.type) {
Expand Down

0 comments on commit 72ba24f

Please sign in to comment.