From d48c24a2e12f448f4def43572e61fe901a28a77c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Salom=C3=A3o?= Date: Mon, 5 Jul 2021 19:56:41 -0300 Subject: [PATCH] avoid relaying changes if theres no one listening to the passthrough --- .../api/storage/IMEMonitorHandlerReceiver.java | 5 +++++ src/main/java/appeng/me/cache/NetworkMonitor.java | 2 +- .../appeng/me/storage/MEMonitorPassThrough.java | 13 +++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/api/java/appeng/api/storage/IMEMonitorHandlerReceiver.java b/src/api/java/appeng/api/storage/IMEMonitorHandlerReceiver.java index c7b0d88a795..dd78056ad2a 100644 --- a/src/api/java/appeng/api/storage/IMEMonitorHandlerReceiver.java +++ b/src/api/java/appeng/api/storage/IMEMonitorHandlerReceiver.java @@ -52,4 +52,9 @@ public interface IMEMonitorHandlerReceiver> * called when the list updates its contents, this is mostly for handling power events. */ void onListUpdate(); + + default boolean hasListeners() + { + return true; + } } diff --git a/src/main/java/appeng/me/cache/NetworkMonitor.java b/src/main/java/appeng/me/cache/NetworkMonitor.java index 8da2a39e1fc..0337d4cf36b 100644 --- a/src/main/java/appeng/me/cache/NetworkMonitor.java +++ b/src/main/java/appeng/me/cache/NetworkMonitor.java @@ -262,7 +262,7 @@ private void notifyListenersOfChange( final Iterable diff, final IActionSourc { final Entry, Object> o = i.next(); final IMEMonitorHandlerReceiver receiver = o.getKey(); - if( receiver.isValid( o.getValue() ) ) + if( receiver.isValid( o.getValue() ) && receiver.hasListeners() ) { receiver.postChange( this, diff, src ); } diff --git a/src/main/java/appeng/me/storage/MEMonitorPassThrough.java b/src/main/java/appeng/me/storage/MEMonitorPassThrough.java index 4c189eaf2f4..00fb7acef48 100644 --- a/src/main/java/appeng/me/storage/MEMonitorPassThrough.java +++ b/src/main/java/appeng/me/storage/MEMonitorPassThrough.java @@ -90,6 +90,13 @@ public IItemList getAvailableItems( final IItemList out ) @Override public void addListener( final IMEMonitorHandlerReceiver l, final Object verificationToken ) { + if( this.listeners.size() == 0 ) + { + if( this.monitor != null ) + { + this.monitor.addListener( this, this.monitor ); + } + } this.listeners.put( l, verificationToken ); } @@ -117,6 +124,12 @@ public boolean isValid( final Object verificationToken ) return verificationToken == this.monitor; } + @Override + public boolean hasListeners() + { + return this.listeners.size() > 0; + } + @Override public void postChange( final IBaseMonitor monitor, final Iterable change, final IActionSource source ) {