Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement getItemInNetwork #147

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/main/scala/li/cil/oc/integration/appeng/NetworkControl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import appeng.api.util.AECableType
import appeng.me.cluster.implementations.CraftingCPUCluster
import appeng.me.helpers.IGridProxyable
import appeng.tile.crafting.TileCraftingMonitorTile
import appeng.util.IterationCounter
import appeng.util.item.AEItemStack
import com.google.common.collect.ImmutableSet
import li.cil.oc.OpenComputers
Expand All @@ -31,13 +32,14 @@ import li.cil.oc.util.DatabaseAccess
import li.cil.oc.util.ExtendedArguments._
import li.cil.oc.util.ExtendedNBT._
import li.cil.oc.util.ResultWrapper._
import net.minecraft.item.Item
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.item.{Item, ItemStack}
import net.minecraft.nbt.{JsonToNBT, NBTTagCompound}
import net.minecraft.tileentity.TileEntity
import net.minecraftforge.common.DimensionManager
import net.minecraftforge.common.util.Constants.NBT
import net.minecraftforge.common.util.ForgeDirection

import javax.annotation.Nonnull
import scala.collection.convert.WrapAsJava._
import scala.collection.convert.WrapAsScala._
import scala.collection.mutable
Expand All @@ -53,6 +55,7 @@ trait NetworkControl[AETile >: Null <: TileEntity with IGridProxyable with IActi
def node: Node

private def allItems: Iterable[IAEItemStack] = tile.getProxy.getStorage.getItemInventory.getStorageList
private def getAvailableItem(@Nonnull request: IAEItemStack, iteration: Int): IAEItemStack = tile.getProxy.getStorage.getItemInventory.getAvailableItem(request, iteration)
private def allCraftables: Iterable[IAEItemStack] = allItems.collect{ case aeItem if aeItem.isCraftable => aeCraftItem(aeItem, tile) }

@Callback(doc = "function():table -- Get a list of tables representing the available CPUs in the network.")
Expand Down Expand Up @@ -93,6 +96,25 @@ trait NetworkControl[AETile >: Null <: TileEntity with IGridProxyable with IActi
.toArray)
}

@Callback(doc = "function(name:string[, damage:number[, nbt:string]]):table -- Retrieves the stored item in the network by its unlocalized name.")
def getItemInNetwork(context: Context, args: Arguments): Array[AnyRef] = {
val item = Item.itemRegistry.getObject(args.checkString(0))
if (item == null) {
return result(null)
}

val itemStack = new ItemStack(item.asInstanceOf[Item])
itemStack.setItemDamage(args.optInteger(1, 0))

// The obfuscated method turns a json string into an NBTBase.
val nbtBase = JsonToNBT.func_150315_a(args.optString(2, "{}"))
itemStack.setTagCompound(nbtBase.asInstanceOf[NBTTagCompound])

val aeItemStack = AEItemStack.create(itemStack)
val availableItem = getAvailableItem(aeItemStack, IterationCounter.fetchNewId())
result(if (availableItem != null) convert(availableItem, tile) else null)
}

@Callback(doc = "function(filter:table):table -- Get a list of the stored items in the network matching the filter. Filter is an Array of Item IDs")
def getItemsInNetworkById(context: Context, args: Arguments): Array[AnyRef] = {
val table = args.checkTable(0)
Expand Down
Loading