SteamWar/SpigotCore
Archiviert
13
0

Add StringConstraint for subCommand definitions

Dieser Commit ist enthalten in:
yoyosource 2021-03-12 15:52:46 +01:00
Ursprung 43a9de08a0
Commit b3c4c45892
3 geänderte Dateien mit 39 neuen und 4 gelöschten Zeilen

Datei anzeigen

@ -80,7 +80,9 @@ public abstract class SWCommand {
if (!CommandSender.class.isAssignableFrom(parameters[0].getType())) return false;
for (int i = 1; i < parameters.length; i++) {
Parameter parameter = parameters[i];
StringConstraint stringConstraint = parameter.getAnnotation(StringConstraint.class);
Class<?> clazz = parameter.getType();
if (stringConstraint != null && clazz != String.class) return false;
if (parameter.isVarArgs() && i == parameters.length - 1) {
clazz = parameter.getType().getComponentType();
}

Datei anzeigen

@ -26,10 +26,7 @@ import org.bukkit.entity.Player;
import java.lang.reflect.Field;
import java.lang.reflect.Parameter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
@ -91,8 +88,13 @@ class SWCommandUtils {
arguments[0] = parameters[0].getType().cast(commandSender);
for (int i = 1; i < parameters.length - (varArgs ? 1 : 0); i++) {
Parameter parameter = parameters[i];
Class<?> clazz = parameters[i].getType();
arguments[i] = mapper(clazz).apply(args[i - 1]);
StringConstraint stringConstraint = parameter.getAnnotation(StringConstraint.class);
if (stringConstraint != null && !Objects.equals(arguments[i], stringConstraint.constraint())) {
throw new SecurityException();
}
}
if (varArgs) {

Datei anzeigen

@ -0,0 +1,31 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface StringConstraint {
String constraint();
}