add setName and getName to robot component

The robot component is only loaded(connected) on computer cases and server racks

closes #2760
This commit is contained in:
payonel 2018-03-04 20:12:54 -08:00
parent 0341b1d0a3
commit 659af9e845
4 changed files with 44 additions and 0 deletions

View File

@ -84,6 +84,7 @@ object PacketHandler extends CommonPacketHandler {
case PacketType.RobotInventoryChange => onRobotInventoryChange(p)
case PacketType.RobotLightChange => onRobotLightChange(p)
case PacketType.RobotMove => onRobotMove(p)
case PacketType.RobotNameChange => onRobotNameChange(p)
case PacketType.RobotSelectedSlotChange => onRobotSelectedSlotChange(p)
case PacketType.RotatableState => onRotatableState(p)
case PacketType.SwitchActivity => onSwitchActivity(p)
@ -528,6 +529,20 @@ object PacketHandler extends CommonPacketHandler {
case _ => // Invalid packet.
}
def onRobotNameChange(p: PacketParser) = {
p.readTileEntity[RobotProxy]() match {
case Some(t) => {
val len = p.readShort()
val name = new Array[Char](len)
for (x <- 0 until len) {
name(x) = p.readChar()
}
t.robot.setName(name.mkString)
}
case _ => // Invalid packet.
}
}
def onRobotMove(p: PacketParser) = {
val dimension = p.readInt()
val x = p.readInt()

View File

@ -45,6 +45,7 @@ object PacketType extends Enumeration {
RobotInventoryChange,
RobotLightChange,
RobotMove,
RobotNameChange,
RobotSelectedSlotChange,
RotatableState,
SwitchActivity,

View File

@ -10,6 +10,7 @@ import li.cil.oc.api.machine.Callback
import li.cil.oc.api.machine.Context
import li.cil.oc.api.network._
import li.cil.oc.integration.Mods
import li.cil.oc.server.{PacketSender => ServerPacketSender}
import mods.immibis.redlogic.api.wiring.IWire
import net.minecraft.entity.Entity
import net.minecraft.entity.player.EntityPlayer
@ -92,6 +93,19 @@ class RobotProxy(val robot: Robot) extends traits.Computer with traits.PowerInfo
def isRunning(context: Context, args: Arguments): Array[AnyRef] =
result(machine.isRunning)
@Callback(doc = "function(name: string):string -- Sets a new name and returns the old name. Robot must not be running")
def setName(context: Context, args: Arguments): Array[AnyRef] = {
val oldName = robot.name
val newName: String = args.checkString(0)
if (machine.isRunning) return result(Unit, "is running")
setName(newName)
ServerPacketSender.sendRobotNameChange(robot)
result(oldName)
}
@Callback(doc = "function():string -- Returns the robot name.")
def getName(context: Context, args: Arguments): Array[AnyRef] = result(robot.name)
override def onMessage(message: Message) {
super.onMessage(message)
if (message.name == "network.message" && message.source != this.node) message.data match {

View File

@ -549,6 +549,20 @@ object PacketSender {
pb.sendToPlayersNearTileEntity(t, Option(64))
}
def sendRobotNameChange(t: tileentity.Robot) {
val pb = new SimplePacketBuilder(PacketType.RobotNameChange)
pb.writeTileEntity(t.proxy)
val name = t.name
val len = name.length.toShort
pb.writeShort(len)
for (x <- 0 until len) {
pb.writeChar(name(x))
}
pb.sendToPlayersNearTileEntity(t)
}
def sendRobotSelectedSlotChange(t: tileentity.Robot) {
val pb = new SimplePacketBuilder(PacketType.RobotSelectedSlotChange)