2013-12-27 23:59:59 +01:00
|
|
|
package appeng.parts.reporting;
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import net.minecraft.client.renderer.RenderBlocks;
|
|
|
|
import net.minecraft.client.renderer.Tessellator;
|
2014-07-19 23:01:24 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2013-12-27 23:59:59 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2014-07-19 23:01:24 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2014-07-12 03:28:46 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2014-07-19 23:01:24 +02:00
|
|
|
import net.minecraft.util.MathHelper;
|
|
|
|
import net.minecraft.util.Vec3;
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
2014-02-22 23:14:43 +01:00
|
|
|
import appeng.api.implementations.IPowerChannelState;
|
2014-01-23 20:02:48 +01:00
|
|
|
import appeng.api.implementations.parts.IPartMonitor;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.networking.GridFlags;
|
|
|
|
import appeng.api.networking.events.MENetworkBootingStatusChange;
|
|
|
|
import appeng.api.networking.events.MENetworkEventSubscribe;
|
|
|
|
import appeng.api.networking.events.MENetworkPowerStatusChange;
|
|
|
|
import appeng.api.parts.IPartCollsionHelper;
|
|
|
|
import appeng.api.parts.IPartRenderHelper;
|
|
|
|
import appeng.client.texture.CableBusTextures;
|
|
|
|
import appeng.me.GridAccessException;
|
|
|
|
import appeng.parts.AEBasePart;
|
|
|
|
import appeng.util.Platform;
|
2014-02-07 06:51:19 +01:00
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-02-22 23:14:43 +01:00
|
|
|
public class PartMonitor extends AEBasePart implements IPartMonitor, IPowerChannelState
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
2014-02-22 23:14:43 +01:00
|
|
|
// CableBusTextures frontSolid = CableBusTextures.PartMonitor_Solid;
|
2013-12-27 23:59:59 +01:00
|
|
|
CableBusTextures frontDark = CableBusTextures.PartMonitor_Colored;
|
|
|
|
CableBusTextures frontBright = CableBusTextures.PartMonitor_Bright;
|
|
|
|
CableBusTextures frontColored = CableBusTextures.PartMonitor_Colored;
|
|
|
|
|
|
|
|
boolean notLightSource = !this.getClass().equals( PartMonitor.class );
|
|
|
|
|
2014-07-19 23:01:24 +02:00
|
|
|
final int POWERED_FLAG = 4;
|
|
|
|
final int BOOTING_FLAG = 8;
|
|
|
|
final int CHANNEL_FLAG = 16;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-07-19 23:01:24 +02:00
|
|
|
byte spin = 0; // 0-3
|
2013-12-27 23:59:59 +01:00
|
|
|
int clientFlags = 0; // sent as byte.
|
2014-08-23 08:25:06 +02:00
|
|
|
float opacity = -1;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-07-19 23:01:24 +02:00
|
|
|
@Override
|
|
|
|
public void onPlacement(EntityPlayer player, ItemStack held, ForgeDirection side)
|
|
|
|
{
|
|
|
|
super.onPlacement( player, held, side );
|
|
|
|
|
|
|
|
byte rotation = (byte) (MathHelper.floor_double( (double) ((player.rotationYaw * 4F) / 360F) + 2.5D ) & 3);
|
|
|
|
if ( side == ForgeDirection.UP )
|
|
|
|
spin = rotation;
|
|
|
|
else if ( side == ForgeDirection.DOWN )
|
|
|
|
spin = rotation;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeToNBT(NBTTagCompound data)
|
|
|
|
{
|
|
|
|
super.writeToNBT( data );
|
2014-08-23 08:25:06 +02:00
|
|
|
data.setFloat( "opacity", opacity );
|
2014-07-19 23:01:24 +02:00
|
|
|
data.setByte( "spin", (byte) spin );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readFromNBT(NBTTagCompound data)
|
|
|
|
{
|
|
|
|
super.readFromNBT( data );
|
2014-08-23 08:25:06 +02:00
|
|
|
if ( data.hasKey( "opacity" ) )
|
|
|
|
opacity = data.getFloat( "opacity" );
|
2014-07-19 23:01:24 +02:00
|
|
|
spin = data.getByte( "spin" );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPartActivate(EntityPlayer player, Vec3 pos)
|
|
|
|
{
|
|
|
|
TileEntity te = getTile();
|
|
|
|
|
|
|
|
if ( !player.isSneaking() && Platform.isWrench( player, player.inventory.getCurrentItem(), te.xCoord, te.yCoord, te.zCoord ) )
|
|
|
|
{
|
|
|
|
if ( Platform.isServer() )
|
|
|
|
{
|
|
|
|
if ( spin > 3 )
|
|
|
|
spin = 0;
|
|
|
|
|
|
|
|
switch (spin)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
spin = 1;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
spin = 3;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
spin = 0;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
spin = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.host.markForUpdate();
|
|
|
|
this.saveChanges();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return super.onPartActivate( player, pos );
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@MENetworkEventSubscribe
|
|
|
|
public void bootingRender(MENetworkBootingStatusChange c)
|
|
|
|
{
|
|
|
|
if ( notLightSource )
|
|
|
|
getHost().markForUpdate();
|
|
|
|
}
|
|
|
|
|
2014-07-12 03:28:46 +02:00
|
|
|
@Override
|
|
|
|
public void onNeighborChanged()
|
|
|
|
{
|
2014-08-23 08:25:06 +02:00
|
|
|
opacity = -1;
|
2014-07-12 03:28:46 +02:00
|
|
|
getHost().markForUpdate();
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@MENetworkEventSubscribe
|
|
|
|
public void powerRender(MENetworkPowerStatusChange c)
|
|
|
|
{
|
|
|
|
getHost().markForUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public void writeToStream(ByteBuf data) throws IOException
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
super.writeToStream( data );
|
2014-07-19 23:01:24 +02:00
|
|
|
clientFlags = spin & 3;
|
|
|
|
;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if ( proxy.getEnergy().isNetworkPowered() )
|
|
|
|
clientFlags = clientFlags | POWERED_FLAG;
|
|
|
|
|
|
|
|
if ( proxy.getPath().isNetworkBooting() )
|
|
|
|
clientFlags = clientFlags | BOOTING_FLAG;
|
|
|
|
|
|
|
|
if ( proxy.getNode().meetsChannelRequirements() )
|
|
|
|
clientFlags = clientFlags | CHANNEL_FLAG;
|
|
|
|
}
|
|
|
|
catch (GridAccessException e)
|
|
|
|
{
|
|
|
|
// um.. nothing.
|
|
|
|
}
|
|
|
|
|
|
|
|
data.writeByte( (byte) clientFlags );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-09 02:34:52 +01:00
|
|
|
public boolean readFromStream(ByteBuf data) throws IOException
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
super.readFromStream( data );
|
|
|
|
int oldFlags = clientFlags;
|
|
|
|
clientFlags = data.readByte();
|
2014-07-19 23:01:24 +02:00
|
|
|
spin = (byte) (clientFlags & 3);
|
2013-12-27 23:59:59 +01:00
|
|
|
if ( clientFlags == oldFlags )
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getLightLevel()
|
|
|
|
{
|
2014-07-12 03:28:46 +02:00
|
|
|
return blockLight( isPowered() ? (notLightSource ? 9 : 15) : 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
private int blockLight(int emit)
|
|
|
|
{
|
2014-08-23 08:25:06 +02:00
|
|
|
if ( opacity < 0 )
|
|
|
|
{
|
|
|
|
TileEntity te = this.getTile();
|
|
|
|
opacity = 255 - te.getWorldObj().getBlockLightOpacity( te.xCoord + side.offsetX, te.yCoord + side.offsetY, te.zCoord + side.offsetZ );
|
|
|
|
}
|
|
|
|
|
2014-07-12 03:28:46 +02:00
|
|
|
return (int) (emit * (opacity / 255.0f));
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isPowered()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if ( Platform.isServer() )
|
|
|
|
return proxy.getEnergy().isNetworkPowered();
|
|
|
|
else
|
|
|
|
return ((clientFlags & POWERED_FLAG) == POWERED_FLAG);
|
|
|
|
}
|
|
|
|
catch (GridAccessException e)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public PartMonitor(ItemStack is) {
|
2014-04-18 02:44:51 +02:00
|
|
|
this( PartMonitor.class, is, false );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-04-18 02:44:51 +02:00
|
|
|
protected PartMonitor(Class c, ItemStack is, boolean requireChannel) {
|
2013-12-27 23:59:59 +01:00
|
|
|
super( c, is );
|
2014-04-18 02:44:51 +02:00
|
|
|
|
2014-04-09 21:22:36 +02:00
|
|
|
if ( requireChannel )
|
2014-01-31 22:12:15 +01:00
|
|
|
proxy.setFlags( GridFlags.REQUIRE_CHANNEL );
|
2013-12-27 23:59:59 +01:00
|
|
|
else
|
|
|
|
proxy.setIdlePowerUsage( 1.0 / 16.0 ); // lights drain a little bit.
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-07 06:51:19 +01:00
|
|
|
@SideOnly(Side.CLIENT)
|
2013-12-27 23:59:59 +01:00
|
|
|
public void renderInventory(IPartRenderHelper rh, RenderBlocks renderer)
|
|
|
|
{
|
|
|
|
rh.setBounds( 2, 2, 14, 14, 14, 16 );
|
|
|
|
|
|
|
|
rh.setTexture( CableBusTextures.PartMonitorSides.getIcon(), CableBusTextures.PartMonitorSides.getIcon(), CableBusTextures.PartMonitorBack.getIcon(),
|
2014-02-22 23:14:43 +01:00
|
|
|
is.getIconIndex(), CableBusTextures.PartMonitorSides.getIcon(), CableBusTextures.PartMonitorSides.getIcon() );
|
2013-12-27 23:59:59 +01:00
|
|
|
rh.renderInventoryBox( renderer );
|
|
|
|
|
|
|
|
rh.setInvColor( getColor().whiteVariant );
|
|
|
|
rh.renderInventoryFace( frontBright.getIcon(), ForgeDirection.SOUTH, renderer );
|
|
|
|
|
|
|
|
rh.setInvColor( getColor().mediumVariant );
|
|
|
|
rh.renderInventoryFace( frontDark.getIcon(), ForgeDirection.SOUTH, renderer );
|
|
|
|
|
|
|
|
rh.setInvColor( getColor().blackVariant );
|
|
|
|
rh.renderInventoryFace( frontColored.getIcon(), ForgeDirection.SOUTH, renderer );
|
|
|
|
|
|
|
|
rh.setBounds( 4, 4, 13, 12, 12, 14 );
|
|
|
|
rh.renderInventoryBox( renderer );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-07 06:51:19 +01:00
|
|
|
@SideOnly(Side.CLIENT)
|
2013-12-27 23:59:59 +01:00
|
|
|
public void renderStatic(int x, int y, int z, IPartRenderHelper rh, RenderBlocks renderer)
|
|
|
|
{
|
2014-04-24 09:14:07 +02:00
|
|
|
renderCache = rh.useSimpliedRendering( x, y, z, this, renderCache );
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
rh.setTexture( CableBusTextures.PartMonitorSides.getIcon(), CableBusTextures.PartMonitorSides.getIcon(), CableBusTextures.PartMonitorBack.getIcon(),
|
2014-02-22 23:14:43 +01:00
|
|
|
is.getIconIndex(), CableBusTextures.PartMonitorSides.getIcon(), CableBusTextures.PartMonitorSides.getIcon() );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
rh.setBounds( 2, 2, 14, 14, 14, 16 );
|
|
|
|
rh.renderBlock( x, y, z, renderer );
|
|
|
|
|
|
|
|
if ( getLightLevel() > 0 )
|
|
|
|
{
|
|
|
|
int l = 13;
|
|
|
|
Tessellator.instance.setBrightness( l << 20 | l << 4 );
|
|
|
|
}
|
|
|
|
|
2014-07-19 23:01:24 +02:00
|
|
|
renderer.uvRotateBottom = renderer.uvRotateEast = renderer.uvRotateNorth = renderer.uvRotateSouth = renderer.uvRotateTop = renderer.uvRotateWest = this.spin;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
Tessellator.instance.setColorOpaque_I( getColor().whiteVariant );
|
|
|
|
rh.renderFace( x, y, z, frontBright.getIcon(), ForgeDirection.SOUTH, renderer );
|
|
|
|
|
|
|
|
Tessellator.instance.setColorOpaque_I( getColor().mediumVariant );
|
|
|
|
rh.renderFace( x, y, z, frontDark.getIcon(), ForgeDirection.SOUTH, renderer );
|
|
|
|
|
|
|
|
Tessellator.instance.setColorOpaque_I( getColor().blackVariant );
|
|
|
|
rh.renderFace( x, y, z, frontColored.getIcon(), ForgeDirection.SOUTH, renderer );
|
|
|
|
|
2014-07-19 23:01:24 +02:00
|
|
|
renderer.uvRotateBottom = renderer.uvRotateEast = renderer.uvRotateNorth = renderer.uvRotateSouth = renderer.uvRotateTop = renderer.uvRotateWest = 0;
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
if ( notLightSource )
|
|
|
|
{
|
|
|
|
rh.setTexture( CableBusTextures.PartMonitorSidesStatus.getIcon(), CableBusTextures.PartMonitorSidesStatus.getIcon(),
|
2014-02-22 23:14:43 +01:00
|
|
|
CableBusTextures.PartMonitorBack.getIcon(), is.getIconIndex(), CableBusTextures.PartMonitorSidesStatus.getIcon(),
|
2013-12-27 23:59:59 +01:00
|
|
|
CableBusTextures.PartMonitorSidesStatus.getIcon() );
|
|
|
|
}
|
|
|
|
|
|
|
|
rh.setBounds( 4, 4, 13, 12, 12, 14 );
|
|
|
|
rh.renderBlock( x, y, z, renderer );
|
|
|
|
|
|
|
|
if ( notLightSource )
|
|
|
|
{
|
|
|
|
boolean hasChan = (clientFlags & (POWERED_FLAG | CHANNEL_FLAG)) == (POWERED_FLAG | CHANNEL_FLAG);
|
|
|
|
boolean hasPower = (clientFlags & POWERED_FLAG) == POWERED_FLAG;
|
|
|
|
|
|
|
|
if ( hasChan )
|
|
|
|
{
|
|
|
|
int l = 14;
|
|
|
|
Tessellator.instance.setBrightness( l << 20 | l << 4 );
|
|
|
|
Tessellator.instance.setColorOpaque_I( getColor().blackVariant );
|
|
|
|
}
|
|
|
|
else if ( hasPower )
|
|
|
|
{
|
|
|
|
int l = 9;
|
|
|
|
Tessellator.instance.setBrightness( l << 20 | l << 4 );
|
|
|
|
Tessellator.instance.setColorOpaque_I( getColor().whiteVariant );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Tessellator.instance.setBrightness( 0 );
|
|
|
|
Tessellator.instance.setColorOpaque_I( 0x000000 );
|
|
|
|
}
|
|
|
|
|
|
|
|
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 getBoxes(IPartCollsionHelper bch)
|
|
|
|
{
|
|
|
|
bch.addBox( 2, 2, 14, 14, 14, 16 );
|
|
|
|
bch.addBox( 4, 4, 13, 12, 12, 14 );
|
|
|
|
}
|
|
|
|
|
2014-02-22 23:14:43 +01:00
|
|
|
@Override
|
|
|
|
public boolean isActive()
|
|
|
|
{
|
|
|
|
if ( notLightSource )
|
|
|
|
return ((clientFlags & (CHANNEL_FLAG | POWERED_FLAG)) == (CHANNEL_FLAG | POWERED_FLAG));
|
|
|
|
else
|
|
|
|
return isPowered();
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|