Skip to content

Commit 6bb1d38

Browse files
Use window notification system for updating window state
1 parent 810c0ee commit 6bb1d38

6 files changed

Lines changed: 111 additions & 58 deletions

File tree

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
final class BrewingWindowImpl extends AbstractSplitWindow<CustomBrewingStandMenu> implements BrewingWindow {
1616

17+
private static final int BREW_PROGRESS_MAGIC_SLOT = 100;
18+
private static final int FUEL_PROGRESS_MAGIC_SLOT = 101;
1719
private static final double DEFAULT_BREW_PROGRESS = 0.0;
1820
private static final double DEFAULT_FUEL_PROGRESS = 0.0;
1921

@@ -50,12 +52,21 @@ public BrewingWindowImpl(
5052
this.brewProgress = brewProgress;
5153
this.fuelProgress = fuelProgress;
5254

53-
brewProgress.observeWeak(this, thisRef -> thisRef.menu.setBrewProgress(thisRef.getBrewProgress()));
54-
fuelProgress.observeWeak(this, thisRef -> thisRef.menu.setFuelProgress(thisRef.getFuelProgress()));
55+
brewProgress.observeWeak(this, thisRef -> thisRef.notifyUpdate(BREW_PROGRESS_MAGIC_SLOT));
56+
fuelProgress.observeWeak(this, thisRef -> thisRef.notifyUpdate(FUEL_PROGRESS_MAGIC_SLOT));
5557
menu.setBrewProgress(getBrewProgress());
5658
menu.setFuelProgress(getFuelProgress());
5759
}
5860

61+
@Override
62+
protected void update(int slot) {
63+
switch (slot) {
64+
case BREW_PROGRESS_MAGIC_SLOT -> menu.setBrewProgress(getBrewProgress());
65+
case FUEL_PROGRESS_MAGIC_SLOT -> menu.setFuelProgress(getFuelProgress());
66+
default -> super.update(slot);
67+
}
68+
}
69+
5970
@Override
6071
public void setBrewProgress(double progress) {
6172
brewProgress.set(progress);

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
final class CartographyWindowImpl extends AbstractSplitWindow<CustomCartographyMenu> implements CartographyWindow {
2424

25+
private static final int VIEW_MAGIC_SLOT = 100;
26+
private static final int ICONS_MAGIC_SLOT = 101;
2527
private static final Set<? extends MapIcon> DEFAULT_ICONS = Set.of();
2628
private static final View DEFAULT_VIEW = View.NORMAL;
2729

@@ -54,8 +56,8 @@ public CartographyWindowImpl(
5456
this.view = view;
5557
this.icons = icons;
5658

57-
view.observeWeak(this, thisRef -> thisRef.menu.setView(thisRef.getView()));
58-
icons.observeWeak(this, thisRef -> thisRef.menu.setIcons(thisRef.getIcons(), isOpen()));
59+
view.observeWeak(this, thisRef -> thisRef.notifyUpdate(VIEW_MAGIC_SLOT));
60+
icons.observeWeak(this, thisRef -> thisRef.notifyUpdate(ICONS_MAGIC_SLOT));
5961

6062
menu.setView(getView());
6163
menu.setIcons(getIcons(), false);
@@ -70,6 +72,15 @@ protected void setMenuItem(int slot, @Nullable ItemStack itemStack) {
7072
}
7173
}
7274

75+
@Override
76+
protected void update(int slot) {
77+
switch(slot) {
78+
case VIEW_MAGIC_SLOT -> menu.setView(getView());
79+
case ICONS_MAGIC_SLOT -> menu.setIcons(getIcons(), isOpen());
80+
default -> super.update(slot);
81+
}
82+
}
83+
7384
@Override
7485
public void resetMap() {
7586
menu.resetMap();

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
final class CrafterWindowImpl extends AbstractSplitWindow<CustomCrafterMenu> implements CrafterWindow {
2020

21+
private static final int SLOTS_MAGIC_SLOT_OFFSET = 100;
2122
private static final int CRAFTING_SLOTS = 9;
2223
private static final boolean DEFAULT_SLOT_DISABLED_STATE = false;
2324

@@ -52,12 +53,21 @@ final class CrafterWindowImpl extends AbstractSplitWindow<CustomCrafterMenu> imp
5253
for (int i = 0; i < CRAFTING_SLOTS; i++) {
5354
int slot = i;
5455
MutableProperty<Boolean> property = slots.get(slot);
55-
property.observeWeak(this, thisRef -> thisRef.menu.setSlotDisabled(slot, thisRef.isSlotDisabled(slot)));
56+
property.observeWeak(this, thisRef -> thisRef.update(SLOTS_MAGIC_SLOT_OFFSET + slot));
5657
menu.setSlotDisabled(slot, isSlotDisabled(slot));
5758
}
5859
menu.setSlotStateChangeHandler(this::playerToggleSlot);
5960
}
6061

62+
@Override
63+
protected void update(int slot) {
64+
if (slot >= SLOTS_MAGIC_SLOT_OFFSET && slot < SLOTS_MAGIC_SLOT_OFFSET + CRAFTING_SLOTS) {
65+
menu.setSlotDisabled(slot - SLOTS_MAGIC_SLOT_OFFSET, isSlotDisabled(slot - SLOTS_MAGIC_SLOT_OFFSET));
66+
} else {
67+
super.update(slot);
68+
}
69+
}
70+
6171
private void playerToggleSlot(int slot, boolean disabled) {
6272
slots.get(slot).set(disabled);
6373
CollectionUtils.forEachCatching(

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
final class FurnaceWindowImpl extends AbstractSplitWindow<CustomFurnaceMenu> implements FurnaceWindow {
2121

22+
private static final int COOK_PROGRESS_MAGIC_SLOT = 100;
23+
private static final int BURN_PROGRESS_MAGIC_SLOT = 101;
2224
private static final double DEFAULT_COOK_PROGRESS = 0.0;
2325
private static final double DEFAULT_BURN_PROGRESS = 0.0;
2426

@@ -51,13 +53,22 @@ public FurnaceWindowImpl(
5153
this.cookProgress = cookProgress;
5254
this.burnProgress = burnProgress;
5355

54-
cookProgress.observeWeak(this, thisRef -> thisRef.menu.setCookProgress(thisRef.getCookProgress()));
55-
burnProgress.observeWeak(this, thisRef -> thisRef.menu.setBurnProgress(thisRef.getBurnProgress()));
56+
cookProgress.observeWeak(this, thisRef -> thisRef.notifyUpdate(COOK_PROGRESS_MAGIC_SLOT));
57+
burnProgress.observeWeak(this, thisRef -> thisRef.notifyUpdate(BURN_PROGRESS_MAGIC_SLOT));
5658
menu.setCookProgress(getCookProgress());
5759
menu.setBurnProgress(getBurnProgress());
5860
menu.setRecipeClickHandler(this::handleRecipeClick);
5961
}
6062

63+
@Override
64+
protected void update(int slot) {
65+
switch (slot) {
66+
case COOK_PROGRESS_MAGIC_SLOT -> menu.setCookProgress(getCookProgress());
67+
case BURN_PROGRESS_MAGIC_SLOT -> menu.setBurnProgress(getBurnProgress());
68+
default -> super.update(slot);
69+
}
70+
}
71+
6172
private void handleRecipeClick(Key recipeId) {
6273
CollectionUtils.forEachCatching(
6374
recipeClickHandlers,

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

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@
2626

2727
final class MerchantWindowImpl extends AbstractSplitWindow<CustomMerchantMenu> implements MerchantWindow {
2828

29-
private static final int TRADE_MAGIC_SLOT = 100; // magic slot number that, when notified, updates trades
29+
private static final int TRADES_REBUILD_MAGIC_SLOT = 100;
30+
private static final int TRADES_RESEND_MAGIC_SLOT = 101;
3031
private static final List<? extends Trade> DEFAULT_TRADES = List.of();
3132
private static final int DEFAULT_LEVEL = 0;
3233
private static final double DEFAULT_PROGRESS = -1.0;
3334
private static final boolean DEFAULT_RESTOCK_MESSAGE_ENABLED = false;
3435

3536
private final AbstractGui upperGui;
3637
private final AbstractGui lowerGui;
37-
private final List<TradeImpl> lastKnownTrades = new ArrayList<>();
38+
private final List<TradeImpl> activeTrades = new ArrayList<>();
3839

39-
private final MutableProperty<List<? extends Trade>> trades;
40+
private final MutableProperty<? super List<? extends Trade>> trades;
4041
private final MutableProperty<Integer> level;
4142
private final MutableProperty<Double> progress;
4243
private final MutableProperty<Boolean> restockMessage;
@@ -49,7 +50,7 @@ final class MerchantWindowImpl extends AbstractSplitWindow<CustomMerchantMenu> i
4950
Supplier<? extends Component> title,
5051
AbstractGui upperGui,
5152
AbstractGui lowerGui,
52-
MutableProperty<List<? extends Trade>> trades,
53+
MutableProperty<? super List<? extends Trade>> trades,
5354
MutableProperty<Integer> level,
5455
MutableProperty<Double> progress,
5556
MutableProperty<Boolean> restockMessage,
@@ -66,19 +67,19 @@ final class MerchantWindowImpl extends AbstractSplitWindow<CustomMerchantMenu> i
6667
this.progress = progress;
6768
this.restockMessage = restockMessage;
6869

69-
trades.observeWeak(this, MerchantWindowImpl::updateTrades);
70-
this.level.observeWeak(this, MerchantWindowImpl::updateTrades);
71-
progress.observeWeak(this, MerchantWindowImpl::updateTrades);
72-
restockMessage.observeWeak(this, MerchantWindowImpl::updateTrades);
70+
trades.observeWeak(this, thisRef -> thisRef.notifyUpdate(TRADES_REBUILD_MAGIC_SLOT));
71+
level.observeWeak(this, thisRef -> thisRef.notifyUpdate(TRADES_RESEND_MAGIC_SLOT));
72+
progress.observeWeak(this, thisRef -> thisRef.notifyUpdate(TRADES_RESEND_MAGIC_SLOT));
73+
restockMessage.observeWeak(this, thisRef -> thisRef.notifyUpdate(TRADES_RESEND_MAGIC_SLOT));
7374

7475
menu.setTradeSelectHandler(this::handleTradeSelect);
75-
updateTrades();
76+
rebuildTrades();
7677
}
7778

7879
private void handleTradeSelect(int tradeIndex) {
79-
if (tradeIndex < 0 || tradeIndex >= lastKnownTrades.size())
80+
if (tradeIndex < 0 || tradeIndex >= activeTrades.size())
8081
return;
81-
lastKnownTrades.get(tradeIndex).handleClick(getViewer());
82+
activeTrades.get(tradeIndex).handleClick(getViewer());
8283

8384
if (tradeIndex != previousSelectedTrade) {
8485
CollectionUtils.forEachCatching(
@@ -112,7 +113,7 @@ public void setTrades(List<? extends Trade> trades) {
112113

113114
@Override
114115
public @UnmodifiableView List<Trade> getTrades() {
115-
return Collections.unmodifiableList(lastKnownTrades);
116+
return Collections.unmodifiableList(activeTrades);
116117
}
117118

118119
@Override
@@ -130,59 +131,58 @@ public boolean isRestockMessageEnabled() {
130131
return FuncUtils.getSafely(restockMessage, DEFAULT_RESTOCK_MESSAGE_ENABLED);
131132
}
132133

133-
@SuppressWarnings("unchecked")
134-
private void updateTrades() {
135-
this.lastKnownTrades.forEach(this::removeTradeViewer);
136-
this.lastKnownTrades.clear();
137-
this.lastKnownTrades.addAll((List<TradeImpl>) FuncUtils.getSafely(trades, DEFAULT_TRADES));
138-
this.lastKnownTrades.forEach(this::addTradeViewer);
134+
@Override
135+
protected void registerAsViewer() {
136+
super.registerAsViewer();
137+
this.activeTrades.forEach(this::addTradeViewer);
139138

140-
notifyUpdate(TRADE_MAGIC_SLOT);
139+
notifyUpdate(TRADES_RESEND_MAGIC_SLOT);
140+
}
141+
142+
@Override
143+
protected void unregisterAsViewer() {
144+
super.unregisterAsViewer();
145+
this.activeTrades.forEach(this::removeTradeViewer);
146+
}
147+
148+
@Override
149+
protected void update(int slot) {
150+
switch (slot) {
151+
case TRADES_REBUILD_MAGIC_SLOT -> rebuildTrades();
152+
case TRADES_RESEND_MAGIC_SLOT ->
153+
menu.sendTrades(activeTrades, getLevel(), getProgress(), isRestockMessageEnabled());
154+
default -> super.update(slot);
155+
}
156+
}
157+
158+
@SuppressWarnings("unchecked")
159+
private void rebuildTrades() {
160+
this.activeTrades.forEach(this::removeTradeViewer);
161+
this.activeTrades.clear();
162+
this.activeTrades.addAll((List<? extends TradeImpl>) FuncUtils.getSafely(trades, DEFAULT_TRADES));
163+
this.activeTrades.forEach(this::addTradeViewer);
141164
}
142165

143166
private void removeTradeViewer(TradeImpl trade) {
144167
if (trade.getFirstInput() != null)
145-
trade.getFirstInput().removeViewer(this, TRADE_MAGIC_SLOT);
168+
trade.getFirstInput().removeViewer(this, TRADES_RESEND_MAGIC_SLOT);
146169
if (trade.getSecondInput() != null)
147-
trade.getSecondInput().removeViewer(this, TRADE_MAGIC_SLOT);
170+
trade.getSecondInput().removeViewer(this, TRADES_RESEND_MAGIC_SLOT);
148171
if (trade.getOutput() != null)
149-
trade.getOutput().removeViewer(this, TRADE_MAGIC_SLOT);
172+
trade.getOutput().removeViewer(this, TRADES_RESEND_MAGIC_SLOT);
150173
trade.getDiscountProperty().unobserveWeak(this);
151174
trade.getAvailableProperty().unobserveWeak(this);
152175
}
153176

154177
private void addTradeViewer(TradeImpl trade) {
155178
if (trade.getFirstInput() != null)
156-
trade.getFirstInput().addViewer(this, TRADE_MAGIC_SLOT);
179+
trade.getFirstInput().addViewer(this, TRADES_RESEND_MAGIC_SLOT);
157180
if (trade.getSecondInput() != null)
158-
trade.getSecondInput().addViewer(this, TRADE_MAGIC_SLOT);
181+
trade.getSecondInput().addViewer(this, TRADES_RESEND_MAGIC_SLOT);
159182
if (trade.getOutput() != null)
160-
trade.getOutput().addViewer(this, TRADE_MAGIC_SLOT);
161-
trade.getDiscountProperty().observeWeak(this, MerchantWindowImpl::updateTrades);
162-
trade.getAvailableProperty().observeWeak(this, MerchantWindowImpl::updateTrades);
163-
}
164-
165-
@Override
166-
protected void registerAsViewer() {
167-
super.registerAsViewer();
168-
this.lastKnownTrades.forEach(this::addTradeViewer);
169-
170-
notifyUpdate(TRADE_MAGIC_SLOT);
171-
}
172-
173-
@Override
174-
protected void unregisterAsViewer() {
175-
super.unregisterAsViewer();
176-
this.lastKnownTrades.forEach(this::removeTradeViewer);
177-
}
178-
179-
@Override
180-
protected void update(int slot) {
181-
if (slot == TRADE_MAGIC_SLOT) {
182-
menu.sendTrades(lastKnownTrades, getLevel(), getProgress(), isRestockMessageEnabled());
183-
} else {
184-
super.update(slot);
185-
}
183+
trade.getOutput().addViewer(this, TRADES_RESEND_MAGIC_SLOT);
184+
trade.getDiscountProperty().observeWeak(this, thisRef -> thisRef.notifyUpdate(TRADES_RESEND_MAGIC_SLOT));
185+
trade.getAvailableProperty().observeWeak(this, thisRef -> thisRef.notifyUpdate(TRADES_RESEND_MAGIC_SLOT));
186186
}
187187

188188
@Override

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
*/
3636
final class StonecutterWindowImpl extends AbstractSplitWindow<CustomStonecutterMenu> implements StonecutterWindow {
3737

38+
private static final int SELECTED_SLOT_MAGIC_SLOT = 100;
3839
private static final int DEFAULT_SELECTED_SLOT = -1;
3940

4041
private final AbstractGui upperGui;
@@ -70,11 +71,20 @@ public StonecutterWindowImpl(
7071
this.selectedSlot = selectedSlot;
7172
this.selectedSlotChangeHandlers = new ArrayList<>(selectedSlotChangeHandlers);
7273

73-
selectedSlot.observeWeak(this, thisRef -> thisRef.menu.setSelectedSlot(thisRef.getSelectedSlot()));
74+
selectedSlot.observeWeak(this, thisRef -> thisRef.notifyUpdate(SELECTED_SLOT_MAGIC_SLOT));
7475
menu.setSelectedSlot(getSelectedSlot());
7576
menu.setClickHandler(this::playerSelectSlot);
7677
}
7778

79+
@Override
80+
protected void update(int slot) {
81+
if (slot == SELECTED_SLOT_MAGIC_SLOT) {
82+
playerSelectSlot(getSelectedSlot(), menu.getSelectedSlot());
83+
} else {
84+
super.update(slot);
85+
}
86+
}
87+
7888
private void playerSelectSlot(int prev, int slot) {
7989
selectedSlot.set(slot);
8090
CollectionUtils.forEachCatching(

0 commit comments

Comments
 (0)