From 41bf1b2f218e2aff476ac8d29fc49f6f2aa43dac Mon Sep 17 00:00:00 2001 From: Alice Isabel Date: Mon, 1 Jan 2024 23:42:34 -0300 Subject: [PATCH 1/4] Add reflection helpers. Currently only has `isInstanceOfAny` and `isInstanceOfNone` functions. --- .../minecity/core/helpers/Reflection.kt | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 core/src/main/kotlin/br/com/gamemods/minecity/core/helpers/Reflection.kt diff --git a/core/src/main/kotlin/br/com/gamemods/minecity/core/helpers/Reflection.kt b/core/src/main/kotlin/br/com/gamemods/minecity/core/helpers/Reflection.kt new file mode 100644 index 0000000..a837f2a --- /dev/null +++ b/core/src/main/kotlin/br/com/gamemods/minecity/core/helpers/Reflection.kt @@ -0,0 +1,42 @@ +package br.com.gamemods.minecity.core.helpers + +import kotlin.reflect.KClass +import kotlin.reflect.full.isSubclassOf + +/** + * Checks if an object **is** an instance of **ANY** of the classes given. + * @param types A list of [KClass] that will be checked. + * + * The following code examples are equivalent. + * ```kt + * obj is TypeA || obj is TypeB + * obj.isInstanceOfAny(TypeA::class, TypeB::class) + * ``` + * @author alikindsys + */ +fun Any.isInstanceOfAny(vararg types: KClass<*>): Boolean { + // Short-circuiting on a list based on an OR operation. + for (cls in types) { + // ⊤ OR { ⊥,⊤ } = ⊤ + // { ⊥,⊤ } OR ⊥ = { ⊥,⊤ } + if (this::class.isSubclassOf(cls)) return true + } + // ⊥ OR ⊥ = ⊥ + return false +} + +/** + * Checks if an object **is NOT** an instance of **ALL** the classes given. + * @param types A list of [KClass] that will be . + * + * The following code examples are equivalent. + * ```kt + * obj !is TypeA && obj !is TypeB + * obj.isInstanceOfNone(TypeA::class, TypeB::class) + * ``` + * @author alikindsys + */ +fun Any.isInstanceOfNone(vararg types: KClass<*>): Boolean { + // !(P OR Q) = (!P AND !Q) + return !this.isInstanceOfAny(*types) +} \ No newline at end of file From 778ceb180b06a813b83d837e0bb8864f58c23413 Mon Sep 17 00:00:00 2001 From: Alice Isabel Date: Mon, 1 Jan 2024 23:42:59 -0300 Subject: [PATCH 2/4] Add permission helpers. Currently only has `UniqueId.hasPermissionIn`. --- .../minecity/fabric/helpers/Permission.kt | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/helpers/Permission.kt diff --git a/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/helpers/Permission.kt b/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/helpers/Permission.kt new file mode 100644 index 0000000..9948312 --- /dev/null +++ b/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/helpers/Permission.kt @@ -0,0 +1,25 @@ +package br.com.gamemods.minecity.fabric.helpers + +import br.com.gamemods.minecity.api.claim.Claim +import br.com.gamemods.minecity.api.id.ClaimPermissionId +import br.com.gamemods.minecity.api.serializer.UniqueId +import net.minecraft.util.ActionResult + +/** + * Checks if a [UniqueId] has a given [ClaimPermissionId] inside a [Claim]. + * + * *Note*: This is platform-dependent code since each loader treats [ActionResult]s differently. + * This code is **expected to work with Fabric API**'s event system. + * @param claim The claim being checked. Usually the one the player is located at. + * @param permissionId The [ClaimPermissionId] of the permission. E.g. [ClaimPermissionId.BUILD]. + * @return An [ActionResult.PASS] if the player **has** the permission. An [ActionResult.FAIL] if the player **doesn't** have the permission. + * @see [ClaimPermissionId] + * @author alikindsys + */ +fun UniqueId.hasPermissionIn(claim: Claim, permissionId: ClaimPermissionId): ActionResult { + return if (claim.hasPermission(this, permissionId)) { + ActionResult.PASS + } else { + ActionResult.FAIL + } +} From 5df569cee119dadca7b3dcad0e89f0a27f8cb919 Mon Sep 17 00:00:00 2001 From: Alice Isabel Date: Mon, 1 Jan 2024 23:43:39 -0300 Subject: [PATCH 3/4] Add world access helpers. Currently only has `BlockHitResult.blockStateBy` and `BlockHitResult.blockEntityBy`. --- .../minecity/fabric/helpers/WorldAccess.kt | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/helpers/WorldAccess.kt diff --git a/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/helpers/WorldAccess.kt b/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/helpers/WorldAccess.kt new file mode 100644 index 0000000..aa9f2d3 --- /dev/null +++ b/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/helpers/WorldAccess.kt @@ -0,0 +1,31 @@ +package br.com.gamemods.minecity.fabric.helpers + +import net.minecraft.block.BlockState +import net.minecraft.block.entity.BlockEntity +import net.minecraft.util.hit.BlockHitResult +import net.minecraft.world.World + +/** + * Gets a [BlockState] from a [BlockHitResult] by accessing a [World] + * + * You *should* have a reference to a world on which this [BlockHitResult] was queried. + * + * **Trivial**: This is a mere convenience extension function. + * @param world The world being queried. + * @author alikindsys + */ +fun BlockHitResult.blockStateBy(world: World) : BlockState { + return world.getBlockState(this.blockPos) +} + +/** + * Gets a [BlockEntity] from a [BlockHitResult] by accessing a [World] + * + * You *should* have a reference to a world on which this [BlockHitResult] was queried. + * + * **Trivial**: This is a mere convenience extension function. + * @param world The world being queried. + * @author alikindsys + */fun BlockHitResult.blockEntityBy(world: World) : BlockEntity? { + return world.getBlockEntity(this.blockPos) +} \ No newline at end of file From 4e26b72cfdfc226837ce2e4f8ad778e9c4179bd7 Mon Sep 17 00:00:00 2001 From: alikindsys Date: Mon, 5 Feb 2024 19:15:53 -0300 Subject: [PATCH 4/4] Add linebreak on `blockEntityBy` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Lobo Metalúrgico <43734867+LoboMetalurgico@users.noreply.github.com> --- .../br/com/gamemods/minecity/fabric/helpers/WorldAccess.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/helpers/WorldAccess.kt b/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/helpers/WorldAccess.kt index aa9f2d3..c02fbcd 100644 --- a/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/helpers/WorldAccess.kt +++ b/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/helpers/WorldAccess.kt @@ -26,6 +26,7 @@ fun BlockHitResult.blockStateBy(world: World) : BlockState { * **Trivial**: This is a mere convenience extension function. * @param world The world being queried. * @author alikindsys - */fun BlockHitResult.blockEntityBy(world: World) : BlockEntity? { + */ + fun BlockHitResult.blockEntityBy(world: World) : BlockEntity? { return world.getBlockEntity(this.blockPos) } \ No newline at end of file