Cable distribution hotfix (#2159)

Temp fix till networks
This commit is contained in:
Justin Vitale 2020-07-09 19:06:46 +10:00 committed by GitHub
parent f7f2903e39
commit 08245d8578
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -143,13 +143,27 @@ public class CableBlockEntity extends BlockEntity
} }
ArrayList<Pair<BlockEntity, Direction>> acceptors = new ArrayList<>(); ArrayList<Pair<BlockEntity, Direction>> acceptors = new ArrayList<>();
ArrayList<CableBlockEntity> cables = new ArrayList<>();
for (Direction face : Direction.values()) { for (Direction face : Direction.values()) {
BlockEntity blockEntity = world.getBlockEntity(pos.offset(face)); BlockEntity blockEntity = world.getBlockEntity(pos.offset(face));
if (blockEntity != null && Energy.valid(blockEntity)) { if (blockEntity != null && Energy.valid(blockEntity)) {
if (blockEntity instanceof CableBlockEntity && energy <= Energy.of(blockEntity).side(face).getEnergy()) { if (blockEntity instanceof CableBlockEntity ) {
continue; CableBlockEntity cableBlockEntity = (CableBlockEntity)blockEntity;
if(cableBlockEntity.getTier() == this.getTier()) {
// Only need ones with energy stores less than ours
if (cableBlockEntity.getEnergy() < this.getEnergy()) {
cables.add(cableBlockEntity);
}
continue;
}else if(energy <= Energy.of(blockEntity).side(face).getEnergy()){
continue;
}
} }
if(Energy.of(blockEntity).side(face.getOpposite()).getMaxInput() > 0){ if(Energy.of(blockEntity).side(face.getOpposite()).getMaxInput() > 0){
acceptors.add(Pair.of(blockEntity, face)); acceptors.add(Pair.of(blockEntity, face));
if (!sendingFace.contains(face)) { if (!sendingFace.contains(face)) {
@ -159,16 +173,27 @@ public class CableBlockEntity extends BlockEntity
} }
} }
if (acceptors.isEmpty()) { if (!acceptors.isEmpty()) {
return; Collections.shuffle(acceptors);
}
Collections.shuffle(acceptors);
acceptors.forEach(pair -> { acceptors.forEach(pair -> {
Energy.of(this) Energy.of(this)
.into(Energy.of(pair.getLeft()).side(pair.getRight().getOpposite())) .into(Energy.of(pair.getLeft()).side(pair.getRight().getOpposite()))
.move(); .move();
}); });
}
// Distribute energy between cables (TODO DIRTY FIX, until we game network cables)
if(!cables.isEmpty()) {
cables.add(this);
double energyTotal = cables.stream().mapToDouble(CableBlockEntity::getEnergy).sum();
double energyPer = energyTotal / cables.size();
cables.forEach(cableBlockEntity -> cableBlockEntity.setEnergy(energyPer));
}
} }
// IListInfoProvider // IListInfoProvider
@ -223,6 +248,25 @@ public class CableBlockEntity extends BlockEntity
return energyOut; return energyOut;
} }
public double addEnergy(double energyIn) {
if(this.isFull()){
return energyIn;
}
double maxStore = this.getMaxStoredPower();
if (energyIn + energy <= maxStore) {
setEnergy(energyIn + energy);
return 0;
}
// Can't fit energy fully, add part of it
double amountCanAdd = (maxStore - energy);
setEnergy(amountCanAdd + energy);
return energyIn - amountCanAdd;
}
public boolean canAcceptEnergy(EnergySide direction) { public boolean canAcceptEnergy(EnergySide direction) {
if (sendingFace.contains(direction)) { if (sendingFace.contains(direction)) {
return false; return false;
@ -230,6 +274,10 @@ public class CableBlockEntity extends BlockEntity
return getMaxStoredPower() != getEnergy(); return getMaxStoredPower() != getEnergy();
} }
public boolean isFull(){
return getMaxStoredPower() == getEnergy();
}
@Override @Override
public double getMaxInput(EnergySide side) { public double getMaxInput(EnergySide side) {
if(!canAcceptEnergy(side)) { if(!canAcceptEnergy(side)) {