Applied-Energistics-2-tiler.../src/main/java/appeng/parts/misc/PartToggleBus.java

262 lines
6.9 KiB
Java
Raw Normal View History

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>.
*/
package appeng.parts.misc;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
2015-06-16 02:44:59 +02:00
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
2015-06-16 02:44:59 +02:00
import net.minecraft.util.BlockPos;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2015-06-16 02:44:59 +02:00
import org.lwjgl.opengl.GL11;
import appeng.api.AEApi;
import appeng.api.exceptions.FailedConnection;
import appeng.api.networking.IGridConnection;
import appeng.api.networking.IGridNode;
2014-09-20 22:20:53 +02:00
import appeng.api.parts.IPartCollisionHelper;
import appeng.api.parts.IPartHost;
import appeng.api.parts.IPartRenderHelper;
import appeng.api.util.AECableType;
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;
import appeng.client.texture.CableBusTextures;
import appeng.helpers.Reflected;
import appeng.me.helpers.AENetworkProxy;
import appeng.parts.PartBasicState;
import appeng.util.Platform;
public class PartToggleBus extends PartBasicState
{
private static final int REDSTONE_FLAG = 4;
final AENetworkProxy outerProxy = new AENetworkProxy( this, "outer", null, true );
IGridConnection connection;
boolean hasRedstone = false;
@Reflected
2015-09-30 14:24:40 +02:00
public PartToggleBus( final ItemStack is )
{
super( is );
2014-12-29 15:13:47 +01:00
this.proxy.setIdlePowerUsage( 0.0 );
this.outerProxy.setIdlePowerUsage( 0.0 );
this.proxy.setFlags();
this.outerProxy.setFlags();
}
@Override
2015-09-30 14:24:40 +02:00
public void setColors( final ModelGenerator renderer, final boolean hasChan, final boolean hasPower )
{
2015-08-06 18:59:31 +02:00
this.hasRedstone = ( this.clientFlags & REDSTONE_FLAG ) == REDSTONE_FLAG;
2015-06-16 02:44:59 +02:00
super.setColors( renderer, hasChan && this.hasRedstone, hasPower && this.hasRedstone );
}
@Override
2015-09-30 14:24:40 +02:00
protected int populateFlags( final int cf )
{
2015-08-06 18:59:31 +02:00
return cf | ( this.getIntention() ? REDSTONE_FLAG : 0 );
}
protected boolean getIntention()
{
2014-12-29 15:13:47 +01:00
return this.getHost().hasRedstone( this.side );
}
@Override
2015-09-30 14:24:40 +02:00
public TextureAtlasSprite getBreakingTexture( final ModelGenerator renderer )
{
2015-09-30 14:26:54 +02:00
return renderer.getIcon( this.is ).getAtlas();
}
@Override
2015-09-30 14:24:40 +02:00
public AECableType getCableConnectionType( final AEPartLocation dir )
{
return AECableType.GLASS;
}
@Override
public void securityBreak()
{
if( this.is.stackSize > 0 )
{
2015-09-30 14:24:40 +02:00
final List<ItemStack> items = new ArrayList<ItemStack>();
items.add( this.is.copy() );
this.host.removePart( this.side, false );
2015-06-16 02:44:59 +02:00
Platform.spawnDrops( this.tile.getWorld(), this.tile.getPos(), items );
this.is.stackSize = 0;
}
}
@Override
2015-09-30 14:24:40 +02:00
public void getBoxes( final IPartCollisionHelper bch )
{
bch.addBox( 6, 6, 11, 10, 10, 16 );
}
@Override
@SideOnly( Side.CLIENT )
2015-09-30 14:24:40 +02:00
public void renderInventory( final IPartRenderHelper rh, final ModelGenerator renderer )
{
GL11.glTranslated( -0.2, -0.3, 0.0 );
2015-09-30 14:26:54 +02:00
rh.setTexture( renderer.getIcon( this.is ) );
rh.setBounds( 6, 6, 14 - 4, 10, 10, 16 - 4 );
rh.renderInventoryBox( renderer );
rh.setBounds( 6, 6, 11 - 4, 10, 10, 13 - 4 );
rh.renderInventoryBox( renderer );
rh.setBounds( 6, 6, 13 - 4, 10, 10, 14 - 4 );
rh.setTexture( CableBusTextures.PartMonitorSidesStatus.getIcon() );
rh.renderInventoryBox( renderer );
rh.setTexture( CableBusTextures.PartMonitorSidesStatusLights.getIcon() );
rh.setInvColor( 0x000000 );
rh.renderInventoryBox( renderer );
rh.setInvColor( 0xffffff );
rh.setTexture( null );
}
@Override
@SideOnly( Side.CLIENT )
2015-09-30 14:24:40 +02:00
public void renderStatic( final BlockPos pos, final IPartRenderHelper rh, final ModelGenerator renderer )
{
2015-09-30 14:26:54 +02:00
rh.setTexture( renderer.getIcon( this.is ) );
rh.setBounds( 6, 6, 14, 10, 10, 16 );
2015-06-16 02:44:59 +02:00
rh.renderBlock( pos, renderer );
rh.setBounds( 6, 6, 11, 10, 10, 13 );
2015-06-16 02:44:59 +02:00
rh.renderBlock( pos, renderer );
2015-09-30 14:26:54 +02:00
rh.setTexture( CableBusTextures.PartMonitorSidesStatus.getIcon(), CableBusTextures.PartMonitorSidesStatus.getIcon(), CableBusTextures.PartMonitorBack.getIcon(), renderer.getIcon( this.is ), CableBusTextures.PartMonitorSidesStatus.getIcon(), CableBusTextures.PartMonitorSidesStatus.getIcon() );
rh.setBounds( 6, 6, 13, 10, 10, 14 );
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 );
}
@Override
public void onNeighborChanged()
{
2015-09-30 14:24:40 +02:00
final boolean oldHasRedstone = this.hasRedstone;
this.hasRedstone = this.getHost().hasRedstone( this.side );
if( this.hasRedstone != oldHasRedstone )
{
this.updateInternalState();
this.getHost().markForUpdate();
}
}
@Override
2015-09-30 14:24:40 +02:00
public void readFromNBT( final NBTTagCompound extra )
{
super.readFromNBT( extra );
this.outerProxy.readFromNBT( extra );
}
@Override
2015-09-30 14:24:40 +02:00
public void writeToNBT( final NBTTagCompound extra )
{
super.writeToNBT( extra );
this.outerProxy.writeToNBT( extra );
}
@Override
public void removeFromWorld()
{
super.removeFromWorld();
this.outerProxy.invalidate();
}
@Override
public void addToWorld()
{
super.addToWorld();
this.outerProxy.onReady();
this.hasRedstone = this.getHost().hasRedstone( this.side );
this.updateInternalState();
}
@Override
2015-09-30 14:24:40 +02:00
public void setPartHostInfo( final AEPartLocation side, final IPartHost host, final TileEntity tile )
{
super.setPartHostInfo( side, host, tile );
2015-06-16 02:44:59 +02:00
this.outerProxy.setValidSides( EnumSet.of( side.getFacing() ) );
}
@Override
public IGridNode getExternalFacingNode()
{
return this.outerProxy.getNode();
}
@Override
public int cableConnectionRenderTo()
{
return 5;
}
@Override
2015-09-30 14:24:40 +02:00
public void onPlacement( final EntityPlayer player, final ItemStack held, final AEPartLocation side )
{
super.onPlacement( player, held, side );
this.outerProxy.setOwner( player );
}
private void updateInternalState()
{
2015-09-30 14:24:40 +02:00
final boolean intention = this.getIntention();
if( intention == ( this.connection == null ) )
{
if( this.proxy.getNode() != null && this.outerProxy.getNode() != null )
{
if( intention )
{
try
{
this.connection = AEApi.instance().createGridConnection( this.proxy.getNode(), this.outerProxy.getNode() );
}
2015-09-30 14:24:40 +02:00
catch( final FailedConnection e )
{
// :(
}
}
else
{
this.connection.destroy();
this.connection = null;
}
}
}
}
}