Cleanup of CableBlockEntity
This commit is contained in:
parent
5df411f26d
commit
2daf57e19c
1 changed files with 102 additions and 125 deletions
|
@ -63,11 +63,11 @@ import java.util.List;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class CableBlockEntity extends BlockEntity
|
public class CableBlockEntity extends BlockEntity
|
||||||
implements Tickable, IListInfoProvider, IToolDrop, EnergyStorage {
|
implements Tickable, IListInfoProvider, IToolDrop, EnergyStorage {
|
||||||
|
|
||||||
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() {
|
||||||
|
@ -87,28 +87,54 @@ public class CableBlockEntity extends BlockEntity
|
||||||
return TRContent.Cables.COPPER;
|
return TRContent.Cables.COPPER;
|
||||||
}
|
}
|
||||||
Block block = world.getBlockState(pos).getBlock();
|
Block block = world.getBlockState(pos).getBlock();
|
||||||
if(block instanceof CableBlock){
|
if (block instanceof CableBlock) {
|
||||||
return ((CableBlock) block).type;
|
return ((CableBlock) block).type;
|
||||||
}
|
}
|
||||||
//Something has gone wrong if this happens
|
//Something has gone wrong if this happens
|
||||||
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BlockEntityUpdateS2CPacket toUpdatePacket() {
|
public BlockEntityUpdateS2CPacket toUpdatePacket() {
|
||||||
CompoundTag nbtTag = new CompoundTag();
|
CompoundTag nbtTag = new CompoundTag();
|
||||||
toTag(nbtTag);
|
toTag(nbtTag);
|
||||||
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);
|
||||||
if (compound.contains("energy")) {
|
if (compound.contains("energy")) {
|
||||||
energy = compound.getDouble("energy");
|
energy = compound.getDouble("energy");
|
||||||
|
@ -120,8 +146,8 @@ public class CableBlockEntity extends BlockEntity
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompoundTag toTag(CompoundTag compound) {
|
public CompoundTag toTag(CompoundTag compound) {
|
||||||
super.toTag(compound);
|
super.toTag(compound);
|
||||||
compound.putDouble("energy", energy);
|
compound.putDouble("energy", energy);
|
||||||
if (cover != null) {
|
if (cover != null) {
|
||||||
|
@ -130,15 +156,19 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
sendingFace.clear();
|
sendingFace.clear();
|
||||||
|
|
||||||
if(getEnergy() == 0){
|
if (getEnergy() == 0) {
|
||||||
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) {
|
||||||
if (blockEntity instanceof CableBlockEntity ) {
|
continue;
|
||||||
CableBlockEntity cableBlockEntity = (CableBlockEntity)blockEntity;
|
}
|
||||||
|
|
||||||
if(cableBlockEntity.getTier() == this.getTier()) {
|
if (!Energy.valid(blockEntity)) {
|
||||||
// Only need ones with energy stores less than ours
|
continue;
|
||||||
if (cableBlockEntity.getEnergy() < this.getEnergy()) {
|
}
|
||||||
cables.add(cableBlockEntity);
|
|
||||||
}
|
|
||||||
|
|
||||||
continue;
|
if (blockEntity instanceof CableBlockEntity) {
|
||||||
}else if(energy <= Energy.of(blockEntity).side(face).getEnergy()){
|
CableBlockEntity cableBlockEntity = (CableBlockEntity) blockEntity;
|
||||||
continue;
|
|
||||||
}
|
if (cableBlockEntity.getTier() != this.getTier()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// Only need ones with energy stores less than ours
|
||||||
|
if (cableBlockEntity.getEnergy() < this.getEnergy()) {
|
||||||
|
cables.add(cableBlockEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
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));
|
acceptors.add(Pair.of(blockEntity, face));
|
||||||
if (!sendingFace.contains(face)) {
|
if (!sendingFace.contains(side)) {
|
||||||
sendingFace.add(face);
|
sendingFace.add(side);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -177,16 +214,16 @@ public class CableBlockEntity extends BlockEntity
|
||||||
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)
|
// Distribute energy between cables (TODO DIRTY FIX, until we game network cables)
|
||||||
|
|
||||||
if(!cables.isEmpty()) {
|
if (!cables.isEmpty()) {
|
||||||
|
|
||||||
cables.add(this);
|
cables.add(this);
|
||||||
double energyTotal = cables.stream().mapToDouble(CableBlockEntity::getEnergy).sum();
|
double energyTotal = cables.stream().mapToDouble(CableBlockEntity::getEnergy).sum();
|
||||||
|
@ -196,26 +233,26 @@ public class CableBlockEntity extends BlockEntity
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IListInfoProvider
|
// IListInfoProvider
|
||||||
@Override
|
@Override
|
||||||
public void addInfo(List<Text> info, boolean isReal, boolean hasData) {
|
public void addInfo(List<Text> info, boolean isReal, boolean hasData) {
|
||||||
info.add(
|
info.add(
|
||||||
new TranslatableText("techreborn.tooltip.transferRate")
|
new TranslatableText("techreborn.tooltip.transferRate")
|
||||||
.formatted(Formatting.GRAY)
|
.formatted(Formatting.GRAY)
|
||||||
.append(": ")
|
.append(": ")
|
||||||
.append(PowerSystem.getLocaliszedPowerFormatted(getCableType().transferRate))
|
.append(PowerSystem.getLocaliszedPowerFormatted(getCableType().transferRate))
|
||||||
.formatted(Formatting.GOLD)
|
.formatted(Formatting.GOLD)
|
||||||
.append("/t")
|
.append("/t")
|
||||||
);
|
);
|
||||||
|
|
||||||
info.add(
|
info.add(
|
||||||
new TranslatableText("techreborn.tooltip.tier")
|
new TranslatableText("techreborn.tooltip.tier")
|
||||||
.formatted(Formatting.GRAY)
|
.formatted(Formatting.GRAY)
|
||||||
.append(": ")
|
.append(": ")
|
||||||
.append(
|
.append(
|
||||||
new LiteralText(StringUtils.toFirstCapitalAllLowercase(getCableType().tier.toString()))
|
new LiteralText(StringUtils.toFirstCapitalAllLowercase(getCableType().tier.toString()))
|
||||||
.formatted(Formatting.GOLD)
|
.formatted(Formatting.GOLD)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!getCableType().canKill) {
|
if (!getCableType().canKill) {
|
||||||
|
@ -229,66 +266,15 @@ 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) {
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double getMaxInput(EnergySide side) {
|
public void setStored(double amount) {
|
||||||
if(!canAcceptEnergy(side)) {
|
this.energy = amount;
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return getCableType().transferRate;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public double getMaxOutput(EnergySide side) {
|
|
||||||
return getCableType().transferRate;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -302,24 +288,15 @@ public class CableBlockEntity extends BlockEntity
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double getStored(EnergySide face) {
|
public double getMaxInput(EnergySide side) {
|
||||||
return energy;
|
if (!canAcceptEnergy(side)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return getCableType().transferRate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setStored(double amount) {
|
public double getMaxOutput(EnergySide side) {
|
||||||
this.energy = amount;
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue