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
P2P Tunnel fix #3039
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c62117c
cleanup PartP2PFEPower
fscan fc5dd22
fix P2P Tunnels randomly resetting to ME
fscan 5b2a80e
remove redundant isActive()
fscan b9421d5
always provide a FE cap, just return an null handler on output part.
fscan aed4c35
invert logic
fscan 4d13eff
allow pull too
fscan d32059e
also forward canExtract()
fscan File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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 ) | ||
{ | ||
|
@@ -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 ) | ||
{ | ||
|
@@ -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 ) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
@@ -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 ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
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 ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
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 | ||
|
@@ -285,7 +278,7 @@ public boolean canExtract() | |
@Override | ||
public boolean canReceive() | ||
{ | ||
return true; | ||
return false; | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either log or rethrow this exception.