|
| 1 | +// |
| 2 | +// Hyperverse - A minecraft world management plugin |
| 3 | +// Copyright © 2020 Alexander Söderberg ([email protected]) |
| 4 | +// |
| 5 | +// This program is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU General Public License as published by |
| 7 | +// the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +// |
| 18 | + |
| 19 | +package com.intellectualsites.hyperverse.flags.implementation; |
| 20 | + |
| 21 | +import com.intellectualsites.hyperverse.configuration.Messages; |
| 22 | +import com.intellectualsites.hyperverse.flags.FlagParseException; |
| 23 | +import com.intellectualsites.hyperverse.flags.WorldFlag; |
| 24 | +import org.jetbrains.annotations.NotNull; |
| 25 | + |
| 26 | +import java.util.regex.Pattern; |
| 27 | + |
| 28 | +public class WorldPermissionFlag extends WorldFlag<String, WorldPermissionFlag> { |
| 29 | + |
| 30 | + private static final Pattern permissionPattern = Pattern.compile("[A-Za-z0-9\\-_.]+"); |
| 31 | + public static final WorldPermissionFlag WORLD_PERMISSION_FLAG_DEFAULT = new WorldPermissionFlag(""); |
| 32 | + |
| 33 | + public WorldPermissionFlag(@NotNull final String value) { |
| 34 | + super(value, Messages.flagWorldPermission); |
| 35 | + } |
| 36 | + |
| 37 | + @Override public WorldPermissionFlag parse(@NotNull final String input) throws FlagParseException { |
| 38 | + if (input.isEmpty()) { |
| 39 | + return WORLD_PERMISSION_FLAG_DEFAULT; |
| 40 | + } |
| 41 | + if (permissionPattern.matcher(input).matches()) { |
| 42 | + return flagOf(input); |
| 43 | + } |
| 44 | + throw new FlagParseException(this, input, "A permission node may only contain alphanumerical characters," |
| 45 | + + " -, . and _"); |
| 46 | + } |
| 47 | + |
| 48 | + @Override public WorldPermissionFlag merge(@NotNull final String newValue) { |
| 49 | + return flagOf(newValue); |
| 50 | + } |
| 51 | + |
| 52 | + @Override public String toString() { |
| 53 | + return getValue(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override public String getExample() { |
| 57 | + return "your.permission.node"; |
| 58 | + } |
| 59 | + |
| 60 | + @Override protected WorldPermissionFlag flagOf(@NotNull final String value) { |
| 61 | + return new WorldPermissionFlag(value); |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments