package appeng.me.storage; import java.util.HashMap; import java.util.Iterator; import java.util.Map.Entry; import appeng.api.networking.security.BaseActionSource; import appeng.api.networking.storage.IBaseMonitor; import appeng.api.storage.IMEInventory; import appeng.api.storage.IMEMonitor; import appeng.api.storage.IMEMonitorHandlerReceiver; import appeng.api.storage.StorageChannel; import appeng.api.storage.data.IAEStack; import appeng.api.storage.data.IItemList; import appeng.util.Platform; import appeng.util.inv.ItemListIgnoreCrafting; public class MEMonitorPassthu> extends MEPassthru implements IMEMonitor, IMEMonitorHandlerReceiver { HashMap, Object> listeners = new HashMap(); IMEMonitor monitor; public BaseActionSource changeSource; public MEMonitorPassthu(IMEInventory i, StorageChannel channel) { super( i, channel ); if ( i instanceof IMEMonitor ) monitor = (IMEMonitor) i; } @Override public void setInternal(IMEInventory i) { if ( monitor != null ) monitor.removeListener( this ); monitor = null; IItemList before = getInternal() == null ? channel.createList() : getInternal() .getAvailableItems( new ItemListIgnoreCrafting( channel.createList() ) ); super.setInternal( i ); if ( i instanceof IMEMonitor ) monitor = (IMEMonitor) i; IItemList after = getInternal() == null ? channel.createList() : getInternal() .getAvailableItems( new ItemListIgnoreCrafting( channel.createList() ) ); if ( monitor != null ) monitor.addListener( this, monitor ); Platform.postListChanges( before, after, this, changeSource ); } @Override public IItemList getAvailableItems(IItemList out) { super.getAvailableItems( new ItemListIgnoreCrafting( out ) ); return out; } @Override public void addListener(IMEMonitorHandlerReceiver l, Object verificationToken) { listeners.put( l, verificationToken ); } @Override public void removeListener(IMEMonitorHandlerReceiver l) { listeners.remove( l ); } @Override public IItemList getStorageList() { if ( monitor == null ) { IItemList out = channel.createList(); getInternal().getAvailableItems( new ItemListIgnoreCrafting( out ) ); return out; } return monitor.getStorageList(); } @Override public boolean isValid(Object verificationToken) { return verificationToken == monitor; } @Override public void postChange(IBaseMonitor monitor, T change, BaseActionSource source) { Iterator, Object>> i = listeners.entrySet().iterator(); while (i.hasNext()) { Entry, Object> e = i.next(); IMEMonitorHandlerReceiver recv = e.getKey(); if ( recv.isValid( e.getValue() ) ) recv.postChange( this, change, source ); else i.remove(); } } @Override public void onListUpdate() { Iterator, Object>> i = listeners.entrySet().iterator(); while (i.hasNext()) { Entry, Object> e = i.next(); IMEMonitorHandlerReceiver recv = e.getKey(); if ( recv.isValid( e.getValue() ) ) recv.onListUpdate(); else i.remove(); } } }