Applied-Energistics-2-tiler.../parts/p2p/PartP2PTunnel.java

390 lines
9.6 KiB
Java
Raw Normal View History

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.client.renderer.RenderBlocks;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2014-02-09 02:34:52 +01:00
import net.minecraft.util.IIcon;
2014-01-20 17:41:37 +01:00
import net.minecraft.util.Vec3;
2014-02-09 02:34:52 +01:00
import net.minecraftforge.common.util.ForgeDirection;
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;
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;
import appeng.client.texture.CableBusTextures;
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;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
2014-01-20 17:41:37 +01:00
public class PartP2PTunnel<T extends PartP2PTunnel> extends PartBasicState
{
public boolean output;
public long freq;
TunnelCollection type = new TunnelCollection<T>( null, getClass() );
public PartP2PTunnel(ItemStack is) {
super( PartP2PTunnel.class, is );
if ( getClass() == PartP2PTunnel.class )
throw new RuntimeException( "Don't construct the root tunnel!" );
}
@Override
public boolean useStandardMemoryCard()
{
return false;
}
2014-01-20 17:41:37 +01:00
@Override
public void writeToNBT(NBTTagCompound data)
{
data.setBoolean( "output", output );
data.setLong( "freq", freq );
}
@Override
public void readFromNBT(NBTTagCompound data)
{
output = data.getBoolean( "output" );
freq = data.getLong( "freq" );
}
@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 );
2014-01-20 17:41:37 +01:00
if ( is != null && is.getItem() instanceof IMemoryCard )
{
IMemoryCard mc = (IMemoryCard) is.getItem();
NBTTagCompound data = mc.getData( is );
ItemStack newType = ItemStack.loadItemStackFromNBT( data );
long freq = data.getLong( "freq" );
2014-01-30 22:07:03 +01:00
if ( newType != null )
2014-01-20 17:41:37 +01:00
{
2014-01-30 22:07:03 +01:00
if ( newType.getItem() instanceof IPartItem )
2014-01-20 17:41:37 +01:00
{
2014-01-30 22:07:03 +01:00
IPart testPart = ((IPartItem) newType.getItem()).createPartFromItemStack( newType );
if ( testPart instanceof PartP2PTunnel )
{
getHost().removePart( side, true );
ForgeDirection dir = getHost().addPart( newType, side, player );
IPart newBus = getHost().getPart( dir );
if ( newBus instanceof PartP2PTunnel )
{
PartP2PTunnel newTunnel = (PartP2PTunnel) newBus;
newTunnel.output = true;
try
{
P2PCache p2p = newTunnel.proxy.getP2P();
p2p.updateFreq( newTunnel, freq );
}
catch (GridAccessException e)
{
// :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
}
2014-01-21 16:59:55 +01:00
else if ( tt != null ) // attune-ment
{
ItemStack newType = null;
switch (tt)
{
2014-07-15 09:25:03 +02:00
case LIGHT:
newType = AEApi.instance().parts().partP2PTunnelLight.stack( 1 );
break;
2014-01-21 16:59:55 +01:00
case RF_POWER:
newType = AEApi.instance().parts().partP2PTunnelRF.stack( 1 );
break;
2014-01-21 16:59:55 +01:00
case BC_POWER:
newType = AEApi.instance().parts().partP2PTunnelMJ.stack( 1 );
break;
case FLUID:
newType = AEApi.instance().parts().partP2PTunnelLiquids.stack( 1 );
break;
case IC2_POWER:
newType = AEApi.instance().parts().partP2PTunnelEU.stack( 1 );
break;
case ITEM:
newType = AEApi.instance().parts().partP2PTunnelItems.stack( 1 );
break;
case ME:
newType = AEApi.instance().parts().partP2PTunnelME.stack( 1 );
break;
case REDSTONE:
newType = AEApi.instance().parts().partP2PTunnelRedstone.stack( 1 );
break;
2014-09-17 17:37:40 +02:00
default:
break;
2014-01-21 16:59:55 +01:00
}
if ( newType != null && !Platform.isSameItem( newType, this.is ) )
{
boolean oldOutput = output;
long myFreq = freq;
getHost().removePart( side, false );
ForgeDirection dir = getHost().addPart( newType, side, player );
2014-01-21 16:59:55 +01:00
IPart newBus = getHost().getPart( dir );
if ( newBus instanceof PartP2PTunnel )
{
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)
{
// :P
}
}
Platform.notifyBlocksOfNeighbors( tile.getWorldObj(), tile.xCoord, tile.yCoord, tile.zCoord );
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;
}
public TunnelType getTunnelType()
{
return null;
}
2014-01-20 17:41:37 +01:00
@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 )
{
IMemoryCard mc = (IMemoryCard) is.getItem();
NBTTagCompound data = new NBTTagCompound();
long newFreq = freq;
boolean wasOutput = output;
2014-01-20 17:41:37 +01:00
output = false;
if ( wasOutput || freq == 0 )
2014-01-20 17:41:37 +01:00
newFreq = System.currentTimeMillis();
try
{
proxy.getP2P().updateFreq( this, newFreq );
}
catch (GridAccessException e)
{
// :P
2014-01-20 17:41:37 +01:00
}
onTunnelConfigChange();
2014-01-20 17:41:37 +01:00
ItemStack p2pItem = getItemStack( PartItemStack.Wrench );
String type = p2pItem.getUnlocalizedName();
p2pItem.writeToNBT( data );
data.setLong( "freq", freq );
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()
{
}
2014-01-20 17:41:37 +01:00
public ItemStack getItemStack(PartItemStack type)
{
if ( type == PartItemStack.World || type == PartItemStack.Network || type == PartItemStack.Wrench || type == PartItemStack.Pick )
2014-01-20 17:41:37 +01:00
return super.getItemStack( type );
return AEApi.instance().parts().partP2PTunnelME.stack( 1 );
}
public TunnelCollection<T> getCollection(Collection<PartP2PTunnel> collection, Class<? extends PartP2PTunnel> c)
2014-01-20 17:41:37 +01:00
{
if ( type.matches( c ) )
{
type.setSource( collection );
return type;
}
return null;
2014-01-20 17:41:37 +01:00
}
public T getInput()
{
2014-01-31 18:39:09 +01:00
if ( freq == 0 )
return null;
2014-01-20 17:41:37 +01:00
PartP2PTunnel tunn;
try
{
tunn = proxy.getP2P().getInput( freq );
if ( getClass().isInstance( tunn ) )
return (T) tunn;
}
catch (GridAccessException e)
{
// :P
}
return null;
}
public TunnelCollection<T> getOutputs() throws GridAccessException
{
2014-05-19 04:36:21 +02:00
if ( proxy.isActive() )
return (TunnelCollection<T>) proxy.getP2P().getOutputs( freq, getClass() );
return new TunnelCollection( new ArrayList(), getClass() );
2014-01-20 17:41:37 +01:00
}
public void onTunnelNetworkChange()
2014-01-20 17:41:37 +01:00
{
}
@Override
@SideOnly(Side.CLIENT)
2014-01-20 17:41:37 +01:00
public void renderInventory(IPartRenderHelper rh, RenderBlocks renderer)
{
rh.setTexture( getTypeTexture() );
rh.setBounds( 2, 2, 14, 14, 14, 16 );
rh.renderInventoryBox( renderer );
2014-04-04 06:31:10 +02:00
rh.setTexture( CableBusTextures.PartTunnelSides.getIcon(), CableBusTextures.PartTunnelSides.getIcon(), CableBusTextures.BlockP2PTunnel2.getIcon(),
is.getIconIndex(), CableBusTextures.PartTunnelSides.getIcon(), CableBusTextures.PartTunnelSides.getIcon() );
2014-01-20 17:41:37 +01:00
rh.setBounds( 2, 2, 14, 14, 14, 16 );
rh.renderInventoryBox( renderer );
}
2014-02-09 02:34:52 +01:00
protected IIcon getTypeTexture()
2014-01-20 17:41:37 +01:00
{
return AEApi.instance().blocks().blockQuartz.block().getIcon( 0, 0 );
}
@Override
@SideOnly(Side.CLIENT)
2014-01-20 17:41:37 +01:00
public void renderStatic(int x, int y, int z, IPartRenderHelper rh, RenderBlocks renderer)
{
2014-09-20 22:29:30 +02:00
renderCache = rh.useSimplifiedRendering( x, y, z, this, renderCache );
2014-01-20 17:41:37 +01:00
rh.setTexture( getTypeTexture() );
rh.setBounds( 2, 2, 14, 14, 14, 16 );
rh.renderBlock( x, y, z, renderer );
2014-04-04 06:31:10 +02:00
rh.setTexture( CableBusTextures.PartTunnelSides.getIcon(), CableBusTextures.PartTunnelSides.getIcon(), CableBusTextures.BlockP2PTunnel2.getIcon(),
is.getIconIndex(), CableBusTextures.PartTunnelSides.getIcon(), CableBusTextures.PartTunnelSides.getIcon() );
2014-01-20 17:41:37 +01:00
rh.setBounds( 2, 2, 14, 14, 14, 16 );
rh.renderBlock( x, y, z, renderer );
rh.setBounds( 3, 3, 13, 13, 13, 14 );
rh.renderBlock( x, y, z, renderer );
rh.setTexture( CableBusTextures.BlockP2PTunnel3.getIcon() );
rh.setBounds( 6, 5, 12, 10, 11, 13 );
rh.renderBlock( x, y, z, renderer );
rh.setBounds( 5, 6, 12, 11, 10, 13 );
rh.renderBlock( x, y, z, renderer );
renderLights( x, y, z, rh, renderer );
}
@Override
@SideOnly(Side.CLIENT)
public IIcon getBreakingTexture()
{
return CableBusTextures.BlockP2PTunnel2.getIcon();
}
2014-05-02 07:40:46 +02:00
protected void QueueTunnelDrain(PowerUnits unit, double f)
2014-01-20 17:41:37 +01:00
{
2014-05-02 07:40:46 +02:00
double ae_to_tax = unit.convertTo( PowerUnits.AE, f * AEConfig.TunnelPowerLoss );
2014-01-20 17:41:37 +01:00
2014-05-02 07:40:46 +02:00
try
{
proxy.getEnergy().extractAEPower( ae_to_tax, Actionable.MODULATE, PowerMultiplier.ONE );
}
catch (GridAccessException e)
{
// :P
}
2014-01-20 17:41:37 +01:00
}
@Override
public void setColors(boolean hasChan, boolean hasPower)
{
super.setColors( hasChan, hasPower );
}
@Override
2014-09-20 22:20:53 +02:00
public void getBoxes(IPartCollisionHelper bch)
2014-01-20 17:41:37 +01:00
{
bch.addBox( 5, 5, 12, 11, 11, 13 );
bch.addBox( 3, 3, 13, 13, 13, 14 );
2014-01-20 17:41:37 +01:00
bch.addBox( 2, 2, 14, 14, 14, 16 );
}
@Override
public int cableConnectionRenderTo()
{
return 1;
}
}