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() {
@ -94,6 +94,33 @@ public class CableBlockEntity extends BlockEntity
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,8 +156,12 @@ public class CableBlockEntity extends BlockEntity
return compound;
}
// Tickable
@Override
public void tick() {
if (world == null) {
return;
}
if (world.isClient) {
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 == null) {
continue;
}
if (!Energy.valid(blockEntity)) {
continue;
}
if (blockEntity instanceof CableBlockEntity) {
CableBlockEntity cableBlockEntity = (CableBlockEntity) blockEntity;
if(cableBlockEntity.getTier() == this.getTier()) {
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;
}
}
} 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);
}
}
}
@ -229,53 +266,25 @@ public class CableBlockEntity extends BlockEntity
return new ItemStack(getCableType().block);
}
public double getEnergy() {
return getStored(EnergySide.UNKNOWN);
// EnergyStorage
@Override
public double getStored(EnergySide face) {
return energy;
}
public void setEnergy(double energy) {
setStored(energy);
@Override
public void setStored(double amount) {
this.energy = amount;
}
public double useEnergy(double energyOut, boolean simulate) {
if (energyOut > energy) {
energyOut = energy;
}
if (!simulate) {
setEnergy(energy - energyOut);
}
return energyOut;
@Override
public double getMaxStoredPower() {
return getCableType().transferRate * 4;
}
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();
@Override
public EnergyTier getTier() {
return PowerAcceptorBlockEntity.getTier(getCableType().transferRate);
}
@Override
@ -290,36 +299,4 @@ public class CableBlockEntity extends BlockEntity
public double getMaxOutput(EnergySide side) {
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);
}
}
}