use of com.enderio.core.common.fluid.IFluidWrapper in project EnderIO by SleepyTrousers.
the class ItemXpTransfer method tranferFromPlayerToBlock.
public static boolean tranferFromPlayerToBlock(@Nonnull EntityPlayer player, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull EnumFacing side) {
if (player.experienceTotal <= 0) {
return false;
}
IFluidWrapper wrapper = FluidWrapper.wrap(world, pos, side);
if (wrapper != null) {
int fluidVolume = XpUtil.experienceToLiquid(XpUtil.getPlayerXP(player));
FluidStack fs = new FluidStack(Fluids.XP_JUICE.getFluid(), fluidVolume);
int takenVolume = wrapper.fill(fs);
if (takenVolume > 0) {
int xpToTake = XpUtil.liquidToExperience(takenVolume);
XpUtil.addPlayerXP(player, -xpToTake);
return true;
}
}
return false;
}
use of com.enderio.core.common.fluid.IFluidWrapper in project EnderIO by SleepyTrousers.
the class LiquidConduitNetwork method flowFrom.
private void flowFrom(@Nonnull LiquidConduit con, List<FlowAction> actions, int pushPoken) {
ConduitTank tank = con.getTank();
int totalAmount = tank.getFluidAmount();
if (totalAmount <= 0) {
return;
}
int maxFlowVolume = 20;
// First flow all we can down, then balance the rest
if (con.getConduitConnections().contains(EnumFacing.DOWN)) {
BlockPos pos = con.getBundle().getLocation().offset(EnumFacing.DOWN);
ILiquidConduit dc = ConduitUtil.getConduit(con.getBundle().getEntity().getWorld(), pos.getX(), pos.getY(), pos.getZ(), ILiquidConduit.class);
if (dc instanceof LiquidConduit) {
LiquidConduit downCon = (LiquidConduit) dc;
int filled = downCon.fill(EnumFacing.UP, tank.getFluid().copy(), false, false, pushPoken);
int actual = filled;
actual = Math.min(actual, tank.getFluidAmount());
actual = Math.min(actual, downCon.getTank().getAvailableSpace());
tank.addAmount(-actual);
downCon.getTank().addAmount(actual);
}
}
totalAmount = tank.getFluidAmount();
if (totalAmount <= 0) {
return;
}
FluidStack available = tank.getFluid();
int totalRequested = 0;
int numRequests = 0;
// Then to external connections
for (EnumFacing dir : con.getExternalConnections()) {
if (con.canOutputToDir(dir)) {
IFluidWrapper extCon = con.getExternalHandler(dir);
if (extCon != null) {
int amount = extCon.offer(available.copy());
if (amount > 0) {
totalRequested += amount;
numRequests++;
}
}
}
}
if (numRequests > 0) {
int amountPerRequest = Math.min(totalAmount, totalRequested) / numRequests;
amountPerRequest = Math.min(maxFlowVolume, amountPerRequest);
FluidStack requestSource = available.copy();
requestSource.amount = amountPerRequest;
for (EnumFacing dir : con.getExternalConnections()) {
if (con.canOutputToDir(dir)) {
IFluidWrapper extCon = con.getExternalHandler(dir);
if (extCon != null) {
int amount = extCon.fill(requestSource.copy());
if (amount > 0) {
outputedToExternal(amount);
tank.addAmount(-amount);
}
}
}
}
}
totalAmount = tank.getFluidAmount();
if (totalAmount <= 0) {
return;
}
int totalCapacity = tank.getCapacity();
BlockPos pos = con.getBundle().getLocation();
Collection<ILiquidConduit> connections = ConduitUtil.getConnectedConduits(con.getBundle().getEntity().getWorld(), pos.getX(), pos.getY(), pos.getZ(), ILiquidConduit.class);
for (ILiquidConduit n : connections) {
LiquidConduit neighbour = (LiquidConduit) n;
if (canFlowTo(con, neighbour)) {
// can only flow within same network
totalAmount += neighbour.getTank().getFluidAmount();
totalCapacity += neighbour.getTank().getCapacity();
}
}
float targetRatio = (float) totalAmount / totalCapacity;
int flowVolume = (int) Math.floor((targetRatio - tank.getFilledRatio()) * tank.getCapacity());
flowVolume = Math.min(maxFlowVolume, flowVolume);
if (Math.abs(flowVolume) < 2) {
// dont bother with transfers of less than a thousands of a bucket
return;
}
for (ILiquidConduit n : connections) {
LiquidConduit neigbour = (LiquidConduit) n;
if (canFlowTo(con, neigbour)) {
// can only flow within same network
flowVolume = (int) Math.floor((targetRatio - neigbour.getTank().getFilledRatio()) * neigbour.getTank().getCapacity());
if (flowVolume != 0) {
actions.add(new FlowAction(con, neigbour, flowVolume));
}
}
}
}
use of com.enderio.core.common.fluid.IFluidWrapper in project EnderIO by SleepyTrousers.
the class LiquidConduitNetwork method doFlow.
private boolean doFlow() {
int pushToken = getNextPushToken();
List<FlowAction> actions = new ArrayList<FlowAction>();
for (int i = 0; i < Math.min(maxFlowsPerTick, getConduits().size()); i++) {
if (lastFlowIndex >= getConduits().size()) {
lastFlowIndex = 0;
}
flowFrom(getConduits().get(lastFlowIndex), actions, pushToken);
++lastFlowIndex;
}
for (FlowAction action : actions) {
action.apply();
}
boolean result = !actions.isEmpty();
// Flush any tanks with a tiny bit left
List<LiquidConduit> toEmpty = new ArrayList<LiquidConduit>();
for (LiquidConduit con : getConduits()) {
if (con != null && con.getTank().getFluidAmount() < 10) {
toEmpty.add(con);
} else {
// some of the conduits have fluid left in them so don't do the final drain yet
return result;
}
}
if (toEmpty.isEmpty()) {
return result;
}
List<LocatedFluidHandler> externals = new ArrayList<LocatedFluidHandler>();
for (AbstractTankConduit con : getConduits()) {
Set<EnumFacing> extCons = con.getExternalConnections();
for (EnumFacing dir : extCons) {
if (con.canOutputToDir(dir)) {
IFluidWrapper externalTank = con.getExternalHandler(dir);
if (externalTank != null) {
externals.add(new LocatedFluidHandler(externalTank, con.getBundle().getLocation().offset(dir), dir.getOpposite()));
}
}
}
}
if (externals.isEmpty()) {
return result;
}
for (LiquidConduit con : toEmpty) {
drainConduitToNearestExternal(con, externals);
}
return result;
}
use of com.enderio.core.common.fluid.IFluidWrapper in project EnderIO by SleepyTrousers.
the class AdvancedLiquidConduitNetwork method tickEnd.
@Override
public void tickEnd(ServerTickEvent event, @Nullable Profiler profiler) {
if (liquidType == null || outputs.isEmpty() || !tank.containsValidLiquid() || tank.isEmpty()) {
Prof.start(profiler, "updateActiveState");
updateActiveState();
Prof.stop(profiler);
return;
}
if (outputIterator == null || !outputIterator.hasNext()) {
outputIterator = outputs.iterator();
}
Prof.start(profiler, "updateActiveState");
updateActiveState();
Prof.next(profiler, "pushFluid");
int numVisited = 0;
while (!tank.isEmpty() && numVisited < outputs.size()) {
if (!outputIterator.hasNext()) {
outputIterator = outputs.iterator();
}
LiquidOutput output = outputIterator.next();
if (output != null) {
Prof.start(profiler, "otherMod_getTankContainer");
IFluidWrapper cont = getTankContainer(output);
Prof.stop(profiler);
if (cont != null) {
FluidStack offer = tank.getFluid().copy();
Prof.start(profiler, "otherMod_fill");
int filled = cont.fill(offer);
Prof.stop(profiler);
if (filled > 0) {
tank.addAmount(-filled);
}
}
}
numVisited++;
}
Prof.stop(profiler);
}
use of com.enderio.core.common.fluid.IFluidWrapper in project EnderIO by SleepyTrousers.
the class AdvancedLiquidConduitNetwork method extractFrom.
public boolean extractFrom(@Nonnull AdvancedLiquidConduit advancedLiquidConduit, @Nonnull EnumFacing dir, int maxExtractPerTick) {
if (tank.isFull()) {
return false;
}
IFluidWrapper extTank = getTankContainer(advancedLiquidConduit, dir);
if (extTank != null) {
int maxExtract = Math.min(maxExtractPerTick, tank.getAvailableSpace());
if (liquidType == null || !tank.containsValidLiquid()) {
FluidStack available = extTank.getAvailableFluid();
if (available == null || available.amount <= 0) {
return false;
}
setFluidType(available);
}
FluidStack couldDrain = liquidType.copy();
couldDrain.amount = maxExtract;
FluidStack drained = extTank.drain(couldDrain);
if (drained == null || drained.amount == 0) {
return false;
} else if (drained.isFluidEqual(getFluidType())) {
tank.addAmount(drained.amount);
} else {
extTank.fill(drained);
}
return true;
}
return false;
}
Aggregations