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

462 lines
12 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;
import net.minecraft.block.Block;
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.init.Blocks;
2014-01-20 17:41:37 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.BlockPos;
2014-01-20 17:41:37 +01:00
import net.minecraft.util.Vec3;
2015-06-16 02:44:59 +02:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
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.IPartRenderHelper;
import appeng.api.parts.PartItemStack;
2015-06-16 02:44:59 +02:00
import appeng.api.util.AEPartLocation;
2015-06-22 18:16:25 +02:00
import appeng.client.render.ModelGenerator;
2014-01-20 17:41:37 +01:00
import appeng.client.texture.CableBusTextures;
2015-06-16 02:44:59 +02:00
import appeng.client.texture.IAESprite;
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
2015-06-16 02:44:59 +02:00
import com.google.common.base.Optional;
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() );
2014-01-20 17:41:37 +01:00
public boolean output;
public long freq;
public PartP2PTunnel( ItemStack is )
{
super( is );
}
public TunnelCollection<T> getCollection( Collection<PartP2PTunnel> collection, Class<? extends PartP2PTunnel> c )
{
if( this.type.matches( c ) )
{
this.type.setSource( collection );
return this.type;
}
return null;
}
public T getInput()
{
if( this.freq == 0 )
2015-04-29 02:30:53 +02:00
{
return null;
2015-04-29 02:30:53 +02:00
}
PartP2PTunnel tunnel;
try
{
tunnel = this.proxy.getP2P().getInput( this.freq );
if( this.getClass().isInstance( tunnel ) )
2015-04-29 02:30:53 +02:00
{
return (T) tunnel;
2015-04-29 02:30:53 +02:00
}
}
catch( GridAccessException e )
{
// :P
}
return null;
}
public TunnelCollection<T> getOutputs() throws GridAccessException
{
if( this.proxy.isActive() )
2015-04-29 02:30:53 +02:00
{
return (TunnelCollection<T>) this.proxy.getP2P().getOutputs( this.freq, 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
public void getBoxes( 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 );
}
@Override
@SideOnly( Side.CLIENT )
2015-06-22 18:16:25 +02:00
public void renderInventory( IPartRenderHelper rh, ModelGenerator renderer )
{
2015-06-16 02:44:59 +02:00
rh.setTexture( this.getTypeTexture(renderer) );
rh.setBounds( 2, 2, 14, 14, 14, 16 );
rh.renderInventoryBox( renderer );
2015-06-16 02:44:59 +02:00
rh.setTexture( CableBusTextures.PartTunnelSides.getIcon(), CableBusTextures.PartTunnelSides.getIcon(), CableBusTextures.BlockP2PTunnel2.getIcon(), renderer.getIcon( is ), CableBusTextures.PartTunnelSides.getIcon(), CableBusTextures.PartTunnelSides.getIcon() );
rh.setBounds( 2, 2, 14, 14, 14, 16 );
rh.renderInventoryBox( renderer );
}
/**
2015-06-16 02:44:59 +02:00
* @param renderer
2015-05-08 22:38:04 +02:00
* @return If enabled it returns the icon of an AE quartz block, else vanilla quartz block icon
*/
2015-06-22 18:16:25 +02:00
protected IAESprite getTypeTexture(ModelGenerator renderer )
{
final Optional<Block> maybeBlock = AEApi.instance().definitions().blocks().quartz().maybeBlock();
if( maybeBlock.isPresent() )
{
2015-06-16 02:44:59 +02:00
return renderer.getIcon( new ItemStack(maybeBlock.get()) );
}
else
{
2015-06-16 02:44:59 +02:00
return renderer.getIcon( new ItemStack(Blocks.quartz_block) );
}
}
2014-01-20 17:41:37 +01:00
@Override
@SideOnly( Side.CLIENT )
2015-06-22 18:16:25 +02:00
public void renderStatic( BlockPos pos, IPartRenderHelper rh, ModelGenerator renderer )
2014-01-20 17:41:37 +01:00
{
2015-06-16 02:44:59 +02:00
rh.setTexture( this.getTypeTexture(renderer) );
rh.setBounds( 2, 2, 14, 14, 14, 16 );
2015-06-16 02:44:59 +02:00
rh.renderBlock( pos, renderer );
2015-06-16 02:44:59 +02:00
rh.setTexture( CableBusTextures.PartTunnelSides.getIcon(), CableBusTextures.PartTunnelSides.getIcon(), CableBusTextures.BlockP2PTunnel2.getIcon(), renderer.getIcon( is ), CableBusTextures.PartTunnelSides.getIcon(), CableBusTextures.PartTunnelSides.getIcon() );
rh.setBounds( 2, 2, 14, 14, 14, 16 );
2015-06-16 02:44:59 +02:00
rh.renderBlock( pos, renderer );
rh.setBounds( 3, 3, 13, 13, 13, 14 );
2015-06-16 02:44:59 +02:00
rh.renderBlock( pos, renderer );
rh.setTexture( CableBusTextures.BlockP2PTunnel3.getIcon() );
rh.setBounds( 6, 5, 12, 10, 11, 13 );
2015-06-16 02:44:59 +02:00
rh.renderBlock( pos, renderer );
rh.setBounds( 5, 6, 12, 11, 10, 13 );
2015-06-16 02:44:59 +02:00
rh.renderBlock( pos, renderer );
2015-06-16 02:44:59 +02:00
this.renderLights( pos, rh, renderer );
2014-01-20 17:41:37 +01:00
}
@Override
public ItemStack getItemStack( 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
public void readFromNBT( NBTTagCompound data )
2014-01-20 17:41:37 +01:00
{
2015-01-19 22:51:41 +01:00
super.readFromNBT( data );
2014-12-29 15:13:47 +01:00
this.output = data.getBoolean( "output" );
this.freq = data.getLong( "freq" );
2014-01-20 17:41:37 +01:00
}
@Override
public void writeToNBT( NBTTagCompound data )
{
super.writeToNBT( data );
data.setBoolean( "output", this.output );
data.setLong( "freq", this.freq );
}
@Override
public int cableConnectionRenderTo()
{
return 1;
}
@Override
public boolean useStandardMemoryCard()
{
return false;
}
@Override
public boolean onPartActivate( EntityPlayer player, Vec3 pos )
2014-01-20 17:41:37 +01:00
{
ItemStack is = player.inventory.getCurrentItem();
// UniqueIdentifier id = GameRegistry.findUniqueIdentifierFor( is.getItem() );
// AELog.info( "ID:" + id.toString() + " : " + is.getItemDamage() );
2014-01-21 16:59:55 +01:00
TunnelType tt = AEApi.instance().registries().p2pTunnel().getTunnelTypeByItem( is );
if( is != null && is.getItem() instanceof IMemoryCard )
2014-01-20 17:41:37 +01:00
{
IMemoryCard mc = (IMemoryCard) is.getItem();
NBTTagCompound data = mc.getData( is );
ItemStack newType = ItemStack.loadItemStackFromNBT( data );
long freq = data.getLong( "freq" );
if( newType != null )
2014-01-20 17:41:37 +01:00
{
if( newType.getItem() instanceof IPartItem )
2014-01-20 17:41:37 +01:00
{
IPart testPart = ( (IPartItem) newType.getItem() ).createPartFromItemStack( newType );
if( testPart instanceof PartP2PTunnel )
2014-01-30 22:07:03 +01:00
{
2014-12-29 15:13:47 +01:00
this.getHost().removePart( this.side, true );
2015-06-16 02:44:59 +02:00
AEPartLocation dir = this.getHost().addPart( newType, this.side, player );
2014-12-29 15:13:47 +01:00
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
{
PartP2PTunnel newTunnel = (PartP2PTunnel) newBus;
newTunnel.output = true;
try
{
P2PCache p2p = newTunnel.proxy.getP2P();
p2p.updateFreq( newTunnel, freq );
}
catch( 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:
for( ItemStack stack : parts.p2PTunnelLight().maybeStack( 1 ).asSet() )
{
newType = stack;
}
break;
2014-01-21 16:59:55 +01:00
2015-06-16 02:44:59 +02:00
/*
case RF_POWER:
for( ItemStack stack : parts.p2PTunnelRF().maybeStack( 1 ).asSet() )
{
newType = stack;
}
break;
2015-06-16 02:44:59 +02:00
*/
case FLUID:
for( ItemStack stack : parts.p2PTunnelLiquids().maybeStack( 1 ).asSet() )
{
newType = stack;
}
break;
2015-06-16 02:44:59 +02:00
/*
case IC2_POWER:
for( ItemStack stack : parts.p2PTunnelEU().maybeStack( 1 ).asSet() )
{
newType = stack;
}
break;
2015-06-16 02:44:59 +02:00
*/
case ITEM:
for( ItemStack stack : parts.p2PTunnelItems().maybeStack( 1 ).asSet() )
{
newType = stack;
}
break;
2014-01-21 16:59:55 +01:00
case ME:
for( ItemStack stack : parts.p2PTunnelME().maybeStack( 1 ).asSet() )
{
newType = stack;
}
break;
2014-01-21 16:59:55 +01:00
case REDSTONE:
for( ItemStack stack : parts.p2PTunnelRedstone().maybeStack( 1 ).asSet() )
{
newType = stack;
}
break;
2015-06-16 02:44:59 +02:00
/*
case COMPUTER_MESSAGE:
for( ItemStack stack : parts.p2PTunnelOpenComputers().maybeStack( 1 ).asSet() )
{
newType = stack;
}
break;
2015-06-16 02:44:59 +02:00
*/
default:
break;
2014-01-21 16:59:55 +01:00
}
if( newType != null && !Platform.isSameItem( newType, this.is ) )
2014-01-21 16:59:55 +01:00
{
2014-12-29 15:13:47 +01:00
boolean oldOutput = this.output;
long myFreq = this.freq;
2014-01-21 16:59:55 +01:00
2014-12-29 15:13:47 +01:00
this.getHost().removePart( this.side, false );
2015-06-16 02:44:59 +02:00
AEPartLocation dir = this.getHost().addPart( newType, this.side, player );
2014-12-29 15:13:47 +01:00
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
{
PartP2PTunnel newTunnel = (PartP2PTunnel) newBus;
newTunnel.output = oldOutput;
newTunnel.onTunnelNetworkChange();
2014-01-21 16:59:55 +01:00
try
{
P2PCache p2p = newTunnel.proxy.getP2P();
p2p.updateFreq( newTunnel, myFreq );
}
catch( GridAccessException e )
2014-01-21 16:59:55 +01:00
{
// :P
}
}
2015-06-16 02:44:59 +02:00
Platform.notifyBlocksOfNeighbors( this.tile.getWorld(), this.tile.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( EntityPlayer player, Vec3 pos )
2014-01-20 17:41:37 +01:00
{
ItemStack is = player.inventory.getCurrentItem();
if( is != null && is.getItem() instanceof IMemoryCard )
2014-01-20 17:41:37 +01:00
{
IMemoryCard mc = (IMemoryCard) is.getItem();
NBTTagCompound data = new NBTTagCompound();
2014-12-29 15:13:47 +01:00
long newFreq = this.freq;
boolean wasOutput = this.output;
this.output = false;
2014-01-20 17:41:37 +01:00
if( wasOutput || this.freq == 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
{
2014-12-29 15:13:47 +01:00
this.proxy.getP2P().updateFreq( this, newFreq );
}
catch( 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
2014-12-29 15:13:47 +01:00
ItemStack p2pItem = this.getItemStack( PartItemStack.Wrench );
2014-01-20 17:41:37 +01:00
String type = p2pItem.getUnlocalizedName();
p2pItem.writeToNBT( data );
2014-12-29 15:13:47 +01:00
data.setLong( "freq", this.freq );
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
{
}
@Override
@SideOnly( Side.CLIENT )
2015-06-22 18:16:25 +02:00
public TextureAtlasSprite getBreakingTexture( ModelGenerator renderer )
{
2015-06-16 02:44:59 +02:00
return CableBusTextures.BlockP2PTunnel2.getIcon().getAtlas();
}
2015-05-18 00:08:05 +02:00
protected void queueTunnelDrain( PowerUnits unit, double f )
2014-01-20 17:41:37 +01:00
{
2015-01-01 22:13:10 +01:00
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
{
2014-12-29 15:13:47 +01:00
this.proxy.getEnergy().extractAEPower( ae_to_tax, Actionable.MODULATE, PowerMultiplier.ONE );
2014-05-02 07:40:46 +02:00
}
catch( GridAccessException e )
2014-05-02 07:40:46 +02:00
{
// :P
}
2014-01-20 17:41:37 +01:00
}
}