Skip to content

Commit

Permalink
Merge pull request #2072 from GiganticMinecraft/refactorBreakFlags
Browse files Browse the repository at this point in the history
refactor: 破壊設定システムの再実装
  • Loading branch information
kory33 authored Jul 2, 2023
2 parents 51dee34 + ab68cb6 commit fe9cf4f
Show file tree
Hide file tree
Showing 14 changed files with 295 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
USE seichiassist;

CREATE TABLE player_break_preference(
uuid CHAR(36) NOT NULL,
block_category ENUM('Chest', 'MadeFromNetherQuartz') NOT NULL,
do_break BOOL NOT NULL DEFAULT TRUE,
PRIMARY KEY(uuid, block_category),
INDEX index_player_break_preference_on_uuid (uuid)
);
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import com.github.unchama.seichiassist.subsystems._
import com.github.unchama.seichiassist.subsystems.anywhereender.AnywhereEnderChestAPI
import com.github.unchama.seichiassist.subsystems.breakcount.{BreakCountAPI, BreakCountReadAPI}
import com.github.unchama.seichiassist.subsystems.breakcountbar.BreakCountBarAPI
import com.github.unchama.seichiassist.subsystems.breakskilltargetconfig.BreakSkillTargetConfigAPI
import com.github.unchama.seichiassist.subsystems.buildcount.BuildCountAPI
import com.github.unchama.seichiassist.subsystems.discordnotification.DiscordNotificationAPI
import com.github.unchama.seichiassist.subsystems.donate.DonatePremiumPointAPI
Expand Down Expand Up @@ -480,6 +481,11 @@ class SeichiAssist extends JavaPlugin() {
subsystems.vote.subsystems.fairyspeech.System.wired[IO]
}

/* TODO: breakSkillTargetConfigSystemは本来privateであるべきだが、
BreakUtilで呼び出されているため、やむを得ずpublicになっている */
lazy val breakSkillTargetConfigSystem: subsystems.breakskilltargetconfig.System[IO, Player] =
subsystems.breakskilltargetconfig.System.wired[IO, SyncIO].unsafeRunSync()

/* TODO: mineStackSystemは本来privateであるべきだが、mineStackにアイテムを格納するAPIが現状の
BreakUtilの実装から呼び出されている都合上やむを得ずpublicになっている。*/
lazy val mineStackSystem: subsystems.minestack.System[IO, Player, ItemStack] =
Expand Down Expand Up @@ -520,7 +526,8 @@ class SeichiAssist extends JavaPlugin() {
sharedInventorySystem,
mineStackSystem,
consumeGachaTicketSystem,
openirontrapdoor.System.wired
openirontrapdoor.System.wired,
breakSkillTargetConfigSystem
)

private lazy val buildAssist: BuildAssist = {
Expand Down Expand Up @@ -695,6 +702,8 @@ class SeichiAssist extends JavaPlugin() {
implicit val gachaAPI: GachaDrawAPI[IO, Player] = gachaSystem.api
implicit val consumeGachaTicketAPI: ConsumeGachaTicketAPI[IO, Player] =
consumeGachaTicketSystem.api
implicit val breakSkillTargetConfigAPI: BreakSkillTargetConfigAPI[IO, Player] =
breakSkillTargetConfigSystem.api

val menuRouter = TopLevelRouter.apply
import SeichiAssist.Scopes.globalChatInterceptionScope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,6 @@ class PlayerData(@Deprecated() val uuid: UUID, val name: String) {
// プレイヤー名
val lowercaseName: String = name.toLowerCase()

// チェスト破壊トグル
var chestflag = true

// ネザー水晶類ブロック破壊トグル
var netherQuartzBlockflag = true

/**
* チェスト破壊のON/OFFを切り替える[UnfocusedEffect]
*/
val toggleChestBreakFlag: TargetedEffect[Player] = UnfocusedEffect {
chestflag = !chestflag
}

/**
* ネザー水晶類ブロック破壊のON/OFFを切り替える[UnfocusedEffect]
*/
val toggleNetherQuartzBlockBreakFlag: TargetedEffect[Player] = UnfocusedEffect {
netherQuartzBlockflag = !netherQuartzBlockflag
}

var canCreateRegion = true
var unitPerClick = 1
// 投票受け取りボタン連打防止用
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.github.unchama.seichiassist.subsystems.anywhereender.AnywhereEnderChe
import com.github.unchama.seichiassist.subsystems.breakcount.BreakCountAPI
import com.github.unchama.seichiassist.subsystems.breakcount.domain.SeichiAmountData
import com.github.unchama.seichiassist.subsystems.breakcountbar.BreakCountBarAPI
import com.github.unchama.seichiassist.subsystems.breakskilltargetconfig.BreakSkillTargetConfigAPI
import com.github.unchama.seichiassist.subsystems.buildcount.domain.playerdata.BuildAmountData
import com.github.unchama.seichiassist.subsystems.discordnotification.DiscordNotificationAPI
import com.github.unchama.seichiassist.subsystems.donate.DonatePremiumPointAPI
Expand Down Expand Up @@ -96,7 +97,8 @@ object TopLevelRouter {
mineStackAPI: MineStackAPI[IO, Player, ItemStack],
gachaDrawAPI: GachaDrawAPI[IO, Player],
consumeGachaTicketAPI: ConsumeGachaTicketAPI[IO, Player],
fairySpeechAPI: FairySpeechAPI[IO, Player]
fairySpeechAPI: FairySpeechAPI[IO, Player],
breakSkillTargetConfigAPI: BreakSkillTargetConfigAPI[IO, Player]
): TopLevelRouter[IO] = new TopLevelRouter[IO] {
import assortedRankingApi._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import com.github.unchama.seichiassist.data.MenuInventoryData
import com.github.unchama.seichiassist.menus.CommonButtons
import com.github.unchama.seichiassist.menus.stickmenu.FirstPage
import com.github.unchama.seichiassist.subsystems.breakcount.BreakCountAPI
import com.github.unchama.seichiassist.subsystems.breakskilltargetconfig.BreakSkillTargetConfigAPI
import com.github.unchama.seichiassist.subsystems.breakskilltargetconfig.domain.BreakSkillTargetConfigKey
import com.github.unchama.targetedeffect._
import com.github.unchama.targetedeffect.commandsender.MessageEffect
import com.github.unchama.targetedeffect.player.FocusedSoundEffect
Expand All @@ -30,6 +32,7 @@ object PassiveSkillMenu extends Menu {

class Environment(
implicit val breakCountApi: BreakCountAPI[IO, SyncIO, Player],
implicit val breakSkillTargetConfigAPI: BreakSkillTargetConfigAPI[IO, Player],
val ioCanOpenFirstPage: IO CanOpen FirstPage.type
)

Expand Down Expand Up @@ -141,16 +144,19 @@ object PassiveSkillMenu extends Menu {
)
}

val computeToggleChestBreakButton: IO[Button] = RecomputedButton(IO {
val openerData = SeichiAssist.playermap(getUniqueId)
import environment._

val computeToggleChestBreakButton: IO[Button] = RecomputedButton(for {
originalBreakChestConfig <- breakSkillTargetConfigAPI
.breakSkillTargetConfig(player, BreakSkillTargetConfigKey.Chest)
} yield {
val baseLore = List(s"${GREEN}スキルでチェストを破壊するスキル")
val statusLore = if (openerData.chestflag) {
val statusLore = if (originalBreakChestConfig) {
List(s"${RED}整地ワールドのみで発動中(デフォルト)", "", s"$DARK_GREEN${UNDERLINE}クリックで切り替え")
} else {
List(s"${RED}発動しません", "", s"$DARK_GREEN${UNDERLINE}クリックで切り替え")
}
val material = if (openerData.chestflag) Material.DIAMOND_AXE else Material.CHEST
val material = if (originalBreakChestConfig) Material.DIAMOND_AXE else Material.CHEST

Button(
new IconItemStackBuilder(material)
Expand All @@ -159,9 +165,11 @@ object PassiveSkillMenu extends Menu {
.build(),
LeftClickButtonEffect {
SequentialEffect(
openerData.toggleChestBreakFlag,
breakSkillTargetConfigAPI.toggleBreakSkillTargetConfig(
BreakSkillTargetConfigKey.Chest
),
DeferredEffect(IO {
if (openerData.chestflag) {
if (!originalBreakChestConfig) {
SequentialEffect(
MessageEffect(s"${GREEN}スキルでのチェスト破壊を有効化しました。"),
FocusedSoundEffect(Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1f, 1f)
Expand All @@ -178,11 +186,12 @@ object PassiveSkillMenu extends Menu {
)
})

val computeToggleNetherQuartzBlockButton: IO[Button] = RecomputedButton(IO {
val openerData = SeichiAssist.playermap(getUniqueId)

val computeToggleNetherQuartzBlockButton: IO[Button] = RecomputedButton(for {
originalBreakQuartz <- breakSkillTargetConfigAPI
.breakSkillTargetConfig(player, BreakSkillTargetConfigKey.MadeFromNetherQuartz)
} yield {
val baseLore = List(s"${YELLOW}スキルでネザー水晶類ブロックを破壊するスキル")
val statusLore = if (openerData.netherQuartzBlockflag) {
val statusLore = if (originalBreakQuartz) {
List(s"${GREEN}ON (スキルでネザー水晶類ブロックを破壊します。)", s"${DARK_RED}クリックでOFF")
} else {
List(s"${RED}OFF (スキルでネザー水晶類ブロックを破壊しません。)", s"${DARK_GREEN}クリックでON")
Expand All @@ -191,17 +200,19 @@ object PassiveSkillMenu extends Menu {
Button(
new IconItemStackBuilder(Material.QUARTZ_BLOCK)
.tap { builder =>
if (openerData.netherQuartzBlockflag)
if (originalBreakQuartz)
builder.enchanted()
}
.title(s"$WHITE$UNDERLINE${BOLD}ネザー水晶類ブロック破壊スキル切り替え")
.lore(baseLore ++ statusLore)
.build(),
LeftClickButtonEffect {
SequentialEffect(
openerData.toggleNetherQuartzBlockBreakFlag,
breakSkillTargetConfigAPI.toggleBreakSkillTargetConfig(
BreakSkillTargetConfigKey.MadeFromNetherQuartz
),
DeferredEffect(IO {
if (openerData.netherQuartzBlockflag) {
if (!originalBreakQuartz) {
SequentialEffect(
MessageEffect(s"${GREEN}スキルでのネザー水晶類ブロック破壊を有効化しました。"),
FocusedSoundEffect(Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1f, 1f)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.unchama.seichiassist.subsystems.breakskilltargetconfig

import cats.data.Kleisli
import com.github.unchama.seichiassist.subsystems.breakskilltargetconfig.domain.BreakSkillTargetConfigKey

trait BreakSkillTargetConfigAPI[F[_], Player] {

/**
* @return 破壊フラグをトグルする作用
*/
def toggleBreakSkillTargetConfig(
configKey: BreakSkillTargetConfigKey
): Kleisli[F, Player, Unit]

/**
* @return 現在の破壊フラグを取得する作用
*/
def breakSkillTargetConfig(player: Player, configKey: BreakSkillTargetConfigKey): F[Boolean]

}

object BreakSkillTargetConfigAPI {

def apply[F[_], Player](
implicit ev: BreakSkillTargetConfigAPI[F, Player]
): BreakSkillTargetConfigAPI[F, Player] = ev

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.github.unchama.seichiassist.subsystems.breakskilltargetconfig

import cats.data.Kleisli
import cats.effect.{Sync, SyncEffect}
import com.github.unchama.datarepository.bukkit.player.BukkitRepositoryControls
import com.github.unchama.generic.ContextCoercion
import com.github.unchama.seichiassist.meta.subsystem.Subsystem
import com.github.unchama.seichiassist.subsystems.breakskilltargetconfig.application.repository.BreakSkillTargetConfigRepositoryDefinition
import com.github.unchama.seichiassist.subsystems.breakskilltargetconfig.domain.{
BreakSkillTargetConfigKey,
BreakSkillTargetConfigPersistence
}
import com.github.unchama.seichiassist.subsystems.breakskilltargetconfig.persistence.JdbcBreakSkillTargetConfigPersistence
import org.bukkit.entity.Player

trait System[F[_], Player] extends Subsystem[F] {
val api: BreakSkillTargetConfigAPI[F, Player]
}

object System {

import cats.implicits._

def wired[F[_]: Sync, G[_]: SyncEffect: ContextCoercion[*[_], F]]: G[System[F, Player]] = {
implicit val breakSkillTargetConfigPersistence: BreakSkillTargetConfigPersistence[G] =
new JdbcBreakSkillTargetConfigPersistence[G]

for {
breakSkillTargetConfigRepositoryControls <- BukkitRepositoryControls.createHandles(
BreakSkillTargetConfigRepositoryDefinition.withContext[G, Player]
)
} yield {
val breakSkillTargetConfigRepository = breakSkillTargetConfigRepositoryControls.repository

new System[F, Player] {
override val api: BreakSkillTargetConfigAPI[F, Player] =
new BreakSkillTargetConfigAPI[F, Player] {
override def toggleBreakSkillTargetConfig(
configKey: BreakSkillTargetConfigKey
): Kleisli[F, Player, Unit] =
Kleisli { player =>
ContextCoercion(
breakSkillTargetConfigRepository(player)
.update(_.toggleBreakSkillTargetConfig(configKey))
)
}

override def breakSkillTargetConfig(
player: Player,
configKey: BreakSkillTargetConfigKey
): F[Boolean] =
ContextCoercion(
breakSkillTargetConfigRepository(player)
.get
.map(_.breakSkillTargetConfig(configKey))
)
}

override val managedRepositoryControls: Seq[BukkitRepositoryControls[F, _]] = Seq(
breakSkillTargetConfigRepositoryControls.coerceFinalizationContextTo[F]
)
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.unchama.seichiassist.subsystems.breakskilltargetconfig.application.repository

import cats.effect.Sync
import cats.effect.concurrent.Ref
import com.github.unchama.datarepository.definitions.RefDictBackedRepositoryDefinition
import com.github.unchama.datarepository.template.RepositoryDefinition
import com.github.unchama.seichiassist.subsystems.breakskilltargetconfig.domain.{
BreakSkillTargetConfig,
BreakSkillTargetConfigPersistence
}

object BreakSkillTargetConfigRepositoryDefinition {

def withContext[F[_]: Sync, Player](
implicit persistence: BreakSkillTargetConfigPersistence[F]
): RepositoryDefinition[F, Player, Ref[F, BreakSkillTargetConfig]] =
RefDictBackedRepositoryDefinition
.usingUuidRefDict[F, Player, BreakSkillTargetConfig](persistence)(
BreakSkillTargetConfig.initial
)
.toRefRepository

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.unchama.seichiassist.subsystems.breakskilltargetconfig.domain

case class BreakSkillTargetConfig(config: Map[BreakSkillTargetConfigKey, Boolean]) {

/**
* @return `configKey`の破壊フラグをトグルする
*/
def toggleBreakSkillTargetConfig(
configKey: BreakSkillTargetConfigKey
): BreakSkillTargetConfig =
this.copy(this.config + (configKey -> this.config.getOrElse(configKey, false)))

/**
* @return 現在の破壊フラグを取得する
*/
def breakSkillTargetConfig(configKey: BreakSkillTargetConfigKey): Boolean =
this.config.getOrElse(configKey, false)

}

object BreakSkillTargetConfig {

/**
* [[BreakSkillTargetConfig]]の初期値
*/
val initial: BreakSkillTargetConfig = BreakSkillTargetConfig(Map.empty)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.github.unchama.seichiassist.subsystems.breakskilltargetconfig.domain

import enumeratum.{Enum, EnumEntry}

/**
* 予め決められたブロックの集合を表現する。
* これらの値は整地スキルでブロックを破壊する際に、
* プレイヤーの設定に応じて一部のブロックを除外する際に使われる。
*/
sealed trait BreakSkillTargetConfigKey extends EnumEntry

object BreakSkillTargetConfigKey extends Enum[BreakSkillTargetConfigKey] {

val values: IndexedSeq[BreakSkillTargetConfigKey] = findValues

/**
* チェストを破壊するかどうか
*/
case object Chest extends BreakSkillTargetConfigKey

/**
* ネザークォーツをクラフトしたブロックを破壊するかどうか
*/
case object MadeFromNetherQuartz extends BreakSkillTargetConfigKey

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.unchama.seichiassist.subsystems.breakskilltargetconfig.domain

import com.github.unchama.generic.RefDict

import java.util.UUID

trait BreakSkillTargetConfigPersistence[F[_]] extends RefDict[F, UUID, BreakSkillTargetConfig]
Loading

0 comments on commit fe9cf4f

Please sign in to comment.