From 97a8862b6281d4204e84f7456f73042dbde56734 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 25 Mar 2021 14:13:35 +0100 Subject: [PATCH] Fix SWCommand Mapper stuff Add copyright to SubCommand --- .../src/de/steamwar/acommand/TestCommand.java | 49 ++++++++++++++++- .../src/de/steamwar/command/SWCommand.java | 53 ++++++++++++++----- .../src/de/steamwar/command/SubCommand.java | 19 +++++++ 3 files changed, 106 insertions(+), 15 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/acommand/TestCommand.java b/SpigotCore_Main/src/de/steamwar/acommand/TestCommand.java index a09a896..aa8ca89 100644 --- a/SpigotCore_Main/src/de/steamwar/acommand/TestCommand.java +++ b/SpigotCore_Main/src/de/steamwar/acommand/TestCommand.java @@ -1,9 +1,33 @@ +/* + * 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.acommand; import de.steamwar.command.SWCommand; +import de.steamwar.command.TypeMapper; import org.bukkit.Material; import org.bukkit.entity.Player; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + public class TestCommand extends SWCommand { public TestCommand() { @@ -18,10 +42,33 @@ public class TestCommand extends SWCommand { } // Another Command, subCommands can be implemented with the Register Annotation, - // you can Implement custom Mappers Annotation by @Mapper on a Parameter + // you can use custom Mappers by putting a Mapper Annotation on a Parameter @Register({"two"}) public void testTwo(Player player, int i, @Mapper("Hello World") Material material) { } + // Add Custom Mapper when this command is registered, all Mapper of you class will be + // created first and than the Commands. Do not use this mapper outside your class, as + // it can only create failures. Use on your own risk. Definition order should be considered. + @Mapper("Hello World") + public TypeMapper materialTypeMapper() { + List tabCompletes = Arrays.stream(Material.values()) + .filter(Material::isSolid) + .map(Material::name) + .map(String::toLowerCase) + .collect(Collectors.toList()); + return new TypeMapper<>() { + @Override + public Material map(String s) { + return Material.valueOf(s.toUpperCase()); + } + + @Override + public List tabCompletes(String s) { + return tabCompletes; + } + }; + } + } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 4919a41..de49a86 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -74,24 +74,26 @@ public abstract class SWCommand { }); for (Method method : getClass().getDeclaredMethods()) { - Register register = method.getDeclaredAnnotation(Register.class); - if (register == null) { - continue; - } - if (!validMethod(method)) { - continue; - } - commandSet.add(new SubCommand(this, method)); + addMapper(method); + } + for (Method method : getClass().getDeclaredMethods()) { + addCommand(method); } } - private boolean validMethod(Method method) { + private void addCommand(Method method) { + Register register = method.getDeclaredAnnotation(Register.class); + Mapper methodMapper = method.getDeclaredAnnotation(Mapper.class); + if (register == null || methodMapper != null) { + return; + } + Parameter[] parameters = method.getParameters(); if (parameters.length == 0) { - return false; + return; } if (!CommandSender.class.isAssignableFrom(parameters[0].getType())) { - return false; + return; } for (int i = 1; i < parameters.length; i++) { Parameter parameter = parameters[i]; @@ -107,9 +109,32 @@ public abstract class SWCommand { if (mapper != null) { name = mapper.value(); } - if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name)) return false; + if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name)) return; + } + commandSet.add(new SubCommand(this, method)); + } + + private void addMapper(Method method) { + Register register = method.getDeclaredAnnotation(Register.class); + Mapper methodMapper = method.getDeclaredAnnotation(Mapper.class); + if (register != null || methodMapper == null) { + return; + } + + Parameter[] parameters = method.getParameters(); + if (parameters.length != 0) { + return; + } + if (method.getReturnType() != TypeMapper.class) { + return; + } + try { + method.setAccessible(true); + Object object = method.invoke(this); + SWCommandUtils.addMapper(methodMapper.value(), (TypeMapper) object); + } catch (Exception e) { + throw new SecurityException(e.getMessage(), e); } - return true; } protected final void setHelpMessage(Consumer helpMessage) { @@ -128,7 +153,7 @@ public abstract class SWCommand { } @Retention(RetentionPolicy.RUNTIME) - @Target({ElementType.PARAMETER}) + @Target({ElementType.PARAMETER, ElementType.METHOD}) protected @interface Mapper { String value(); } diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index ab0c8ca..b04abf5 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -1,3 +1,22 @@ +/* + * 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 org.bukkit.command.CommandSender;