From 312c31f66fa69f83548cf789c03b6f093c747b61 Mon Sep 17 00:00:00 2001 From: zeng-github01 Date: Fri, 29 Aug 2025 00:39:35 +0800 Subject: [PATCH 1/7] Now, lead entity need a leash (fixes 3376) --- .../opencomputers/DriverUpgradeLeash.scala | 3 +- .../oc/server/component/UpgradeLeash.scala | 71 ++++++++++++++++--- 2 files changed, 62 insertions(+), 12 deletions(-) diff --git a/src/main/scala/li/cil/oc/integration/opencomputers/DriverUpgradeLeash.scala b/src/main/scala/li/cil/oc/integration/opencomputers/DriverUpgradeLeash.scala index 0af34be1c8..3003c9db69 100644 --- a/src/main/scala/li/cil/oc/integration/opencomputers/DriverUpgradeLeash.scala +++ b/src/main/scala/li/cil/oc/integration/opencomputers/DriverUpgradeLeash.scala @@ -4,6 +4,7 @@ import li.cil.oc.Constants import li.cil.oc.api import li.cil.oc.api.driver.EnvironmentProvider import li.cil.oc.api.driver.item.HostAware +import li.cil.oc.api.internal.Drone import li.cil.oc.api.network.EnvironmentHost import li.cil.oc.common.Slot import li.cil.oc.common.Tier @@ -18,7 +19,7 @@ object DriverUpgradeLeash extends Item with HostAware { override def createEnvironment(stack: ItemStack, host: EnvironmentHost) = if (host.world != null && host.world.isRemote) null else host match { - case entity: Entity => new component.UpgradeLeash(entity) + case entity: Entity with Drone => new component.UpgradeLeash(entity) case _ => null } diff --git a/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala b/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala index 3e995e1978..0476482da8 100644 --- a/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala +++ b/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala @@ -2,7 +2,6 @@ package li.cil.oc.server.component import java.util import java.util.UUID - import li.cil.oc.Constants import li.cil.oc.api.driver.DeviceInfo.DeviceAttribute import li.cil.oc.api.driver.DeviceInfo.DeviceClass @@ -14,7 +13,6 @@ import li.cil.oc.api.machine.Callback import li.cil.oc.api.machine.Context import li.cil.oc.api.network.Node import li.cil.oc.api.network.Visibility -import li.cil.oc.api.prefab import li.cil.oc.api.prefab.AbstractManagedEnvironment import li.cil.oc.common.EventHandler import li.cil.oc.util.BlockPosition @@ -22,6 +20,8 @@ import li.cil.oc.util.ExtendedArguments._ import li.cil.oc.util.ExtendedNBT._ import net.minecraft.entity.Entity import net.minecraft.entity.EntityLiving +import net.minecraft.init.Items +import net.minecraft.item.ItemStack import net.minecraft.nbt.NBTTagCompound import net.minecraft.nbt.NBTTagString import net.minecraftforge.common.util.Constants.NBT @@ -30,7 +30,7 @@ import scala.collection.convert.WrapAsJava._ import scala.collection.convert.WrapAsScala._ import scala.collection.mutable -class UpgradeLeash(val host: Entity) extends AbstractManagedEnvironment with traits.WorldAware with DeviceInfo { +class UpgradeLeash(val host: Entity with li.cil.oc.api.internal.Drone) extends AbstractManagedEnvironment with traits.WorldAware with DeviceInfo { override val node = Network.newNode(this, Visibility.Network). withComponent("leash"). create() @@ -49,7 +49,7 @@ class UpgradeLeash(val host: Entity) extends AbstractManagedEnvironment with tra val leashedEntities = mutable.Set.empty[UUID] - override def position = BlockPosition(host) + override def position = BlockPosition(host.asInstanceOf[Entity]) @Callback(doc = """function(side:number):boolean -- Tries to put an entity on the specified side of the device onto a leash.""") def leash(context: Context, args: Arguments): Array[AnyRef] = { @@ -60,11 +60,16 @@ class UpgradeLeash(val host: Entity) extends AbstractManagedEnvironment with tra val bounds = nearBounds.union(farBounds) entitiesInBounds[EntityLiving](classOf[EntityLiving], bounds).find(_.canBeLeashedTo(fakePlayer)) match { case Some(entity) => - entity.setLeashHolder(host, true) - leashedEntities += entity.getUniqueID - context.pause(0.1) - result(true) - case _ => result(Unit, "no unleashed entity") + if (shrinkLeash()) { + entity.setLeashHolder(host, true) + leashedEntities += entity.getUniqueID + context.pause(0.1) + result(true) + } + else { + result(false, "don't has leash to be shrink") + } + case _ => result(false, "no unleashed entity") } } @@ -84,10 +89,54 @@ class UpgradeLeash(val host: Entity) extends AbstractManagedEnvironment with tra private def unleashAll() { entitiesInBounds(classOf[EntityLiving], position.bounds.grow(5, 5, 5)).foreach(entity => { if (leashedEntities.contains(entity.getUniqueID) && entity.getLeashHolder == host) { - entity.clearLeashed(true, false) + if (returnLeash()) { + entity.clearLeashed(true, false) + leashedEntities -= entity.getUniqueID + } } }) - leashedEntities.clear() + } + + private def shrinkLeash(): Boolean = { + var hasLeash = false + val size = host.mainInventory().getSizeInventory + for (index <- 0 until size if !hasLeash) { + val stack = host.mainInventory().getStackInSlot(index) + if (stack != null && !stack.isEmpty && stack.getItem == Items.LEAD) { + stack.shrink(1) + hasLeash = true + if (stack.getCount == 0) { + host.mainInventory().setInventorySlotContents(index, ItemStack.EMPTY) + } + } + } + + hasLeash + } + + private def returnLeash(): Boolean = { + val size = host.mainInventory().getSizeInventory + var added = false + + for (index <- 0 until size if !added) { + val stack = host.mainInventory().getStackInSlot(index) + if (stack != null && !stack.isEmpty && stack.getItem == Items.LEAD && stack.getCount < stack.getMaxStackSize) { + stack.grow(1) + added = true + } + } + + if (!added) { + for (index <- 0 until size if !added) { + val stack = host.mainInventory().getStackInSlot(index) + if (stack == null || stack.isEmpty) { + host.mainInventory().setInventorySlotContents(index, new ItemStack(Items.LEAD)) + added = true + } + } + } + + added } private final val LeashedEntitiesTag = "leashedEntities" From a2a2b5f9de5b8d1cc9e77f179d0a53f3420e5192 Mon Sep 17 00:00:00 2001 From: zeng-github01 Date: Fri, 29 Aug 2025 00:45:08 +0800 Subject: [PATCH 2/7] minor fixes --- src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala b/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala index 0476482da8..6255843b95 100644 --- a/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala +++ b/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala @@ -51,7 +51,7 @@ class UpgradeLeash(val host: Entity with li.cil.oc.api.internal.Drone) extends A override def position = BlockPosition(host.asInstanceOf[Entity]) - @Callback(doc = """function(side:number):boolean -- Tries to put an entity on the specified side of the device onto a leash.""") + @Callback(doc = """function(side:number):boolean, string -- Tries to put an entity on the specified side of the device onto a leash.""") def leash(context: Context, args: Arguments): Array[AnyRef] = { if (leashedEntities.size >= MaxLeashedEntities) return result(Unit, "too many leashed entities") val side = args.checkSideAny(0) @@ -67,7 +67,7 @@ class UpgradeLeash(val host: Entity with li.cil.oc.api.internal.Drone) extends A result(true) } else { - result(false, "don't has leash to be shrink") + result(false, "don't have leash to be shrink") } case _ => result(false, "no unleashed entity") } From 1721e58ab5fed4142aee05ea52e2a3d33f161f59 Mon Sep 17 00:00:00 2001 From: zeng-github01 Date: Fri, 29 Aug 2025 00:54:19 +0800 Subject: [PATCH 3/7] minor fixes again --- src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala b/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala index 6255843b95..1774eddba3 100644 --- a/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala +++ b/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala @@ -49,7 +49,7 @@ class UpgradeLeash(val host: Entity with li.cil.oc.api.internal.Drone) extends A val leashedEntities = mutable.Set.empty[UUID] - override def position = BlockPosition(host.asInstanceOf[Entity]) + override def position = BlockPosition(host.posX, host.posY, host.posZ) @Callback(doc = """function(side:number):boolean, string -- Tries to put an entity on the specified side of the device onto a leash.""") def leash(context: Context, args: Arguments): Array[AnyRef] = { From b5e1997702a705815498260075a968b8b1dba0be Mon Sep 17 00:00:00 2001 From: zeng-github01 Date: Fri, 29 Aug 2025 01:00:19 +0800 Subject: [PATCH 4/7] fix missing argument --- src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala b/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala index 1774eddba3..c65ae3d128 100644 --- a/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala +++ b/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala @@ -49,7 +49,7 @@ class UpgradeLeash(val host: Entity with li.cil.oc.api.internal.Drone) extends A val leashedEntities = mutable.Set.empty[UUID] - override def position = BlockPosition(host.posX, host.posY, host.posZ) + override def position = BlockPosition(host.posX, host.posY, host.posZ, host.world) @Callback(doc = """function(side:number):boolean, string -- Tries to put an entity on the specified side of the device onto a leash.""") def leash(context: Context, args: Arguments): Array[AnyRef] = { From 7f2a47e60008d9ec9ec5bd0514ed8a53d4666d97 Mon Sep 17 00:00:00 2001 From: zeng-github01 Date: Fri, 29 Aug 2025 01:03:23 +0800 Subject: [PATCH 5/7] add optional tag for error message --- src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala b/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala index c65ae3d128..d530c43fc6 100644 --- a/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala +++ b/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala @@ -51,7 +51,7 @@ class UpgradeLeash(val host: Entity with li.cil.oc.api.internal.Drone) extends A override def position = BlockPosition(host.posX, host.posY, host.posZ, host.world) - @Callback(doc = """function(side:number):boolean, string -- Tries to put an entity on the specified side of the device onto a leash.""") + @Callback(doc = """function(side:number):boolean, [string] -- Tries to put an entity on the specified side of the device onto a leash.""") def leash(context: Context, args: Arguments): Array[AnyRef] = { if (leashedEntities.size >= MaxLeashedEntities) return result(Unit, "too many leashed entities") val side = args.checkSideAny(0) From 420bb95e22ad0abf22c76a7c2f2ad996fca8baed Mon Sep 17 00:00:00 2001 From: zeng-github01 Date: Fri, 29 Aug 2025 01:06:05 +0800 Subject: [PATCH 6/7] add optional tag for error message change syntax --- src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala b/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala index d530c43fc6..be41f9e019 100644 --- a/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala +++ b/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala @@ -51,7 +51,7 @@ class UpgradeLeash(val host: Entity with li.cil.oc.api.internal.Drone) extends A override def position = BlockPosition(host.posX, host.posY, host.posZ, host.world) - @Callback(doc = """function(side:number):boolean, [string] -- Tries to put an entity on the specified side of the device onto a leash.""") + @Callback(doc = """function(side:number):boolean[, string] -- Tries to put an entity on the specified side of the device onto a leash.""") def leash(context: Context, args: Arguments): Array[AnyRef] = { if (leashedEntities.size >= MaxLeashedEntities) return result(Unit, "too many leashed entities") val side = args.checkSideAny(0) From 7534d4671ce462b6a9419eb22488254f0ecba9ee Mon Sep 17 00:00:00 2001 From: zeng-github01 Date: Fri, 29 Aug 2025 01:12:35 +0800 Subject: [PATCH 7/7] minor fix --- src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala b/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala index be41f9e019..ebadf0c7ed 100644 --- a/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala +++ b/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala @@ -53,7 +53,7 @@ class UpgradeLeash(val host: Entity with li.cil.oc.api.internal.Drone) extends A @Callback(doc = """function(side:number):boolean[, string] -- Tries to put an entity on the specified side of the device onto a leash.""") def leash(context: Context, args: Arguments): Array[AnyRef] = { - if (leashedEntities.size >= MaxLeashedEntities) return result(Unit, "too many leashed entities") + if (leashedEntities.size >= MaxLeashedEntities) return result(false, "too many leashed entities") val side = args.checkSideAny(0) val nearBounds = position.bounds val farBounds = nearBounds.offset(side.getXOffset * 2.0, side.getYOffset * 2.0, side.getZOffset * 2.0)