From 522f434513001454cca50f7c679a22599737919a Mon Sep 17 00:00:00 2001 From: repo_alt Date: Tue, 1 Mar 2022 19:05:20 +0300 Subject: [PATCH] New pattern manipulation functions: storeInterfacePatternInput(slot:number, index:number, database:address, entry:number):boolean -- Store pattern input at the given index to the database entry. storeInterfacePatternOutput(slot:number, index:number, database:address, entry:number):boolean -- Store pattern output at the given index to the database entry. clearInterfacePatternInput(slot:number, index:number):boolean -- Clear pattern input at the given index. clearInterfacePatternOutput(slot:number, index:number):boolean -- Clear pattern output at the given index. --- .../appeng/DriverBlockInterface.scala | 76 ++++++++++++++++--- 1 file changed, 66 insertions(+), 10 deletions(-) diff --git a/src/main/scala/li/cil/oc/integration/appeng/DriverBlockInterface.scala b/src/main/scala/li/cil/oc/integration/appeng/DriverBlockInterface.scala index 85db380a6..9c1e368f4 100644 --- a/src/main/scala/li/cil/oc/integration/appeng/DriverBlockInterface.scala +++ b/src/main/scala/li/cil/oc/integration/appeng/DriverBlockInterface.scala @@ -11,10 +11,11 @@ import li.cil.oc.api.network.Component import li.cil.oc.api.network.ManagedEnvironment import li.cil.oc.api.prefab.DriverSidedTileEntity import li.cil.oc.integration.ManagedTileEntityEnvironment +import li.cil.oc.util.DatabaseAccess import li.cil.oc.util.ExtendedArguments._ -import li.cil.oc.util.ResultWrapper._ +import li.cil.oc.util.ResultWrapper.result import net.minecraft.item.ItemStack -import net.minecraft.nbt.{NBTTagCompound, NBTTagList} +import net.minecraft.nbt.NBTTagCompound import net.minecraft.world.World import net.minecraftforge.common.util.ForgeDirection @@ -29,6 +30,7 @@ object DriverBlockInterface extends DriverSidedTileEntity { override def priority = 5 + //noinspection ScalaUnusedSymbol @Callback(doc = "function([slot:number]):table -- Get the configuration of the interface.") def getInterfaceConfiguration(context: Context, args: Arguments): Array[AnyRef] = { val config = tileEntity.getInventoryByName("config") @@ -68,6 +70,7 @@ object DriverBlockInterface extends DriverSidedTileEntity { } else null + //noinspection ScalaUnusedSymbol @Callback(doc = "function([slot:number]):table -- Get the given pattern in the interface.") def getInterfacePattern(context: Context, args: Arguments): Array[AnyRef] = { val inv = tileEntity.getInventoryByName("patterns") @@ -77,17 +80,65 @@ object DriverBlockInterface extends DriverSidedTileEntity { } @Callback(doc = "function(slot:number, database:address, entry:number, size:number, index:number):boolean -- Set the pattern input at the given index.") - def setInterfacePatternInput(context: Context, args: Arguments): Array[AnyRef] = { + def setInterfacePatternInput(context: Context, args: Arguments): Array[AnyRef] = setPatternSlot(context, args, "in") - result(true) - } + @Callback(doc = "function(slot:number, database:address, entry:number, size:number, index:number):boolean -- Set the pattern output at the given index.") - def setInterfacePatternOutput(context: Context, args: Arguments): Array[AnyRef] = { + def setInterfacePatternOutput(context: Context, args: Arguments): Array[AnyRef] = setPatternSlot(context, args, "out") + + @Callback(doc = "function(slot:number, index:number, database:address, entry:number):boolean -- Store pattern input at the given index to the database entry.") + def storeInterfacePatternInput(context: Context, args: Arguments): Array[AnyRef] = + storeInterfacePattern(context, args, "in") + + @Callback(doc = "function(slot:number, index:number, database:address, entry:number):boolean -- Store pattern output at the given index to the database entry.") + def storeInterfacePatternOutput(context: Context, args: Arguments): Array[AnyRef] = + storeInterfacePattern(context, args, "out") + + @Callback(doc = "function(slot:number, index:number):boolean -- Clear pattern input at the given index.") + def clearInterfacePatternInput(context: Context, args: Arguments): Array[AnyRef] = + clearInterfacePattern(context, args, "in") + + @Callback(doc = "function(slot:number, index:number):boolean -- Clear pattern output at the given index.") + def clearInterfacePatternOutput(context: Context, args: Arguments): Array[AnyRef] = + clearInterfacePattern(context, args, "out") + + + private def storeInterfacePattern(context: Context, args: Arguments, tag: String): Array[AnyRef] = { + val inv = tileEntity.getInventoryByName("patterns") + val pattern = inv.getStackInSlot(args.checkSlot(inv, 0)) + val encodedValue = pattern.getTagCompound + if (encodedValue == null) + throw new IllegalArgumentException("No pattern here!") + val nbt = encodedValue.getTagList(tag, 10) + val stackNBT = nbt.getCompoundTagAt(args.checkInteger(1)) + val stack = ItemStack.loadItemStackFromNBT(stackNBT) + DatabaseAccess.withDatabase(node, args.checkString(2), database => { + val slot = args.optSlot(database.data, 3, 0) + database.setStackInSlot(slot, stack) + context.pause(0.1) + result(true) + }) + } + + private def clearInterfacePattern(context: Context, args: Arguments, tag: String): Array[AnyRef] = { + val inv = tileEntity.getInventoryByName("patterns") + val slot = args.checkSlot(inv, 0) + val pattern = inv.getStackInSlot(slot) + val encodedValue = pattern.getTagCompound + if (encodedValue == null) + throw new IllegalArgumentException("No pattern here!") + val nbt = encodedValue.getTagList(tag, 10) + val index = args.checkInteger(1) + nbt.removeTag(index) + encodedValue.setTag(tag, nbt) + pattern.setTagCompound(encodedValue) + inv.setInventorySlotContents(slot, pattern) + context.pause(0.1) result(true) } - private def setPatternSlot(context: Context, args: Arguments, tag: String) = { + private def setPatternSlot(context: Context, args: Arguments, tag: String): Array[AnyRef] = { val inv = tileEntity.getInventoryByName("patterns") val slot = if (args.isString(0)) 0 else args.optSlot(inv, 0, 0) val stack = getStack(args) @@ -102,13 +153,18 @@ object DriverBlockInterface extends DriverSidedTileEntity { val inTag = encodedValue.getTagList(tag, 10) while (inTag.tagCount() <= index) inTag.appendTag(new NBTTagCompound()) - val nbt = new NBTTagCompound() - stack.writeToNBT(nbt) - inTag.func_150304_a(index, nbt) + if (stack != null) { + val nbt = new NBTTagCompound() + stack.writeToNBT(nbt) + inTag.func_150304_a(index, nbt) + } + else + inTag.removeTag(index) encodedValue.setTag(tag, inTag) pattern.setTagCompound(encodedValue) inv.setInventorySlotContents(slot, pattern) context.pause(0.1) + result(true) } }