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

190 lines
4.4 KiB
Java
Raw Normal View History

2014-01-20 17:41:37 +01:00
package appeng.parts.p2p;
import net.minecraft.block.Block;
import net.minecraft.block.BlockRedstoneWire;
2014-02-09 02:34:52 +01:00
import net.minecraft.init.Blocks;
2014-01-20 17:41:37 +01:00
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.world.World;
import appeng.api.config.TunnelType;
2014-01-20 17:41:37 +01:00
import appeng.api.networking.events.MENetworkBootingStatusChange;
import appeng.api.networking.events.MENetworkChannelsChanged;
import appeng.api.networking.events.MENetworkEventSubscribe;
import appeng.api.networking.events.MENetworkPowerStatusChange;
import appeng.me.GridAccessException;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class PartP2PRedstone extends PartP2PTunnel<PartP2PRedstone>
{
public TunnelType getTunnelType()
{
return TunnelType.REDSTONE;
}
2014-01-20 17:41:37 +01:00
public PartP2PRedstone(ItemStack is) {
super( is );
}
int power;
@Override
public boolean canConnectRedstone()
{
return true;
}
@Override
public int isProvidingStrongPower()
{
return output ? power : 0;
}
@Override
public int isProvidingWeakPower()
{
return output ? power : 0;
}
@Override
public void onTunnelNetworkChange()
2014-01-20 17:41:37 +01:00
{
setNetworkReady();
}
@MENetworkEventSubscribe
public void changeStateA(MENetworkBootingStatusChange bs)
{
setNetworkReady();
}
@MENetworkEventSubscribe
public void changeStateB(MENetworkChannelsChanged bs)
{
setNetworkReady();
}
@MENetworkEventSubscribe
public void changeStateC(MENetworkPowerStatusChange bs)
{
setNetworkReady();
}
public void setNetworkReady()
{
if ( output )
{
PartP2PRedstone in = getInput();
if ( in != null )
putInput( ((PartP2PRedstone) in).power );
}
}
boolean recursive = false;
protected void putInput(Object o)
{
if ( recursive )
return;
recursive = true;
if ( output && proxy.isActive() )
{
int newPower = (Integer) o;
if ( power != newPower )
{
power = newPower;
notifyNeightbors();
}
}
recursive = false;
}
public void notifyNeightbors()
{
2014-02-09 02:34:52 +01:00
World worldObj = tile.getWorldObj();
2014-01-20 17:41:37 +01:00
int xCoord = tile.xCoord;
int yCoord = tile.yCoord;
int zCoord = tile.zCoord;
2014-02-09 02:34:52 +01:00
worldObj.notifyBlocksOfNeighborChange( xCoord, yCoord, zCoord, worldObj.getBlock( xCoord, yCoord, zCoord ) );
2014-01-20 17:41:37 +01:00
// and this cause somtimes it can go thought walls.
2014-02-09 02:34:52 +01:00
worldObj.notifyBlocksOfNeighborChange( xCoord - 1, yCoord, zCoord, worldObj.getBlock( xCoord, yCoord, zCoord ) );
worldObj.notifyBlocksOfNeighborChange( xCoord, yCoord - 1, zCoord, worldObj.getBlock( xCoord, yCoord, zCoord ) );
worldObj.notifyBlocksOfNeighborChange( xCoord, yCoord, zCoord - 1, worldObj.getBlock( xCoord, yCoord, zCoord ) );
worldObj.notifyBlocksOfNeighborChange( xCoord, yCoord, zCoord + 1, worldObj.getBlock( xCoord, yCoord, zCoord ) );
worldObj.notifyBlocksOfNeighborChange( xCoord, yCoord + 1, zCoord, worldObj.getBlock( xCoord, yCoord, zCoord ) );
worldObj.notifyBlocksOfNeighborChange( xCoord + 1, yCoord, zCoord, worldObj.getBlock( xCoord, yCoord, zCoord ) );
2014-01-20 17:41:37 +01:00
}
@Override
public void writeToNBT(NBTTagCompound tag)
{
super.writeToNBT( tag );
tag.setInteger( "power", power );
}
@Override
public void readFromNBT(NBTTagCompound tag)
{
super.readFromNBT( tag );
power = tag.getInteger( "power" );
}
@SideOnly(Side.CLIENT)
2014-02-09 02:34:52 +01:00
public IIcon getTypeTexture()
2014-01-20 17:41:37 +01:00
{
2014-02-09 02:34:52 +01:00
return Blocks.redstone_block.getBlockTextureFromSide( 0 );
2014-01-20 17:41:37 +01:00
}
public float getPowerDrainPerTick()
{
return 0.5f;
};
@Override
public void onNeighborChanged()
{
if ( !output )
{
int x = tile.xCoord + side.offsetX;
int y = tile.yCoord + side.offsetY;
int z = tile.zCoord + side.offsetZ;
2014-02-09 02:34:52 +01:00
Block b = tile.getWorldObj().getBlock( x, y, z );
2014-01-20 17:41:37 +01:00
if ( b != null && !output )
{
int srcSide = side.ordinal();
if ( b instanceof BlockRedstoneWire )
srcSide = 1;
2014-02-09 02:34:52 +01:00
power = b.isProvidingStrongPower( tile.getWorldObj(), x, y, z, srcSide );
power = Math.max( power, b.isProvidingWeakPower( tile.getWorldObj(), x, y, z, srcSide ) );
2014-01-20 17:41:37 +01:00
sendToOutput( power );
}
else
sendToOutput( 0 );
}
}
private void sendToOutput(int power)
{
try
{
for (PartP2PRedstone rs : getOutputs())
{
rs.putInput( power );
}
}
catch (GridAccessException e)
{
// :P
}
}
}