| package li.cil.oc.common.component |
| |
| import cpw.mods.fml.relauncher.{Side, SideOnly} |
| import li.cil.oc.api.network.{Node, Visibility} |
| import li.cil.oc.common.item |
| import li.cil.oc.common.tileentity |
| import li.cil.oc.server.component |
| import li.cil.oc.server.{PacketSender => ServerPacketSender} |
| import li.cil.oc.util.ExtendedNBT._ |
| import li.cil.oc.util.PackedColor.Depth |
| import li.cil.oc.{Items, Settings, common} |
| import net.minecraft.entity.player.EntityPlayer |
| import net.minecraft.nbt.NBTTagCompound |
| |
| class Terminal(val rack: tileentity.Rack, val number: Int) extends Buffer.Owner { |
| val buffer = new common.component.Buffer(this) |
| val keyboard = if (buffer.node != null) { |
| buffer.node.setVisibility(Visibility.Neighbors) |
| new component.Keyboard { |
| node.setVisibility(Visibility.Neighbors) |
| |
| override def isUseableByPlayer(p: EntityPlayer) = { |
| val stack = p.getCurrentEquippedItem |
| Items.multi.subItem(stack) match { |
| case Some(t: item.Terminal) if key.isDefined && stack.hasTagCompound => |
| key.get == stack.getTagCompound.getString(Settings.namespace + "key") |
| case _ => false |
| } |
| } |
| } |
| } |
| else null |
| |
| var key: Option[String] = None |
| |
| def isServer = rack.isServer |
| |
| def connect(node: Node) { |
| node.connect(buffer.node) |
| node.connect(keyboard.node) |
| buffer.node.connect(keyboard.node) |
| } |
| |
| override def tier = 1 |
| |
| // ----------------------------------------------------------------------- // |
| |
| def load(nbt: NBTTagCompound) { |
| buffer.load(nbt.getCompoundTag(Settings.namespace + "buffer")) |
| keyboard.load(nbt.getCompoundTag(Settings.namespace + "keyboard")) |
| if (nbt.hasKey(Settings.namespace + "key")) { |
| key = Option(nbt.getString(Settings.namespace + "key")) |
| } |
| } |
| |
| def save(nbt: NBTTagCompound) { |
| nbt.setNewCompoundTag(Settings.namespace + "buffer", buffer.save) |
| nbt.setNewCompoundTag(Settings.namespace + "keyboard", keyboard.save) |
| key match { |
| case Some(value) => nbt.setString(Settings.namespace + "key", value) |
| case _ => |
| } |
| } |
| |
| @SideOnly(Side.CLIENT) |
| def readFromNBTForClient(nbt: NBTTagCompound) { |
| buffer.buffer.load(nbt) |
| if (nbt.hasKey("key")) { |
| key = Option(nbt.getString("key")) |
| } |
| } |
| |
| def writeToNBTForClient(nbt: NBTTagCompound) { |
| buffer.buffer.save(nbt) |
| key match { |
| case Some(value) => nbt.setString("key", value) |
| case _ => |
| } |
| } |
| |
| // ----------------------------------------------------------------------- // |
| |
| override def onScreenColorChange(foreground: Int, background: Int) { |
| if (isServer) { |
| rack.markAsChanged() |
| ServerPacketSender.sendScreenColorChange(buffer, foreground, background) |
| } |
| else currentGui.foreach(_.recompileDisplayLists()) |
| } |
| |
| override def onScreenCopy(col: Int, row: Int, w: Int, h: Int, tx: Int, ty: Int) { |
| if (isServer) { |
| rack.markAsChanged() |
| ServerPacketSender.sendScreenCopy(buffer, col, row, w, h, tx, ty) |
| } |
| else currentGui.foreach(_.recompileDisplayLists()) |
| } |
| |
| override def onScreenDepthChange(depth: Depth.Value) { |
| if (isServer) { |
| rack.markAsChanged() |
| ServerPacketSender.sendScreenDepthChange(buffer, depth) |
| } |
| else currentGui.foreach(_.recompileDisplayLists()) |
| } |
| |
| override def onScreenFill(col: Int, row: Int, w: Int, h: Int, c: Char) { |
| if (isServer) { |
| rack.markAsChanged() |
| ServerPacketSender.sendScreenFill(buffer, col, row, w, h, c) |
| } |
| else currentGui.foreach(_.recompileDisplayLists()) |
| } |
| |
| override def onScreenResolutionChange(w: Int, h: Int) { |
| if (isServer) { |
| rack.markAsChanged() |
| ServerPacketSender.sendScreenResolutionChange(buffer, w, h) |
| } |
| else currentGui.foreach(_.recompileDisplayLists()) |
| } |
| |
| override def onScreenSet(col: Int, row: Int, s: String) { |
| if (isServer) { |
| rack.markAsChanged() |
| ServerPacketSender.sendScreenSet(buffer, col, row, s) |
| } |
| else currentGui.foreach(_.recompileDisplayLists()) |
| } |
| } |