SteamWar/SpigotCore
Archiviert
13
0

Fix SWCommand Mapper stuff

Add copyright to SubCommand
Dieser Commit ist enthalten in:
yoyosource 2021-03-25 14:13:35 +01:00
Ursprung 9773b9d7ea
Commit 97a8862b62
3 geänderte Dateien mit 106 neuen und 15 gelöschten Zeilen

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.acommand; package de.steamwar.acommand;
import de.steamwar.command.SWCommand; import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeMapper;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class TestCommand extends SWCommand { public class TestCommand extends SWCommand {
public TestCommand() { public TestCommand() {
@ -18,10 +42,33 @@ public class TestCommand extends SWCommand {
} }
// Another Command, subCommands can be implemented with the Register Annotation, // 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"}) @Register({"two"})
public void testTwo(Player player, int i, @Mapper("Hello World") Material material) { 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<Material> materialTypeMapper() {
List<String> 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<String> tabCompletes(String s) {
return tabCompletes;
}
};
}
} }

Datei anzeigen

@ -74,24 +74,26 @@ public abstract class SWCommand {
}); });
for (Method method : getClass().getDeclaredMethods()) { for (Method method : getClass().getDeclaredMethods()) {
Register register = method.getDeclaredAnnotation(Register.class); addMapper(method);
if (register == null) { }
continue; for (Method method : getClass().getDeclaredMethods()) {
} addCommand(method);
if (!validMethod(method)) {
continue;
}
commandSet.add(new SubCommand(this, 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(); Parameter[] parameters = method.getParameters();
if (parameters.length == 0) { if (parameters.length == 0) {
return false; return;
} }
if (!CommandSender.class.isAssignableFrom(parameters[0].getType())) { if (!CommandSender.class.isAssignableFrom(parameters[0].getType())) {
return false; return;
} }
for (int i = 1; i < parameters.length; i++) { for (int i = 1; i < parameters.length; i++) {
Parameter parameter = parameters[i]; Parameter parameter = parameters[i];
@ -107,9 +109,32 @@ public abstract class SWCommand {
if (mapper != null) { if (mapper != null) {
name = mapper.value(); 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<CommandSender> helpMessage) { protected final void setHelpMessage(Consumer<CommandSender> helpMessage) {
@ -128,7 +153,7 @@ public abstract class SWCommand {
} }
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER}) @Target({ElementType.PARAMETER, ElementType.METHOD})
protected @interface Mapper { protected @interface Mapper {
String value(); String value();
} }

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command; package de.steamwar.command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;