Updated cable to push energy to other cables. Closes #1296
This commit is contained in:
parent
f5e73d23fa
commit
b80bf1240d
1 changed files with 59 additions and 8 deletions
|
@ -25,6 +25,10 @@
|
|||
package techreborn.tiles.cable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.network.NetworkManager;
|
||||
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.ITickable;
|
||||
|
@ -64,7 +68,7 @@ public class TileCable extends TileEntity implements ITickable, IEnergyStorage {
|
|||
continue;
|
||||
} else if (tile instanceof TileCable) {
|
||||
TileCable cable = (TileCable) tile;
|
||||
if (power < cable.power) {
|
||||
if (power > cable.power) {
|
||||
cables.add(cable);
|
||||
}
|
||||
} else if (tile.hasCapability(CapabilityEnergy.ENERGY, face.getOpposite())) {
|
||||
|
@ -77,17 +81,19 @@ public class TileCable extends TileEntity implements ITickable, IEnergyStorage {
|
|||
}
|
||||
|
||||
if (cables.size() > 0){
|
||||
int drain = maxPower - power;
|
||||
int drain = Math.min(power, maxPower);
|
||||
int energyShare = drain / cables.size();
|
||||
int remainingEnergy = drain;
|
||||
|
||||
for (TileCable cable : cables){
|
||||
int move = cable.extractEnergy(Math.min(energyShare, remainingEnergy), false);
|
||||
if (move > 0){
|
||||
remainingEnergy -= move;
|
||||
if (energyShare > 0) {
|
||||
for (TileCable cable : cables) {
|
||||
int move = cable.receiveEnergy(Math.min(energyShare, remainingEnergy), false);
|
||||
if (move > 0) {
|
||||
remainingEnergy -= move;
|
||||
}
|
||||
}
|
||||
extractEnergy(drain - remainingEnergy, false);
|
||||
}
|
||||
receiveEnergy(drain - remainingEnergy, false);
|
||||
}
|
||||
|
||||
if (acceptors.size() > 0){
|
||||
|
@ -102,8 +108,8 @@ public class TileCable extends TileEntity implements ITickable, IEnergyStorage {
|
|||
remainingEnergy -= move;
|
||||
}
|
||||
}
|
||||
extractEnergy(drain - remainingEnergy, false);
|
||||
}
|
||||
extractEnergy(drain - remainingEnergy, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -172,4 +178,49 @@ public class TileCable extends TileEntity implements ITickable, IEnergyStorage {
|
|||
}
|
||||
return super.getCapability(capability, facing);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NBTTagCompound getUpdateTag() {
|
||||
// getUpdateTag() is called whenever the chunkdata is sent to the
|
||||
// client. In contrast getUpdatePacket() is called when the tile entity
|
||||
// itself wants to sync to the client. In many cases you want to send
|
||||
// over the same information in getUpdateTag() as in getUpdatePacket().
|
||||
return writeToNBT(new NBTTagCompound());
|
||||
}
|
||||
|
||||
@Override
|
||||
public SPacketUpdateTileEntity getUpdatePacket() {
|
||||
// Prepare a packet for syncing our TE to the client. Since we only have to sync the stack
|
||||
// and that's all we have we just write our entire NBT here. If you have a complex
|
||||
// tile entity that doesn't need to have all information on the client you can write
|
||||
// a more optimal NBT here.
|
||||
NBTTagCompound nbtTag = new NBTTagCompound();
|
||||
this.writeToNBT(nbtTag);
|
||||
return new SPacketUpdateTileEntity(getPos(), 1, nbtTag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) {
|
||||
// Here we get the packet from the server and read it into our client side tile entity
|
||||
this.readFromNBT(packet.getNbtCompound());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound compound) {
|
||||
super.readFromNBT(compound);
|
||||
if (compound.hasKey("TileCable")) {
|
||||
power = compound.getCompoundTag("TileCable").getInteger("power");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
|
||||
super.writeToNBT(compound);
|
||||
if (power > 0) {
|
||||
NBTTagCompound data = new NBTTagCompound();
|
||||
data.setInteger("power", getEnergyStored());
|
||||
compound.setTag("TileCable", data);
|
||||
}
|
||||
return compound;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue