use of blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.Connection in project ImmersiveEngineering by BluSunrize.
the class ItemSkyhook method onItemRightClick.
@Override
public ActionResult<ItemStack> onItemRightClick(ItemStack stack, World world, EntityPlayer player, EnumHand hand) {
TileEntity connector = null;
double lastDist = 0;
Connection line = null;
double py = player.posY + player.getEyeHeight();
for (int xx = -2; xx <= 2; xx++) for (int zz = -2; zz <= 2; zz++) for (int yy = 0; yy <= 3; yy++) {
TileEntity tile = world.getTileEntity(new BlockPos(player.posX + xx, py + yy, player.posZ + zz));
if (tile != null) {
Connection con = SkylineHelper.getTargetConnection(world, tile.getPos(), player, null);
if (con != null) {
double d = tile.getDistanceSq(player.posX, py, player.posZ);
if (connector == null || d < lastDist) {
connector = tile;
lastDist = d;
line = con;
}
}
}
}
if (line != null && connector != null) {
SkylineHelper.spawnHook(player, connector, line);
player.setActiveHand(hand);
return new ActionResult(EnumActionResult.SUCCESS, stack);
}
return new ActionResult(EnumActionResult.PASS, stack);
}
use of blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.Connection in project ImmersiveEngineering by BluSunrize.
the class SkylineHelper method getTargetConnection.
public static Connection getTargetConnection(World world, BlockPos pos, EntityLivingBase living, Connection invalidCon) {
if (!(world.getTileEntity(pos) instanceof IImmersiveConnectable))
return null;
Set<Connection> outputs = ImmersiveNetHandler.INSTANCE.getConnections(world, pos);
if (outputs != null && outputs.size() > 0) {
Vec3d vec = living.getLookVec();
vec = vec.normalize();
Connection line = null;
for (Connection c : outputs) if (c != null && !c.hasSameConnectors(invalidCon)) {
if (line == null)
line = c;
else {
Vec3d lineVec = new Vec3d(line.end.getX() - line.start.getX(), line.end.getY() - line.start.getY(), line.end.getZ() - line.start.getZ()).normalize();
Vec3d conVec = new Vec3d(c.end.getX() - c.start.getX(), c.end.getY() - c.start.getY(), c.end.getZ() - c.start.getZ()).normalize();
if (conVec.distanceTo(vec) < lineVec.distanceTo(vec))
line = c;
}
}
return line;
}
return null;
}
use of blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.Connection in project ImmersiveEngineering by BluSunrize.
the class TileEntityImmersiveConnectable method writeConnsToNBT.
private void writeConnsToNBT(NBTTagCompound nbt) {
if (worldObj != null && !worldObj.isRemote && nbt != null) {
NBTTagList connectionList = new NBTTagList();
Set<Connection> conL = ImmersiveNetHandler.INSTANCE.getConnections(worldObj, Utils.toCC(this));
if (conL != null)
for (Connection con : conL) connectionList.appendTag(con.writeToNBT());
nbt.setTag("connectionList", connectionList);
}
}
use of blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.Connection in project ImmersiveEngineering by BluSunrize.
the class TileEntityImmersiveConnectable method loadConnsFromNBT.
private void loadConnsFromNBT(NBTTagCompound nbt) {
if (worldObj != null && worldObj.isRemote && !Minecraft.getMinecraft().isSingleplayer() && nbt != null) {
NBTTagList connectionList = nbt.getTagList("connectionList", 10);
ImmersiveNetHandler.INSTANCE.clearConnectionsOriginatingFrom(Utils.toCC(this), worldObj);
for (int i = 0; i < connectionList.tagCount(); i++) {
NBTTagCompound conTag = connectionList.getCompoundTagAt(i);
Connection con = Connection.readFromNBT(conTag);
if (con != null) {
ImmersiveNetHandler.INSTANCE.addConnection(worldObj, Utils.toCC(this), con);
} else
IELogger.error("CLIENT read connection as null");
}
}
}
use of blusunrize.immersiveengineering.api.energy.wires.ImmersiveNetHandler.Connection in project ImmersiveEngineering by BluSunrize.
the class TileEntityConnectorLV method transferEnergy.
public int transferEnergy(int energy, boolean simulate, final int energyType) {
int received = 0;
if (!worldObj.isRemote) {
Set<AbstractConnection> outputs = ImmersiveNetHandler.INSTANCE.getIndirectEnergyConnections(Utils.toCC(this), worldObj);
int powerLeft = Math.min(Math.min(getMaxOutput(), getMaxInput()), energy);
final int powerForSort = powerLeft;
if (outputs.size() < 1)
return 0;
int sum = 0;
HashMap<AbstractConnection, Integer> powerSorting = new HashMap<AbstractConnection, Integer>();
for (AbstractConnection con : outputs) {
IImmersiveConnectable end = ApiUtils.toIIC(con.end, worldObj);
if (con.cableType != null && end != null) {
int atmOut = Math.min(powerForSort, con.cableType.getTransferRate());
int tempR = end.outputEnergy(atmOut, true, energyType);
if (tempR > 0) {
powerSorting.put(con, tempR);
sum += tempR;
}
}
}
if (sum > 0)
for (AbstractConnection con : powerSorting.keySet()) {
IImmersiveConnectable end = ApiUtils.toIIC(con.end, worldObj);
if (con.cableType != null && end != null) {
float prio = powerSorting.get(con) / (float) sum;
int output = (int) (powerForSort * prio);
int tempR = end.outputEnergy(Math.min(output, con.cableType.getTransferRate()), true, energyType);
int r = tempR;
int maxInput = getMaxInput();
tempR -= (int) Math.max(0, Math.floor(tempR * con.getPreciseLossRate(tempR, maxInput)));
end.outputEnergy(tempR, simulate, energyType);
HashSet<IImmersiveConnectable> passedConnectors = new HashSet<IImmersiveConnectable>();
float intermediaryLoss = 0;
for (Connection sub : con.subConnections) {
float length = sub.length / (float) sub.cableType.getMaxLength();
float baseLoss = (float) sub.cableType.getLossRatio();
float mod = (((maxInput - tempR) / (float) maxInput) / .25f) * .1f;
intermediaryLoss = MathHelper.clamp_float(intermediaryLoss + length * (baseLoss + baseLoss * mod), 0, 1);
int transferredPerCon = ImmersiveNetHandler.INSTANCE.getTransferedRates(worldObj.provider.getDimension()).containsKey(sub) ? ImmersiveNetHandler.INSTANCE.getTransferedRates(worldObj.provider.getDimension()).get(sub) : 0;
transferredPerCon += r;
if (!simulate) {
ImmersiveNetHandler.INSTANCE.getTransferedRates(worldObj.provider.getDimension()).put(sub, transferredPerCon);
IImmersiveConnectable subStart = ApiUtils.toIIC(sub.start, worldObj);
IImmersiveConnectable subEnd = ApiUtils.toIIC(sub.end, worldObj);
if (subStart != null && passedConnectors.add(subStart))
subStart.onEnergyPassthrough((int) (r - r * intermediaryLoss));
if (subEnd != null && passedConnectors.add(subEnd))
subEnd.onEnergyPassthrough((int) (r - r * intermediaryLoss));
}
}
received += r;
powerLeft -= r;
if (powerLeft <= 0)
break;
}
}
}
return received;
}
Aggregations