From 72793aff25a7df08a6d5b38bfac2a936466daed4 Mon Sep 17 00:00:00 2001 From: repo_alt Date: Tue, 28 Dec 2021 11:50:29 +0300 Subject: [PATCH 1/2] Increase JVM memory pool in gradle script Allow debug card to insert huge amounts of items, e.g. to fill infinity chest --- gradlew | 2 +- gradlew.bat | 2 +- .../cil/oc/server/component/DebugCard.scala | 19 +++++++++++++++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/gradlew b/gradlew index 83f2acfdc..ebe7a79c6 100755 --- a/gradlew +++ b/gradlew @@ -44,7 +44,7 @@ APP_NAME="Gradle" APP_BASE_NAME=`basename "$0"` # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' +DEFAULT_JVM_OPTS='"-Xmx2G" "-Xms128m"' # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD="maximum" diff --git a/gradlew.bat b/gradlew.bat index 9618d8d96..7ebc3623d 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -30,7 +30,7 @@ set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" +set DEFAULT_JVM_OPTS="-Xmx2G" "-Xms128m" @rem Find java.exe if defined JAVA_HOME goto findJavaFromJavaHome diff --git a/src/main/scala/li/cil/oc/server/component/DebugCard.scala b/src/main/scala/li/cil/oc/server/component/DebugCard.scala index 8d2f0e301..667098358 100644 --- a/src/main/scala/li/cil/oc/server/component/DebugCard.scala +++ b/src/main/scala/li/cil/oc/server/component/DebugCard.scala @@ -619,7 +619,7 @@ object DebugCard { if (item == null) { throw new IllegalArgumentException("invalid item id") } - val count = args.checkInteger(1) + var count = args.checkInteger(1) val damage = args.checkInteger(2) val tagJson = args.optString(3, "") val tag = if (Strings.isNullOrEmpty(tagJson)) null else JsonToNBT.func_150315_a(tagJson).asInstanceOf[NBTTagCompound] @@ -629,7 +629,22 @@ object DebugCard { case Some(inventory) => val stack = new ItemStack(item, count, damage) stack.setTagCompound(tag) - result(InventoryUtils.insertIntoInventory(stack, inventory, Option(side))) + val res = InventoryUtils.insertIntoInventory(stack, inventory, Option(side), count) + if (!res) + result(res) + else { + var stored = count - stack.stackSize + while (stored > 0 && stack.stackSize > 0) { + count = stack.stackSize + // InventoryUtils.insertIntoInventory honors max stack size for the stack, + // but debug card may ignore that e.g. for infinity chest + // so we try to insert the rest until we fail + if (!InventoryUtils.insertIntoInventory(stack, inventory, Option(side), count)) + return result(true) + stored = count - stack.stackSize + } + result(true) + } case _ => result(Unit, "no inventory") } } From 962e7d67ef8e2015c8806dcfb05a05ed7c4a9b70 Mon Sep 17 00:00:00 2001 From: repo_alt Date: Wed, 29 Dec 2021 16:52:16 +0300 Subject: [PATCH 2/2] Added a function to set database slot with an item constructed from description (also some warnings cleanup) --- .../oc/server/component/UpgradeDatabase.scala | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/main/scala/li/cil/oc/server/component/UpgradeDatabase.scala b/src/main/scala/li/cil/oc/server/component/UpgradeDatabase.scala index d38f9ae79..050967bd1 100644 --- a/src/main/scala/li/cil/oc/server/component/UpgradeDatabase.scala +++ b/src/main/scala/li/cil/oc/server/component/UpgradeDatabase.scala @@ -2,6 +2,7 @@ package li.cil.oc.server.component import java.util +import com.google.common.base.Strings import com.google.common.hash.Hashing import li.cil.oc.Constants import li.cil.oc.api.driver.DeviceInfo.DeviceAttribute @@ -12,19 +13,19 @@ import li.cil.oc.api.internal import li.cil.oc.api.machine.Arguments import li.cil.oc.api.machine.Callback import li.cil.oc.api.machine.Context -import li.cil.oc.api.network.Visibility +import li.cil.oc.api.network.{Node, Visibility} import li.cil.oc.api.prefab import li.cil.oc.util.DatabaseAccess import li.cil.oc.util.ExtendedArguments._ import li.cil.oc.util.ExtendedNBT._ import net.minecraft.inventory.IInventory -import net.minecraft.item.ItemStack -import net.minecraft.nbt.CompressedStreamTools +import net.minecraft.item.{Item, ItemStack} +import net.minecraft.nbt.{CompressedStreamTools, JsonToNBT, NBTTagCompound} import scala.collection.convert.WrapAsJava._ class UpgradeDatabase(val data: IInventory) extends prefab.ManagedEnvironment with internal.Database with DeviceInfo { - override val node = Network.newNode(this, Visibility.Network). + override val node: Node = Network.newNode(this, Visibility.Network). withComponent("database"). create() @@ -38,17 +39,19 @@ class UpgradeDatabase(val data: IInventory) extends prefab.ManagedEnvironment wi override def getDeviceInfo: util.Map[String, String] = deviceInfo - override def size = data.getSizeInventory + override def size: Int = data.getSizeInventory - override def getStackInSlot(slot: Int) = Option(data.getStackInSlot(slot)).map(_.copy()).orNull + override def getStackInSlot(slot: Int): ItemStack = Option(data.getStackInSlot(slot)).map(_.copy()).orNull - override def setStackInSlot(slot: Int, stack: ItemStack) = data.setInventorySlotContents(slot, stack) + override def setStackInSlot(slot: Int, stack: ItemStack): Unit = data.setInventorySlotContents(slot, stack) - override def findStackWithHash(needle: String) = indexOf(needle) + override def findStackWithHash(needle: String): Int = indexOf(needle) + //noinspection ScalaUnusedSymbol @Callback(doc = "function(slot:number):table -- Get the representation of the item stack stored in the specified slot.") def get(context: Context, args: Arguments): Array[AnyRef] = result(data.getStackInSlot(args.checkSlot(data, 0))) + //noinspection ScalaUnusedSymbol @Callback(doc = "function(slot:number):string -- Computes a hash value for the item stack in the specified slot.") def computeHash(context: Context, args: Arguments): Array[AnyRef] = { data.getStackInSlot(args.checkSlot(data, 0)) match { @@ -59,9 +62,11 @@ class UpgradeDatabase(val data: IInventory) extends prefab.ManagedEnvironment wi } } + //noinspection ScalaUnusedSymbol @Callback(doc = "function(hash:string):number -- Get the index of an item stack with the specified hash. Returns a negative value if no such stack was found.") def indexOf(context: Context, args: Arguments): Array[AnyRef] = result(indexOf(args.checkString(0), 1)) + //noinspection ScalaUnusedSymbol @Callback(doc = "function(slot:number):boolean -- Clears the specified slot. Returns true if there was something in the slot before.") def clear(context: Context, args: Arguments): Array[AnyRef] = { val slot = args.checkSlot(data, 0) @@ -70,6 +75,7 @@ class UpgradeDatabase(val data: IInventory) extends prefab.ManagedEnvironment wi result(nonEmpty) } + //noinspection ScalaUnusedSymbol @Callback(doc = "function(fromSlot:number, toSlot:number[, address:string]):boolean -- Copies an entry to another slot, optionally to another database. Returns true if something was overwritten.") def copy(context: Context, args: Arguments): Array[AnyRef] = { val fromSlot = args.checkSlot(data, 0) @@ -84,6 +90,7 @@ class UpgradeDatabase(val data: IInventory) extends prefab.ManagedEnvironment wi else set(data) } + //noinspection ScalaUnusedSymbol @Callback(doc = "function(address:string):number -- Copies the data stored in this database to another database with the specified address.") def clone(context: Context, args: Arguments): Array[AnyRef] = { DatabaseAccess.withDatabase(node, args.checkString(0), database => { @@ -96,6 +103,22 @@ class UpgradeDatabase(val data: IInventory) extends prefab.ManagedEnvironment wi }) } + //noinspection ScalaUnusedSymbol + @Callback(doc = """function(slot:number, id:string, damage:number, nbt:string):boolean - Set an item into the specified database slot. NBT tag is expected in JSON format.""") + def set(context: Context, args: Arguments): Array[AnyRef] = { + val slot = args.checkSlot(data, 0) + val item = Item.itemRegistry.getObject(args.checkString(1)).asInstanceOf[Item] + if (item == null) + return result(false, "invalid item id") + val damage = args.checkInteger(2) + val tagJson = args.optString(3, "") + val tag = if (Strings.isNullOrEmpty(tagJson)) null else JsonToNBT.func_150315_a(tagJson).asInstanceOf[NBTTagCompound] + val stack = new ItemStack(item, 1, damage) + stack.setTagCompound(tag) + data.setInventorySlotContents(slot, stack) + result(true) + } + private def indexOf(needle: String, offset: Int = 0): Int = { for (slot <- 0 until data.getSizeInventory) data.getStackInSlot(slot) match { case stack: ItemStack =>