From b63a5a410d9bb2a6b2486e8f8f32ce5a0817a485 Mon Sep 17 00:00:00 2001 From: Alice Isabel Date: Mon, 1 Jan 2024 23:48:48 -0300 Subject: [PATCH] Add `FabricButtonClaimPermission` This implements PC-1051 --- .../minecity/fabric/MineCityFabric.kt | 2 + .../permission/FabricButtonClaimPermission.kt | 58 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/service/permission/FabricButtonClaimPermission.kt diff --git a/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/MineCityFabric.kt b/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/MineCityFabric.kt index 43e94f6..dd60edd 100644 --- a/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/MineCityFabric.kt +++ b/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/MineCityFabric.kt @@ -17,6 +17,7 @@ import br.com.gamemods.minecity.fabric.math.pos.FabricEntityLocation import br.com.gamemods.minecity.fabric.server.MineCityFabricServer import br.com.gamemods.minecity.fabric.service.FabricNamedPlayerService import br.com.gamemods.minecity.fabric.service.FabricWorldService +import br.com.gamemods.minecity.fabric.service.permission.FabricButtonClaimPermission import br.com.gamemods.minecity.fabric.service.permission.FabricDoorClaimPermission import br.com.gamemods.minecity.fabric.wrapper.FabricBlockPosWrapper import br.com.gamemods.minecity.fabric.wrapper.FabricChunkPosWrapper @@ -80,6 +81,7 @@ object MineCityFabric : ModInitializer, MineCityPlatform { private fun registerPermissions() { core.permission += FabricDoorClaimPermission() + core.permission += FabricButtonClaimPermission() } @ServerSideOnly diff --git a/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/service/permission/FabricButtonClaimPermission.kt b/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/service/permission/FabricButtonClaimPermission.kt new file mode 100644 index 0000000..0bcb24f --- /dev/null +++ b/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/service/permission/FabricButtonClaimPermission.kt @@ -0,0 +1,58 @@ +package br.com.gamemods.minecity.fabric.service.permission + +import br.com.gamemods.minecity.api.MineCity +import br.com.gamemods.minecity.api.annotation.internal.InternalMineCityApi +import br.com.gamemods.minecity.api.id.ClaimPermissionId +import br.com.gamemods.minecity.api.service.permission.ClaimPermission +import br.com.gamemods.minecity.core.helpers.isInstanceOfNone +import br.com.gamemods.minecity.fabric.helpers.blockStateBy +import br.com.gamemods.minecity.fabric.helpers.hasPermissionIn +import br.com.gamemods.minecity.fabric.service.claim.FabricClaimService.Companion.get +import net.fabricmc.fabric.api.event.player.UseBlockCallback +import net.kyori.adventure.text.Component +import net.minecraft.block.ButtonBlock +import net.minecraft.block.DaylightDetectorBlock +import net.minecraft.block.LeverBlock +import net.minecraft.entity.player.PlayerEntity +import net.minecraft.util.ActionResult +import net.minecraft.util.Hand +import net.minecraft.util.hit.BlockHitResult +import net.minecraft.util.hit.HitResult +import net.minecraft.world.World + +@InternalMineCityApi +class FabricButtonClaimPermission: ClaimPermission( + id = ClaimPermissionId.BUTTONS, + name = Component.text("Buttons"), + description = Component.text("Allows the player to trigger buttons, daylight sensors, switches and more.") +) { + override fun onRegister() { + UseBlockCallback.EVENT.register(OnUseBlock()) + } + + private inner class OnUseBlock() : UseBlockCallback { + override fun interact( player: PlayerEntity, world: World, hand: Hand, hitResult: BlockHitResult): ActionResult { + if (world.isClient) { + return ActionResult.PASS + } + + if (hitResult.type != HitResult.Type.BLOCK) { + return ActionResult.PASS + } + + val hitPos = hitResult.blockPos + val block = hitResult.blockStateBy(world).block + + if (block.isInstanceOfNone(ButtonBlock::class, LeverBlock::class, DaylightDetectorBlock::class)) { + return ActionResult.PASS + } + + val claim = MineCity.claims[world, hitPos] ?: return ActionResult.PASS + + return player.uuid.hasPermissionIn(claim, permissionId = ClaimPermissionId.BUTTONS) + } + } +} + + +