From 1d3948c9028abf450857d4c2a5ef27351e46f7fd Mon Sep 17 00:00:00 2001 From: zyxkad Date: Fri, 26 Apr 2024 17:24:09 -0600 Subject: [PATCH] rework on RS Bridge fix #577 again --- .../peripheral/RsBridgePeripheral.java | 171 ++++++++---- .../addons/refinedstorage/RefinedStorage.java | 253 +++++++++++------- .../addons/refinedstorage/RsFluidHandler.java | 8 +- .../addons/refinedstorage/RsItemHandler.java | 6 +- .../advancedperipherals/common/util/Pair.java | 11 + .../common/util/inventory/ItemFilter.java | 3 + .../common/util/inventory/ItemUtil.java | 17 +- 7 files changed, 314 insertions(+), 155 deletions(-) diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/RsBridgePeripheral.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/RsBridgePeripheral.java index 15ca27785..fb174084e 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/RsBridgePeripheral.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/RsBridgePeripheral.java @@ -92,7 +92,7 @@ public final MethodResult listCraftableItems() { return notConnected(); List items = new ArrayList<>(); - RefinedStorage.getCraftableItems(getNetwork()).forEach(item -> items.add(RefinedStorage.getObjectFromStack(item.copy(), getNetwork()))); + RefinedStorage.getCraftableItems(getNetwork()).forEach(item -> items.add(RefinedStorage.getObjectFromStack(item))); return MethodResult.of(items); } @@ -103,7 +103,7 @@ public final MethodResult listCraftableFluids() { return notConnected(); List fluids = new ArrayList<>(); - RefinedStorage.getCraftableFluids(getNetwork()).forEach(fluid -> fluids.add(RefinedStorage.getObjectFromFluid(fluid, getNetwork()))); + RefinedStorage.getCraftableFluids(getNetwork()).forEach(fluid -> fluids.add(RefinedStorage.getObjectFromFluid(fluid))); return MethodResult.of(fluids); } @@ -248,19 +248,24 @@ public final MethodResult getAvgPowerInjection() { @Override @LuaFunction(mainThread = true) public final MethodResult getFilteredPatterns(IArguments arguments) throws LuaException { - if (!isAvailable()) + if (!isAvailable()) { return notConnected(); + } Pair filter = ItemFilter.parse(arguments.getTable(0)); - if (filter.rightPresent()) + if (filter.rightPresent()) { return MethodResult.of(false, filter.getRight()); + } ItemFilter parsedFilter = filter.getLeft(); - if (parsedFilter.isEmpty()) + if (parsedFilter.isEmpty()) { return MethodResult.of(false, "EMPTY_FILTER"); + } - ItemStack patternItem = RefinedStorage.findStackFromFilter(getNetwork(), getNetwork().getCraftingManager(), parsedFilter); - + ItemStack patternItem = RefinedStorage.findPatternFromFilter(getNetwork().getCraftingManager(), parsedFilter); + if (patternItem == null || patternItem == ItemStack.EMPTY) { + return MethodResult.of(null, "NOT_CRAFTABLE"); + } return MethodResult.of(RefinedStorage.getObjectFromPattern(getNetwork().getCraftingManager().getPattern(patternItem), getNetwork())); } @@ -272,48 +277,56 @@ public final MethodResult getPatterns() { protected MethodResult exportToChest(@NotNull IArguments arguments, @Nullable IItemHandler targetInventory) throws LuaException { RsItemHandler itemHandler = new RsItemHandler(getNetwork()); - if (targetInventory == null) + if (targetInventory == null) { return MethodResult.of(0, "INVALID_TARGET"); + } Pair filter = ItemFilter.parse(arguments.getTable(0)); - if (filter.rightPresent()) + if (filter.rightPresent()) { return MethodResult.of(0, filter.getRight()); + } return MethodResult.of(InventoryUtil.moveItem(itemHandler, targetInventory, filter.getLeft()), null); } protected MethodResult importToSystem(@NotNull IArguments arguments, @Nullable IItemHandler targetInventory) throws LuaException { RsItemHandler itemHandler = new RsItemHandler(getNetwork()); - if (targetInventory == null) + if (targetInventory == null) { return MethodResult.of(0, "INVALID_TARGET"); + } Pair filter = ItemFilter.parse(arguments.getTable(0)); - if (filter.rightPresent()) + if (filter.rightPresent()) { return MethodResult.of(0, filter.getRight()); + } return MethodResult.of(InventoryUtil.moveItem(targetInventory, itemHandler, filter.getLeft()), null); } protected MethodResult exportToTank(@NotNull IArguments arguments, @Nullable IFluidHandler targetInventory) throws LuaException { RsFluidHandler itemHandler = new RsFluidHandler(getNetwork()); - if (targetInventory == null) + if (targetInventory == null) { return MethodResult.of(0, "INVALID_TARGET"); + } Pair filter = FluidFilter.parse(arguments.getTable(0)); - if (filter.rightPresent()) + if (filter.rightPresent()) { return MethodResult.of(0, filter.getRight()); + } return MethodResult.of(InventoryUtil.moveFluid(itemHandler, targetInventory, filter.getLeft()), null); } protected MethodResult importToSystem(@NotNull IArguments arguments, @Nullable IFluidHandler targetInventory) throws LuaException { RsFluidHandler itemHandler = new RsFluidHandler(getNetwork()); - if (targetInventory == null) + if (targetInventory == null) { return MethodResult.of(0, "INVALID_TARGET"); + } Pair filter = FluidFilter.parse(arguments.getTable(0)); - if (filter.rightPresent()) + if (filter.rightPresent()) { return MethodResult.of(0, filter.getRight()); + } return MethodResult.of(InventoryUtil.moveFluid(targetInventory, itemHandler, filter.getLeft()), null); } @@ -321,8 +334,9 @@ protected MethodResult importToSystem(@NotNull IArguments arguments, @Nullable I @Override @LuaFunction(mainThread = true) public final MethodResult exportItem(IComputerAccess computer, IArguments arguments) throws LuaException { - if (!isAvailable()) + if (!isAvailable()) { return notConnected(); + } String side = arguments.getString(1); IItemHandler inventory; @@ -339,8 +353,9 @@ public final MethodResult exportItem(IComputerAccess computer, IArguments argume @Override @LuaFunction(mainThread = true) public final MethodResult importItem(IComputerAccess computer, IArguments arguments) throws LuaException { - if (!isAvailable()) + if (!isAvailable()) { return notConnected(); + } String side = arguments.getString(1); IItemHandler inventory; @@ -357,8 +372,9 @@ public final MethodResult importItem(IComputerAccess computer, IArguments argume @Override @LuaFunction(mainThread = true) public final MethodResult exportFluid(IComputerAccess computer, IArguments arguments) throws LuaException { - if (!isAvailable()) + if (!isAvailable()) { return notConnected(); + } String side = arguments.getString(1); IFluidHandler fluidHandler; @@ -390,8 +406,9 @@ public final MethodResult importFluid(IComputerAccess computer, IArguments argum fluidHandler = FluidUtil.getHandlerFromName(computer, arguments.getString(1)); } - if (fluidHandler == null) + if (fluidHandler == null) { return MethodResult.of(0, "The target tank does not exist. Make sure the bridge is exposed in the computer network. Reach out to our discord or our documentation for help."); + } return importToSystem(arguments, fluidHandler); } @@ -399,14 +416,21 @@ public final MethodResult importFluid(IComputerAccess computer, IArguments argum @Override @LuaFunction(mainThread = true) public final MethodResult getItem(IArguments arguments) throws LuaException { - if (!isAvailable()) + if (!isAvailable()) { return notConnected(); + } Pair filter = ItemFilter.parse(arguments.getTable(0)); - if (filter.rightPresent()) + if (filter.rightPresent()) { return MethodResult.of(null, filter.getRight()); - - return MethodResult.of(RefinedStorage.getObjectFromStack(RefinedStorage.findStackFromFilter(getNetwork(), getNetwork().getCraftingManager(), filter.getLeft()), getNetwork())); + } + ItemStack stored = RefinedStorage.findStackFromFilter(getNetwork(), filter.getLeft()); + ItemStack craftable = RefinedStorage.findPatternFromFilter(getNetwork().getCraftingManager(), filter.getLeft()); + Object res = RefinedStorage.getObjectFromStack(stored, craftable); + if (res == null) { + return MethodResult.of(null, "ITEM_NOT_FOUND"); + } + return MethodResult.of(res); } @Override @@ -418,22 +442,26 @@ public final MethodResult getFluid(IArguments arguments) throws LuaException { @Override @LuaFunction(mainThread = true) public final MethodResult craftItem(IComputerAccess computerAccess, IArguments arguments) throws LuaException { - if (!isAvailable()) + if (!isAvailable()) { return notConnected(); + } Pair filter = ItemFilter.parse(arguments.getTable(0)); - if (filter.rightPresent()) + if (filter.rightPresent()) { return MethodResult.of(null, filter.getRight()); + } - ItemStack stack = RefinedStorage.findStackFromFilter(getNetwork(), getNetwork().getCraftingManager(), filter.getLeft()); - if (stack == null) + ItemStack stack = RefinedStorage.findPatternFromFilter(getNetwork().getCraftingManager(), filter.getLeft()); + if (stack == null || stack == ItemStack.EMPTY) { return MethodResult.of(null, "NOT_CRAFTABLE"); + } ICalculationResult result = getNetwork().getCraftingManager().create(stack, filter.getLeft().getCount()); CalculationResultType type = result.getType(); - if (type == CalculationResultType.OK) + if (type == CalculationResultType.OK) { getNetwork().getCraftingManager().start(result.getTask()); + } AdvancedPeripherals.debug("Crafting Result of '" + ItemUtil.getRegistryKey(stack).toString() + "':" + type); return MethodResult.of(type == CalculationResultType.OK); } @@ -448,14 +476,16 @@ public final MethodResult craftFluid(IComputerAccess computerAccess, IArguments if (filter.rightPresent()) return MethodResult.of(null, filter.getRight()); - FluidStack stack = RefinedStorage.findFluidFromFilter(getNetwork(), getNetwork().getCraftingManager(), filter.getLeft()); - if (stack == null) + FluidStack stack = RefinedStorage.findFluidPatternFromFilter(getNetwork().getCraftingManager(), filter.getLeft()); + if (stack == null || stack == FluidStack.EMPTY) { return MethodResult.of(null, "NOT_CRAFTABLE"); + } ICalculationResult result = getNetwork().getCraftingManager().create(stack, filter.getLeft().getCount()); CalculationResultType type = result.getType(); - if (type == CalculationResultType.OK) + if (type == CalculationResultType.OK) { getNetwork().getCraftingManager().start(result.getTask()); + } AdvancedPeripherals.debug("Crafting Result of '" + FluidUtil.getRegistryKey(stack).toString() + "':" + type); return MethodResult.of(type == CalculationResultType.OK); } @@ -470,23 +500,46 @@ public MethodResult cancelCraftingTasks(IArguments arguments) throws LuaExceptio return null; } + @Override + @LuaFunction(mainThread = true) + public final MethodResult isItemCraftable(IArguments arguments) throws LuaException { + if (!isAvailable()) { + return notConnected(); + } + + Pair filter = ItemFilter.parse(arguments.getTable(0)); + if (filter.rightPresent()) { + return MethodResult.of(false, filter.getRight()); + } + + ItemFilter parsedFilter = filter.getLeft(); + if (parsedFilter.isEmpty()) { + return MethodResult.of(false, "EMPTY_FILTER"); + } + return MethodResult.of(RefinedStorage.isItemCraftable(getNetwork(), parsedFilter.toItemStack())); + } + @Override @LuaFunction(mainThread = true) public final MethodResult isItemCrafting(IArguments arguments) throws LuaException { - if (!isAvailable()) + if (!isAvailable()) { return notConnected(); + } Pair filter = ItemFilter.parse(arguments.getTable(0)); - if (filter.rightPresent()) + if (filter.rightPresent()) { return MethodResult.of(null, filter.getRight()); + } - ItemStack stack = RefinedStorage.findStackFromFilter(getNetwork(), getNetwork().getCraftingManager(), filter.getLeft()); - if (stack == null) + ItemStack stack = RefinedStorage.findPatternFromFilter(getNetwork().getCraftingManager(), filter.getLeft()); + if (stack == null || stack == ItemStack.EMPTY) { return MethodResult.of(null, "NOT_CRAFTABLE"); + } for (ICraftingTask task : getNetwork().getCraftingManager().getTasks()) { ItemStack taskStack = task.getRequested().getItem(); - if (taskStack != null && taskStack.sameItem(stack)) + if (taskStack != null && taskStack.sameItem(stack)) { return MethodResult.of(true); + } } return MethodResult.of(false); } @@ -494,28 +547,44 @@ public final MethodResult isItemCrafting(IArguments arguments) throws LuaExcepti @Override @LuaFunction(mainThread = true) public final MethodResult isFluidCraftable(IArguments arguments) throws LuaException { - return null; - } + if (!isAvailable()) { + return notConnected(); + } - @Override - @LuaFunction(mainThread = true) - public final MethodResult isFluidCrafting(IArguments arguments) throws LuaException { - return null; + Pair filter = FluidFilter.parse(arguments.getTable(0)); + if (filter.rightPresent()) { + return MethodResult.of(false, filter.getRight()); + } + + FluidFilter parsedFilter = filter.getLeft(); + if (parsedFilter.isEmpty()) { + return MethodResult.of(false, "EMPTY_FILTER"); + } + return MethodResult.of(RefinedStorage.isFluidCraftable(getNetwork(), parsedFilter.toFluidStack())); } @Override @LuaFunction(mainThread = true) - public final MethodResult isItemCraftable(IArguments arguments) throws LuaException { - if (!isAvailable()) + public final MethodResult isFluidCrafting(IArguments arguments) throws LuaException { + if (!isAvailable()) { return notConnected(); + } + Pair filter = FluidFilter.parse(arguments.getTable(0)); + if (filter.rightPresent()) { + return MethodResult.of(null, filter.getRight()); + } - Pair filter = ItemFilter.parse(arguments.getTable(0)); - if (filter.rightPresent()) - return MethodResult.of(false, filter.getRight()); + FluidStack stack = RefinedStorage.findFluidPatternFromFilter(getNetwork().getCraftingManager(), filter.getLeft()); + if (stack == null || stack == FluidStack.EMPTY) { + return MethodResult.of(null, "NOT_CRAFTABLE"); + } - ItemFilter parsedFilter = filter.getLeft(); - if (parsedFilter.isEmpty()) - return MethodResult.of(false, "EMPTY_FILTER"); - return MethodResult.of(RefinedStorage.isItemCraftable(getNetwork(), parsedFilter.toItemStack())); + for (ICraftingTask task : getNetwork().getCraftingManager().getTasks()) { + FluidStack taskStack = task.getRequested().getFluid(); + if (taskStack != null && taskStack.isFluidEqual(stack)) { + return MethodResult.of(true); + } + } + return MethodResult.of(false); } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/refinedstorage/RefinedStorage.java b/src/main/java/de/srendi/advancedperipherals/common/addons/refinedstorage/RefinedStorage.java index 2a102e1ea..1187169fe 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/refinedstorage/RefinedStorage.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/refinedstorage/RefinedStorage.java @@ -25,7 +25,7 @@ import org.jetbrains.annotations.Nullable; import java.util.*; -import java.util.stream.Collectors; +import java.util.stream.Stream; public class RefinedStorage { @@ -43,60 +43,77 @@ private static INetworkNode read(CompoundTag tag, NetworkNode node) { return node; } - public static ItemStack findStackFromStack(INetwork network, @Nullable ICraftingManager crafting, ItemStack item) { - return findStackFromFilter(network, crafting, ItemFilter.fromStack(item)); + public static ItemStack findStackFromStack(INetwork network, ItemStack item) { + return findStackFromFilter(network, ItemFilter.fromStack(item)); } - public static ItemStack findStackFromFilter(INetwork network, @Nullable ICraftingManager crafting, ItemFilter filter) { + public static ItemStack findPatternFromStack(ICraftingManager crafting, ItemStack item) { + return findPatternFromFilter(crafting, ItemFilter.fromStack(item)); + } + + public static ItemStack findStackFromFilter(INetwork network, ItemFilter filter) { for (StackListEntry temp : network.getItemStorageCache().getList().getStacks()) { - if (filter.test(temp.getStack().copy())) - return temp.getStack().copy(); + ItemStack stack = temp.getStack(); + if (filter.test(stack)) { + return stack.copy(); + } } - if (crafting == null) - return ItemStack.EMPTY; + return ItemStack.EMPTY; + } + public static ItemStack findPatternFromFilter(ICraftingManager crafting, ItemFilter filter) { for (ICraftingPattern pattern : crafting.getPatterns()) { for (ItemStack stack : pattern.getOutputs()) { - if (filter.test(stack.copy())) + if (filter.test(stack)) { return stack.copy(); + } } } return ItemStack.EMPTY; } - public static FluidStack findFluidFromStack(INetwork network, @Nullable ICraftingManager crafting, FluidStack stack) { - return findFluidFromFilter(network, crafting, FluidFilter.fromStack(stack)); + public static FluidStack findFluidFromStack(INetwork network, FluidStack stack) { + return findFluidFromFilter(network, FluidFilter.fromStack(stack)); + } + + public static FluidStack findFluidPatternFromStack(ICraftingManager crafting, FluidStack stack) { + return findFluidPatternFromFilter(crafting, FluidFilter.fromStack(stack)); } - public static FluidStack findFluidFromFilter(INetwork network, @Nullable ICraftingManager crafting, FluidFilter filter) { + public static FluidStack findFluidFromFilter(INetwork network, FluidFilter filter) { for (StackListEntry temp : network.getFluidStorageCache().getList().getStacks()) { - if (filter.test(temp.getStack().copy())) - return temp.getStack().copy(); + FluidStack stack = temp.getStack(); + if (filter.test(stack)) { + return stack.copy(); + } } - if (crafting == null) - return FluidStack.EMPTY; + return FluidStack.EMPTY; + } + public static FluidStack findFluidPatternFromFilter(ICraftingManager crafting, FluidFilter filter) { for (ICraftingPattern pattern : crafting.getPatterns()) { - if (pattern.getFluidOutputs().stream().anyMatch(filter::test)) - return pattern.getFluidOutputs().stream().filter(filter::test).findFirst().orElse(FluidStack.EMPTY).copy(); + FluidStack stack = pattern.getFluidOutputs().stream().filter(filter::test).findFirst().orElse(FluidStack.EMPTY); + if (stack != FluidStack.EMPTY) { + return stack.copy(); + } } return FluidStack.EMPTY; } - public static Object listFluids(INetwork network) { - List fluids = new ArrayList<>(); - getFluids(network).forEach(item -> fluids.add(getObjectFromFluid(item, network))); - return fluids; + public static Object listItems(INetwork network) { + Map craftables = new HashMap<>(); + getCraftableItemsStream(network).forEach(stack -> craftables.put(ItemUtil.asMapKey(stack), stack)); + return getItemsStream(network).map(item -> getObjectFromStack(item, craftables.get(ItemUtil.asMapKey(item)))).toList(); } - public static Object listItems(INetwork network) { - List items = new ArrayList<>(); - getItems(network).forEach(item -> items.add(getObjectFromStack(item, network))); - return items; + public static Object listFluids(INetwork network) { + Map craftables = new HashMap<>(); + getCraftableFluidsStream(network).forEach(stack -> craftables.put(ItemUtil.fluidAsMapKey(stack), stack)); + return getFluidsStream(network).map(item -> getObjectFromFluid(item, craftables.get(ItemUtil.fluidAsMapKey(item)))).toList(); } public static boolean isItemCraftable(INetwork network, ItemStack stack) { @@ -158,26 +175,18 @@ public static Object getObjectFromPattern(ICraftingPattern pattern, INetwork net return null; Map map = new HashMap<>(); - map.put("outputs", pattern.getOutputs().stream().map(stack -> getObjectFromStack(stack.copy(), network)).toList()); - map.put("fluidOutputs", pattern.getFluidOutputs().stream().map(stack -> getObjectFromFluid(stack.copy(), network)).toList()); - - List>> inputs = pattern.getInputs().stream() - .map(singleInputList -> singleInputList.stream() - .map(stack -> getObjectFromStack(stack.copy(), network)) - .collect(Collectors.toList())) - .collect(Collectors.toList()); - - List>> fluidInputs = pattern.getInputs().stream() - .map(singleInputList -> singleInputList.stream() - .map(stack -> getObjectFromStack(stack.copy(), network)) - .collect(Collectors.toList())) - .collect(Collectors.toList()); - - List byproducts = new ArrayList<>(); + map.put("outputs", pattern.getOutputs().stream().map(RefinedStorage::getObjectFromStack).toList()); + map.put("fluidOutputs", pattern.getFluidOutputs().stream().map(RefinedStorage::getObjectFromFluid).toList()); + + List>> inputs = pattern.getInputs().stream().map(singleInputList -> + singleInputList.stream().map(RefinedStorage::getObjectFromStack).toList()).toList(); + + List>> fluidInputs = pattern.getInputs().stream().map(singleInputList -> + singleInputList.stream().map(RefinedStorage::getObjectFromStack).toList()).toList(); + + List> byproducts = new ArrayList<>(); if (!pattern.isProcessing()) { - byproducts = pattern.getByproducts().stream() - .map(stack -> getObjectFromStack(stack.copy(), network)) - .collect(Collectors.toList()); + byproducts = pattern.getByproducts().stream().map(RefinedStorage::getObjectFromStack).toList(); } map.put("fluidInputs", fluidInputs); @@ -194,84 +203,138 @@ public static Object getObjectFromPattern(ICraftingPattern pattern, INetwork net return map; } - public static Map getObjectFromStack(@Nullable ItemStack itemStack, INetwork network) { - if (itemStack == null) - return Collections.emptyMap(); + @Nullable + public static Map getObjectFromStack(@Nullable ItemStack item) { + if (item == null) { + return null; + } + Map map = LuaConverter.itemToObject(item.getItem()); + map.put("amount", item.getCount()); + map.put("fingerprint", ItemUtil.getFingerprint(item)); + map.put("displayName", item.getDisplayName().getString()); + CompoundTag nbt = item.getTag(); + map.put("nbt", nbt == null ? null : NBTUtil.toLua(nbt)); + return map; + } + + @Nullable + public static Map getObjectFromStack(@Nullable ItemStack stored, @Nullable ItemStack craftable) { + Map map; + if (stored == null || stored == ItemStack.EMPTY) { + if (craftable == null || craftable == ItemStack.EMPTY) { + return null; + } + map = LuaConverter.itemToObject(craftable.getItem()); + map.put("amount", 0); + stored = craftable; + } else { + map = LuaConverter.itemToObject(stored.getItem()); + map.put("amount", stored.getCount()); + } - Map map = LuaConverter.itemToObject(itemStack.getItem()); - CompoundTag nbt = itemStack.getTag(); - map.put("fingerprint", ItemUtil.getFingerprint(itemStack)); - map.put("amount", itemStack.getCount()); - map.put("displayName", itemStack.getDisplayName().getString()); - map.put("isCraftable", isItemCraftable(network, itemStack)); + map.put("fingerprint", ItemUtil.getFingerprint(stored)); + map.put("displayName", stored.getDisplayName().getString()); + CompoundTag nbt = stored.getTag(); map.put("nbt", nbt == null ? null : NBTUtil.toLua(nbt)); + if (craftable == null) { + map.put("isCraftable", false); + } else { + map.put("isCraftable", true); + map.put("craftAmount", craftable.getCount()); + } + + return map; + } + + @Nullable + public static Map getObjectFromFluid(@Nullable FluidStack item) { + if (item == null) { + return null; + } + Map map = LuaConverter.fluidToObject(item.getFluid()); + map.put("amount", item.getAmount()); + map.put("displayName", item.getDisplayName().getString()); + CompoundTag nbt = item.getTag(); + map.put("nbt", nbt == null ? null : NBTUtil.toLua(nbt)); return map; } - public static Map getObjectFromFluid(@Nullable FluidStack fluidStack, INetwork network) { - if (fluidStack == null) - return Collections.emptyMap(); + @Nullable + public static Map getObjectFromFluid(@Nullable FluidStack stored, @Nullable FluidStack craftable) { + Map map; + if (stored == null) { + if (craftable == null) { + return null; + } + map = LuaConverter.fluidToObject(craftable.getFluid()); + map.put("amount", 0); + stored = craftable; + } else { + map = LuaConverter.fluidToObject(stored.getFluid()); + map.put("amount", stored.getAmount()); + } - Map map = LuaConverter.fluidToObject(fluidStack.getFluid()); - map.put("amount", fluidStack.getAmount()); - map.put("displayName", fluidStack.getDisplayName().getString()); - map.put("isCraftable", isFluidCraftable(network, fluidStack)); + map.put("displayName", stored.getDisplayName().getString()); + + if (craftable == null) { + map.put("isCraftable", false); + } else { + map.put("isCraftable", true); + map.put("craftAmount", craftable.getAmount()); + } return map; } public static Object getItem(INetwork network, ItemStack item) { - for (ItemStack itemStack : getItems(network)) { - if (itemStack.sameItem(item) && Objects.equals(itemStack.getTag(), item.getTag())) - return getObjectFromStack(itemStack.copy(), network); - } - return null; + ItemFilter filter = ItemFilter.fromStack(item); + ItemStack stored = findStackFromFilter(network, filter); + ItemStack craftable = findPatternFromFilter(network.getCraftingManager(), filter); + return getObjectFromStack(stored, craftable); } - public static List getCraftableItems(INetwork network) { - IStorageCache cache = network.getItemStorageCache(); - Collection> craftableEntries = cache.getCraftablesList().getStacks(); - List result = new ArrayList<>(craftableEntries.size()); + public static Object getFluid(INetwork network, FluidStack fluid) { + FluidFilter filter = FluidFilter.fromStack(fluid); + FluidStack stored = findFluidFromFilter(network, filter); + FluidStack craftable = findFluidPatternFromFilter(network.getCraftingManager(), filter); + return getObjectFromFluid(stored, craftable); + } - for (StackListEntry entry : craftableEntries) { - result.add(entry.getStack().copy()); - } + public static Stream getCraftableItemsStream(INetwork network) { + IStorageCache cache = network.getItemStorageCache(); + return cache.getCraftablesList().getStacks().stream().map(entry -> entry.getStack().copy()); + } - return result; + public static List getCraftableItems(INetwork network) { + return getCraftableItemsStream(network).toList(); } - public static List getCraftableFluids(INetwork network) { + public static Stream getCraftableFluidsStream(INetwork network) { IStorageCache cache = network.getFluidStorageCache(); - Collection> craftableEntries = cache.getCraftablesList().getStacks(); - List result = new ArrayList<>(craftableEntries.size()); - - for (StackListEntry entry : craftableEntries) - result.add(entry.getStack().copy()); + return cache.getCraftablesList().getStacks().stream().map(entry -> entry.getStack().copy()); + } - return result; + public static List getCraftableFluids(INetwork network) { + return getCraftableFluidsStream(network).toList(); } - public static List getItems(INetwork network) { + public static Stream getItemsStream(INetwork network) { IStorageCache cache = network.getItemStorageCache(); - Collection> entries = cache.getList().getStacks(); - List result = new ArrayList<>(entries.size()); - - for (StackListEntry entry : entries) - result.add(entry.getStack().copy()); + return cache.getList().getStacks().stream().map(entry -> entry.getStack().copy()); + } - return result; + public static List getItems(INetwork network) { + return getItemsStream(network).toList(); } - public static List getFluids(INetwork network) { + public static Stream getFluidsStream(INetwork network) { IStorageCache cache = network.getFluidStorageCache(); - Collection> entries = cache.getList().getStacks(); - List result = new ArrayList<>(entries.size()); - - for (StackListEntry entry : entries) - result.add(entry.getStack().copy()); + return cache.getList().getStacks().stream().map(entry -> entry.getStack().copy()); + } - return result; + public static List getFluids(INetwork network) { + return getFluidsStream(network).toList(); } public void initiate() { diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/refinedstorage/RsFluidHandler.java b/src/main/java/de/srendi/advancedperipherals/common/addons/refinedstorage/RsFluidHandler.java index 06da6c0bf..aaf928b0e 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/refinedstorage/RsFluidHandler.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/refinedstorage/RsFluidHandler.java @@ -19,17 +19,19 @@ public RsFluidHandler(@NotNull INetwork network) { @Override public int fill(FluidStack resource, FluidAction action) { - if (resource.isEmpty()) + if (resource.isEmpty()) { return 0; + } return resource.getAmount() - network.insertFluid(resource, resource.getAmount(), action == FluidAction.SIMULATE ? Action.SIMULATE : Action.PERFORM).getAmount(); } @NotNull @Override public FluidStack drain(FluidFilter filter, FluidAction simulate) { - FluidStack fluid = RefinedStorage.findFluidFromFilter(network, null, filter); - if (fluid == null) + FluidStack fluid = RefinedStorage.findFluidFromFilter(network, filter); + if (fluid == null) { return FluidStack.EMPTY; + } return network.extractFluid(fluid, filter.getCount(), IComparer.COMPARE_QUANTITY, simulate == FluidAction.SIMULATE ? Action.SIMULATE : Action.PERFORM); } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/refinedstorage/RsItemHandler.java b/src/main/java/de/srendi/advancedperipherals/common/addons/refinedstorage/RsItemHandler.java index 900c8a5b0..2a0392c32 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/refinedstorage/RsItemHandler.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/refinedstorage/RsItemHandler.java @@ -31,11 +31,11 @@ public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate @Override public ItemStack extractItem(ItemFilter filter, int count, boolean simulate) { - ItemStack item = RefinedStorage.findStackFromFilter(network, network.getCraftingManager(), filter); - if (item == null) + ItemStack item = RefinedStorage.findStackFromFilter(network, filter); + if (item == null || item == ItemStack.EMPTY) { AdvancedPeripherals.debug("Trying to extract item: " + item + " from filter: " + filter); - if(item == null) return ItemStack.EMPTY; + } ItemStack extracted = network.extractItem(item, count, IComparer.COMPARE_NBT, simulate ? Action.SIMULATE : Action.PERFORM); AdvancedPeripherals.debug("Extracted item: " + extracted + " from filter: " + filter); return extracted; diff --git a/src/main/java/de/srendi/advancedperipherals/common/util/Pair.java b/src/main/java/de/srendi/advancedperipherals/common/util/Pair.java index fe2b993dd..6f925b0fb 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/util/Pair.java +++ b/src/main/java/de/srendi/advancedperipherals/common/util/Pair.java @@ -1,5 +1,6 @@ package de.srendi.advancedperipherals.common.util; +import java.util.Objects; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Function; @@ -74,4 +75,14 @@ public Pair ignoreRight() { public R reduce(BiFunction reduceFunc) { return reduceFunc.apply(left, right); } + + @Override + public int hashCode() { + return Objects.hash(getLeft(), getRight()); + } + + @Override + public boolean equals(Object other) { + return this == other || (other instanceof Pair pair && Objects.equals(this.getLeft(), pair.getLeft()) && Objects.equals(this.getRight(), pair.getRight())); + } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/util/inventory/ItemFilter.java b/src/main/java/de/srendi/advancedperipherals/common/util/inventory/ItemFilter.java index cfc9d4892..dce7c3f37 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/util/inventory/ItemFilter.java +++ b/src/main/java/de/srendi/advancedperipherals/common/util/inventory/ItemFilter.java @@ -95,6 +95,9 @@ public static Pair parse(Map item) { public static ItemFilter fromStack(ItemStack stack) { ItemFilter filter = empty(); + if (stack == null || stack == ItemStack.EMPTY) { + return filter; + } filter.item = stack.getItem(); filter.nbt = stack.hasTag() ? stack.getTag() : null; return filter; diff --git a/src/main/java/de/srendi/advancedperipherals/common/util/inventory/ItemUtil.java b/src/main/java/de/srendi/advancedperipherals/common/util/inventory/ItemUtil.java index fbd61f7f2..011b21b28 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/util/inventory/ItemUtil.java +++ b/src/main/java/de/srendi/advancedperipherals/common/util/inventory/ItemUtil.java @@ -2,12 +2,14 @@ import dan200.computercraft.shared.Registry; import de.srendi.advancedperipherals.AdvancedPeripherals; +import de.srendi.advancedperipherals.common.util.Pair; import de.srendi.advancedperipherals.common.util.StringUtil; import net.minecraft.ResourceLocationException; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraftforge.items.IItemHandler; +import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.IForgeRegistry; import org.apache.logging.log4j.Level; @@ -49,11 +51,12 @@ public static T getRegistryEntry(String name, IForgeRegistry forgeRegistr * @return A generated MD5 hash from the item stack */ public static String getFingerprint(ItemStack stack) { - String fingerprint = stack.getOrCreateTag() + getRegistryKey(stack).toString() + stack.getDisplayName().getString(); try { - byte[] bytesOfHash = fingerprint.getBytes(StandardCharsets.UTF_8); MessageDigest md = MessageDigest.getInstance("MD5"); - return StringUtil.toHexString(md.digest(bytesOfHash)); + md.update(stack.getOrCreateTag().toString().getBytes(StandardCharsets.UTF_8)); + md.update(getRegistryKey(stack).toString().getBytes(StandardCharsets.UTF_8)); + md.update(stack.getDisplayName().getString().getBytes(StandardCharsets.UTF_8)); + return StringUtil.toHexString(md.digest()); } catch (NoSuchAlgorithmException ex) { AdvancedPeripherals.debug("Could not parse fingerprint.", Level.ERROR); ex.printStackTrace(); @@ -61,6 +64,14 @@ public static String getFingerprint(ItemStack stack) { return ""; } + public static Object asMapKey(ItemStack stack) { + return new Pair(stack.getItem(), stack.hasTag() ? stack.getTag() : null); + } + + public static Object fluidAsMapKey(FluidStack stack) { + return new Pair(stack.getFluid(), stack.hasTag() ? stack.getTag() : null); + } + public static ItemStack makeTurtle(Item turtle, String upgrade) { ItemStack stack = new ItemStack(turtle); stack.getOrCreateTag().putString("RightUpgrade", upgrade);