2014-11-14 12:02:52 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
|
|
|
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
|
|
|
*/
|
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
package appeng.me.storage;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
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;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
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
|
|
|
public BaseActionSource changeSource;
|
2015-04-03 08:54:31 +02:00
|
|
|
IMEMonitor<T> monitor;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
public MEMonitorPassThrough( IMEInventory<T> i, StorageChannel channel )
|
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
super( i, channel );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( i instanceof IMEMonitor )
|
2014-12-29 15:13:47 +01:00
|
|
|
this.monitor = (IMEMonitor<T>) i;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public void setInternal( IMEInventory<T> i )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.monitor != null )
|
2014-12-29 15:13:47 +01:00
|
|
|
this.monitor.removeListener( this );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.monitor = null;
|
2015-04-03 08:54:31 +02:00
|
|
|
IItemList<T> before = this.getInternal() == null ? this.channel.createList() : this.getInternal().getAvailableItems( new ItemListIgnoreCrafting( this.channel.createList() ) );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
super.setInternal( i );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( i instanceof IMEMonitor )
|
2014-12-29 15:13:47 +01:00
|
|
|
this.monitor = (IMEMonitor<T>) i;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
IItemList<T> after = this.getInternal() == null ? this.channel.createList() : this.getInternal().getAvailableItems( new ItemListIgnoreCrafting( this.channel.createList() ) );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.monitor != null )
|
2014-12-29 15:13:47 +01:00
|
|
|
this.monitor.addListener( this, this.monitor );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
Platform.postListChanges( before, after, this, this.changeSource );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public IItemList<T> getAvailableItems( IItemList out )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
super.getAvailableItems( new ItemListIgnoreCrafting( out ) );
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public void addListener( IMEMonitorHandlerReceiver<T> l, Object verificationToken )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.listeners.put( l, verificationToken );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public void removeListener( IMEMonitorHandlerReceiver<T> l )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.listeners.remove( l );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IItemList<T> getStorageList()
|
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.monitor == null )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
IItemList<T> out = this.channel.createList();
|
|
|
|
this.getInternal().getAvailableItems( new ItemListIgnoreCrafting( out ) );
|
2014-09-24 02:26:27 +02:00
|
|
|
return out;
|
|
|
|
}
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.monitor.getStorageList();
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public boolean isValid( Object verificationToken )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return verificationToken == this.monitor;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public void postChange( IBaseMonitor<T> monitor, Iterable<T> change, BaseActionSource source )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
Iterator<Entry<IMEMonitorHandlerReceiver<T>, Object>> i = this.listeners.entrySet().iterator();
|
2015-04-03 08:54:31 +02:00
|
|
|
while( i.hasNext() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
Entry<IMEMonitorHandlerReceiver<T>, Object> e = i.next();
|
2014-09-28 11:47:17 +02:00
|
|
|
IMEMonitorHandlerReceiver<T> receiver = e.getKey();
|
2015-04-03 08:54:31 +02:00
|
|
|
if( receiver.isValid( e.getValue() ) )
|
2014-09-28 11:47:17 +02:00
|
|
|
receiver.postChange( this, change, source );
|
2014-09-24 02:26:27 +02:00
|
|
|
else
|
|
|
|
i.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onListUpdate()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
Iterator<Entry<IMEMonitorHandlerReceiver<T>, Object>> i = this.listeners.entrySet().iterator();
|
2015-04-03 08:54:31 +02:00
|
|
|
while( i.hasNext() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
Entry<IMEMonitorHandlerReceiver<T>, Object> e = i.next();
|
2014-09-28 11:47:17 +02:00
|
|
|
IMEMonitorHandlerReceiver<T> receiver = e.getKey();
|
2015-04-03 08:54:31 +02:00
|
|
|
if( receiver.isValid( e.getValue() ) )
|
2014-09-28 11:47:17 +02:00
|
|
|
receiver.onListUpdate();
|
2014-09-24 02:26:27 +02:00
|
|
|
else
|
|
|
|
i.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|