From 6e56ecdc99039bd84be1de6ca9ef578b9339121f Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 19 Jan 2023 16:57:23 +0100 Subject: [PATCH] Add Permission.Perm Signed-off-by: yoyosource --- .../src/de/steamwar/bausystem/Permission.java | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/Permission.java b/BauSystem_Main/src/de/steamwar/bausystem/Permission.java index 539a0f80..fb1902f4 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/Permission.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/Permission.java @@ -20,12 +20,17 @@ package de.steamwar.bausystem; import de.steamwar.bausystem.config.BauServer; +import de.steamwar.command.CommandMetaData; +import de.steamwar.command.TypeValidator; import de.steamwar.sql.BauweltMember; -import de.steamwar.sql.SteamwarUser; import lombok.AllArgsConstructor; -import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; import java.util.function.Predicate; @AllArgsConstructor @@ -54,4 +59,33 @@ public enum Permission { public static boolean hasPermission(Player member, Permission permission) { return permission.hasPermission(member); } + + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.PARAMETER) + @CommandMetaData.Parameter({Player.class}) + @CommandMetaData.ImplicitValidator(handler = Perm.Handler.class, order = -10) + public @interface Perm { + Permission value(); + String message() default ""; + + class Handler implements TypeValidator { + + private Permission permission; + private String message; + + public Handler(Perm perm) { + this.permission = perm.value(); + this.message = perm.message(); + if (message != null && message.isEmpty()) message = null; + } + + @Override + public boolean validate(CommandSender commandSender, Player player, MessageSender messageSender) { + if (message == null) { + return permission.hasPermission(player); + } + return !messageSender.send(!permission.hasPermission((Player) commandSender), message); + } + } + } } \ No newline at end of file