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

160 lines
4.6 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;
import java.io.IOException;
2014-12-29 21:59:05 +01:00
import io.netty.buffer.ByteBuf;
2014-09-24 02:26:27 +02:00
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraftforge.common.util.ForgeDirection;
2014-12-29 21:59:05 +01:00
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
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;
import appeng.client.texture.CableBusTextures;
import appeng.me.GridAccessException;
public class PartBasicState extends AEBasePart implements IPowerChannelState
{
protected final int POWERED_FLAG = 1;
protected final int CHANNEL_FLAG = 2;
protected int clientFlags = 0; // sent as byte.
@MENetworkEventSubscribe
public void chanRender(MENetworkChannelsChanged c)
{
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-12-29 15:13:47 +01:00
this.getHost().markForUpdate();
2014-09-24 02:26:27 +02:00
}
public void setColors(boolean hasChan, boolean hasPower)
{
if ( hasChan )
{
int l = 14;
Tessellator.instance.setBrightness( l << 20 | l << 4 );
2014-12-29 15:13:47 +01:00
Tessellator.instance.setColorOpaque_I( this.getColor().blackVariant );
2014-09-24 02:26:27 +02:00
}
else if ( hasPower )
{
int l = 9;
Tessellator.instance.setBrightness( l << 20 | l << 4 );
2014-12-29 15:13:47 +01:00
Tessellator.instance.setColorOpaque_I( this.getColor().whiteVariant );
2014-09-24 02:26:27 +02:00
}
else
{
Tessellator.instance.setBrightness( 0 );
Tessellator.instance.setColorOpaque_I( 0x000000 );
}
}
@SideOnly(Side.CLIENT)
public void renderLights(int x, int y, int z, IPartRenderHelper rh, RenderBlocks renderer)
{
rh.normalRendering();
2014-12-29 15:13:47 +01:00
this.setColors( (this.clientFlags & (this.POWERED_FLAG | this.CHANNEL_FLAG)) == (this.POWERED_FLAG | this.CHANNEL_FLAG), (this.clientFlags & this.POWERED_FLAG) == this.POWERED_FLAG );
2014-09-24 02:26:27 +02:00
rh.renderFace( x, y, z, CableBusTextures.PartMonitorSidesStatusLights.getIcon(), ForgeDirection.EAST, renderer );
rh.renderFace( x, y, z, CableBusTextures.PartMonitorSidesStatusLights.getIcon(), ForgeDirection.WEST, renderer );
rh.renderFace( x, y, z, CableBusTextures.PartMonitorSidesStatusLights.getIcon(), ForgeDirection.UP, renderer );
rh.renderFace( x, y, z, CableBusTextures.PartMonitorSidesStatusLights.getIcon(), ForgeDirection.DOWN, renderer );
}
@Override
public void writeToStream(ByteBuf data) throws IOException
{
super.writeToStream( data );
2014-12-29 15:13:47 +01:00
this.clientFlags = 0;
2014-09-24 02:26:27 +02:00
try
{
2014-12-29 15:13:47 +01:00
if ( this.proxy.getEnergy().isNetworkPowered() )
this.clientFlags |= this.POWERED_FLAG;
2014-09-24 02:26:27 +02:00
2014-12-29 15:13:47 +01:00
if ( this.proxy.getNode().meetsChannelRequirements() )
this.clientFlags |= this.CHANNEL_FLAG;
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)
{
// 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)
{
return cf;
}
@Override
public boolean readFromStream(ByteBuf data) throws IOException
{
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
}
public PartBasicState(Class c, ItemStack is) {
super( c, is );
2014-12-29 15:13:47 +01:00
this.proxy.setFlags( GridFlags.REQUIRE_CHANNEL );
2014-09-24 02:26:27 +02:00
}
@Override
public boolean isPowered()
{
2014-12-29 15:13:47 +01:00
return (this.clientFlags & this.POWERED_FLAG) == this.POWERED_FLAG;
2014-09-24 02:26:27 +02:00
}
@Override
public boolean isActive()
{
2014-12-29 15:13:47 +01:00
return (this.clientFlags & this.CHANNEL_FLAG) == this.CHANNEL_FLAG;
2014-09-24 02:26:27 +02:00
}
@Override
@SideOnly(Side.CLIENT)
public IIcon getBreakingTexture()
{
return CableBusTextures.PartTransitionPlaneBack.getIcon();
}
}