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

165 lines
4.5 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>.
*/
2014-09-24 02:26:27 +02:00
package appeng.parts;
2015-06-16 02:44:59 +02:00
import java.io.IOException;
2014-12-29 21:59:05 +01:00
2015-08-06 18:59:31 +02:00
import io.netty.buffer.ByteBuf;
2015-06-16 02:44:59 +02:00
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2015-08-06 18:59:31 +02:00
2014-09-24 02:26:27 +02:00
import appeng.api.implementations.IPowerChannelState;
import appeng.api.networking.GridFlags;
import appeng.api.networking.events.MENetworkChannelsChanged;
import appeng.api.networking.events.MENetworkEventSubscribe;
import appeng.api.networking.events.MENetworkPowerStatusChange;
import appeng.api.parts.IPartRenderHelper;
2015-06-22 18:16:25 +02:00
import appeng.client.render.ModelGenerator;
2014-09-24 02:26:27 +02:00
import appeng.client.texture.CableBusTextures;
import appeng.me.GridAccessException;
public abstract class PartBasicState extends AEBasePart implements IPowerChannelState
2014-09-24 02:26:27 +02:00
{
protected static final int POWERED_FLAG = 1;
protected static final int CHANNEL_FLAG = 2;
2014-09-24 02:26:27 +02:00
protected int clientFlags = 0; // sent as byte.
public PartBasicState( ItemStack is )
{
super( is );
this.proxy.setFlags( GridFlags.REQUIRE_CHANNEL );
}
2014-09-24 02:26:27 +02:00
@MENetworkEventSubscribe
public void chanRender( MENetworkChannelsChanged c )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.getHost().markForUpdate();
2014-09-24 02:26:27 +02:00
}
@MENetworkEventSubscribe
public void powerRender( MENetworkPowerStatusChange c )
2014-09-24 02:26:27 +02:00
{
2014-12-29 15:13:47 +01:00
this.getHost().markForUpdate();
2014-09-24 02:26:27 +02:00
}
@SideOnly( Side.CLIENT )
2015-06-22 18:16:25 +02:00
public void renderLights( BlockPos pos, IPartRenderHelper rh, ModelGenerator renderer )
{
2015-08-06 18:59:31 +02:00
this.setColors( renderer, ( this.clientFlags & ( POWERED_FLAG | CHANNEL_FLAG ) ) == ( POWERED_FLAG | CHANNEL_FLAG ), ( this.clientFlags & POWERED_FLAG ) == POWERED_FLAG );
2015-06-16 02:44:59 +02:00
rh.renderFace( pos, CableBusTextures.PartMonitorSidesStatusLights.getIcon(), EnumFacing.EAST, renderer );
rh.renderFace( pos, CableBusTextures.PartMonitorSidesStatusLights.getIcon(), EnumFacing.WEST, renderer );
rh.renderFace( pos, CableBusTextures.PartMonitorSidesStatusLights.getIcon(), EnumFacing.UP, renderer );
rh.renderFace( pos, CableBusTextures.PartMonitorSidesStatusLights.getIcon(), EnumFacing.DOWN, renderer );
}
2015-06-22 18:16:25 +02:00
public void setColors( ModelGenerator renderer, boolean hasChan, boolean hasPower )
2014-09-24 02:26:27 +02:00
{
if( hasChan )
2014-09-24 02:26:27 +02:00
{
int l = 14;
2015-06-16 02:44:59 +02:00
renderer.setBrightness( l << 20 | l << 4 );
renderer.setColorOpaque_I( this.getColor().blackVariant );
2014-09-24 02:26:27 +02:00
}
else if( hasPower )
2014-09-24 02:26:27 +02:00
{
int l = 9;
2015-06-16 02:44:59 +02:00
renderer.setBrightness( l << 20 | l << 4 );
renderer.setColorOpaque_I( this.getColor().whiteVariant );
2014-09-24 02:26:27 +02:00
}
else
{
2015-06-16 02:44:59 +02:00
renderer.setBrightness( 0 );
renderer.setColorOpaque_I( 0x000000 );
2014-09-24 02:26:27 +02:00
}
}
@Override
public void writeToStream( ByteBuf data ) throws IOException
2014-09-24 02:26:27 +02:00
{
super.writeToStream( data );
2014-12-29 15:13:47 +01:00
this.clientFlags = 0;
2014-09-24 02:26:27 +02:00
try
{
if( this.proxy.getEnergy().isNetworkPowered() )
2015-04-29 02:30:53 +02:00
{
2015-08-06 18:59:31 +02:00
this.clientFlags |= POWERED_FLAG;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
if( this.proxy.getNode().meetsChannelRequirements() )
2015-04-29 02:30:53 +02:00
{
2015-08-06 18:59:31 +02:00
this.clientFlags |= CHANNEL_FLAG;
2015-04-29 02:30:53 +02:00
}
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
this.clientFlags = this.populateFlags( this.clientFlags );
2014-09-24 02:26:27 +02:00
}
catch( GridAccessException e )
2014-09-24 02:26:27 +02:00
{
// meh
}
2014-12-29 15:13:47 +01:00
data.writeByte( (byte) this.clientFlags );
2014-09-24 02:26:27 +02:00
}
protected int populateFlags( int cf )
2014-09-24 02:26:27 +02:00
{
return cf;
}
@Override
public boolean readFromStream( ByteBuf data ) throws IOException
2014-09-24 02:26:27 +02:00
{
boolean eh = super.readFromStream( data );
2014-12-29 15:13:47 +01:00
int old = this.clientFlags;
this.clientFlags = data.readByte();
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
return eh || old != this.clientFlags;
2014-09-24 02:26:27 +02:00
}
@Override
@SideOnly( Side.CLIENT )
2015-06-22 18:16:25 +02:00
public TextureAtlasSprite getBreakingTexture( ModelGenerator renderer )
2014-09-24 02:26:27 +02:00
{
2015-06-16 02:44:59 +02:00
return CableBusTextures.PartTransitionPlaneBack.getIcon().getAtlas();
2014-09-24 02:26:27 +02:00
}
@Override
public boolean isPowered()
2014-09-24 02:26:27 +02:00
{
2015-08-06 18:59:31 +02:00
return ( this.clientFlags & POWERED_FLAG ) == POWERED_FLAG;
2014-09-24 02:26:27 +02:00
}
@Override
public boolean isActive()
2014-09-24 02:26:27 +02:00
{
2015-08-06 18:59:31 +02:00
return ( this.clientFlags & CHANNEL_FLAG ) == CHANNEL_FLAG;
2014-09-24 02:26:27 +02:00
}
}