Cleanup of CableBlockEntity

This commit is contained in:
drcrazy 2020-08-20 21:42:29 +03:00
parent 5df411f26d
commit 2daf57e19c

View file

@ -67,7 +67,7 @@ public class CableBlockEntity extends BlockEntity
private double energy = 0;
private TRContent.Cables cableType = null;
private ArrayList<Direction> sendingFace = new ArrayList<>();
private ArrayList<EnergySide> sendingFace = new ArrayList<>();
private BlockState cover = null;
public CableBlockEntity() {
@ -87,13 +87,40 @@ public class CableBlockEntity extends BlockEntity
return TRContent.Cables.COPPER;
}
Block block = world.getBlockState(pos).getBlock();
if(block instanceof CableBlock){
if (block instanceof CableBlock) {
return ((CableBlock) block).type;
}
//Something has gone wrong if this happens
return TRContent.Cables.COPPER;
}
public BlockState getCover() {
return cover;
}
public void setCover(BlockState cover) {
this.cover = cover;
if (world != null && !world.isClient) {
NetworkManager.sendToTracking(ClientBoundPackets.createCustomDescriptionPacket(this), this);
}
}
public boolean canAcceptEnergy(EnergySide direction) {
if (sendingFace.contains(direction)) {
return false;
}
return getMaxStoredPower() != getEnergy();
}
public double getEnergy() {
return getStored(EnergySide.UNKNOWN);
}
public void setEnergy(double energy) {
setStored(energy);
}
// BlockEntity
@Override
public CompoundTag toInitialChunkDataTag() {
return toTag(new CompoundTag());
@ -106,7 +133,6 @@ public class CableBlockEntity extends BlockEntity
return new BlockEntityUpdateS2CPacket(getPos(), 1, nbtTag);
}
@Override
public void fromTag(BlockState blockState, CompoundTag compound) {
super.fromTag(blockState, compound);
@ -130,15 +156,19 @@ public class CableBlockEntity extends BlockEntity
return compound;
}
// Tickable
@Override
public void tick() {
if (world == null) {
return;
}
if (world.isClient) {
return;
}
sendingFace.clear();
if(getEnergy() == 0){
if (getEnergy() == 0) {
return;
}
@ -148,26 +178,33 @@ public class CableBlockEntity extends BlockEntity
for (Direction face : Direction.values()) {
BlockEntity blockEntity = world.getBlockEntity(pos.offset(face));
if (blockEntity != null && Energy.valid(blockEntity)) {
if (blockEntity instanceof CableBlockEntity ) {
CableBlockEntity cableBlockEntity = (CableBlockEntity)blockEntity;
if (blockEntity == null) {
continue;
}
if(cableBlockEntity.getTier() == this.getTier()) {
if (!Energy.valid(blockEntity)) {
continue;
}
if (blockEntity instanceof CableBlockEntity) {
CableBlockEntity cableBlockEntity = (CableBlockEntity) blockEntity;
if (cableBlockEntity.getTier() != this.getTier()) {
continue;
}
// 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){
} else {
EnergySide side = EnergySide.fromMinecraft(face);
if (Energy.of(blockEntity).side(face.getOpposite()).getMaxInput() > 0) {
acceptors.add(Pair.of(blockEntity, face));
if (!sendingFace.contains(face)) {
sendingFace.add(face);
if (!sendingFace.contains(side)) {
sendingFace.add(side);
}
}
}
@ -186,7 +223,7 @@ public class CableBlockEntity extends BlockEntity
// Distribute energy between cables (TODO DIRTY FIX, until we game network cables)
if(!cables.isEmpty()) {
if (!cables.isEmpty()) {
cables.add(this);
double energyTotal = cables.stream().mapToDouble(CableBlockEntity::getEnergy).sum();
@ -229,66 +266,15 @@ public class CableBlockEntity extends BlockEntity
return new ItemStack(getCableType().block);
}
public double getEnergy() {
return getStored(EnergySide.UNKNOWN);
}
public void setEnergy(double energy) {
setStored(energy);
}
public double useEnergy(double energyOut, boolean simulate) {
if (energyOut > energy) {
energyOut = energy;
}
if (!simulate) {
setEnergy(energy - 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) {
if (sendingFace.contains(direction)) {
return false;
}
return getMaxStoredPower() != getEnergy();
}
public boolean isFull(){
return getMaxStoredPower() == getEnergy();
// EnergyStorage
@Override
public double getStored(EnergySide face) {
return energy;
}
@Override
public double getMaxInput(EnergySide side) {
if(!canAcceptEnergy(side)) {
return 0;
}
return getCableType().transferRate;
}
@Override
public double getMaxOutput(EnergySide side) {
return getCableType().transferRate;
public void setStored(double amount) {
this.energy = amount;
}
@Override
@ -302,24 +288,15 @@ public class CableBlockEntity extends BlockEntity
}
@Override
public double getStored(EnergySide face) {
return energy;
public double getMaxInput(EnergySide side) {
if (!canAcceptEnergy(side)) {
return 0;
}
return getCableType().transferRate;
}
@Override
public void setStored(double amount) {
this.energy = amount;
public double getMaxOutput(EnergySide side) {
return getCableType().transferRate;
}
public BlockState getCover() {
return cover;
}
public void setCover(BlockState cover) {
this.cover = cover;
if (!world.isClient) {
NetworkManager.sendToTracking(ClientBoundPackets.createCustomDescriptionPacket(this), this);
}
}
}