package net.knarcraft.minigames.arena.reward; import net.knarcraft.minigames.MiniGames; import net.knarcraft.minigames.config.MiniGameMessage; import net.knarcraft.minigames.manager.PermissionManager; import org.bukkit.World; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.HashMap; import java.util.Map; import java.util.logging.Level; /** * A reward that grants a specified permission when granted */ public class PermissionReward implements Reward { private final @NotNull String permission; private final @Nullable World world; /** * Instantiates a new permission reward * * @param world
The world to grant the permission for, or null
* @param permissionThe permission to grant
*/ public PermissionReward(@Nullable World world, @NotNull String permission) { this.world = world; this.permission = permission; } @Override public boolean grant(@NotNull Player player) { if (!PermissionManager.isInitialized()) { MiniGames.log(Level.SEVERE, "A permission reward has been set, but no Vault-compatible permission" + " plugin has been initialized."); return false; } if (PermissionManager.hasPermission(player, this.permission, this.world != null ? this.world.getName() : null)) { return false; } else { PermissionManager.addPermission(player, this.permission, this.world); return true; } } @Override public @NotNull String getGrantMessage() { if (world == null) { return MiniGames.getInstance().getStringFormatter().replacePlaceholder( MiniGameMessage.SUCCESS_PERMISSION_REWARDED, "{permission}", permission); } else { return MiniGames.getInstance().getStringFormatter().replacePlaceholders( MiniGameMessage.SUCCESS_PERMISSION_REWARDED_WORLD, new String[]{"{permission}", "{world}"}, new String[]{permission, world.getName()}); } } @NotNull @Override public MapThe data to deserialize from
* @returnThe deserialized data
*/ @SuppressWarnings("unused") public static PermissionReward deserialize(Map