parent
f7f2903e39
commit
08245d8578
1 changed files with 59 additions and 11 deletions
|
@ -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 ) {
|
||||||
|
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;
|
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,9 +173,7 @@ public class CableBlockEntity extends BlockEntity
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (acceptors.isEmpty()) {
|
if (!acceptors.isEmpty()) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
Collections.shuffle(acceptors);
|
Collections.shuffle(acceptors);
|
||||||
|
|
||||||
acceptors.forEach(pair -> {
|
acceptors.forEach(pair -> {
|
||||||
|
@ -171,6 +183,19 @@ public class CableBlockEntity extends BlockEntity
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 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
|
||||||
@Override
|
@Override
|
||||||
public void addInfo(List<Text> info, boolean isReal, boolean hasData) {
|
public void addInfo(List<Text> info, boolean isReal, boolean hasData) {
|
||||||
|
@ -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)) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue