From b3c4c45892340a42815095529f97e333d523fa97 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 12 Mar 2021 15:52:46 +0100 Subject: [PATCH] Add StringConstraint for subCommand definitions --- .../src/de/steamwar/command/SWCommand.java | 2 ++ .../de/steamwar/command/SWCommandUtils.java | 10 +++--- .../de/steamwar/command/StringConstraint.java | 31 +++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/command/StringConstraint.java diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 725b891..5c31b83 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -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(); } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index aaa194e..c225205 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -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) { diff --git a/SpigotCore_Main/src/de/steamwar/command/StringConstraint.java b/SpigotCore_Main/src/de/steamwar/command/StringConstraint.java new file mode 100644 index 0000000..368ad18 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/command/StringConstraint.java @@ -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 . + */ + +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(); +}