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 double energy = 0;
private TRContent.Cables cableType = null; private TRContent.Cables cableType = null;
private ArrayList<Direction> sendingFace = new ArrayList<>(); private ArrayList<EnergySide> sendingFace = new ArrayList<>();
private BlockState cover = null; private BlockState cover = null;
public CableBlockEntity() { public CableBlockEntity() {
@ -94,6 +94,33 @@ public class CableBlockEntity extends BlockEntity
return TRContent.Cables.COPPER; 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 @Override
public CompoundTag toInitialChunkDataTag() { public CompoundTag toInitialChunkDataTag() {
return toTag(new CompoundTag()); return toTag(new CompoundTag());
@ -106,7 +133,6 @@ public class CableBlockEntity extends BlockEntity
return new BlockEntityUpdateS2CPacket(getPos(), 1, nbtTag); return new BlockEntityUpdateS2CPacket(getPos(), 1, nbtTag);
} }
@Override @Override
public void fromTag(BlockState blockState, CompoundTag compound) { public void fromTag(BlockState blockState, CompoundTag compound) {
super.fromTag(blockState, compound); super.fromTag(blockState, compound);
@ -130,8 +156,12 @@ public class CableBlockEntity extends BlockEntity
return compound; return compound;
} }
// Tickable
@Override @Override
public void tick() { public void tick() {
if (world == null) {
return;
}
if (world.isClient) { if (world.isClient) {
return; return;
} }
@ -148,26 +178,33 @@ public class CableBlockEntity extends BlockEntity
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) {
continue;
}
if (!Energy.valid(blockEntity)) {
continue;
}
if (blockEntity instanceof CableBlockEntity) { if (blockEntity instanceof CableBlockEntity) {
CableBlockEntity cableBlockEntity = (CableBlockEntity) blockEntity; CableBlockEntity cableBlockEntity = (CableBlockEntity) blockEntity;
if(cableBlockEntity.getTier() == this.getTier()) { if (cableBlockEntity.getTier() != this.getTier()) {
continue;
}
// Only need ones with energy stores less than ours // Only need ones with energy stores less than ours
if (cableBlockEntity.getEnergy() < this.getEnergy()) { if (cableBlockEntity.getEnergy() < this.getEnergy()) {
cables.add(cableBlockEntity); cables.add(cableBlockEntity);
} }
continue;
}else if(energy <= Energy.of(blockEntity).side(face).getEnergy()){ } else {
continue; EnergySide side = EnergySide.fromMinecraft(face);
}
}
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(side)) {
sendingFace.add(face); sendingFace.add(side);
} }
} }
} }
@ -229,53 +266,25 @@ public class CableBlockEntity extends BlockEntity
return new ItemStack(getCableType().block); return new ItemStack(getCableType().block);
} }
// EnergyStorage
public double getEnergy() { @Override
return getStored(EnergySide.UNKNOWN); public double getStored(EnergySide face) {
return energy;
} }
public void setEnergy(double energy) { @Override
setStored(energy); public void setStored(double amount) {
this.energy = amount;
} }
public double useEnergy(double energyOut, boolean simulate) { @Override
if (energyOut > energy) { public double getMaxStoredPower() {
energyOut = energy; return getCableType().transferRate * 4;
}
if (!simulate) {
setEnergy(energy - energyOut);
}
return energyOut;
} }
public double addEnergy(double energyIn) { @Override
if(this.isFull()){ public EnergyTier getTier() {
return energyIn; return PowerAcceptorBlockEntity.getTier(getCableType().transferRate);
}
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();
} }
@Override @Override
@ -290,36 +299,4 @@ public class CableBlockEntity extends BlockEntity
public double getMaxOutput(EnergySide side) { public double getMaxOutput(EnergySide side) {
return getCableType().transferRate; return getCableType().transferRate;
} }
@Override
public double getMaxStoredPower() {
return getCableType().transferRate * 4;
}
@Override
public EnergyTier getTier() {
return PowerAcceptorBlockEntity.getTier(getCableType().transferRate);
}
@Override
public double getStored(EnergySide face) {
return energy;
}
@Override
public void setStored(double amount) {
this.energy = amount;
}
public BlockState getCover() {
return cover;
}
public void setCover(BlockState cover) {
this.cover = cover;
if (!world.isClient) {
NetworkManager.sendToTracking(ClientBoundPackets.createCustomDescriptionPacket(this), this);
}
}
} }