Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P2P Tunnel fix #3039

Merged
merged 7 commits into from Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
215 changes: 104 additions & 111 deletions src/main/java/appeng/parts/p2p/PartP2PFEPower.java
Expand Up @@ -26,8 +26,6 @@

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;

Expand All @@ -38,17 +36,12 @@
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 )
{
Expand All @@ -73,29 +66,62 @@ public void onTunnelNetworkChange()
this.getHost().notifyNeighbors();
}

@Override
public void onNeighborChanged( IBlockAccess w, BlockPos pos, BlockPos neighbor )
private IEnergyStorage getAttachedEnergyStorage()
{
super.onNeighborChanged( w, pos, neighbor );
if( this.isActive() )
{
final TileEntity self = this.getTile();
final TileEntity te = self.getWorld().getTileEntity( self.getPos().offset( this.getSide().getFacing() ) );

this.cachedTarget = false;
if( te != null && te.hasCapability( Capabilities.FORGE_ENERGY, this.getSide().getOpposite().getFacing() ) )
{
return te.getCapability( Capabilities.FORGE_ENERGY, this.getSide().getOpposite().getFacing() );
}
}
return NULL_ENERGY_STORAGE;
}

@Override
public boolean hasCapability( @Nonnull Capability<?> capability )
{
if( capability == Capabilities.FORGE_ENERGY )
{
return true;
}
return super.hasCapability( capability );
}

@Nullable
@Override
public int receiveEnergy( int maxReceive, boolean simulate )
public <T> T getCapability( @Nonnull Capability<T> capability )
{
if( this.isOutput() )
if( capability == Capabilities.FORGE_ENERGY )
{
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;
}

if( this.isActive() )
@Override
public int receiveEnergy( int maxReceive, boolean simulate )
{
int total = 0;

try
{
final int outputTunnels = this.getOutputs().size();
final int outputTunnels = PartP2PFEPower.this.getOutputs().size();

if( outputTunnels == 0 )
{
Expand All @@ -105,17 +131,17 @@ public int receiveEnergy( int maxReceive, boolean simulate )
final int amountPerOutput = maxReceive / outputTunnels;
int overflow = maxReceive % amountPerOutput;

for( PartP2PFEPower target : this.getOutputs() )
for( PartP2PFEPower target : PartP2PFEPower.this.getOutputs() )
{
final IEnergyStorage output = target.getOutput();
final IEnergyStorage output = target.getAttachedEnergyStorage();
final int toSend = amountPerOutput + overflow;
final int received = output.receiveEnergy( toSend, simulate );

overflow = toSend - received;
total += received;
}

this.queueTunnelDrain( PowerUnits.RF, total );
PartP2PFEPower.this.queueTunnelDrain( PowerUnits.RF, total );
}
catch( GridAccessException ignored )

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Either log or rethrow this exception. rule

{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Either remove or fill this block of code. rule

Expand All @@ -124,129 +150,96 @@ public int receiveEnergy( int maxReceive, boolean simulate )
return total;
}

return 0;
}
@Override
public boolean canExtract()
{
return false;
}

@Override
public int extractEnergy( int maxExtract, boolean simulate )
{
return 0;
}
@Override
public boolean canReceive()
{
return true;
}

private IEnergyStorage getOutput()
{
if( this.isOutput() )
@Override
public int getMaxEnergyStored()
{
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 ) );
int total = 0;

if( te != null && te.hasCapability( Capabilities.FORGE_ENERGY, this.getSide().getOpposite().getFacing() ) )
{
this.outputTarget = te.getCapability( Capabilities.FORGE_ENERGY, this.getSide().getOpposite().getFacing() );
}
else
try
{
for( PartP2PFEPower t : PartP2PFEPower.this.getOutputs() )
{
this.outputTarget = null;
total += t.getAttachedEnergyStorage().getMaxEnergyStored();
}

this.cachedTarget = true;
}

if( this.outputTarget != null && this.outputTarget.canReceive() )
catch( GridAccessException e )

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Either log or rethrow this exception. rule

{
return this.outputTarget;
return 0;
}
}

return NULL_ENERGY_STORAGE;
}

@Override
public int getEnergyStored()
{
if( this.isOutput() || !this.isActive() )
{
return 0;
return total;
}

int total = 0;

try
@Override
public int getEnergyStored()
{
for( PartP2PFEPower t : this.getOutputs() )
int total = 0;

try
{
total += t.getOutput().getEnergyStored();
for( PartP2PFEPower t : PartP2PFEPower.this.getOutputs() )
{
total += t.getAttachedEnergyStorage().getEnergyStored();
}
}
catch( GridAccessException e )

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAJOR Either log or rethrow this exception. rule

{
return 0;
}
}
catch( GridAccessException e )
{
return 0;
}

return total;
return total;
}
}

@Override
public int getMaxEnergyStored()
private class OutputEnergyStorage implements IEnergyStorage
{
if( this.isOutput() || !this.isActive() )
@Override
public int extractEnergy( int maxExtract, boolean simulate )
{
return 0;
return PartP2PFEPower.this.getAttachedEnergyStorage().extractEnergy( maxExtract, simulate );
}

int total = 0;

try
{
for( PartP2PFEPower t : this.getOutputs() )
{
total += t.getOutput().getMaxEnergyStored();
}
}
catch( GridAccessException e )
@Override
public int receiveEnergy( int maxReceive, boolean simulate )
{
return 0;
}

return total;
}

@Override
public boolean canExtract()
{
return false;
}

@Override
public boolean canReceive()
{
return true;
}

@Override
public boolean hasCapability( @Nonnull Capability<?> capability )
{
if( capability == Capabilities.FORGE_ENERGY )
@Override
public boolean canExtract()
{
return true;
}

return super.hasCapability( capability );
}
@Override
public boolean canReceive()
{
return false;
}

@Nullable
@Override
public <T> T getCapability( @Nonnull Capability<T> capability )
{
if( capability == Capabilities.FORGE_ENERGY )
@Override
public int getMaxEnergyStored()
{
return (T) this;
return PartP2PFEPower.this.getAttachedEnergyStorage().getMaxEnergyStored();
}

return super.getCapability( capability );
@Override
public int getEnergyStored()
{
return PartP2PFEPower.this.getAttachedEnergyStorage().getEnergyStored();
}
}

private static class NullEnergyStorage implements IEnergyStorage
Expand Down Expand Up @@ -285,7 +278,7 @@ public boolean canExtract()
@Override
public boolean canReceive()
{
return true;
return false;
}

}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/appeng/parts/p2p/PartP2PTunnel.java
Expand Up @@ -160,6 +160,11 @@ public boolean useStandardMemoryCard()
@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() );
Expand Down