From 7f52097896f5e4bd75f08d0fd3a92d63f3b1d081 Mon Sep 17 00:00:00 2001 From: asiekierka Date: Mon, 6 Apr 2015 09:53:31 +0200 Subject: [PATCH] add weighted fluid sorting for diamond fluid pipes, mirroring diamond item pipes --- .../transport/PipeTransportFluids.java | 8 ++-- .../transport/pipes/PipeFluidsDiamond.java | 45 +++++++++---------- .../pipes/events/PipeEventFluid.java | 5 ++- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/common/buildcraft/transport/PipeTransportFluids.java b/common/buildcraft/transport/PipeTransportFluids.java index 3c6e8129..2f91b482 100644 --- a/common/buildcraft/transport/PipeTransportFluids.java +++ b/common/buildcraft/transport/PipeTransportFluids.java @@ -12,6 +12,8 @@ import java.util.ArrayList; import java.util.BitSet; import java.util.HashMap; import java.util.Map; +import com.google.common.collect.HashMultiset; +import com.google.common.collect.Multiset; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraftforge.common.util.ForgeDirection; @@ -408,16 +410,16 @@ public class PipeTransportFluids extends PipeTransport implements IFluidHandler FluidStack testStack = pushStack.copy(); testStack.amount = flowRate; // Move liquid from the center to the output sides - ArrayList realDirections = new ArrayList(); + Multiset realDirections = HashMultiset.create(6); for (ForgeDirection direction : directions) { if (transferState[direction.ordinal()] == TransferState.Output) { realDirections.add(direction); } } container.pipe.eventBus.handleEvent(PipeEventFluid.FindDest.class, new PipeEventFluid.FindDest(container.pipe, pushStack, realDirections)); - for (ForgeDirection direction : realDirections) { + for (ForgeDirection direction : realDirections.elementSet()) { int available = internalTanks[direction.ordinal()].fill(testStack, false); - int ammountToPush = (int) (available / (double) flowRate / outputCount * Math.min(flowRate, totalAvailable)); + int ammountToPush = (int) (available / (double) flowRate / realDirections.size() * Math.min(flowRate, totalAvailable) * realDirections.count(direction)); if (ammountToPush < 1) { ammountToPush++; } diff --git a/common/buildcraft/transport/pipes/PipeFluidsDiamond.java b/common/buildcraft/transport/pipes/PipeFluidsDiamond.java index 48aafc6d..0cf7a46a 100644 --- a/common/buildcraft/transport/pipes/PipeFluidsDiamond.java +++ b/common/buildcraft/transport/pipes/PipeFluidsDiamond.java @@ -9,7 +9,9 @@ package buildcraft.transport.pipes; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import io.netty.buffer.ByteBuf; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; @@ -128,40 +130,37 @@ public class PipeFluidsDiamond extends Pipe implements IDia public void eventHandler(PipeEventFluid.FindDest event) { Fluid fluidInTank = event.fluidStack.getFluid(); - List removedDestinations = new ArrayList(); + Set originalDestinations = new HashSet(); + originalDestinations.addAll(event.destinations.elementSet()); + boolean isFiltered = true; + int[] filterCount = new int[6]; - boolean[] validFilter = new boolean[6]; - boolean isFiltered = false; - for (ForgeDirection dir : event.destinations) { + for (ForgeDirection dir : originalDestinations) { if (container.isPipeConnected(dir) && filters.filteredDirections[dir.ordinal()]) { for (int slot = dir.ordinal() * 9; slot < dir.ordinal() * 9 + 9; ++slot) { if (filters.fluids[slot] != null && filters.fluids[slot].getID() == fluidInTank.getID()) { - validFilter[dir.ordinal()] = true; + filterCount[dir.ordinal()]++; isFiltered = true; - break; } } } } - for (ForgeDirection to : event.destinations) { - // the direction is filtered and liquids match - if (filters.filteredDirections[to.ordinal()] && validFilter[to.ordinal()]) { - continue; - } + event.destinations.clear(); - // we haven't found a filter for this liquid and the direction is free - if (!isFiltered && !filters.filteredDirections[to.ordinal()]) { - continue; - } - - // we have a filter for the liquid, but not a valid direction - removedDestinations.add(to); - } - - for (ForgeDirection dir : removedDestinations) { - event.destinations.remove(dir); - } + if (!isFiltered) { + for (ForgeDirection to : originalDestinations) { + if (!filters.filteredDirections[to.ordinal()]) { + event.destinations.add(to); + } + } + } else { + for (ForgeDirection to : originalDestinations) { + if (filterCount[to.ordinal()] > 0) { + event.destinations.add(to, filterCount[to.ordinal()]); + } + } + } } /* SAVING & LOADING */ diff --git a/common/buildcraft/transport/pipes/events/PipeEventFluid.java b/common/buildcraft/transport/pipes/events/PipeEventFluid.java index 056c3f27..a57c7c09 100644 --- a/common/buildcraft/transport/pipes/events/PipeEventFluid.java +++ b/common/buildcraft/transport/pipes/events/PipeEventFluid.java @@ -1,6 +1,7 @@ package buildcraft.transport.pipes.events; import java.util.List; +import com.google.common.collect.Multiset; import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.fluids.FluidStack; import buildcraft.transport.Pipe; @@ -14,9 +15,9 @@ public abstract class PipeEventFluid extends PipeEvent { } public static class FindDest extends PipeEventFluid { - public final List destinations; + public final Multiset destinations; - public FindDest(Pipe pipe, FluidStack fluidStack, List destinations) { + public FindDest(Pipe pipe, FluidStack fluidStack, Multiset destinations) { super(pipe, fluidStack); this.destinations = destinations; }