/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
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 lombok.AllArgsConstructor;
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
public enum Permission {
WORLD(BauweltMember::isWorld),
WORLDEDIT(BauweltMember::isWorldEdit),
MEMBER(bauweltMember -> true),
OWNER(bauweltMember -> false);
private final Predicate permissionPredicate;
public boolean hasPermission(Player member) {
if (member.getUniqueId().equals(BauServer.getInstance().getOwner())) {
return true;
}
BauweltMember bauMember = BauweltMember.getBauMember(BauServer.getInstance().getOwner(), member.getUniqueId());
if (bauMember == null) {
return false;
}
return permissionPredicate.test(bauMember);
}
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);
}
}
}
}