Skip to content

Instantly share code, notes, and snippets.

@richie3366
Created May 1, 2024 14:23
Show Gist options
  • Save richie3366/458a3f4d290533c3cf694a34cfd2d1fe to your computer and use it in GitHub Desktop.
Save richie3366/458a3f4d290533c3cf694a34cfd2d1fe to your computer and use it in GitHub Desktop.
event.registerComplexPeripheral("gt_input_machine", metaMachineWrapper((machine) => {
return !!machine && (!!machine.importItems || !!machine.combinedInventory)
}))
.mainThreadMethod("exportInputItem", metaMachineWrapper((machine, block, _, args, computer) => {
if (args.length < 4) throw new Error("exportInputItem(fromSlot, strPeriphToName, toSlot, amount)")
const slotIndex = toInt(args[0]) - 1
const toName = args[1]
const toSlot = toInt(args[2]) - 1
const amount = toInt(args[3])
const storage = (machine.importItems || machine.inventory).storage
// simulate extract
let simulate = storage.extractItem(slotIndex, amount, true, false)
if (!Item.empty.equals(simulate)) {
// simulate insert
let peripheral = computer.getAvailablePeripheral(toName)
if (peripheral) {
let target = peripheral.getTarget()
if (target && target.getCapability) {
let cap = target.getCapability(ForgeCapabilities.ITEM_HANDLER).resolve()
if (cap && cap.isPresent()) {
let handler = cap.get()
let simulateInsert = handler.insertItem(toSlot, simulate, true)
if (Item.empty.equals(simulateInsert)) {
storage.extractItem(slotIndex, amount, false, true)
handler.insertItem(toSlot, simulate, false)
return true
}
}
}
}
}
return false
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment