2013-12-27 23:59:59 +01:00
|
|
|
package appeng.me.storage;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
2014-01-05 09:43:49 +01:00
|
|
|
import appeng.api.networking.security.BaseActionSource;
|
2014-06-27 07:44:56 +02:00
|
|
|
import appeng.api.networking.storage.IBaseMonitor;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.storage.IMEInventory;
|
|
|
|
import appeng.api.storage.IMEMonitor;
|
2014-02-12 17:35:42 +01:00
|
|
|
import appeng.api.storage.IMEMonitorHandlerReceiver;
|
2014-07-23 21:11:12 +02:00
|
|
|
import appeng.api.storage.StorageChannel;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.storage.data.IAEStack;
|
|
|
|
import appeng.api.storage.data.IItemList;
|
|
|
|
import appeng.util.Platform;
|
2014-07-21 23:28:52 +02:00
|
|
|
import appeng.util.inv.ItemListIgnoreCrafting;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-02-12 17:35:42 +01:00
|
|
|
public class MEMonitorPassthu<T extends IAEStack<T>> extends MEPassthru<T> implements IMEMonitor<T>, IMEMonitorHandlerReceiver<T>
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
2014-02-12 17:35:42 +01:00
|
|
|
HashMap<IMEMonitorHandlerReceiver<T>, Object> listeners = new HashMap();
|
2013-12-27 23:59:59 +01:00
|
|
|
IMEMonitor<T> monitor;
|
|
|
|
|
2014-01-05 09:43:49 +01:00
|
|
|
public BaseActionSource changeSource;
|
|
|
|
|
2014-07-24 06:20:16 +02:00
|
|
|
public MEMonitorPassthu(IMEInventory<T> i, StorageChannel channel) {
|
|
|
|
super( i, channel );
|
2013-12-27 23:59:59 +01:00
|
|
|
if ( i instanceof IMEMonitor )
|
|
|
|
monitor = (IMEMonitor<T>) i;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setInternal(IMEInventory<T> i)
|
|
|
|
{
|
|
|
|
if ( monitor != null )
|
|
|
|
monitor.removeListener( this );
|
|
|
|
|
|
|
|
monitor = null;
|
2014-07-24 06:20:16 +02:00
|
|
|
IItemList<T> before = getInternal() == null ? channel.createList() : getInternal()
|
|
|
|
.getAvailableItems( new ItemListIgnoreCrafting( channel.createList() ) );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
super.setInternal( i );
|
|
|
|
if ( i instanceof IMEMonitor )
|
|
|
|
monitor = (IMEMonitor<T>) i;
|
|
|
|
|
2014-07-24 06:20:16 +02:00
|
|
|
IItemList<T> after = getInternal() == null ? channel.createList() : getInternal()
|
|
|
|
.getAvailableItems( new ItemListIgnoreCrafting( channel.createList() ) );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
if ( monitor != null )
|
|
|
|
monitor.addListener( this, monitor );
|
|
|
|
|
2014-01-05 09:43:49 +01:00
|
|
|
Platform.postListChanges( before, after, this, changeSource );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-07-21 23:28:52 +02:00
|
|
|
@Override
|
|
|
|
public IItemList<T> getAvailableItems(IItemList out)
|
|
|
|
{
|
|
|
|
super.getAvailableItems( new ItemListIgnoreCrafting( out ) );
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
2014-02-12 17:35:42 +01:00
|
|
|
public void addListener(IMEMonitorHandlerReceiver<T> l, Object verificationToken)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
listeners.put( l, verificationToken );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-12 17:35:42 +01:00
|
|
|
public void removeListener(IMEMonitorHandlerReceiver<T> l)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
listeners.remove( l );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IItemList<T> getStorageList()
|
|
|
|
{
|
|
|
|
if ( monitor == null )
|
2014-07-21 23:28:52 +02:00
|
|
|
{
|
2014-07-23 21:11:12 +02:00
|
|
|
IItemList<T> out = channel.createList();
|
2014-07-21 23:28:52 +02:00
|
|
|
getInternal().getAvailableItems( new ItemListIgnoreCrafting( out ) );
|
|
|
|
return out;
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
return monitor.getStorageList();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isValid(Object verificationToken)
|
|
|
|
{
|
|
|
|
return verificationToken == monitor;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-06-27 07:44:56 +02:00
|
|
|
public void postChange(IBaseMonitor<T> monitor, T change, BaseActionSource source)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-02-12 17:35:42 +01:00
|
|
|
Iterator<Entry<IMEMonitorHandlerReceiver<T>, Object>> i = listeners.entrySet().iterator();
|
2013-12-27 23:59:59 +01:00
|
|
|
while (i.hasNext())
|
|
|
|
{
|
2014-02-12 17:35:42 +01:00
|
|
|
Entry<IMEMonitorHandlerReceiver<T>, Object> e = i.next();
|
|
|
|
IMEMonitorHandlerReceiver<T> recv = e.getKey();
|
2013-12-27 23:59:59 +01:00
|
|
|
if ( recv.isValid( e.getValue() ) )
|
2014-01-05 09:43:49 +01:00
|
|
|
recv.postChange( this, change, source );
|
2013-12-27 23:59:59 +01:00
|
|
|
else
|
|
|
|
i.remove();
|
|
|
|
}
|
|
|
|
}
|
2014-03-28 05:31:06 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onListUpdate()
|
|
|
|
{
|
|
|
|
Iterator<Entry<IMEMonitorHandlerReceiver<T>, Object>> i = listeners.entrySet().iterator();
|
|
|
|
while (i.hasNext())
|
|
|
|
{
|
|
|
|
Entry<IMEMonitorHandlerReceiver<T>, Object> e = i.next();
|
|
|
|
IMEMonitorHandlerReceiver<T> recv = e.getKey();
|
|
|
|
if ( recv.isValid( e.getValue() ) )
|
|
|
|
recv.onListUpdate();
|
|
|
|
else
|
|
|
|
i.remove();
|
|
|
|
}
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|