generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PC-1059 Partial implementation of build permission
- Loading branch information
Polyana
committed
Dec 28, 2023
1 parent
c10268a
commit c3a5f80
Showing
11 changed files
with
214 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
.../fabric/src/main/java/br/com/gamemods/minecity/fabric/common/mixin/BlockItemAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package br.com.gamemods.minecity.fabric.common.mixin; | ||
|
||
import net.minecraft.fluid.Fluid; | ||
import net.minecraft.item.BucketItem; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.gen.Accessor; | ||
|
||
@Mixin(BucketItem.class) | ||
public interface BlockItemAccessor { | ||
@Accessor | ||
Fluid getFluid(); | ||
} |
20 changes: 20 additions & 0 deletions
20
.../src/main/java/br/com/gamemods/minecity/fabric/common/mixin/PlayerCanPlaceBlockMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package br.com.gamemods.minecity.fabric.common.mixin; | ||
|
||
import br.com.gamemods.minecity.fabric.event.BlockCanPlaceCallback; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.item.BlockItem; | ||
import net.minecraft.item.ItemPlacementContext; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
@Mixin(BlockItem.class) | ||
public class PlayerCanPlaceBlockMixin { | ||
@Inject(method = "canPlace(Lnet/minecraft/item/ItemPlacementContext;Lnet/minecraft/block/BlockState;)Z", at = @At("HEAD"), cancellable = true) | ||
private void restrict(ItemPlacementContext context, BlockState state, CallbackInfoReturnable<Boolean> cir) { | ||
if (!BlockCanPlaceCallback.EVENT.invoker().canPlace(context, state)) { | ||
cir.setReturnValue(false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...orm/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/event/BlockCanPlaceCallback.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package br.com.gamemods.minecity.fabric.event | ||
|
||
import br.com.gamemods.minecity.api.annotation.internal.InternalMineCityApi | ||
import net.fabricmc.fabric.api.event.Event | ||
import net.fabricmc.fabric.api.event.EventFactory | ||
import net.minecraft.block.BlockState | ||
import net.minecraft.item.ItemPlacementContext | ||
|
||
@InternalMineCityApi | ||
interface BlockCanPlaceCallback { | ||
|
||
fun canPlace(context: ItemPlacementContext, state: BlockState): Boolean | ||
|
||
companion object { | ||
@JvmField | ||
val EVENT: Event<BlockCanPlaceCallback> = EventFactory.createArrayBacked(BlockCanPlaceCallback::class.java) { listeners -> | ||
object : BlockCanPlaceCallback { | ||
override fun canPlace(context: ItemPlacementContext, state: BlockState): Boolean { | ||
for (listener in listeners) { | ||
if (!listener.canPlace(context, state)) | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
} | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...n/kotlin/br/com/gamemods/minecity/fabric/service/permission/FabricBuildClaimPermission.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package br.com.gamemods.minecity.fabric.service.permission | ||
|
||
import br.com.gamemods.minecity.api.annotation.internal.InternalMineCityApi | ||
import br.com.gamemods.minecity.api.id.ClaimPermissionId | ||
import br.com.gamemods.minecity.fabric.MineCityFabric | ||
import br.com.gamemods.minecity.fabric.common.mixin.BlockItemAccessor | ||
import br.com.gamemods.minecity.fabric.event.BlockCanPlaceCallback | ||
import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents | ||
import net.fabricmc.fabric.api.event.player.UseBlockCallback | ||
import net.kyori.adventure.text.Component | ||
import net.minecraft.block.BlockState | ||
import net.minecraft.block.entity.BlockEntity | ||
import net.minecraft.entity.player.PlayerEntity | ||
import net.minecraft.fluid.Fluids | ||
import net.minecraft.item.BucketItem | ||
import net.minecraft.item.ItemPlacementContext | ||
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.util.math.BlockPos | ||
import net.minecraft.world.World | ||
|
||
/** | ||
* Represents a permission that can be granted to a player in a claim. | ||
* | ||
* @property id The unique identifier for the permission. | ||
* @property name The name of the permission. | ||
* @property description The description of the permission. | ||
*/ | ||
@InternalMineCityApi | ||
class FabricBuildClaimPermission: FabricClaimPermission( | ||
id = ClaimPermissionId.BUILD, | ||
name = Component.text("Build"), | ||
description = Component.text("Allows the player to build in the claim.") | ||
) { | ||
override fun onRegister() { | ||
PlayerBlockBreakEvents.BEFORE.register(OnBlockBreak()) | ||
BlockCanPlaceCallback.EVENT.register(OnBlockCanPlace()) | ||
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 (hand != Hand.MAIN_HAND || hitResult.type != HitResult.Type.BLOCK) { | ||
return ActionResult.PASS | ||
} | ||
|
||
val bucket = player.mainHandStack.item as? BucketItem ?: return ActionResult.PASS | ||
val fluid = (bucket as BlockItemAccessor).fluid | ||
val affectedBlockPos = if (fluid == Fluids.EMPTY) { | ||
hitResult.blockPos | ||
} else { | ||
hitResult.blockPos.offset(hitResult.side) | ||
} | ||
MineCityFabric.core.log.info { "Bucket: $bucket, fluid: $fluid, affectedBlockPos: $affectedBlockPos, hitResult.blockPos: ${hitResult.blockPos}" } | ||
return if (checkPermission(world, player, affectedBlockPos, ClaimPermissionId.BUILD)) { | ||
ActionResult.PASS | ||
} else { | ||
ActionResult.FAIL | ||
} | ||
} | ||
} | ||
|
||
private inner class OnBlockBreak : PlayerBlockBreakEvents.Before { | ||
override fun beforeBlockBreak(world: World, player: PlayerEntity, pos: BlockPos, state: BlockState, blockEntity: BlockEntity?): Boolean { | ||
return checkPermission(world, player, pos, ClaimPermissionId.BUILD) | ||
} | ||
} | ||
|
||
private inner class OnBlockCanPlace : BlockCanPlaceCallback { | ||
override fun canPlace(context: ItemPlacementContext, state: BlockState): Boolean { | ||
val player = context.player ?: return true // TODO: If the player is null, might be a piston, CC turtle, etc; Must check if the owner of the block has permission to build on the target claim | ||
return checkPermission(context.world, player, context.blockPos, ClaimPermissionId.BUILD) | ||
} | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...c/main/kotlin/br/com/gamemods/minecity/fabric/service/permission/FabricClaimPermission.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
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.fabric.service.claim.FabricClaimService.Companion.get | ||
import net.kyori.adventure.text.Component | ||
import net.minecraft.entity.player.PlayerEntity | ||
import net.minecraft.util.math.BlockPos | ||
import net.minecraft.world.World | ||
|
||
/** | ||
* Represents a permission that can be granted to a player in a claim. | ||
* | ||
* @property id The unique identifier for the permission. | ||
* @property name The name of the permission. | ||
* @property description The description of the permission. | ||
*/ | ||
@InternalMineCityApi | ||
abstract class FabricClaimPermission( | ||
id: ClaimPermissionId, | ||
name: Component, | ||
description: Component | ||
) : ClaimPermission(id, name, description) { | ||
|
||
/** | ||
* Checks if the player has the permission in the claim. | ||
* | ||
* @param world The world where the permission is being checked. | ||
* @param player The player that is being checked. | ||
* @param pos The position where the permission is being checked. | ||
* @param permissionId The permission that is being checked. | ||
* @return `true` if the player has the permission, `false` otherwise. | ||
*/ | ||
protected fun checkPermission(world: World, player: PlayerEntity, pos: BlockPos, permissionId: ClaimPermissionId): Boolean { | ||
if (world.isClient) { | ||
return true | ||
} | ||
|
||
val claim = MineCity.claims[world, pos] ?: return true | ||
return claim.hasPermission(player.identity().uuid(), permissionId) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
{ | ||
"required": true, | ||
"package": "br.com.gamemods.minecity.fabric.common.mixin", | ||
"compatibilityLevel": "JAVA_17", | ||
"mixins": [ | ||
"ExampleMixin" | ||
], | ||
"injectors": { | ||
"defaultRequire": 1 | ||
"required": true, | ||
"package": "br.com.gamemods.minecity.fabric.common.mixin", | ||
"compatibilityLevel": "JAVA_17", | ||
"mixins": [ | ||
"BlockItemAccessor", | ||
"ExampleMixin", | ||
"PlayerCanPlaceBlockMixin" | ||
], | ||
"injectors": { | ||
"defaultRequire": 1 | ||
} | ||
} |