Updated cable to push energy to other cables. Closes #1296

This commit is contained in:
drcrazy 2017-10-12 02:05:20 +03:00
parent f5e73d23fa
commit b80bf1240d

View file

@ -25,6 +25,10 @@
package techreborn.tiles.cable; package techreborn.tiles.cable;
import java.util.ArrayList; 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.tileentity.TileEntity;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable; import net.minecraft.util.ITickable;
@ -64,7 +68,7 @@ public class TileCable extends TileEntity implements ITickable, IEnergyStorage {
continue; continue;
} else if (tile instanceof TileCable) { } else if (tile instanceof TileCable) {
TileCable cable = (TileCable) tile; TileCable cable = (TileCable) tile;
if (power < cable.power) { if (power > cable.power) {
cables.add(cable); cables.add(cable);
} }
} else if (tile.hasCapability(CapabilityEnergy.ENERGY, face.getOpposite())) { } else if (tile.hasCapability(CapabilityEnergy.ENERGY, face.getOpposite())) {
@ -77,17 +81,19 @@ public class TileCable extends TileEntity implements ITickable, IEnergyStorage {
} }
if (cables.size() > 0){ if (cables.size() > 0){
int drain = maxPower - power; int drain = Math.min(power, maxPower);
int energyShare = drain / cables.size(); int energyShare = drain / cables.size();
int remainingEnergy = drain; int remainingEnergy = drain;
for (TileCable cable : cables){ if (energyShare > 0) {
int move = cable.extractEnergy(Math.min(energyShare, remainingEnergy), false); for (TileCable cable : cables) {
if (move > 0){ int move = cable.receiveEnergy(Math.min(energyShare, remainingEnergy), false);
remainingEnergy -= move; if (move > 0) {
remainingEnergy -= move;
}
} }
extractEnergy(drain - remainingEnergy, false);
} }
receiveEnergy(drain - remainingEnergy, false);
} }
if (acceptors.size() > 0){ if (acceptors.size() > 0){
@ -102,8 +108,8 @@ public class TileCable extends TileEntity implements ITickable, IEnergyStorage {
remainingEnergy -= move; 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); 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;
}
} }