add address key check in item Delegator to limit max stack size to 1

closes #3226
This commit is contained in:
payonel 2021-05-13 05:44:19 -07:00
parent ff3c2f51a9
commit 278e7bdbef
5 changed files with 37 additions and 10 deletions

View File

@ -2,7 +2,6 @@ package li.cil.oc.common.item
import java.util import java.util
import java.util.Random import java.util.Random
import cpw.mods.fml.relauncher.Side import cpw.mods.fml.relauncher.Side
import cpw.mods.fml.relauncher.SideOnly import cpw.mods.fml.relauncher.SideOnly
import li.cil.oc.CreativeTab import li.cil.oc.CreativeTab
@ -13,6 +12,7 @@ import li.cil.oc.api.driver.item.Chargeable
import li.cil.oc.api.event.RobotRenderEvent.MountPoint import li.cil.oc.api.event.RobotRenderEvent.MountPoint
import li.cil.oc.api.internal.Robot import li.cil.oc.api.internal.Robot
import li.cil.oc.client.renderer.item.UpgradeRenderer import li.cil.oc.client.renderer.item.UpgradeRenderer
import li.cil.oc.integration.opencomputers.{Item => OpenComputersItem}
import li.cil.oc.util.BlockPosition import li.cil.oc.util.BlockPosition
import net.minecraft.client.renderer.texture.IIconRegister import net.minecraft.client.renderer.texture.IIconRegister
import net.minecraft.creativetab.CreativeTabs import net.minecraft.creativetab.CreativeTabs
@ -48,9 +48,12 @@ class Delegator extends Item with driver.item.UpgradeRenderer with Chargeable {
// SubItem // SubItem
// ----------------------------------------------------------------------- // // ----------------------------------------------------------------------- //
override def getItemStackLimit(stack: ItemStack) = override def getItemStackLimit(stack: ItemStack): Int =
Delegator.subItem(stack) match { Delegator.subItem(stack) match {
case Some(subItem) => subItem.maxStackSize case Some(subItem) => OpenComputersItem.address(stack) match {
case Some(address) => 1
case _ => subItem.maxStackSize
}
case _ => maxStackSize case _ => maxStackSize
} }

View File

@ -4,11 +4,11 @@ import li.cil.oc.Settings
import net.minecraft.item.ItemStack import net.minecraft.item.ItemStack
class HardDiskDrive(val parent: Delegator, val tier: Int) extends traits.Delegate with traits.ItemTier with traits.FileSystemLike { class HardDiskDrive(val parent: Delegator, val tier: Int) extends traits.Delegate with traits.ItemTier with traits.FileSystemLike {
override val unlocalizedName = super.unlocalizedName + tier override val unlocalizedName: String = super.unlocalizedName + tier
val kiloBytes = Settings.get.hddSizes(tier) val kiloBytes: Int = Settings.get.hddSizes(tier)
val platterCount = Settings.get.hddPlatterCounts(tier) val platterCount: Int = Settings.get.hddPlatterCounts(tier)
override def displayName(stack: ItemStack) = { override def displayName(stack: ItemStack): Option[String] = {
val localizedName = parent.internalGetItemStackDisplayName(stack) val localizedName = parent.internalGetItemStackDisplayName(stack)
Some(if (kiloBytes >= 1024) { Some(if (kiloBytes >= 1024) {
localizedName + s" (${kiloBytes / 1024}MB)" localizedName + s" (${kiloBytes / 1024}MB)"

View File

@ -41,7 +41,7 @@ object DriveData {
val data = new DriveData(stack) val data = new DriveData(stack)
if (!data.isLocked) { if (!data.isLocked) {
data.lockInfo = key match { data.lockInfo = key match {
case name: String if name != null && !name.isEmpty => name case name: String if name != null && name.nonEmpty => name
case _ => "notch" // meaning: "unknown" case _ => "notch" // meaning: "unknown"
} }
data.save(stack) data.save(stack)

View File

@ -1,5 +1,6 @@
package li.cil.oc.integration.opencomputers package li.cil.oc.integration.opencomputers
import com.google.common.base.Strings
import li.cil.oc.Settings import li.cil.oc.Settings
import li.cil.oc.api import li.cil.oc.api
import li.cil.oc.api.driver import li.cil.oc.api.driver
@ -10,6 +11,8 @@ import li.cil.oc.server.driver.Registry
import net.minecraft.item.ItemStack import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound import net.minecraft.nbt.NBTTagCompound
import scala.annotation.tailrec
trait Item extends driver.Item { trait Item extends driver.Item {
def worksWith(stack: ItemStack, host: Class[_ <: EnvironmentHost]): Boolean = def worksWith(stack: ItemStack, host: Class[_ <: EnvironmentHost]): Boolean =
worksWith(stack) && !Registry.blacklist.exists { worksWith(stack) && !Registry.blacklist.exists {
@ -52,4 +55,25 @@ object Item {
} }
nbt.getCompoundTag(Settings.namespace + "data") nbt.getCompoundTag(Settings.namespace + "data")
} }
@tailrec
private def getTag(tagCompound: NBTTagCompound, keys: Array[String]): Option[NBTTagCompound] = {
if (keys.length == 0) Option(tagCompound)
else if (!tagCompound.hasKey(keys(0))) None
else getTag(tagCompound.getCompoundTag(keys(0)), keys.drop(1))
}
private def getTag(stack: ItemStack, keys: Array[String]): Option[NBTTagCompound] = {
if (stack == null || stack.stackSize == 0) None
else if (!stack.hasTagCompound) None
else getTag(stack.getTagCompound, keys)
}
def address(stack: ItemStack): Option[String] = {
val addressKey = "address"
getTag(stack, Array(Settings.namespace + "data", "node")) match {
case Some(tag) if tag.hasKey(addressKey) => Option(tag.getString(addressKey))
case _ => None
}
}
} }

View File

@ -101,7 +101,7 @@ object FileSystem extends api.detail.FileSystemAPI {
} }
} }
override def fromSaveDirectory(root: String, capacity: Long, buffered: Boolean) = { override def fromSaveDirectory(root: String, capacity: Long, buffered: Boolean): Capacity = {
val path = new io.File(DimensionManager.getCurrentSaveRootDirectory, Settings.savePath + root) val path = new io.File(DimensionManager.getCurrentSaveRootDirectory, Settings.savePath + root)
if (!path.isDirectory) { if (!path.isDirectory) {
path.delete() path.delete()
@ -200,7 +200,7 @@ object FileSystem extends api.detail.FileSystemAPI {
extends VirtualFileSystem extends VirtualFileSystem
with Buffered with Buffered
with Capacity { with Capacity {
protected override def segments(path: String) = { protected override def segments(path: String): Array[String] = {
val parts = super.segments(path) val parts = super.segments(path)
if (isCaseInsensitive) toCaseInsensitive(parts) else parts if (isCaseInsensitive) toCaseInsensitive(parts) else parts
} }