From d29b91a1068fa007bb2604f17b717aa2852d48c7 Mon Sep 17 00:00:00 2001 From: WayofTime Date: Sun, 29 Apr 2018 09:57:29 -0400 Subject: [PATCH] Fixed the Fluid filters so that they now work perfectly in the routing system. --- changelog.txt | 1 + .../item/routing/ItemFluidRouterFilter.java | 71 ++++++---- .../item/routing/ItemRouterFilter.java | 106 ++++++++------- .../routing/RoutingFluidFilter.java | 125 ++++++++++++------ 4 files changed, 189 insertions(+), 114 deletions(-) diff --git a/changelog.txt b/changelog.txt index 94038314..f449eb05 100644 --- a/changelog.txt +++ b/changelog.txt @@ -3,6 +3,7 @@ Version 2.2.12 ------------------------------------------------------ - Fixed Hellfire Forge and Alchemy Table's shape-crafted-ness. - Fixed Cutting Fluids so they now work properly. +- Fixed the Fluid filters so that they now work perfectly in the routing system. ------------------------------------------------------ Version 2.2.11 diff --git a/src/main/java/WayofTime/bloodmagic/item/routing/ItemFluidRouterFilter.java b/src/main/java/WayofTime/bloodmagic/item/routing/ItemFluidRouterFilter.java index 534ab3c4..a72dfd3f 100644 --- a/src/main/java/WayofTime/bloodmagic/item/routing/ItemFluidRouterFilter.java +++ b/src/main/java/WayofTime/bloodmagic/item/routing/ItemFluidRouterFilter.java @@ -23,10 +23,12 @@ import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.List; -public class ItemFluidRouterFilter extends Item implements IFluidFilterProvider, IVariantProvider { - public static String[] names = {"exact"}; +public class ItemFluidRouterFilter extends Item implements IFluidFilterProvider, IVariantProvider +{ + public static String[] names = { "exact" }; - public ItemFluidRouterFilter() { + public ItemFluidRouterFilter() + { super(); setUnlocalizedName(BloodMagic.MODID + ".fluidFilter."); @@ -35,13 +37,15 @@ public class ItemFluidRouterFilter extends Item implements IFluidFilterProvider, } @Override - public String getUnlocalizedName(ItemStack stack) { + public String getUnlocalizedName(ItemStack stack) + { return super.getUnlocalizedName(stack) + names[stack.getItemDamage()]; } @Override @SideOnly(Side.CLIENT) - public void getSubItems(CreativeTabs creativeTab, NonNullList list) { + public void getSubItems(CreativeTabs creativeTab, NonNullList list) + { if (!isInCreativeTab(creativeTab)) return; @@ -51,35 +55,39 @@ public class ItemFluidRouterFilter extends Item implements IFluidFilterProvider, @Override @SideOnly(Side.CLIENT) - public void addInformation(ItemStack stack, World world, List tooltip, ITooltipFlag flag) { + public void addInformation(ItemStack stack, World world, List tooltip, ITooltipFlag flag) + { tooltip.add(TextHelper.localize("tooltip.bloodmagic.fluidFilter." + names[stack.getItemDamage()])); super.addInformation(stack, world, tooltip, flag); } @Override - public IFluidFilter getInputFluidFilter(ItemStack filterStack, TileEntity tile, IFluidHandler handler) { + public IFluidFilter getInputFluidFilter(ItemStack filterStack, TileEntity tile, IFluidHandler handler) + { IFluidFilter testFilter; - switch (filterStack.getMetadata()) { - case 0: - testFilter = new RoutingFluidFilter(); - break; + switch (filterStack.getMetadata()) + { + case 0: + testFilter = new RoutingFluidFilter(); + break; - default: - testFilter = new RoutingFluidFilter(); + default: + testFilter = new RoutingFluidFilter(); } List filteredList = new ArrayList<>(); ItemInventory inv = new ItemInventory(filterStack, 9, ""); - for (int i = 0; i < inv.getSizeInventory(); i++) { + for (int i = 0; i < inv.getSizeInventory(); i++) + { ItemStack stack = inv.getStackInSlot(i); - if (stack == null) { + if (stack.isEmpty()) + { continue; } ItemStack ghostStack = GhostItemHelper.getStackFromGhost(stack); - filteredList.add(ghostStack); } @@ -88,27 +96,32 @@ public class ItemFluidRouterFilter extends Item implements IFluidFilterProvider, } @Override - public IFluidFilter getOutputFluidFilter(ItemStack filterStack, TileEntity tile, IFluidHandler handler) { + public IFluidFilter getOutputFluidFilter(ItemStack filterStack, TileEntity tile, IFluidHandler handler) + { IFluidFilter testFilter; - switch (filterStack.getMetadata()) { - case 0: - testFilter = new RoutingFluidFilter(); - break; + switch (filterStack.getMetadata()) + { + case 0: + testFilter = new RoutingFluidFilter(); + break; - default: - testFilter = new RoutingFluidFilter(); + default: + testFilter = new RoutingFluidFilter(); } List filteredList = new ArrayList<>(); ItemInventory inv = new ItemInventory(filterStack, 9, ""); //TODO: Change to grab the filter from the Item later. - for (int i = 0; i < inv.getSizeInventory(); i++) { + for (int i = 0; i < inv.getSizeInventory(); i++) + { ItemStack stack = inv.getStackInSlot(i); - if (stack.isEmpty()) { + if (stack.isEmpty()) + { continue; } ItemStack ghostStack = GhostItemHelper.getStackFromGhost(stack); - if (ghostStack.isEmpty()) { + if (ghostStack.isEmpty()) + { ghostStack.setCount(Integer.MAX_VALUE); } @@ -120,12 +133,14 @@ public class ItemFluidRouterFilter extends Item implements IFluidFilterProvider, } @Override - public void gatherVariants(@Nonnull Int2ObjectMap variants) { + public void gatherVariants(@Nonnull Int2ObjectMap variants) + { variants.put(0, "type=exact"); } @Override - public ItemStack getContainedStackForItem(ItemStack filterStack, ItemStack keyStack) { + public ItemStack getContainedStackForItem(ItemStack filterStack, ItemStack keyStack) + { ItemStack copyStack = keyStack.copy(); GhostItemHelper.setItemGhostAmount(copyStack, 0); copyStack.setCount(1); diff --git a/src/main/java/WayofTime/bloodmagic/item/routing/ItemRouterFilter.java b/src/main/java/WayofTime/bloodmagic/item/routing/ItemRouterFilter.java index 840f1c27..f80cf9aa 100644 --- a/src/main/java/WayofTime/bloodmagic/item/routing/ItemRouterFilter.java +++ b/src/main/java/WayofTime/bloodmagic/item/routing/ItemRouterFilter.java @@ -22,10 +22,12 @@ import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.List; -public class ItemRouterFilter extends Item implements IItemFilterProvider, IVariantProvider { - public static String[] names = {"exact", "ignoreNBT", "modItems", "oreDict"}; +public class ItemRouterFilter extends Item implements IItemFilterProvider, IVariantProvider +{ + public static String[] names = { "exact", "ignoreNBT", "modItems", "oreDict" }; - public ItemRouterFilter() { + public ItemRouterFilter() + { super(); setUnlocalizedName(BloodMagic.MODID + ".itemFilter."); @@ -34,13 +36,15 @@ public class ItemRouterFilter extends Item implements IItemFilterProvider, IVari } @Override - public String getUnlocalizedName(ItemStack stack) { + public String getUnlocalizedName(ItemStack stack) + { return super.getUnlocalizedName(stack) + names[stack.getItemDamage()]; } @Override @SideOnly(Side.CLIENT) - public void getSubItems(CreativeTabs creativeTab, NonNullList list) { + public void getSubItems(CreativeTabs creativeTab, NonNullList list) + { if (!isInCreativeTab(creativeTab)) return; @@ -50,39 +54,44 @@ public class ItemRouterFilter extends Item implements IItemFilterProvider, IVari @Override @SideOnly(Side.CLIENT) - public void addInformation(ItemStack stack, World world, List tooltip, ITooltipFlag flag) { + public void addInformation(ItemStack stack, World world, List tooltip, ITooltipFlag flag) + { tooltip.add(TextHelper.localize("tooltip.bloodmagic.itemFilter." + names[stack.getItemDamage()])); super.addInformation(stack, world, tooltip, flag); } @Override - public IItemFilter getInputItemFilter(ItemStack filterStack, TileEntity tile, IItemHandler handler) { + public IItemFilter getInputItemFilter(ItemStack filterStack, TileEntity tile, IItemHandler handler) + { IItemFilter testFilter = new TestItemFilter(); - switch (filterStack.getMetadata()) { - case 0: - testFilter = new TestItemFilter(); - break; - case 1: - testFilter = new IgnoreNBTItemFilter(); - break; - case 2: - testFilter = new ModIdItemFilter(); - break; - case 3: - testFilter = new OreDictItemFilter(); - break; + switch (filterStack.getMetadata()) + { + case 0: + testFilter = new TestItemFilter(); + break; + case 1: + testFilter = new IgnoreNBTItemFilter(); + break; + case 2: + testFilter = new ModIdItemFilter(); + break; + case 3: + testFilter = new OreDictItemFilter(); + break; - default: - testFilter = new DefaultItemFilter(); + default: + testFilter = new DefaultItemFilter(); } List filteredList = new ArrayList<>(); ItemInventory inv = new ItemInventory(filterStack, 9, ""); - for (int i = 0; i < inv.getSizeInventory(); i++) { + for (int i = 0; i < inv.getSizeInventory(); i++) + { ItemStack stack = inv.getStackInSlot(i); - if (stack == null) { + if (stack.isEmpty()) + { continue; } @@ -96,37 +105,42 @@ public class ItemRouterFilter extends Item implements IItemFilterProvider, IVari } @Override - public IItemFilter getOutputItemFilter(ItemStack filterStack, TileEntity tile, IItemHandler handler) { + public IItemFilter getOutputItemFilter(ItemStack filterStack, TileEntity tile, IItemHandler handler) + { IItemFilter testFilter; - switch (filterStack.getMetadata()) { - case 0: - testFilter = new TestItemFilter(); - break; - case 1: - testFilter = new IgnoreNBTItemFilter(); - break; - case 2: - testFilter = new ModIdItemFilter(); - break; - case 3: - testFilter = new OreDictItemFilter(); - break; + switch (filterStack.getMetadata()) + { + case 0: + testFilter = new TestItemFilter(); + break; + case 1: + testFilter = new IgnoreNBTItemFilter(); + break; + case 2: + testFilter = new ModIdItemFilter(); + break; + case 3: + testFilter = new OreDictItemFilter(); + break; - default: - testFilter = new DefaultItemFilter(); + default: + testFilter = new DefaultItemFilter(); } List filteredList = new ArrayList<>(); ItemInventory inv = new ItemInventory(filterStack, 9, ""); //TODO: Change to grab the filter from the Item later. - for (int i = 0; i < inv.getSizeInventory(); i++) { + for (int i = 0; i < inv.getSizeInventory(); i++) + { ItemStack stack = inv.getStackInSlot(i); - if (stack == null) { + if (stack.isEmpty()) + { continue; } ItemStack ghostStack = GhostItemHelper.getStackFromGhost(stack); - if (ghostStack.isEmpty()) { + if (ghostStack.isEmpty()) + { ghostStack.setCount(Integer.MAX_VALUE); } @@ -138,7 +152,8 @@ public class ItemRouterFilter extends Item implements IItemFilterProvider, IVari } @Override - public void gatherVariants(@Nonnull Int2ObjectMap variants) { + public void gatherVariants(@Nonnull Int2ObjectMap variants) + { variants.put(0, "type=exact"); variants.put(1, "type=ignorenbt"); variants.put(2, "type=moditems"); @@ -146,7 +161,8 @@ public class ItemRouterFilter extends Item implements IItemFilterProvider, IVari } @Override - public ItemStack getContainedStackForItem(ItemStack filterStack, ItemStack keyStack) { + public ItemStack getContainedStackForItem(ItemStack filterStack, ItemStack keyStack) + { ItemStack copyStack = keyStack.copy(); GhostItemHelper.setItemGhostAmount(copyStack, 0); copyStack.setCount(1); diff --git a/src/main/java/WayofTime/bloodmagic/routing/RoutingFluidFilter.java b/src/main/java/WayofTime/bloodmagic/routing/RoutingFluidFilter.java index 5de7f9cb..6ff9f8d9 100644 --- a/src/main/java/WayofTime/bloodmagic/routing/RoutingFluidFilter.java +++ b/src/main/java/WayofTime/bloodmagic/routing/RoutingFluidFilter.java @@ -1,5 +1,11 @@ package WayofTime.bloodmagic.routing; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import javax.annotation.Nullable; + import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.math.BlockPos; @@ -9,47 +15,54 @@ import net.minecraftforge.fluids.FluidUtil; import net.minecraftforge.fluids.capability.IFluidHandler; import net.minecraftforge.fluids.capability.IFluidTankProperties; -import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -public class RoutingFluidFilter implements IFluidFilter { +public class RoutingFluidFilter implements IFluidFilter +{ protected List requestList; protected TileEntity accessedTile; protected IFluidHandler fluidHandler; @Override - public void initializeFilter(List filteredList, TileEntity tile, IFluidHandler fluidHandler, boolean isFilterOutput) { + public void initializeFilter(List filteredList, TileEntity tile, IFluidHandler fluidHandler, boolean isFilterOutput) + { this.accessedTile = tile; this.fluidHandler = fluidHandler; - if (isFilterOutput) { + if (isFilterOutput) + { //The requestList contains a list of how much can be extracted. requestList = new ArrayList<>(); - for (ItemStack filterStack : filteredList) { + for (ItemStack filterStack : filteredList) + { FluidStack fluidFilterStack = getFluidStackFromItemStack(filterStack); - if (fluidFilterStack != null) { + if (fluidFilterStack != null) + { requestList.add(fluidFilterStack); } } IFluidTankProperties[] properties = fluidHandler.getTankProperties(); - for (IFluidTankProperties property : properties) { + for (IFluidTankProperties property : properties) + { FluidStack containedStack = property.getContents(); - if (containedStack != null) { - for (FluidStack fluidFilterStack : requestList) { - if (doStacksMatch(fluidFilterStack, containedStack)) { + if (containedStack != null) + { + for (FluidStack fluidFilterStack : requestList) + { + if (doStacksMatch(fluidFilterStack, containedStack)) + { fluidFilterStack.amount = Math.max(fluidFilterStack.amount - containedStack.amount, 0); } } } } - } else { + } else + { requestList = new ArrayList<>(); - for (ItemStack filterStack : filteredList) { + for (ItemStack filterStack : filteredList) + { FluidStack fluidFilterStack = getFluidStackFromItemStack(filterStack); - if (fluidFilterStack != null) { + if (fluidFilterStack != null) + { fluidFilterStack.amount *= -1; requestList.add(fluidFilterStack); } @@ -57,11 +70,15 @@ public class RoutingFluidFilter implements IFluidFilter { IFluidTankProperties[] properties = fluidHandler.getTankProperties(); - for (IFluidTankProperties property : properties) { + for (IFluidTankProperties property : properties) + { FluidStack containedStack = property.getContents(); - if (containedStack != null) { - for (FluidStack fluidFilterStack : requestList) { - if (doStacksMatch(fluidFilterStack, containedStack)) { + if (containedStack != null) + { + for (FluidStack fluidFilterStack : requestList) + { + if (doStacksMatch(fluidFilterStack, containedStack)) + { fluidFilterStack.amount += containedStack.amount; } } @@ -74,16 +91,20 @@ public class RoutingFluidFilter implements IFluidFilter { * Gives the remainder~ */ @Override - public FluidStack transferStackThroughOutputFilter(FluidStack fluidStack) { + public FluidStack transferStackThroughOutputFilter(FluidStack fluidStack) + { int allowedAmount = 0; - for (FluidStack filterStack : requestList) { - if (doStacksMatch(filterStack, fluidStack)) { + for (FluidStack filterStack : requestList) + { + if (doStacksMatch(filterStack, fluidStack)) + { allowedAmount = Math.min(filterStack.amount, fluidStack.amount); break; } } - if (allowedAmount <= 0) { + if (allowedAmount <= 0) + { return fluidStack; } @@ -92,11 +113,14 @@ public class RoutingFluidFilter implements IFluidFilter { copyStack.amount = fluidStack.amount - filledAmount; Iterator itr = requestList.iterator(); - while (itr.hasNext()) { + while (itr.hasNext()) + { FluidStack filterStack = itr.next(); - if (doStacksMatch(filterStack, copyStack)) { + if (doStacksMatch(filterStack, copyStack)) + { filterStack.amount -= filledAmount; - if (filterStack.amount <= 0) { + if (filterStack.amount <= 0) + { itr.remove(); } } @@ -110,10 +134,13 @@ public class RoutingFluidFilter implements IFluidFilter { } @Override - public int transferThroughInputFilter(IFluidFilter outputFilter, int maxTransfer) { - for (FluidStack filterFluidStack : requestList) { + public int transferThroughInputFilter(IFluidFilter outputFilter, int maxTransfer) + { + for (FluidStack filterFluidStack : requestList) + { int allowedAmount = Math.min(filterFluidStack.amount, maxTransfer); - if (allowedAmount <= 0) { + if (allowedAmount <= 0) + { continue; } @@ -125,7 +152,8 @@ public class RoutingFluidFilter implements IFluidFilter { FluidStack remainderStack = outputFilter.transferStackThroughOutputFilter(drainStack); int drained = remainderStack == null ? copyStack.amount : (copyStack.amount - remainderStack.amount); - if (drained > 0) { + if (drained > 0) + { drainStack.amount = drained; fluidHandler.drain(drainStack, true); @@ -133,11 +161,14 @@ public class RoutingFluidFilter implements IFluidFilter { } Iterator itr = requestList.iterator(); - while (itr.hasNext()) { + while (itr.hasNext()) + { FluidStack filterStack = itr.next(); - if (doStacksMatch(filterStack, copyStack)) { + if (doStacksMatch(filterStack, copyStack)) + { filterStack.amount -= drained; - if (filterStack.amount <= 0) { + if (filterStack.amount <= 0) + { itr.remove(); } } @@ -155,9 +186,12 @@ public class RoutingFluidFilter implements IFluidFilter { } @Override - public boolean doesStackMatchFilter(FluidStack testStack) { - for (FluidStack filterStack : requestList) { - if (doStacksMatch(filterStack, testStack)) { + public boolean doesStackMatchFilter(FluidStack testStack) + { + for (FluidStack filterStack : requestList) + { + if (doStacksMatch(filterStack, testStack)) + { return true; } } @@ -166,17 +200,26 @@ public class RoutingFluidFilter implements IFluidFilter { } @Override - public boolean doStacksMatch(FluidStack filterStack, FluidStack testStack) { + public boolean doStacksMatch(FluidStack filterStack, FluidStack testStack) + { return testStack != null && filterStack.getFluid() == testStack.getFluid(); } @Nullable - public static FluidStack getFluidStackFromItemStack(ItemStack inputStack) { + public static FluidStack getFluidStackFromItemStack(ItemStack inputStack) + { + boolean isEmpty = false; + if (inputStack.getCount() == 0) + { + isEmpty = true; + inputStack.setCount(1); + } + FluidStack fluidStack = FluidUtil.getFluidContained(inputStack); if (fluidStack == null) return null; - fluidStack.amount = inputStack.getCount(); + fluidStack.amount = isEmpty ? 0 : inputStack.getCount(); return fluidStack; } }