2014-09-24 02:26:27 +02:00
|
|
|
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;
|
|
|
|
|
2014-09-28 11:47:17 +02:00
|
|
|
public class MEMonitorPassThrough<T extends IAEStack<T>> extends MEPassThrough<T> implements IMEMonitor<T>, IMEMonitorHandlerReceiver<T>
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
|
2014-09-29 09:54:34 +02:00
|
|
|
final HashMap<IMEMonitorHandlerReceiver<T>, Object> listeners = new HashMap<IMEMonitorHandlerReceiver<T>, Object>();
|
2014-09-24 02:26:27 +02:00
|
|
|
IMEMonitor<T> monitor;
|
|
|
|
|
|
|
|
public BaseActionSource changeSource;
|
|
|
|
|
2014-09-28 11:47:17 +02:00
|
|
|
public MEMonitorPassThrough(IMEInventory<T> i, StorageChannel channel) {
|
2014-09-24 02:26:27 +02:00
|
|
|
super( i, channel );
|
|
|
|
if ( i instanceof IMEMonitor )
|
|
|
|
monitor = (IMEMonitor<T>) i;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setInternal(IMEInventory<T> i)
|
|
|
|
{
|
|
|
|
if ( monitor != null )
|
|
|
|
monitor.removeListener( this );
|
|
|
|
|
|
|
|
monitor = null;
|
|
|
|
IItemList<T> before = getInternal() == null ? channel.createList() : getInternal()
|
|
|
|
.getAvailableItems( new ItemListIgnoreCrafting( channel.createList() ) );
|
|
|
|
|
|
|
|
super.setInternal( i );
|
|
|
|
if ( i instanceof IMEMonitor )
|
|
|
|
monitor = (IMEMonitor<T>) i;
|
|
|
|
|
|
|
|
IItemList<T> 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<T> getAvailableItems(IItemList out)
|
|
|
|
{
|
|
|
|
super.getAvailableItems( new ItemListIgnoreCrafting( out ) );
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void addListener(IMEMonitorHandlerReceiver<T> l, Object verificationToken)
|
|
|
|
{
|
|
|
|
listeners.put( l, verificationToken );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeListener(IMEMonitorHandlerReceiver<T> l)
|
|
|
|
{
|
|
|
|
listeners.remove( l );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IItemList<T> getStorageList()
|
|
|
|
{
|
|
|
|
if ( monitor == null )
|
|
|
|
{
|
|
|
|
IItemList<T> 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<T> monitor, Iterable<T> change, BaseActionSource source)
|
|
|
|
{
|
|
|
|
Iterator<Entry<IMEMonitorHandlerReceiver<T>, Object>> i = listeners.entrySet().iterator();
|
|
|
|
while (i.hasNext())
|
|
|
|
{
|
|
|
|
Entry<IMEMonitorHandlerReceiver<T>, Object> e = i.next();
|
2014-09-28 11:47:17 +02:00
|
|
|
IMEMonitorHandlerReceiver<T> receiver = e.getKey();
|
|
|
|
if ( receiver.isValid( e.getValue() ) )
|
|
|
|
receiver.postChange( this, change, source );
|
2014-09-24 02:26:27 +02:00
|
|
|
else
|
|
|
|
i.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onListUpdate()
|
|
|
|
{
|
|
|
|
Iterator<Entry<IMEMonitorHandlerReceiver<T>, Object>> i = listeners.entrySet().iterator();
|
|
|
|
while (i.hasNext())
|
|
|
|
{
|
|
|
|
Entry<IMEMonitorHandlerReceiver<T>, Object> e = i.next();
|
2014-09-28 11:47:17 +02:00
|
|
|
IMEMonitorHandlerReceiver<T> receiver = e.getKey();
|
|
|
|
if ( receiver.isValid( e.getValue() ) )
|
|
|
|
receiver.onListUpdate();
|
2014-09-24 02:26:27 +02:00
|
|
|
else
|
|
|
|
i.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|