add permission check event

This commit is contained in:
Flemmli97 2021-03-08 21:42:12 +01:00
parent ff969ce245
commit d82cec790e
2 changed files with 39 additions and 0 deletions

View File

@ -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<ClaimPermissionEvent> 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);
}

View File

@ -1,6 +1,7 @@
package com.flemmli97.flan.claim; package com.flemmli97.flan.claim;
import com.flemmli97.flan.api.ClaimPermission; import com.flemmli97.flan.api.ClaimPermission;
import com.flemmli97.flan.api.ClaimPermissionEvent;
import com.flemmli97.flan.api.PermissionRegistry; import com.flemmli97.flan.api.PermissionRegistry;
import com.flemmli97.flan.config.ConfigHandler; import com.flemmli97.flan.config.ConfigHandler;
import com.flemmli97.flan.player.PlayerClaimData; 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.server.world.ServerWorld;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting; import net.minecraft.util.Formatting;
import net.minecraft.util.math.BlockPos; 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) { 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) { if (perm != null) {
ClaimPermission.PermissionFlag flag = perm.test.test(this, player, pos); ClaimPermission.PermissionFlag flag = perm.test.test(this, player, pos);
if (flag != ClaimPermission.PermissionFlag.PASS) { if (flag != ClaimPermission.PermissionFlag.PASS) {