Fixed the Fluid filters so that they now work perfectly in the routing system.

This commit is contained in:
WayofTime 2018-04-29 09:57:29 -04:00
parent b20e1ac4d7
commit d29b91a106
4 changed files with 189 additions and 114 deletions

View file

@ -3,6 +3,7 @@ Version 2.2.12
------------------------------------------------------ ------------------------------------------------------
- Fixed Hellfire Forge and Alchemy Table's shape-crafted-ness. - Fixed Hellfire Forge and Alchemy Table's shape-crafted-ness.
- Fixed Cutting Fluids so they now work properly. - 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 Version 2.2.11

View file

@ -23,10 +23,12 @@ import javax.annotation.Nonnull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ItemFluidRouterFilter extends Item implements IFluidFilterProvider, IVariantProvider { public class ItemFluidRouterFilter extends Item implements IFluidFilterProvider, IVariantProvider
public static String[] names = {"exact"}; {
public static String[] names = { "exact" };
public ItemFluidRouterFilter() { public ItemFluidRouterFilter()
{
super(); super();
setUnlocalizedName(BloodMagic.MODID + ".fluidFilter."); setUnlocalizedName(BloodMagic.MODID + ".fluidFilter.");
@ -35,13 +37,15 @@ public class ItemFluidRouterFilter extends Item implements IFluidFilterProvider,
} }
@Override @Override
public String getUnlocalizedName(ItemStack stack) { public String getUnlocalizedName(ItemStack stack)
{
return super.getUnlocalizedName(stack) + names[stack.getItemDamage()]; return super.getUnlocalizedName(stack) + names[stack.getItemDamage()];
} }
@Override @Override
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
public void getSubItems(CreativeTabs creativeTab, NonNullList<ItemStack> list) { public void getSubItems(CreativeTabs creativeTab, NonNullList<ItemStack> list)
{
if (!isInCreativeTab(creativeTab)) if (!isInCreativeTab(creativeTab))
return; return;
@ -51,35 +55,39 @@ public class ItemFluidRouterFilter extends Item implements IFluidFilterProvider,
@Override @Override
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
public void addInformation(ItemStack stack, World world, List<String> tooltip, ITooltipFlag flag) { public void addInformation(ItemStack stack, World world, List<String> tooltip, ITooltipFlag flag)
{
tooltip.add(TextHelper.localize("tooltip.bloodmagic.fluidFilter." + names[stack.getItemDamage()])); tooltip.add(TextHelper.localize("tooltip.bloodmagic.fluidFilter." + names[stack.getItemDamage()]));
super.addInformation(stack, world, tooltip, flag); super.addInformation(stack, world, tooltip, flag);
} }
@Override @Override
public IFluidFilter getInputFluidFilter(ItemStack filterStack, TileEntity tile, IFluidHandler handler) { public IFluidFilter getInputFluidFilter(ItemStack filterStack, TileEntity tile, IFluidHandler handler)
{
IFluidFilter testFilter; IFluidFilter testFilter;
switch (filterStack.getMetadata()) { switch (filterStack.getMetadata())
case 0: {
testFilter = new RoutingFluidFilter(); case 0:
break; testFilter = new RoutingFluidFilter();
break;
default: default:
testFilter = new RoutingFluidFilter(); testFilter = new RoutingFluidFilter();
} }
List<ItemStack> filteredList = new ArrayList<>(); List<ItemStack> filteredList = new ArrayList<>();
ItemInventory inv = new ItemInventory(filterStack, 9, ""); 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); ItemStack stack = inv.getStackInSlot(i);
if (stack == null) { if (stack.isEmpty())
{
continue; continue;
} }
ItemStack ghostStack = GhostItemHelper.getStackFromGhost(stack); ItemStack ghostStack = GhostItemHelper.getStackFromGhost(stack);
filteredList.add(ghostStack); filteredList.add(ghostStack);
} }
@ -88,27 +96,32 @@ public class ItemFluidRouterFilter extends Item implements IFluidFilterProvider,
} }
@Override @Override
public IFluidFilter getOutputFluidFilter(ItemStack filterStack, TileEntity tile, IFluidHandler handler) { public IFluidFilter getOutputFluidFilter(ItemStack filterStack, TileEntity tile, IFluidHandler handler)
{
IFluidFilter testFilter; IFluidFilter testFilter;
switch (filterStack.getMetadata()) { switch (filterStack.getMetadata())
case 0: {
testFilter = new RoutingFluidFilter(); case 0:
break; testFilter = new RoutingFluidFilter();
break;
default: default:
testFilter = new RoutingFluidFilter(); testFilter = new RoutingFluidFilter();
} }
List<ItemStack> filteredList = new ArrayList<>(); List<ItemStack> filteredList = new ArrayList<>();
ItemInventory inv = new ItemInventory(filterStack, 9, ""); //TODO: Change to grab the filter from the Item later. 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); ItemStack stack = inv.getStackInSlot(i);
if (stack.isEmpty()) { if (stack.isEmpty())
{
continue; continue;
} }
ItemStack ghostStack = GhostItemHelper.getStackFromGhost(stack); ItemStack ghostStack = GhostItemHelper.getStackFromGhost(stack);
if (ghostStack.isEmpty()) { if (ghostStack.isEmpty())
{
ghostStack.setCount(Integer.MAX_VALUE); ghostStack.setCount(Integer.MAX_VALUE);
} }
@ -120,12 +133,14 @@ public class ItemFluidRouterFilter extends Item implements IFluidFilterProvider,
} }
@Override @Override
public void gatherVariants(@Nonnull Int2ObjectMap<String> variants) { public void gatherVariants(@Nonnull Int2ObjectMap<String> variants)
{
variants.put(0, "type=exact"); variants.put(0, "type=exact");
} }
@Override @Override
public ItemStack getContainedStackForItem(ItemStack filterStack, ItemStack keyStack) { public ItemStack getContainedStackForItem(ItemStack filterStack, ItemStack keyStack)
{
ItemStack copyStack = keyStack.copy(); ItemStack copyStack = keyStack.copy();
GhostItemHelper.setItemGhostAmount(copyStack, 0); GhostItemHelper.setItemGhostAmount(copyStack, 0);
copyStack.setCount(1); copyStack.setCount(1);

View file

@ -22,10 +22,12 @@ import javax.annotation.Nonnull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ItemRouterFilter extends Item implements IItemFilterProvider, IVariantProvider { public class ItemRouterFilter extends Item implements IItemFilterProvider, IVariantProvider
public static String[] names = {"exact", "ignoreNBT", "modItems", "oreDict"}; {
public static String[] names = { "exact", "ignoreNBT", "modItems", "oreDict" };
public ItemRouterFilter() { public ItemRouterFilter()
{
super(); super();
setUnlocalizedName(BloodMagic.MODID + ".itemFilter."); setUnlocalizedName(BloodMagic.MODID + ".itemFilter.");
@ -34,13 +36,15 @@ public class ItemRouterFilter extends Item implements IItemFilterProvider, IVari
} }
@Override @Override
public String getUnlocalizedName(ItemStack stack) { public String getUnlocalizedName(ItemStack stack)
{
return super.getUnlocalizedName(stack) + names[stack.getItemDamage()]; return super.getUnlocalizedName(stack) + names[stack.getItemDamage()];
} }
@Override @Override
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
public void getSubItems(CreativeTabs creativeTab, NonNullList<ItemStack> list) { public void getSubItems(CreativeTabs creativeTab, NonNullList<ItemStack> list)
{
if (!isInCreativeTab(creativeTab)) if (!isInCreativeTab(creativeTab))
return; return;
@ -50,39 +54,44 @@ public class ItemRouterFilter extends Item implements IItemFilterProvider, IVari
@Override @Override
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
public void addInformation(ItemStack stack, World world, List<String> tooltip, ITooltipFlag flag) { public void addInformation(ItemStack stack, World world, List<String> tooltip, ITooltipFlag flag)
{
tooltip.add(TextHelper.localize("tooltip.bloodmagic.itemFilter." + names[stack.getItemDamage()])); tooltip.add(TextHelper.localize("tooltip.bloodmagic.itemFilter." + names[stack.getItemDamage()]));
super.addInformation(stack, world, tooltip, flag); super.addInformation(stack, world, tooltip, flag);
} }
@Override @Override
public IItemFilter getInputItemFilter(ItemStack filterStack, TileEntity tile, IItemHandler handler) { public IItemFilter getInputItemFilter(ItemStack filterStack, TileEntity tile, IItemHandler handler)
{
IItemFilter testFilter = new TestItemFilter(); IItemFilter testFilter = new TestItemFilter();
switch (filterStack.getMetadata()) { switch (filterStack.getMetadata())
case 0: {
testFilter = new TestItemFilter(); case 0:
break; testFilter = new TestItemFilter();
case 1: break;
testFilter = new IgnoreNBTItemFilter(); case 1:
break; testFilter = new IgnoreNBTItemFilter();
case 2: break;
testFilter = new ModIdItemFilter(); case 2:
break; testFilter = new ModIdItemFilter();
case 3: break;
testFilter = new OreDictItemFilter(); case 3:
break; testFilter = new OreDictItemFilter();
break;
default: default:
testFilter = new DefaultItemFilter(); testFilter = new DefaultItemFilter();
} }
List<ItemStack> filteredList = new ArrayList<>(); List<ItemStack> filteredList = new ArrayList<>();
ItemInventory inv = new ItemInventory(filterStack, 9, ""); 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); ItemStack stack = inv.getStackInSlot(i);
if (stack == null) { if (stack.isEmpty())
{
continue; continue;
} }
@ -96,37 +105,42 @@ public class ItemRouterFilter extends Item implements IItemFilterProvider, IVari
} }
@Override @Override
public IItemFilter getOutputItemFilter(ItemStack filterStack, TileEntity tile, IItemHandler handler) { public IItemFilter getOutputItemFilter(ItemStack filterStack, TileEntity tile, IItemHandler handler)
{
IItemFilter testFilter; IItemFilter testFilter;
switch (filterStack.getMetadata()) { switch (filterStack.getMetadata())
case 0: {
testFilter = new TestItemFilter(); case 0:
break; testFilter = new TestItemFilter();
case 1: break;
testFilter = new IgnoreNBTItemFilter(); case 1:
break; testFilter = new IgnoreNBTItemFilter();
case 2: break;
testFilter = new ModIdItemFilter(); case 2:
break; testFilter = new ModIdItemFilter();
case 3: break;
testFilter = new OreDictItemFilter(); case 3:
break; testFilter = new OreDictItemFilter();
break;
default: default:
testFilter = new DefaultItemFilter(); testFilter = new DefaultItemFilter();
} }
List<ItemStack> filteredList = new ArrayList<>(); List<ItemStack> filteredList = new ArrayList<>();
ItemInventory inv = new ItemInventory(filterStack, 9, ""); //TODO: Change to grab the filter from the Item later. 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); ItemStack stack = inv.getStackInSlot(i);
if (stack == null) { if (stack.isEmpty())
{
continue; continue;
} }
ItemStack ghostStack = GhostItemHelper.getStackFromGhost(stack); ItemStack ghostStack = GhostItemHelper.getStackFromGhost(stack);
if (ghostStack.isEmpty()) { if (ghostStack.isEmpty())
{
ghostStack.setCount(Integer.MAX_VALUE); ghostStack.setCount(Integer.MAX_VALUE);
} }
@ -138,7 +152,8 @@ public class ItemRouterFilter extends Item implements IItemFilterProvider, IVari
} }
@Override @Override
public void gatherVariants(@Nonnull Int2ObjectMap<String> variants) { public void gatherVariants(@Nonnull Int2ObjectMap<String> variants)
{
variants.put(0, "type=exact"); variants.put(0, "type=exact");
variants.put(1, "type=ignorenbt"); variants.put(1, "type=ignorenbt");
variants.put(2, "type=moditems"); variants.put(2, "type=moditems");
@ -146,7 +161,8 @@ public class ItemRouterFilter extends Item implements IItemFilterProvider, IVari
} }
@Override @Override
public ItemStack getContainedStackForItem(ItemStack filterStack, ItemStack keyStack) { public ItemStack getContainedStackForItem(ItemStack filterStack, ItemStack keyStack)
{
ItemStack copyStack = keyStack.copy(); ItemStack copyStack = keyStack.copy();
GhostItemHelper.setItemGhostAmount(copyStack, 0); GhostItemHelper.setItemGhostAmount(copyStack, 0);
copyStack.setCount(1); copyStack.setCount(1);

View file

@ -1,5 +1,11 @@
package WayofTime.bloodmagic.routing; 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.item.ItemStack;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos; 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.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidTankProperties; import net.minecraftforge.fluids.capability.IFluidTankProperties;
import javax.annotation.Nullable; public class RoutingFluidFilter implements IFluidFilter
import java.util.ArrayList; {
import java.util.Iterator;
import java.util.List;
public class RoutingFluidFilter implements IFluidFilter {
protected List<FluidStack> requestList; protected List<FluidStack> requestList;
protected TileEntity accessedTile; protected TileEntity accessedTile;
protected IFluidHandler fluidHandler; protected IFluidHandler fluidHandler;
@Override @Override
public void initializeFilter(List<ItemStack> filteredList, TileEntity tile, IFluidHandler fluidHandler, boolean isFilterOutput) { public void initializeFilter(List<ItemStack> filteredList, TileEntity tile, IFluidHandler fluidHandler, boolean isFilterOutput)
{
this.accessedTile = tile; this.accessedTile = tile;
this.fluidHandler = fluidHandler; this.fluidHandler = fluidHandler;
if (isFilterOutput) { if (isFilterOutput)
{
//The requestList contains a list of how much can be extracted. //The requestList contains a list of how much can be extracted.
requestList = new ArrayList<>(); requestList = new ArrayList<>();
for (ItemStack filterStack : filteredList) { for (ItemStack filterStack : filteredList)
{
FluidStack fluidFilterStack = getFluidStackFromItemStack(filterStack); FluidStack fluidFilterStack = getFluidStackFromItemStack(filterStack);
if (fluidFilterStack != null) { if (fluidFilterStack != null)
{
requestList.add(fluidFilterStack); requestList.add(fluidFilterStack);
} }
} }
IFluidTankProperties[] properties = fluidHandler.getTankProperties(); IFluidTankProperties[] properties = fluidHandler.getTankProperties();
for (IFluidTankProperties property : properties) { for (IFluidTankProperties property : properties)
{
FluidStack containedStack = property.getContents(); FluidStack containedStack = property.getContents();
if (containedStack != null) { if (containedStack != null)
for (FluidStack fluidFilterStack : requestList) { {
if (doStacksMatch(fluidFilterStack, containedStack)) { for (FluidStack fluidFilterStack : requestList)
{
if (doStacksMatch(fluidFilterStack, containedStack))
{
fluidFilterStack.amount = Math.max(fluidFilterStack.amount - containedStack.amount, 0); fluidFilterStack.amount = Math.max(fluidFilterStack.amount - containedStack.amount, 0);
} }
} }
} }
} }
} else { } else
{
requestList = new ArrayList<>(); requestList = new ArrayList<>();
for (ItemStack filterStack : filteredList) { for (ItemStack filterStack : filteredList)
{
FluidStack fluidFilterStack = getFluidStackFromItemStack(filterStack); FluidStack fluidFilterStack = getFluidStackFromItemStack(filterStack);
if (fluidFilterStack != null) { if (fluidFilterStack != null)
{
fluidFilterStack.amount *= -1; fluidFilterStack.amount *= -1;
requestList.add(fluidFilterStack); requestList.add(fluidFilterStack);
} }
@ -57,11 +70,15 @@ public class RoutingFluidFilter implements IFluidFilter {
IFluidTankProperties[] properties = fluidHandler.getTankProperties(); IFluidTankProperties[] properties = fluidHandler.getTankProperties();
for (IFluidTankProperties property : properties) { for (IFluidTankProperties property : properties)
{
FluidStack containedStack = property.getContents(); FluidStack containedStack = property.getContents();
if (containedStack != null) { if (containedStack != null)
for (FluidStack fluidFilterStack : requestList) { {
if (doStacksMatch(fluidFilterStack, containedStack)) { for (FluidStack fluidFilterStack : requestList)
{
if (doStacksMatch(fluidFilterStack, containedStack))
{
fluidFilterStack.amount += containedStack.amount; fluidFilterStack.amount += containedStack.amount;
} }
} }
@ -74,16 +91,20 @@ public class RoutingFluidFilter implements IFluidFilter {
* Gives the remainder~ * Gives the remainder~
*/ */
@Override @Override
public FluidStack transferStackThroughOutputFilter(FluidStack fluidStack) { public FluidStack transferStackThroughOutputFilter(FluidStack fluidStack)
{
int allowedAmount = 0; int allowedAmount = 0;
for (FluidStack filterStack : requestList) { for (FluidStack filterStack : requestList)
if (doStacksMatch(filterStack, fluidStack)) { {
if (doStacksMatch(filterStack, fluidStack))
{
allowedAmount = Math.min(filterStack.amount, fluidStack.amount); allowedAmount = Math.min(filterStack.amount, fluidStack.amount);
break; break;
} }
} }
if (allowedAmount <= 0) { if (allowedAmount <= 0)
{
return fluidStack; return fluidStack;
} }
@ -92,11 +113,14 @@ public class RoutingFluidFilter implements IFluidFilter {
copyStack.amount = fluidStack.amount - filledAmount; copyStack.amount = fluidStack.amount - filledAmount;
Iterator<FluidStack> itr = requestList.iterator(); Iterator<FluidStack> itr = requestList.iterator();
while (itr.hasNext()) { while (itr.hasNext())
{
FluidStack filterStack = itr.next(); FluidStack filterStack = itr.next();
if (doStacksMatch(filterStack, copyStack)) { if (doStacksMatch(filterStack, copyStack))
{
filterStack.amount -= filledAmount; filterStack.amount -= filledAmount;
if (filterStack.amount <= 0) { if (filterStack.amount <= 0)
{
itr.remove(); itr.remove();
} }
} }
@ -110,10 +134,13 @@ public class RoutingFluidFilter implements IFluidFilter {
} }
@Override @Override
public int transferThroughInputFilter(IFluidFilter outputFilter, int maxTransfer) { public int transferThroughInputFilter(IFluidFilter outputFilter, int maxTransfer)
for (FluidStack filterFluidStack : requestList) { {
for (FluidStack filterFluidStack : requestList)
{
int allowedAmount = Math.min(filterFluidStack.amount, maxTransfer); int allowedAmount = Math.min(filterFluidStack.amount, maxTransfer);
if (allowedAmount <= 0) { if (allowedAmount <= 0)
{
continue; continue;
} }
@ -125,7 +152,8 @@ public class RoutingFluidFilter implements IFluidFilter {
FluidStack remainderStack = outputFilter.transferStackThroughOutputFilter(drainStack); FluidStack remainderStack = outputFilter.transferStackThroughOutputFilter(drainStack);
int drained = remainderStack == null ? copyStack.amount : (copyStack.amount - remainderStack.amount); int drained = remainderStack == null ? copyStack.amount : (copyStack.amount - remainderStack.amount);
if (drained > 0) { if (drained > 0)
{
drainStack.amount = drained; drainStack.amount = drained;
fluidHandler.drain(drainStack, true); fluidHandler.drain(drainStack, true);
@ -133,11 +161,14 @@ public class RoutingFluidFilter implements IFluidFilter {
} }
Iterator<FluidStack> itr = requestList.iterator(); Iterator<FluidStack> itr = requestList.iterator();
while (itr.hasNext()) { while (itr.hasNext())
{
FluidStack filterStack = itr.next(); FluidStack filterStack = itr.next();
if (doStacksMatch(filterStack, copyStack)) { if (doStacksMatch(filterStack, copyStack))
{
filterStack.amount -= drained; filterStack.amount -= drained;
if (filterStack.amount <= 0) { if (filterStack.amount <= 0)
{
itr.remove(); itr.remove();
} }
} }
@ -155,9 +186,12 @@ public class RoutingFluidFilter implements IFluidFilter {
} }
@Override @Override
public boolean doesStackMatchFilter(FluidStack testStack) { public boolean doesStackMatchFilter(FluidStack testStack)
for (FluidStack filterStack : requestList) { {
if (doStacksMatch(filterStack, testStack)) { for (FluidStack filterStack : requestList)
{
if (doStacksMatch(filterStack, testStack))
{
return true; return true;
} }
} }
@ -166,17 +200,26 @@ public class RoutingFluidFilter implements IFluidFilter {
} }
@Override @Override
public boolean doStacksMatch(FluidStack filterStack, FluidStack testStack) { public boolean doStacksMatch(FluidStack filterStack, FluidStack testStack)
{
return testStack != null && filterStack.getFluid() == testStack.getFluid(); return testStack != null && filterStack.getFluid() == testStack.getFluid();
} }
@Nullable @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); FluidStack fluidStack = FluidUtil.getFluidContained(inputStack);
if (fluidStack == null) if (fluidStack == null)
return null; return null;
fluidStack.amount = inputStack.getCount(); fluidStack.amount = isEmpty ? 0 : inputStack.getCount();
return fluidStack; return fluidStack;
} }
} }