Cables are working

This commit is contained in:
drcrazy 2019-07-26 02:37:02 +03:00
parent 0dc2636046
commit 316def3086
2 changed files with 128 additions and 43 deletions

View file

@ -37,6 +37,8 @@ import net.minecraft.util.Tickable;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
import reborncore.api.IListInfoProvider; import reborncore.api.IListInfoProvider;
import reborncore.api.IToolDrop; import reborncore.api.IToolDrop;
import reborncore.api.power.EnergyBlockEntity;
import reborncore.api.power.EnumPowerTier;
import reborncore.common.RebornCoreConfig; import reborncore.common.RebornCoreConfig;
import reborncore.common.powerSystem.PowerSystem; import reborncore.common.powerSystem.PowerSystem;
import reborncore.common.util.StringUtils; import reborncore.common.util.StringUtils;
@ -45,6 +47,7 @@ import techreborn.init.TRContent;
import techreborn.init.TRBlockEntities; import techreborn.init.TRBlockEntities;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
@ -52,9 +55,9 @@ import java.util.List;
*/ */
public class CableBlockEntity extends BlockEntity public class CableBlockEntity extends BlockEntity
implements Tickable, IListInfoProvider, IToolDrop { implements Tickable, IListInfoProvider, IToolDrop, EnergyBlockEntity {
public int power = 0; private double energy = 0;
private int transferRate = 0; private int transferRate = 0;
private TRContent.Cables cableType = null; private TRContent.Cables cableType = null;
private ArrayList<Direction> sendingFace = new ArrayList<Direction>(); private ArrayList<Direction> sendingFace = new ArrayList<Direction>();
@ -90,14 +93,14 @@ public class CableBlockEntity extends BlockEntity
public void fromTag(CompoundTag compound) { public void fromTag(CompoundTag compound) {
super.fromTag(compound); super.fromTag(compound);
if (compound.containsKey("CableBlockEntity")) { if (compound.containsKey("CableBlockEntity")) {
power = compound.getCompound("CableBlockEntity").getInt("power"); energy = compound.getCompound("CableBlockEntity").getInt("power");
} }
} }
@Override @Override
public CompoundTag toTag(CompoundTag compound) { public CompoundTag toTag(CompoundTag compound) {
super.toTag(compound); super.toTag(compound);
if (power > 0) { if (energy > 0) {
CompoundTag data = new CompoundTag(); CompoundTag data = new CompoundTag();
compound.put("CableBlockEntity", data); compound.put("CableBlockEntity", data);
} }
@ -110,51 +113,49 @@ public class CableBlockEntity extends BlockEntity
if (world.isClient) { if (world.isClient) {
return; return;
} }
if (cableType == null ){ if (cableType == null) {
cableType = getCableType(); cableType = getCableType();
transferRate = cableType.transferRate * RebornCoreConfig.euPerFU; transferRate = cableType.transferRate * RebornCoreConfig.euPerFU;
} }
ticksSinceLastChange++; ticksSinceLastChange++;
if (ticksSinceLastChange >= 10) { if (ticksSinceLastChange >= 20) {
sendingFace.clear(); sendingFace.clear();
ticksSinceLastChange = 0; ticksSinceLastChange = 0;
} }
//TODO needs a full recode to not use a specific power net // TODO needs a full recode to not use a specific power net
// ArrayList<IEnergyStorage> acceptors = new ArrayList<IEnergyStorage>(); ArrayList<EnergyBlockEntity> acceptors = new ArrayList<EnergyBlockEntity>();
// 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) { if (blockEntity == null) {
// continue; continue;
// } else if (blockEntity instanceof TileCable) { } else if (blockEntity instanceof EnergyBlockEntity) {
// TileCable cable = (TileCable) blockEntity; EnergyBlockEntity acceptor = (EnergyBlockEntity) blockEntity;
// if (power > cable.power && cable.canReceiveFromFace(face.getOpposite())) { if (energy <= acceptor.getEnergy() || !acceptor.canAcceptEnergy(face.getOpposite())) {
// acceptors.add((IEnergyStorage) blockEntity); continue;
// if (!sendingFace.contains(face)) { }
// sendingFace.add(face); acceptors.add(acceptor);
// } if (!sendingFace.contains(face)) {
// } sendingFace.add(face);
// } else if (blockEntity.getCapability(CapabilityEnergy.ENERGY, face.getOpposite()).isPresent()) { }
// IEnergyStorage energyTile = blockEntity.getCapability(CapabilityEnergy.ENERGY, face.getOpposite()).orElse(null); }
// if (energyTile != null && energyTile.canReceive()) { }
// acceptors.add(energyTile);
// } if (acceptors.size() == 0) {
// } return;
// } }
// Collections.shuffle(acceptors);
// if (acceptors.size() > 0 ) { for (EnergyBlockEntity blockEntity : acceptors) {
// for (IEnergyStorage blockEntity : acceptors) { double drain = Math.min(energy, transferRate);
// int drain = Math.min(power, transferRate); if (drain > 0 && blockEntity.addEnergy(drain, true) > 0) {
// if (drain > 0 && blockEntity.receiveEnergy(drain, true) > 0) { double move = blockEntity.addEnergy(drain, false);
// int move = blockEntity.receiveEnergy(drain, false); useEnergy(move, false);
// extractEnergy(move, false); }
// } }
// }
// }
} }
// IListInfoProvider // IListInfoProvider
@ -174,4 +175,88 @@ public class CableBlockEntity extends BlockEntity
public ItemStack getToolDrop(PlayerEntity playerIn) { public ItemStack getToolDrop(PlayerEntity playerIn) {
return new ItemStack(getCableType().block); return new ItemStack(getCableType().block);
} }
@Override
public double getEnergy() {
return energy;
}
@Override
public void setEnergy(double energy) {
this.energy = energy;
}
@Override
public double getMaxPower() {
return transferRate * 4;
}
@Override
public boolean canAddEnergy(double energyIn) {
return getEnergy() + energyIn <= getMaxPower();
}
@Override
public double addEnergy(double energy) {
return addEnergy(energy, false);
}
@Override
public double addEnergy(double energyIn, boolean simulate) {
double energyReceived = Math.min(getMaxPower(), Math.min(getMaxPower() - getEnergy(), energyIn));
if (!simulate) {
setEnergy(getEnergy() + energyReceived);
}
return energyReceived;
}
@Override
public boolean canUseEnergy(double energy) {
return this.energy >= energy;
}
@Override
public double useEnergy(double energy) {
return useEnergy(energy, false);
}
@Override
public double useEnergy(double energyOut, boolean simulate) {
if (energyOut > energy) {
energyOut = energy;
}
if (!simulate) {
setEnergy(energy - energyOut);
}
return energyOut;
}
@Override
public boolean canAcceptEnergy(Direction direction) {
if (sendingFace.contains(direction)) {
return false;
}
return getMaxPower() != getEnergy();
}
@Override
public boolean canProvideEnergy(Direction direction) {
return true;
}
@Override
public double getMaxOutput() {
return transferRate;
}
@Override
public double getMaxInput() {
return transferRate;
}
@Override
public EnumPowerTier getTier() {
return EnumPowerTier.getTier(cableType.transferRate);
}
} }

View file

@ -212,7 +212,7 @@ public class BlockCable extends BlockWithEntity {
} }
CableBlockEntity blockEntityCable = (CableBlockEntity) blockEntity; CableBlockEntity blockEntityCable = (CableBlockEntity) blockEntity;
if (blockEntityCable.power <= 0) { if (blockEntityCable.getEnergy() <= 0) {
return; return;
} }