Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix infinite leash #3707

Open
wants to merge 5 commits into
base: master-MC1.12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package li.cil.oc.integration.opencomputers

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
import li.cil.oc.api.network.EnvironmentHost
import li.cil.oc.common.Slot
import li.cil.oc.common.Tier
import li.cil.oc.common.{Slot, Tier}
import li.cil.oc.server.component
import li.cil.oc.{Constants, api}
import net.minecraft.entity.Entity
import net.minecraft.item.ItemStack

Expand All @@ -18,7 +17,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 internal.Drone => new component.UpgradeLeash(entity)
case _ => null
}

Expand Down
70 changes: 43 additions & 27 deletions src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
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
import li.cil.oc.OpenComputers
import li.cil.oc.api.Network
import li.cil.oc.api.driver.DeviceInfo
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.Node
import li.cil.oc.api.network.Visibility
import li.cil.oc.api.prefab
import li.cil.oc.api.driver.DeviceInfo.{DeviceAttribute, DeviceClass}
import li.cil.oc.api.machine.{Arguments, Callback, Context}
import li.cil.oc.api.network.{Node, Visibility}
import li.cil.oc.api.prefab.AbstractManagedEnvironment
import li.cil.oc.api.{Network, internal}
import li.cil.oc.common.EventHandler
import li.cil.oc.util.BlockPosition
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.nbt.NBTTagCompound
import net.minecraft.nbt.NBTTagString
import li.cil.oc.{Constants, OpenComputers}
import net.minecraft.entity.{Entity, EntityLiving}
import net.minecraft.init.Items
import net.minecraft.item.ItemStack
import net.minecraft.nbt.{NBTTagCompound, NBTTagString}
import net.minecraftforge.common.util.Constants.NBT

import java.util
import java.util.UUID
import scala.collection.convert.WrapAsJava._
import scala.collection.convert.WrapAsScala._
import scala.collection.mutable
import scala.util.control.Breaks._

class UpgradeLeash(val host: Entity) extends AbstractManagedEnvironment with traits.WorldAware with DeviceInfo {
class UpgradeLeash(val host: Entity with internal.Drone) extends AbstractManagedEnvironment with traits.WorldAware with DeviceInfo {
override val node = Network.newNode(this, Visibility.Network).
withComponent("leash").
create()
Expand All @@ -49,7 +43,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.getPosition)

@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] = {
Expand All @@ -58,14 +52,22 @@ class UpgradeLeash(val host: Entity) extends AbstractManagedEnvironment with tra
val nearBounds = position.bounds
val farBounds = nearBounds.offset(side.getXOffset * 2.0, side.getYOffset * 2.0, side.getZOffset * 2.0)
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")

for (index <- 0 to host.mainInventory().getSizeInventory) {
val stack = host.mainInventory().getStackInSlot(index)
if (stack.getItem == Items.LEAD) {
entitiesInBounds[EntityLiving](classOf[EntityLiving], bounds).find(_.canBeLeashedTo(fakePlayer)) match {
case Some(entity) =>
entity.setLeashHolder(host, true)
leashedEntities += entity.getUniqueID
stack.shrink(1)
context.pause(0.1)
result(true)
case _ => result(Unit, "no unleashed entity")
}
}
}
result(Unit, "don't have any lead")
}

@Callback(doc = """function() -- Unleashes all currently leashed entities.""")
Expand All @@ -85,6 +87,20 @@ class UpgradeLeash(val host: Entity) extends AbstractManagedEnvironment with tra
entitiesInBounds(classOf[EntityLiving], position.bounds.grow(5, 5, 5)).foreach(entity => {
if (leashedEntities.contains(entity.getUniqueID) && entity.getLeashHolder == host) {
entity.clearLeashed(true, false)
breakable {
for (index <- 0 to host.mainInventory().getSizeInventory) {
val itemStack = host.mainInventory().getStackInSlot(index)
if (itemStack == ItemStack.EMPTY) {
host.mainInventory().setInventorySlotContents(index, new ItemStack(Items.LEAD))
break()
}

if (itemStack.getItem == Items.LEAD) {
itemStack.grow(1)
break()
}
}
}
}
})
leashedEntities.clear()
Expand Down