Skip to content

Commit

Permalink
Merge pull request #1917 from GiganticMinecraft/refactorSharedInventory
Browse files Browse the repository at this point in the history
共有インベントリ機能のコマンド処理周りをリファクタリング
  • Loading branch information
Lucky3028 authored Feb 26, 2023
2 parents a0e2b4f + baf9111 commit 9333f7d
Showing 1 changed file with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.github.unchama.seichiassist.subsystems.sharedinventory.SharedInventor
import com.github.unchama.seichiassist.subsystems.sharedinventory.domain.SharedFlag
import com.github.unchama.seichiassist.subsystems.sharedinventory.domain.bukkit.InventoryContents
import com.github.unchama.seichiassist.util.InventoryOperations
import com.github.unchama.targetedeffect.TargetedEffect.emptyEffect
import com.github.unchama.targetedeffect.commandsender.MessageEffect
import com.github.unchama.targetedeffect.player.CommandEffect
import com.github.unchama.targetedeffect.{SequentialEffect, TargetedEffect}
Expand All @@ -21,33 +22,35 @@ class ShareInventoryCommand[F[_]: ConcurrentEffect](
implicit sharedInventoryAPI: SharedInventoryAPI[F, Player]
) {

import cats.implicits._

val executor: TabExecutor = playerCommandBuilder
.execution { context =>
val sender = context.sender

if (sharedInventoryAPI.sharedFlag(sender).toIO.unsafeRunSync() == SharedFlag.Sharing)
withdrawFromSharedInventory(sender)
else depositToSharedInventory(sender)

for {
sharedFlag <- sharedInventoryAPI.sharedFlag(sender).toIO
_ <-
if (sharedFlag == SharedFlag.Sharing) {
withdrawFromSharedInventory(sender)
} else {
depositToSharedInventory(sender)
}
} yield emptyEffect
}
.build()
.asNonBlockingTabExecutor()

import cats.implicits._

private def withdrawFromSharedInventory(player: Player): IO[TargetedEffect[Player]] = {
val uuid = player.getUniqueId
val eff = for {
oldSharedFlag <- sharedInventoryAPI.sharedFlag(player)
loadedInventory <- sharedInventoryAPI.load(uuid)
_ <- sharedInventoryAPI.clear(uuid)
newSharedFlag <- sharedInventoryAPI.sharedFlag(player)
playerInventory = player.getInventory
_ <- Sync[F]
.delay {
val inventoryContents = loadedInventory
.getOrElse(return IO.pure(MessageEffect(s"$RESET$RED${BOLD}収納アイテムが存在しません。")))
.inventoryContents
val playerInventory = player.getInventory
val inventoryContents = loadedInventory.get.inventoryContents
// 手持ちのアイテムをドロップする
playerInventory
.getContents
Expand Down

0 comments on commit 9333f7d

Please sign in to comment.