From 6aa75d1bc6d79229e4535076c4fc653073f75c98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Sun, 9 Feb 2014 00:43:10 +0100 Subject: [PATCH] number of terminals that can be bound to a single server rack now increases with server tier (2, 4, 8 by default) --- .../java/li/cil/oc/client/GuiHandler.scala | 2 +- .../java/li/cil/oc/client/PacketHandler.scala | 8 +++--- .../li/cil/oc/common/component/Terminal.scala | 26 +++++++------------ .../java/li/cil/oc/common/item/Terminal.scala | 9 +++++-- .../java/li/cil/oc/server/PacketSender.scala | 6 ++++- .../li/cil/oc/server/component/Server.scala | 10 ++++--- 6 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/main/java/li/cil/oc/client/GuiHandler.scala b/src/main/java/li/cil/oc/client/GuiHandler.scala index cf1236d32b..7bc08bd892 100644 --- a/src/main/java/li/cil/oc/client/GuiHandler.scala +++ b/src/main/java/li/cil/oc/client/GuiHandler.scala @@ -48,7 +48,7 @@ object GuiHandler extends CommonGuiHandler { case Some(term) => def inRange = player.isEntityAlive && term.rack.getDistanceFrom(player.posX, player.posY, player.posZ) < term.rack.range * term.rack.range if (inRange) { - if (term.key.isDefined && term.key.get == key) return new gui.Screen(term.buffer, true, () => { + if (term.keys.contains(key)) return new gui.Screen(term.buffer, true, () => { // Check if someone else bound a term to our server. if (stack.getTagCompound.getString(Settings.namespace + "key") != key) { Minecraft.getMinecraft.displayGuiScreen(null) diff --git a/src/main/java/li/cil/oc/client/PacketHandler.scala b/src/main/java/li/cil/oc/client/PacketHandler.scala index 6a3b79ad82..e397328fb4 100644 --- a/src/main/java/li/cil/oc/client/PacketHandler.scala +++ b/src/main/java/li/cil/oc/client/PacketHandler.scala @@ -79,9 +79,11 @@ class PacketHandler extends CommonPacketHandler { else { t.setRunning(number, p.readBoolean()) t.sides(number) = p.readDirection() - val key = p.readUTF() - if (key != "") { - t.terminals(number).key = Option(key) + val keyCount = p.readInt() + val keys = t.terminals(number).keys + keys.clear() + for (i <- 0 until keyCount) { + keys += p.readUTF() } } case _ => // Invalid packet. diff --git a/src/main/java/li/cil/oc/common/component/Terminal.scala b/src/main/java/li/cil/oc/common/component/Terminal.scala index cdebbf1e42..5d26b21f64 100644 --- a/src/main/java/li/cil/oc/common/component/Terminal.scala +++ b/src/main/java/li/cil/oc/common/component/Terminal.scala @@ -10,7 +10,8 @@ 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 +import net.minecraft.nbt.{NBTTagString, NBTTagCompound} +import scala.collection.mutable class Terminal(val rack: tileentity.Rack, val number: Int) extends Buffer.Owner { val buffer = new common.component.Buffer(this) @@ -22,8 +23,7 @@ class Terminal(val rack: tileentity.Rack, val number: Int) extends Buffer.Owner 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 Some(t: item.Terminal) if stack.hasTagCompound => keys.contains(stack.getTagCompound.getString(Settings.namespace + "key")) case _ => false } } @@ -31,7 +31,7 @@ class Terminal(val rack: tileentity.Rack, val number: Int) extends Buffer.Owner } else null - var key: Option[String] = None + val keys = mutable.ListBuffer.empty[String] def isServer = rack.isServer @@ -48,34 +48,28 @@ class Terminal(val rack: tileentity.Rack, val number: Int) extends Buffer.Owner def load(nbt: NBTTagCompound) { buffer.load(nbt.getCompoundTag(Settings.namespace + "buffer")) keyboard.load(nbt.getCompoundTag(Settings.namespace + "keyboard")) + // Compatibility for previous dev versions where there was only one term. if (nbt.hasKey(Settings.namespace + "key")) { - key = Option(nbt.getString(Settings.namespace + "key")) + keys += nbt.getString(Settings.namespace + "key") } + nbt.getTagList(Settings.namespace + "keys").foreach[NBTTagString](keys += _.data) } 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 _ => - } + nbt.setNewTagList("keys", keys) } @SideOnly(Side.CLIENT) def readFromNBTForClient(nbt: NBTTagCompound) { buffer.buffer.load(nbt) - if (nbt.hasKey("key")) { - key = Option(nbt.getString("key")) - } + nbt.getTagList("keys").foreach[NBTTagString](keys += _.data) } def writeToNBTForClient(nbt: NBTTagCompound) { buffer.buffer.save(nbt) - key match { - case Some(value) => nbt.setString("key", value) - case _ => - } + nbt.setNewTagList("keys", keys) } // ----------------------------------------------------------------------- // diff --git a/src/main/java/li/cil/oc/common/item/Terminal.scala b/src/main/java/li/cil/oc/common/item/Terminal.scala index 10c4c5349a..cf62e077fd 100644 --- a/src/main/java/li/cil/oc/common/item/Terminal.scala +++ b/src/main/java/li/cil/oc/common/item/Terminal.scala @@ -58,11 +58,16 @@ class Terminal(val parent: Delegator) extends Delegate { if (!world.isRemote) { rack.servers(slot) match { case Some(server) => + val key = UUID.randomUUID().toString + val keys = rack.terminals(slot).keys if (!stack.hasTagCompound) { stack.setTagCompound(new NBTTagCompound("tag")) } - val key = UUID.randomUUID().toString - rack.terminals(slot).key = Some(key) + else { + keys -= stack.getTagCompound.getString(Settings.namespace + "key") + } + keys.remove(0, math.max(0, 1 + keys.length - Settings.get.terminalsPerTier(server.tier))) + keys += key ServerPacketSender.sendServerState(rack, slot) stack.getTagCompound.setString(Settings.namespace + "key", key) stack.getTagCompound.setString(Settings.namespace + "server", server.machine.node.address) diff --git a/src/main/java/li/cil/oc/server/PacketSender.scala b/src/main/java/li/cil/oc/server/PacketSender.scala index 45263b1415..2e4ee1aaaa 100644 --- a/src/main/java/li/cil/oc/server/PacketSender.scala +++ b/src/main/java/li/cil/oc/server/PacketSender.scala @@ -317,7 +317,11 @@ object PacketSender { pb.writeInt(number) pb.writeBoolean(t.isRunning(number)) pb.writeDirection(t.sides(number)) - pb.writeUTF(t.terminals(number).key.getOrElse("")) + val keys = t.terminals(number).keys + pb.writeInt(keys.length) + for (key <- keys) { + pb.writeUTF(key) + } player match { case Some(p) => pb.sendToPlayer(p) diff --git a/src/main/java/li/cil/oc/server/component/Server.scala b/src/main/java/li/cil/oc/server/component/Server.scala index eb1c3a5bdf..5e74fa0b03 100644 --- a/src/main/java/li/cil/oc/server/component/Server.scala +++ b/src/main/java/li/cil/oc/server/component/Server.scala @@ -18,6 +18,11 @@ class Server(val rack: tileentity.Rack, val number: Int) extends Machine.Owner { val inventory = new NetworkedInventory() + def tier = Items.multi.subItem(rack.getStackInSlot(number)) match { + case Some(server: item.Server) => server.tier + case _ => 0 + } + // ----------------------------------------------------------------------- // override def address() = machine.node.address @@ -92,10 +97,7 @@ class Server(val rack: tileentity.Rack, val number: Int) extends Machine.Owner { } } - override def tier = Items.multi.subItem(container) match { - case Some(server: item.Server) => server.tier - case _ => 0 - } + override def tier = Server.this.tier var containerOverride: ItemStack = _