Applied-Energistics-2-tiler.../src/main/java/appeng/parts/p2p/PartP2PTunnel.java

416 lines
10 KiB
Java
Raw Normal View History

2014-11-14 12:02:52 +01:00
/*
* This file is part of Applied Energistics 2.
2015-05-18 00:08:05 +02:00
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
2014-11-14 12:02:52 +01:00
*
* 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-01-20 17:41:37 +01:00
package appeng.parts.p2p;
2014-05-19 04:36:21 +02:00
import java.util.ArrayList;
2014-01-20 17:41:37 +01:00
import java.util.Collection;
2015-12-24 02:07:03 +01:00
import com.google.common.base.Optional;
2015-06-16 02:44:59 +02:00
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
2014-01-20 17:41:37 +01:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.Vec3d;
2015-06-16 02:44:59 +02:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2015-12-24 02:07:03 +01:00
2014-01-20 17:41:37 +01:00
import appeng.api.AEApi;
2014-05-02 07:40:46 +02:00
import appeng.api.config.Actionable;
import appeng.api.config.PowerMultiplier;
2014-01-20 17:41:37 +01:00
import appeng.api.config.PowerUnits;
2014-01-21 16:59:55 +01:00
import appeng.api.config.TunnelType;
import appeng.api.definitions.IParts;
2014-01-23 20:02:48 +01:00
import appeng.api.implementations.items.IMemoryCard;
2014-01-30 22:07:03 +01:00
import appeng.api.implementations.items.MemoryCardMessages;
2014-01-20 17:41:37 +01:00
import appeng.api.parts.IPart;
2014-09-20 22:20:53 +02:00
import appeng.api.parts.IPartCollisionHelper;
2014-01-30 22:07:03 +01:00
import appeng.api.parts.IPartItem;
2014-01-20 17:41:37 +01:00
import appeng.api.parts.PartItemStack;
import appeng.api.util.AECableType;
2015-06-16 02:44:59 +02:00
import appeng.api.util.AEPartLocation;
2014-05-02 07:40:46 +02:00
import appeng.core.AEConfig;
2014-01-20 17:41:37 +01:00
import appeng.me.GridAccessException;
import appeng.me.cache.P2PCache;
import appeng.me.cache.helpers.TunnelCollection;
import appeng.parts.PartBasicState;
2014-01-21 16:59:55 +01:00
import appeng.util.Platform;
2014-01-20 17:41:37 +01:00
public abstract class PartP2PTunnel<T extends PartP2PTunnel> extends PartBasicState
{
private final TunnelCollection type = new TunnelCollection<T>( null, this.getClass() );
private boolean output;
private long freq;
2014-01-20 17:41:37 +01:00
2015-09-30 14:24:40 +02:00
public PartP2PTunnel( final ItemStack is )
{
super( is );
}
2015-09-30 14:24:40 +02:00
public TunnelCollection<T> getCollection( final Collection<PartP2PTunnel> collection, final Class<? extends PartP2PTunnel> c )
{
if( this.type.matches( c ) )
{
this.type.setSource( collection );
return this.type;
}
return null;
}
T getInput()
{
if( this.getFrequency() == 0 )
2015-04-29 02:30:53 +02:00
{
return null;
2015-04-29 02:30:53 +02:00
}
try
{
final PartP2PTunnel tunnel = this.getProxy().getP2P().getInput( this.getFrequency() );
if( this.getClass().isInstance( tunnel ) )
2015-04-29 02:30:53 +02:00
{
return (T) tunnel;
2015-04-29 02:30:53 +02:00
}
}
2015-09-30 14:24:40 +02:00
catch( final GridAccessException e )
{
// :P
}
return null;
}
TunnelCollection<T> getOutputs() throws GridAccessException
{
if( this.getProxy().isActive() )
2015-04-29 02:30:53 +02:00
{
return (TunnelCollection<T>) this.getProxy().getP2P().getOutputs( this.getFrequency(), this.getClass() );
2015-04-29 02:30:53 +02:00
}
return new TunnelCollection( new ArrayList(), this.getClass() );
2014-01-20 17:41:37 +01:00
}
@Override
2015-09-30 14:24:40 +02:00
public void getBoxes( final IPartCollisionHelper bch )
{
bch.addBox( 5, 5, 12, 11, 11, 13 );
bch.addBox( 3, 3, 13, 13, 13, 14 );
bch.addBox( 2, 2, 14, 14, 14, 16 );
}
2014-01-20 17:41:37 +01:00
@Override
2015-09-30 14:24:40 +02:00
public ItemStack getItemStack( final PartItemStack type )
{
if( type == PartItemStack.World || type == PartItemStack.Network || type == PartItemStack.Wrench || type == PartItemStack.Pick )
2015-04-29 02:30:53 +02:00
{
return super.getItemStack( type );
2015-04-29 02:30:53 +02:00
}
final Optional<ItemStack> maybeMEStack = AEApi.instance().definitions().parts().p2PTunnelME().maybeStack( 1 );
if( maybeMEStack.isPresent() )
{
return maybeMEStack.get();
}
return super.getItemStack( type );
}
@Override
2015-09-30 14:24:40 +02:00
public void readFromNBT( final NBTTagCompound data )
2014-01-20 17:41:37 +01:00
{
2015-01-19 22:51:41 +01:00
super.readFromNBT( data );
this.setOutput( data.getBoolean( "output" ) );
this.setFrequency( data.getLong( "freq" ) );
2014-01-20 17:41:37 +01:00
}
@Override
2015-09-30 14:24:40 +02:00
public void writeToNBT( final NBTTagCompound data )
{
super.writeToNBT( data );
data.setBoolean( "output", this.isOutput() );
data.setLong( "freq", this.getFrequency() );
}
@Override
public float getCableConnectionLength( AECableType cable )
{
return 1;
}
@Override
public boolean useStandardMemoryCard()
{
return false;
}
@Override
public boolean onPartActivate( final EntityPlayer player, final EnumHand hand, final Vec3d pos )
2014-01-20 17:41:37 +01:00
{
2015-09-30 14:24:40 +02:00
final ItemStack is = player.inventory.getCurrentItem();
2014-01-20 17:41:37 +01:00
// UniqueIdentifier id = GameRegistry.findUniqueIdentifierFor( is.getItem() );
// AELog.info( "ID:" + id.toString() + " : " + is.getItemDamage() );
2015-09-30 14:24:40 +02:00
final TunnelType tt = AEApi.instance().registries().p2pTunnel().getTunnelTypeByItem( is );
if( is != null && is.getItem() instanceof IMemoryCard )
2014-01-20 17:41:37 +01:00
{
2015-09-30 14:24:40 +02:00
final IMemoryCard mc = (IMemoryCard) is.getItem();
final NBTTagCompound data = mc.getData( is );
2014-01-20 17:41:37 +01:00
2015-09-30 14:24:40 +02:00
final ItemStack newType = ItemStack.loadItemStackFromNBT( data );
final long freq = data.getLong( "freq" );
2014-01-20 17:41:37 +01:00
if( newType != null )
2014-01-20 17:41:37 +01:00
{
if( newType.getItem() instanceof IPartItem )
2014-01-20 17:41:37 +01:00
{
2015-09-30 14:24:40 +02:00
final IPart testPart = ( (IPartItem) newType.getItem() ).createPartFromItemStack( newType );
if( testPart instanceof PartP2PTunnel )
2014-01-30 22:07:03 +01:00
{
this.getHost().removePart( this.getSide(), true );
final AEPartLocation dir = this.getHost().addPart( newType, this.getSide(), player, hand );
2015-09-30 14:24:40 +02:00
final IPart newBus = this.getHost().getPart( dir );
2014-01-30 22:07:03 +01:00
if( newBus instanceof PartP2PTunnel )
2014-01-30 22:07:03 +01:00
{
2015-09-30 14:24:40 +02:00
final PartP2PTunnel newTunnel = (PartP2PTunnel) newBus;
newTunnel.setOutput( true );
2014-01-30 22:07:03 +01:00
try
{
final P2PCache p2p = newTunnel.getProxy().getP2P();
2014-01-30 22:07:03 +01:00
p2p.updateFreq( newTunnel, freq );
}
2015-09-30 14:24:40 +02:00
catch( final GridAccessException e )
2014-01-30 22:07:03 +01:00
{
// :P
}
newTunnel.onTunnelNetworkChange();
2014-01-30 22:07:03 +01:00
}
mc.notifyUser( player, MemoryCardMessages.SETTINGS_LOADED );
return true;
}
2014-01-20 17:41:37 +01:00
}
}
2014-01-30 22:07:03 +01:00
mc.notifyUser( player, MemoryCardMessages.INVALID_MACHINE );
2014-01-20 17:41:37 +01:00
}
else if( tt != null ) // attunement
2014-01-21 16:59:55 +01:00
{
ItemStack newType = null;
final IParts parts = AEApi.instance().definitions().parts();
switch( tt )
{
case LIGHT:
2015-09-30 14:24:40 +02:00
for( final ItemStack stack : parts.p2PTunnelLight().maybeStack( 1 ).asSet() )
{
newType = stack;
}
break;
2014-01-21 16:59:55 +01:00
/*
* case RF_POWER:
* for( ItemStack stack : parts.p2PTunnelRF().maybeStack( 1 ).asSet() )
* {
* newType = stack;
* }
* break;
*/
case FLUID:
2015-09-30 14:24:40 +02:00
for( final ItemStack stack : parts.p2PTunnelLiquids().maybeStack( 1 ).asSet() )
{
newType = stack;
}
break;
/*
* case IC2_POWER:
* for( ItemStack stack : parts.p2PTunnelEU().maybeStack( 1 ).asSet() )
* {
* newType = stack;
* }
* break;
*/
case ITEM:
2015-09-30 14:24:40 +02:00
for( final ItemStack stack : parts.p2PTunnelItems().maybeStack( 1 ).asSet() )
{
newType = stack;
}
break;
2014-01-21 16:59:55 +01:00
case ME:
2015-09-30 14:24:40 +02:00
for( final ItemStack stack : parts.p2PTunnelME().maybeStack( 1 ).asSet() )
{
newType = stack;
}
break;
2014-01-21 16:59:55 +01:00
case REDSTONE:
2015-09-30 14:24:40 +02:00
for( final ItemStack stack : parts.p2PTunnelRedstone().maybeStack( 1 ).asSet() )
{
newType = stack;
}
break;
/*
* case COMPUTER_MESSAGE:
* for( ItemStack stack : parts.p2PTunnelOpenComputers().maybeStack( 1 ).asSet() )
* {
* newType = stack;
* }
* break;
* case PRESSURE:
* for( ItemStack stack : parts.p2PTunnelPneumaticCraft().maybeStack( 1 ).asSet() )
* {
* newType = stack;
* }
* break;
*/
default:
break;
2014-01-21 16:59:55 +01:00
}
if( newType != null && !Platform.isSameItem( newType, this.getItemStack() ) )
2014-01-21 16:59:55 +01:00
{
final boolean oldOutput = this.isOutput();
final long myFreq = this.getFrequency();
2014-01-21 16:59:55 +01:00
this.getHost().removePart( this.getSide(), false );
final AEPartLocation dir = this.getHost().addPart( newType, this.getSide(), player, hand );
2015-09-30 14:24:40 +02:00
final IPart newBus = this.getHost().getPart( dir );
2014-01-21 16:59:55 +01:00
if( newBus instanceof PartP2PTunnel )
2014-01-21 16:59:55 +01:00
{
2015-09-30 14:24:40 +02:00
final PartP2PTunnel newTunnel = (PartP2PTunnel) newBus;
newTunnel.setOutput( oldOutput );
newTunnel.onTunnelNetworkChange();
2014-01-21 16:59:55 +01:00
try
{
final P2PCache p2p = newTunnel.getProxy().getP2P();
2014-01-21 16:59:55 +01:00
p2p.updateFreq( newTunnel, myFreq );
}
2015-09-30 14:24:40 +02:00
catch( final GridAccessException e )
2014-01-21 16:59:55 +01:00
{
// :P
}
}
Platform.notifyBlocksOfNeighbors( this.getTile().getWorld(), this.getTile().getPos() );
2014-01-30 22:07:03 +01:00
return true;
2014-01-21 16:59:55 +01:00
}
}
2014-01-20 17:41:37 +01:00
return false;
}
@Override
public boolean onPartShiftActivate( final EntityPlayer player, final EnumHand hand, final Vec3d pos )
2014-01-20 17:41:37 +01:00
{
2015-09-30 14:24:40 +02:00
final ItemStack is = player.inventory.getCurrentItem();
if( is != null && is.getItem() instanceof IMemoryCard )
2014-01-20 17:41:37 +01:00
{
2015-09-30 14:24:40 +02:00
final IMemoryCard mc = (IMemoryCard) is.getItem();
final NBTTagCompound data = new NBTTagCompound();
2014-01-20 17:41:37 +01:00
long newFreq = this.getFrequency();
final boolean wasOutput = this.isOutput();
this.setOutput( false );
2014-01-20 17:41:37 +01:00
if( wasOutput || this.getFrequency() == 0 )
2015-04-29 02:30:53 +02:00
{
2014-01-20 17:41:37 +01:00
newFreq = System.currentTimeMillis();
2015-04-29 02:30:53 +02:00
}
try
{
this.getProxy().getP2P().updateFreq( this, newFreq );
}
2015-09-30 14:24:40 +02:00
catch( final GridAccessException e )
{
// :P
2014-01-20 17:41:37 +01:00
}
2014-12-29 15:13:47 +01:00
this.onTunnelConfigChange();
2014-01-20 17:41:37 +01:00
2015-09-30 14:24:40 +02:00
final ItemStack p2pItem = this.getItemStack( PartItemStack.Wrench );
final String type = p2pItem.getUnlocalizedName();
2014-01-20 17:41:37 +01:00
p2pItem.writeToNBT( data );
data.setLong( "freq", this.getFrequency() );
2014-01-20 17:41:37 +01:00
mc.setMemoryCardContents( is, type + ".name", data );
2014-01-30 22:07:03 +01:00
mc.notifyUser( player, MemoryCardMessages.SETTINGS_SAVED );
2014-01-20 17:41:37 +01:00
return true;
}
return false;
}
public void onTunnelConfigChange()
{
}
public void onTunnelNetworkChange()
2014-01-20 17:41:37 +01:00
{
}
2015-09-30 14:24:40 +02:00
protected void queueTunnelDrain( final PowerUnits unit, final double f )
2014-01-20 17:41:37 +01:00
{
2015-09-30 14:24:40 +02:00
final double ae_to_tax = unit.convertTo( PowerUnits.AE, f * AEConfig.TUNNEL_POWER_LOSS );
2014-01-20 17:41:37 +01:00
2014-05-02 07:40:46 +02:00
try
{
this.getProxy().getEnergy().extractAEPower( ae_to_tax, Actionable.MODULATE, PowerMultiplier.ONE );
2014-05-02 07:40:46 +02:00
}
2015-09-30 14:24:40 +02:00
catch( final GridAccessException e )
2014-05-02 07:40:46 +02:00
{
// :P
}
2014-01-20 17:41:37 +01:00
}
public long getFrequency()
{
return this.freq;
}
public void setFrequency( final long freq )
{
this.freq = freq;
}
public boolean isOutput()
{
return this.output;
}
void setOutput( final boolean output )
{
this.output = output;
}
2014-01-20 17:41:37 +01:00
}