Skip to content

Commit

Permalink
Fix scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
petterarvidsson committed Dec 29, 2017
1 parent 88151dc commit 59e1714
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
41 changes: 25 additions & 16 deletions src/main/scala/konstructs/inventory.scala
Expand Up @@ -30,46 +30,46 @@ class InventoryActor(val ns: String, val jsonStorage: ActorRef)
var otherInventories: java.util.Map[String, java.util.Map[String, Inventory]] = null

private def inventoryExists(blockId: UUID, inventoryId: InventoryId) =
if(inventoryId == InventoryId.STORAGE) {
if (inventoryId == InventoryId.STORAGE) {
storageInventories.containsKey(blockId.toString)
} else {
if(otherInventories.containsKey(blockId.toString)) {
if (otherInventories.containsKey(blockId.toString)) {
otherInventories.get(blockId.toString).containsKey(inventoryId.idString)
} else {
false
}
}

private def getInventory(blockId: UUID, inventoryId: InventoryId) =
if(inventoryId == InventoryId.STORAGE) {
if (inventoryId == InventoryId.STORAGE) {
storageInventories.get(blockId.toString)
} else {
val blockInventories = otherInventories.get(blockId.toString)
if(blockInventories != null) {
if (blockInventories != null) {
blockInventories.get(inventoryId.idString)
} else {
null
}
}

private def putInventory(blockId: UUID, inventoryId: InventoryId, inventory: Inventory): Unit = {
if(inventoryId == InventoryId.STORAGE) {
if (inventoryId == InventoryId.STORAGE) {
storageInventories.put(blockId.toString, inventory)
} else {
if(otherInventories.get(blockId.toString) == null) {
if (otherInventories.get(blockId.toString) == null) {
otherInventories.put(blockId.toString, new java.util.HashMap[String, Inventory]())
}
otherInventories.get(blockId.toString).put(inventoryId.idString, inventory)
}
}

private def removeInventory(blockId: UUID, inventoryId: InventoryId) =
if(inventoryId == InventoryId.STORAGE) {
if (inventoryId == InventoryId.STORAGE) {
storageInventories.remove(blockId.toString)
} else {
val blockInventories = otherInventories.get(blockId.toString)
blockInventories.remove(inventoryId.idString)
if(blockInventories.isEmpty) {
if (blockInventories.isEmpty) {
otherInventories.remove(blockId.toString)
}
}
Expand Down Expand Up @@ -134,17 +134,22 @@ class InventoryActor(val ns: String, val jsonStorage: ActorRef)
null
}

private def transfer(fromBlockId: UUID, fromInventoryId: InventoryId, toBlockId: UUID, toInventoryId: InventoryId, blockTypeId: BlockTypeId, amount: Int) {
if(inventoryExists(fromBlockId, fromInventoryId) && inventoryExists(toBlockId, toInventoryId)) {
private def transfer(fromBlockId: UUID,
fromInventoryId: InventoryId,
toBlockId: UUID,
toInventoryId: InventoryId,
blockTypeId: BlockTypeId,
amount: Int) {
if (inventoryExists(fromBlockId, fromInventoryId) && inventoryExists(toBlockId, toInventoryId)) {
val from = getInventory(fromBlockId, fromInventoryId)
val to = getInventory(toBlockId, toInventoryId)

val stack = from.take(blockTypeId, amount)
if(stack != null) {
if (stack != null) {
val leftovers = to.acceptPartOf(stack)

/* Check if we could successfully add all blocks from the stack to the inventory */
if(leftovers.getGiving == null) {
if (leftovers.getGiving == null) {
/* Save the transfer */
putInventory(fromBlockId, fromInventoryId, from.drop(blockTypeId, amount))
putInventory(toBlockId, toInventoryId, leftovers.getAccepting)
Expand All @@ -153,7 +158,6 @@ class InventoryActor(val ns: String, val jsonStorage: ActorRef)
}
}


def receive = {
case GsonLoaded(_, json) if json != null =>
storageInventories = gson.fromJson(json, typeOfInventories)
Expand Down Expand Up @@ -213,7 +217,12 @@ class InventoryActor(val ns: String, val jsonStorage: ActorRef)
sender ! new ReceiveStack(remove(r.getBlockId, r.getInventoryId, r.getSlot, r.getAmount))

case t: TransferBetweenInventories =>
transfer(t.getFromBlockId, t.getFromInventoryId, t.getToBlockId, t.getToInventoryId, t.getBlockTypeId, t.getAmount)
transfer(t.getFromBlockId,
t.getFromInventoryId,
t.getToBlockId,
t.getToInventoryId,
t.getBlockTypeId,
t.getAmount)

case d: DeleteInventory =>
removeInventory(d.getBlockId, d.getInventoryId)
Expand All @@ -224,9 +233,9 @@ class InventoryActor(val ns: String, val jsonStorage: ActorRef)

case g: GetInventoriesView =>
var view = View.EMPTY
for((inventoryId, inventoryView) <- g.getInventories.asScala) {
for ((inventoryId, inventoryView) <- g.getInventories.asScala) {
val inventory = getInventory(g.getBlockId, inventoryId)
if(inventory != null) {
if (inventory != null) {
view = view.add(inventoryView, inventory)
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/konstructs/konstructing.scala
Expand Up @@ -230,7 +230,8 @@ class KonstructingViewActor(player: ActorRef,
val amount = r.getAmount
if (inventoryView.contains(from)) {
context.become(awaitInventory)
universe.forward(new RemoveStackFromSlot(inventoryId, InventoryId.STORAGE, inventoryView.translate(from), amount))
universe.forward(
new RemoveStackFromSlot(inventoryId, InventoryId.STORAGE, inventoryView.translate(from), amount))
universe ! new GetInventory(inventoryId)
} else if (konstructingView.contains(from)) {
val stack = konstructing.getStack(konstructingView.translate(from))
Expand Down

0 comments on commit 59e1714

Please sign in to comment.