2626
2727final 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
0 commit comments