make it runnable

This commit is contained in:
thatsIch
2015-06-17 19:39:18 +02:00
parent 38afde724b
commit a95b9962b9
17 changed files with 1422 additions and 1583 deletions
+234 -252
View File
@@ -19,255 +19,237 @@
package appeng.parts.p2p;
import java.util.Stack;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import cofh.api.energy.IEnergyReceiver;
import appeng.api.config.PowerUnits;
import appeng.api.util.ForgeDirection;
import appeng.integration.modules.helpers.NullRFHandler;
import appeng.me.GridAccessException;
import appeng.transformer.annotations.Integration.Interface;
import appeng.transformer.annotations.Integration.InterfaceList;
import appeng.util.Platform;
@InterfaceList( value = { @Interface( iface = "cofh.api.energy.IEnergyReceiver", iname = "RF" ) } )
public final class PartP2PRFPower extends PartP2PTunnel<PartP2PRFPower> implements IEnergyReceiver
{
private static final ThreadLocal<Stack<PartP2PRFPower>> THREAD_STACK = new ThreadLocal<Stack<PartP2PRFPower>>();
/**
* Default element based on the null element pattern
*/
private static final IEnergyReceiver NULL_HANDLER = new NullRFHandler();
private boolean cachedTarget = false;
private IEnergyReceiver outputTarget;
public PartP2PRFPower( ItemStack is )
{
super( is );
}
@Override
@SideOnly( Side.CLIENT )
public TextureAtlasSprite getTypeTexture()
{
return Blocks.iron_block.getBlockTextureFromSide( 0 );
}
@Override
public void onTunnelNetworkChange()
{
this.getHost().notifyNeighbors();
}
@Override
public void onNeighborChanged()
{
super.onNeighborChanged();
this.cachedTarget = false;
}
@Override
public int receiveEnergy( ForgeDirection from, int maxReceive, boolean simulate )
{
if( this.output )
{
return 0;
}
if( this.isActive() )
{
Stack<PartP2PRFPower> stack = this.getDepth();
for( PartP2PRFPower t : stack )
{
if( t == this )
{
return 0;
}
}
stack.push( this );
int total = 0;
try
{
for( PartP2PRFPower t : this.getOutputs() )
{
if( Platform.getRandomInt() % 2 > 0 )
{
int receiver = t.getOutput().receiveEnergy( t.side.getOpposite(), maxReceive, simulate );
maxReceive -= receiver;
total += receiver;
if( maxReceive <= 0 )
{
break;
}
}
}
if( maxReceive > 0 )
{
for( PartP2PRFPower t : this.getOutputs() )
{
int receiver = t.getOutput().receiveEnergy( t.side.getOpposite(), maxReceive, simulate );
maxReceive -= receiver;
total += receiver;
if( maxReceive <= 0 )
{
break;
}
}
}
this.queueTunnelDrain( PowerUnits.RF, total );
}
catch( GridAccessException ignored )
{
}
if( stack.pop() != this )
{
throw new IllegalStateException( "Invalid Recursion detected." );
}
return total;
}
return 0;
}
private Stack<PartP2PRFPower> getDepth()
{
Stack<PartP2PRFPower> s = THREAD_STACK.get();
if( s == null )
{
THREAD_STACK.set( s = new Stack<PartP2PRFPower>() );
}
return s;
}
private IEnergyReceiver getOutput()
{
if( this.output )
{
if( !this.cachedTarget )
{
TileEntity self = this.getTile();
TileEntity te = self.getWorld().getTileEntity( self.xCoord + this.side.offsetX, self.yCoord + this.side.offsetY, self.zCoord + this.side.offsetZ );
this.outputTarget = te instanceof IEnergyReceiver ? (IEnergyReceiver) te : null;
this.cachedTarget = true;
}
if( this.outputTarget == null || !this.outputTarget.canConnectEnergy( this.side.getOpposite() ) )
{
return NULL_HANDLER;
}
return this.outputTarget;
}
return NULL_HANDLER;
}
@Override
public int getEnergyStored( ForgeDirection from )
{
if( this.output || !this.isActive() )
{
return 0;
}
int total = 0;
Stack<PartP2PRFPower> stack = this.getDepth();
for( PartP2PRFPower t : stack )
{
if( t == this )
{
return 0;
}
}
stack.push( this );
try
{
for( PartP2PRFPower t : this.getOutputs() )
{
total += t.getOutput().getEnergyStored( t.side.getOpposite() );
}
}
catch( GridAccessException e )
{
return 0;
}
if( stack.pop() != this )
{
throw new IllegalStateException( "Invalid Recursion detected." );
}
return total;
}
@Override
public int getMaxEnergyStored( ForgeDirection from )
{
if( this.output || !this.isActive() )
{
return 0;
}
int total = 0;
Stack<PartP2PRFPower> stack = this.getDepth();
for( PartP2PRFPower t : stack )
{
if( t == this )
{
return 0;
}
}
stack.push( this );
try
{
for( PartP2PRFPower t : this.getOutputs() )
{
total += t.getOutput().getMaxEnergyStored( t.side.getOpposite() );
}
}
catch( GridAccessException e )
{
return 0;
}
if( stack.pop() != this )
{
throw new IllegalStateException( "Invalid Recursion detected." );
}
return total;
}
@Override
public boolean canConnectEnergy( ForgeDirection from )
{
return true;
}
}
//@InterfaceList( value = { @Interface( iface = "cofh.api.energy.IEnergyReceiver", iname = "RF" ) } )
//public final class PartP2PRFPower extends PartP2PTunnel<PartP2PRFPower> implements IEnergyReceiver
//{
// private static final ThreadLocal<Stack<PartP2PRFPower>> THREAD_STACK = new ThreadLocal<Stack<PartP2PRFPower>>();
// /**
// * Default element based on the null element pattern
// */
// private static final IEnergyReceiver NULL_HANDLER = new NullRFHandler();
// private boolean cachedTarget = false;
// private IEnergyReceiver outputTarget;
//
// public PartP2PRFPower( ItemStack is )
// {
// super( is );
// }
//
// @Override
// @SideOnly( Side.CLIENT )
// public TextureAtlasSprite getTypeTexture()
// {
// return Blocks.iron_block.getBlockTextureFromSide( 0 );
// }
//
// @Override
// public void onTunnelNetworkChange()
// {
// this.getHost().notifyNeighbors();
// }
//
// @Override
// public void onNeighborChanged()
// {
// super.onNeighborChanged();
//
// this.cachedTarget = false;
// }
//
// @Override
// public int receiveEnergy( ForgeDirection from, int maxReceive, boolean simulate )
// {
// if( this.output )
// {
// return 0;
// }
//
// if( this.isActive() )
// {
// Stack<PartP2PRFPower> stack = this.getDepth();
//
// for( PartP2PRFPower t : stack )
// {
// if( t == this )
// {
// return 0;
// }
// }
//
// stack.push( this );
//
// int total = 0;
//
// try
// {
// for( PartP2PRFPower t : this.getOutputs() )
// {
// if( Platform.getRandomInt() % 2 > 0 )
// {
// int receiver = t.getOutput().receiveEnergy( t.side.getOpposite(), maxReceive, simulate );
// maxReceive -= receiver;
// total += receiver;
//
// if( maxReceive <= 0 )
// {
// break;
// }
// }
// }
//
// if( maxReceive > 0 )
// {
// for( PartP2PRFPower t : this.getOutputs() )
// {
// int receiver = t.getOutput().receiveEnergy( t.side.getOpposite(), maxReceive, simulate );
// maxReceive -= receiver;
// total += receiver;
//
// if( maxReceive <= 0 )
// {
// break;
// }
// }
// }
//
// this.queueTunnelDrain( PowerUnits.RF, total );
// }
// catch( GridAccessException ignored )
// {
// }
//
// if( stack.pop() != this )
// {
// throw new IllegalStateException( "Invalid Recursion detected." );
// }
//
// return total;
// }
//
// return 0;
// }
//
// private Stack<PartP2PRFPower> getDepth()
// {
// Stack<PartP2PRFPower> s = THREAD_STACK.get();
//
// if( s == null )
// {
// THREAD_STACK.set( s = new Stack<PartP2PRFPower>() );
// }
//
// return s;
// }
//
// private IEnergyReceiver getOutput()
// {
// if( this.output )
// {
// if( !this.cachedTarget )
// {
// TileEntity self = this.getTile();
// TileEntity te = self.getWorld().getTileEntity( self.xCoord + this.side.offsetX, self.yCoord + this.side.offsetY, self.zCoord + this.side.offsetZ );
// this.outputTarget = te instanceof IEnergyReceiver ? (IEnergyReceiver) te : null;
// this.cachedTarget = true;
// }
//
// if( this.outputTarget == null || !this.outputTarget.canConnectEnergy( this.side.getOpposite() ) )
// {
// return NULL_HANDLER;
// }
//
// return this.outputTarget;
// }
// return NULL_HANDLER;
// }
//
// @Override
// public int getEnergyStored( ForgeDirection from )
// {
// if( this.output || !this.isActive() )
// {
// return 0;
// }
//
// int total = 0;
//
// Stack<PartP2PRFPower> stack = this.getDepth();
//
// for( PartP2PRFPower t : stack )
// {
// if( t == this )
// {
// return 0;
// }
// }
//
// stack.push( this );
//
// try
// {
// for( PartP2PRFPower t : this.getOutputs() )
// {
// total += t.getOutput().getEnergyStored( t.side.getOpposite() );
// }
// }
// catch( GridAccessException e )
// {
// return 0;
// }
//
// if( stack.pop() != this )
// {
// throw new IllegalStateException( "Invalid Recursion detected." );
// }
//
// return total;
// }
//
// @Override
// public int getMaxEnergyStored( ForgeDirection from )
// {
// if( this.output || !this.isActive() )
// {
// return 0;
// }
//
// int total = 0;
//
// Stack<PartP2PRFPower> stack = this.getDepth();
//
// for( PartP2PRFPower t : stack )
// {
// if( t == this )
// {
// return 0;
// }
// }
//
// stack.push( this );
//
// try
// {
// for( PartP2PRFPower t : this.getOutputs() )
// {
// total += t.getOutput().getMaxEnergyStored( t.side.getOpposite() );
// }
// }
// catch( GridAccessException e )
// {
// return 0;
// }
//
// if( stack.pop() != this )
// {
// throw new IllegalStateException( "Invalid Recursion detected." );
// }
//
// return total;
// }
//
// @Override
// public boolean canConnectEnergy( ForgeDirection from )
// {
// return true;
// }
//}