Skip to content

Commit e071404

Browse files
Only re-create path list if path actually changed in window tick
1 parent 9059a51 commit e071404

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

invui/src/main/java/xyz/xenondevs/invui/gui/SlotElement.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,25 @@ public ItemStack getItemStack(Player player) {
249249
return Collections.unmodifiableList(elements);
250250
}
251251

252+
/**
253+
* Follows {@link #getHoldingElement()} until a non-{@link GuiLink} is reached and compares the path
254+
* to the given list as returned by {@link #traverse()}.
255+
*
256+
* @return {@code true} if the path matches the given list, {@code false} otherwise.
257+
*/
258+
public boolean checkTraverse(List<SlotElement> other) {
259+
int i = 0;
260+
SlotElement current = this;
261+
while (current instanceof GuiLink(Gui g, int s)) {
262+
if (i >= other.size() || !current.equals(other.get(i)))
263+
return false;
264+
current = g.getSlotElement(s);
265+
i++;
266+
}
267+
return (i == other.size() && current == null)
268+
|| (i == other.size() - 1 && other.getLast().equals(current));
269+
}
270+
252271
@Override
253272
public void addObserver(Observer who, int how) {
254273
gui.addObserver(who, slot, how);

invui/src/main/java/xyz/xenondevs/invui/window/AbstractWindow.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,19 +109,20 @@ protected void update(int slot) {
109109
if (root == null)
110110
return;
111111

112-
List<SlotElement> newPath = root.traverse();
113-
List<SlotElement> oldPath = elementsDisplayed.get(slot);
112+
List<SlotElement> path = elementsDisplayed.get(slot);
114113

115114
// if path has changed, viewers need to be updated
116-
if (!newPath.equals(oldPath)) {
117-
unregisterAsViewer(slot, oldPath);
118-
registerAsViewer(slot, newPath);
119-
elementsDisplayed.set(slot, newPath);
115+
if (!root.checkTraverse(path)) {
116+
unregisterAsViewer(slot, path);
117+
path = root.traverse();
118+
119+
registerAsViewer(slot, path);
120+
elementsDisplayed.set(slot, path);
120121
}
121122

122123
// create and place item stack in inventory
123124
ItemStack itemStack;
124-
SlotElement lastElement = newPath.getLast();
125+
SlotElement lastElement = path.getLast();
125126
if (!(lastElement instanceof SlotElement.GuiLink)) {
126127
itemStack = lastElement.getItemStack(getViewer());
127128
if (itemStack != null && lastElement instanceof SlotElement.Item) {
@@ -131,7 +132,7 @@ protected void update(int slot) {
131132
}
132133
} else { // there is no holding element
133134
// background by gui
134-
itemStack = newPath.reversed().stream()
135+
itemStack = path.reversed().stream()
135136
.filter(e -> e instanceof SlotElement.GuiLink)
136137
.map(e -> ((SlotElement.GuiLink) e).gui().getBackground())
137138
.filter(Objects::nonNull)

0 commit comments

Comments
 (0)