diff --git a/src/main/java/com/flemmli97/flan/api/ClaimPermissionEvent.java b/src/main/java/com/flemmli97/flan/api/ClaimPermissionEvent.java new file mode 100644 index 0000000..049a338 --- /dev/null +++ b/src/main/java/com/flemmli97/flan/api/ClaimPermissionEvent.java @@ -0,0 +1,34 @@ +package com.flemmli97.flan.api; + +import com.sun.istack.internal.Nullable; +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.ActionResult; +import net.minecraft.util.math.BlockPos; + +public interface ClaimPermissionEvent { + + Event CHECK = EventFactory.createArrayBacked(ClaimPermissionEvent.class, + (listeners) -> (player, permission, pos) -> { + for (ClaimPermissionEvent event : listeners) { + ActionResult result = event.check(player, permission, pos); + + if (result != ActionResult.PASS) { + return result; + } + } + + return ActionResult.PASS; + } + ); + + /** + * Callback for when permissions are checked + * @param player The corresponding player. Can be null if the check is e.g. caused by tnt explosions + * @param permission The permission to check + * @param pos The block pos where the action is occuring + * @return ActionResult#PASS to do nothing. ActionResult#FAIL to prevent the action. Else to allow the action + */ + ActionResult check(@Nullable ServerPlayerEntity player, ClaimPermission permission, BlockPos pos); +} diff --git a/src/main/java/com/flemmli97/flan/claim/Claim.java b/src/main/java/com/flemmli97/flan/claim/Claim.java index f25bddc..f6e999f 100644 --- a/src/main/java/com/flemmli97/flan/claim/Claim.java +++ b/src/main/java/com/flemmli97/flan/claim/Claim.java @@ -1,6 +1,7 @@ package com.flemmli97.flan.claim; import com.flemmli97.flan.api.ClaimPermission; +import com.flemmli97.flan.api.ClaimPermissionEvent; import com.flemmli97.flan.api.PermissionRegistry; import com.flemmli97.flan.config.ConfigHandler; import com.flemmli97.flan.player.PlayerClaimData; @@ -17,6 +18,7 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; import net.minecraft.text.LiteralText; import net.minecraft.text.Text; +import net.minecraft.util.ActionResult; import net.minecraft.util.Formatting; import net.minecraft.util.math.BlockPos; @@ -181,6 +183,9 @@ public class Claim implements IPermissionContainer { } public boolean canInteract(ServerPlayerEntity player, ClaimPermission perm, BlockPos pos, boolean message) { + ActionResult res = ClaimPermissionEvent.CHECK.invoker().check(player, perm, pos); + if (res != ActionResult.PASS) + return res != ActionResult.FAIL; if (perm != null) { ClaimPermission.PermissionFlag flag = perm.test.test(this, player, pos); if (flag != ClaimPermission.PermissionFlag.PASS) {