Skip to content

Commit

Permalink
number of terminals that can be bound to a single server rack now inc…
Browse files Browse the repository at this point in the history
…reases with server tier (2, 4, 8 by default)
  • Loading branch information
fnuecke committed Feb 8, 2014
1 parent 05fb8f0 commit 6aa75d1
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/main/java/li/cil/oc/client/GuiHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/li/cil/oc/client/PacketHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 10 additions & 16 deletions src/main/java/li/cil/oc/common/component/Terminal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -22,16 +23,15 @@ 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
}
}
}
}
else null

var key: Option[String] = None
val keys = mutable.ListBuffer.empty[String]

def isServer = rack.isServer

Expand All @@ -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)
}

// ----------------------------------------------------------------------- //
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/li/cil/oc/common/item/Terminal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/li/cil/oc/server/PacketSender.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/li/cil/oc/server/component/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = _

Expand Down

0 comments on commit 6aa75d1

Please sign in to comment.