P2P Tunnel improvements (#3039)
* Fix P2P Tunnels randomly resetting to ME * Allow one way push and pull. Outputs can pull from the input side, input can push into outputs.
This commit is contained in:
parent
80eaa032b9
commit
8e4265aa33
2 changed files with 155 additions and 157 deletions
|
@ -26,8 +26,6 @@ import javax.annotation.Nullable;
|
|||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.energy.IEnergyStorage;
|
||||
|
||||
|
@ -38,17 +36,12 @@ import appeng.items.parts.PartModels;
|
|||
import appeng.me.GridAccessException;
|
||||
|
||||
|
||||
/**
|
||||
* @author GuntherDW
|
||||
*/
|
||||
public class PartP2PFEPower extends PartP2PTunnel<PartP2PFEPower> implements IEnergyStorage
|
||||
public class PartP2PFEPower extends PartP2PTunnel<PartP2PFEPower>
|
||||
{
|
||||
private static final P2PModels MODELS = new P2PModels( "part/p2p/p2p_tunnel_fe" );
|
||||
private static final IEnergyStorage NULL_ENERGY_STORAGE = new NullEnergyStorage();
|
||||
|
||||
private boolean cachedTarget = false;
|
||||
|
||||
private IEnergyStorage outputTarget;
|
||||
private final IEnergyStorage inputHandler = new InputEnergyStorage();
|
||||
private final IEnergyStorage outputHandler = new OutputEnergyStorage();
|
||||
|
||||
public PartP2PFEPower( ItemStack is )
|
||||
{
|
||||
|
@ -73,159 +66,21 @@ public class PartP2PFEPower extends PartP2PTunnel<PartP2PFEPower> implements IEn
|
|||
this.getHost().notifyNeighbors();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNeighborChanged( IBlockAccess w, BlockPos pos, BlockPos neighbor )
|
||||
private IEnergyStorage getAttachedEnergyStorage()
|
||||
{
|
||||
super.onNeighborChanged( w, pos, neighbor );
|
||||
|
||||
this.cachedTarget = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int receiveEnergy( int maxReceive, boolean simulate )
|
||||
{
|
||||
if( this.isOutput() )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if( this.isActive() )
|
||||
{
|
||||
int total = 0;
|
||||
final TileEntity self = this.getTile();
|
||||
final TileEntity te = self.getWorld().getTileEntity( self.getPos().offset( this.getSide().getFacing() ) );
|
||||
|
||||
try
|
||||
if( te != null && te.hasCapability( Capabilities.FORGE_ENERGY, this.getSide().getOpposite().getFacing() ) )
|
||||
{
|
||||
final int outputTunnels = this.getOutputs().size();
|
||||
|
||||
if( outputTunnels == 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
final int amountPerOutput = maxReceive / outputTunnels;
|
||||
int overflow = maxReceive % amountPerOutput;
|
||||
|
||||
for( PartP2PFEPower target : this.getOutputs() )
|
||||
{
|
||||
final IEnergyStorage output = target.getOutput();
|
||||
final int toSend = amountPerOutput + overflow;
|
||||
final int received = output.receiveEnergy( toSend, simulate );
|
||||
|
||||
overflow = toSend - received;
|
||||
total += received;
|
||||
}
|
||||
|
||||
this.queueTunnelDrain( PowerUnits.RF, total );
|
||||
}
|
||||
catch( GridAccessException ignored )
|
||||
{
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int extractEnergy( int maxExtract, boolean simulate )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
private IEnergyStorage getOutput()
|
||||
{
|
||||
if( this.isOutput() )
|
||||
{
|
||||
if( !this.cachedTarget )
|
||||
{
|
||||
final TileEntity self = this.getTile();
|
||||
final TileEntity te = self.getWorld().getTileEntity( new BlockPos( self.getPos().getX() + this.getSide().xOffset, self.getPos()
|
||||
.getY() + this.getSide().yOffset, self.getPos().getZ() + this.getSide().zOffset ) );
|
||||
|
||||
if( te != null && te.hasCapability( Capabilities.FORGE_ENERGY, this.getSide().getOpposite().getFacing() ) )
|
||||
{
|
||||
this.outputTarget = te.getCapability( Capabilities.FORGE_ENERGY, this.getSide().getOpposite().getFacing() );
|
||||
}
|
||||
else
|
||||
{
|
||||
this.outputTarget = null;
|
||||
}
|
||||
|
||||
this.cachedTarget = true;
|
||||
}
|
||||
|
||||
if( this.outputTarget != null && this.outputTarget.canReceive() )
|
||||
{
|
||||
return this.outputTarget;
|
||||
return te.getCapability( Capabilities.FORGE_ENERGY, this.getSide().getOpposite().getFacing() );
|
||||
}
|
||||
}
|
||||
|
||||
return NULL_ENERGY_STORAGE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEnergyStored()
|
||||
{
|
||||
if( this.isOutput() || !this.isActive() )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int total = 0;
|
||||
|
||||
try
|
||||
{
|
||||
for( PartP2PFEPower t : this.getOutputs() )
|
||||
{
|
||||
total += t.getOutput().getEnergyStored();
|
||||
}
|
||||
}
|
||||
catch( GridAccessException e )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxEnergyStored()
|
||||
{
|
||||
if( this.isOutput() || !this.isActive() )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int total = 0;
|
||||
|
||||
try
|
||||
{
|
||||
for( PartP2PFEPower t : this.getOutputs() )
|
||||
{
|
||||
total += t.getOutput().getMaxEnergyStored();
|
||||
}
|
||||
}
|
||||
catch( GridAccessException e )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canExtract()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canReceive()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCapability( @Nonnull Capability<?> capability )
|
||||
{
|
||||
|
@ -233,7 +88,6 @@ public class PartP2PFEPower extends PartP2PTunnel<PartP2PFEPower> implements IEn
|
|||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return super.hasCapability( capability );
|
||||
}
|
||||
|
||||
|
@ -243,10 +97,149 @@ public class PartP2PFEPower extends PartP2PTunnel<PartP2PFEPower> implements IEn
|
|||
{
|
||||
if( capability == Capabilities.FORGE_ENERGY )
|
||||
{
|
||||
return (T) this;
|
||||
if( this.isOutput() )
|
||||
{
|
||||
return (T) this.outputHandler;
|
||||
}
|
||||
return (T) this.inputHandler;
|
||||
}
|
||||
return super.getCapability( capability );
|
||||
}
|
||||
|
||||
private class InputEnergyStorage implements IEnergyStorage
|
||||
{
|
||||
@Override
|
||||
public int extractEnergy( int maxExtract, boolean simulate )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return super.getCapability( capability );
|
||||
@Override
|
||||
public int receiveEnergy( int maxReceive, boolean simulate )
|
||||
{
|
||||
int total = 0;
|
||||
|
||||
try
|
||||
{
|
||||
final int outputTunnels = PartP2PFEPower.this.getOutputs().size();
|
||||
|
||||
if( outputTunnels == 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
final int amountPerOutput = maxReceive / outputTunnels;
|
||||
int overflow = maxReceive % amountPerOutput;
|
||||
|
||||
for( PartP2PFEPower target : PartP2PFEPower.this.getOutputs() )
|
||||
{
|
||||
final IEnergyStorage output = target.getAttachedEnergyStorage();
|
||||
final int toSend = amountPerOutput + overflow;
|
||||
final int received = output.receiveEnergy( toSend, simulate );
|
||||
|
||||
overflow = toSend - received;
|
||||
total += received;
|
||||
}
|
||||
|
||||
PartP2PFEPower.this.queueTunnelDrain( PowerUnits.RF, total );
|
||||
}
|
||||
catch( GridAccessException ignored )
|
||||
{
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canExtract()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canReceive()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxEnergyStored()
|
||||
{
|
||||
int total = 0;
|
||||
|
||||
try
|
||||
{
|
||||
for( PartP2PFEPower t : PartP2PFEPower.this.getOutputs() )
|
||||
{
|
||||
total += t.getAttachedEnergyStorage().getMaxEnergyStored();
|
||||
}
|
||||
}
|
||||
catch( GridAccessException e )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEnergyStored()
|
||||
{
|
||||
int total = 0;
|
||||
|
||||
try
|
||||
{
|
||||
for( PartP2PFEPower t : PartP2PFEPower.this.getOutputs() )
|
||||
{
|
||||
total += t.getAttachedEnergyStorage().getEnergyStored();
|
||||
}
|
||||
}
|
||||
catch( GridAccessException e )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
}
|
||||
|
||||
private class OutputEnergyStorage implements IEnergyStorage
|
||||
{
|
||||
@Override
|
||||
public int extractEnergy( int maxExtract, boolean simulate )
|
||||
{
|
||||
return PartP2PFEPower.this.getAttachedEnergyStorage().extractEnergy( maxExtract, simulate );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int receiveEnergy( int maxReceive, boolean simulate )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canExtract()
|
||||
{
|
||||
return PartP2PFEPower.this.getAttachedEnergyStorage().canExtract();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canReceive()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxEnergyStored()
|
||||
{
|
||||
return PartP2PFEPower.this.getAttachedEnergyStorage().getMaxEnergyStored();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEnergyStored()
|
||||
{
|
||||
return PartP2PFEPower.this.getAttachedEnergyStorage().getEnergyStored();
|
||||
}
|
||||
}
|
||||
|
||||
private static class NullEnergyStorage implements IEnergyStorage
|
||||
|
@ -285,7 +278,7 @@ public class PartP2PFEPower extends PartP2PTunnel<PartP2PFEPower> implements IEn
|
|||
@Override
|
||||
public boolean canReceive()
|
||||
{
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -160,6 +160,11 @@ public abstract class PartP2PTunnel<T extends PartP2PTunnel> extends PartBasicSt
|
|||
@Override
|
||||
public boolean onPartActivate( final EntityPlayer player, final EnumHand hand, final Vec3d pos )
|
||||
{
|
||||
if ( hand == EnumHand.OFF_HAND )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
final ItemStack is = player.getHeldItem( hand );
|
||||
|
||||
// UniqueIdentifier id = GameRegistry.findUniqueIdentifierFor( is.getItem() );
|
||||
|
|
Loading…
Reference in a new issue