Skip to content

Commit 1aadfe7

Browse files
committed
* NEW: Commands: I changed everything
1 parent 05c4072 commit 1aadfe7

28 files changed

+1071
-227
lines changed

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343

4444
<properties>
4545
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
46-
<maven.compiler.source>1.7</maven.compiler.source>
47-
<maven.compiler.target>1.7</maven.compiler.target>
46+
<maven.compiler.source>1.8</maven.compiler.source>
47+
<maven.compiler.target>1.8</maven.compiler.target>
4848
</properties>
4949

5050
<build>
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,84 @@
11
/*
2-
* To change this license header, choose License Headers in Project Properties.
3-
* To change this template file, choose Tools | Templates
4-
* and open the template in the editor.
2+
* Copyright or © or Copr. ZLib contributors (2015 - 2016)
3+
*
4+
* This software is governed by the CeCILL-B license under French law and
5+
* abiding by the rules of distribution of free software. You can use,
6+
* modify and/ or redistribute the software under the terms of the CeCILL-B
7+
* license as circulated by CEA, CNRS and INRIA at the following URL
8+
* "http://www.cecill.info".
9+
*
10+
* As a counterpart to the access to the source code and rights to copy,
11+
* modify and redistribute granted by the license, users are provided only
12+
* with a limited warranty and the software's author, the holder of the
13+
* economic rights, and the successive licensors have only limited
14+
* liability.
15+
*
16+
* In this respect, the user's attention is drawn to the risks associated
17+
* with loading, using, modifying and/or developing or reproducing the
18+
* software by the user in light of its specific status of free software,
19+
* that may mean that it is complicated to manipulate, and that also
20+
* therefore means that it is reserved for developers and experienced
21+
* professionals having in-depth computer knowledge. Users are therefore
22+
* encouraged to load and test the software's suitability as regards their
23+
* requirements in conditions enabling the security of their systems and/or
24+
* data to be ensured and, more generally, to use and operate it in the
25+
* same conditions as regards security.
26+
*
27+
* The fact that you are presently reading this means that you have had
28+
* knowledge of the CeCILL-B license and that you accept its terms.
529
*/
630

731
package fr.zcraft.zlib.components.commands2;
832

9-
public class Command<T extends CommandNode> {
33+
import java.util.List;
34+
import java.util.Optional;
1035

11-
36+
/**
37+
* This class represents a registered command.
38+
* @param <T> The command runnable type this command is bound to.
39+
*/
40+
public class Command<T extends CommandRunnable> {
41+
42+
private final String name;
43+
private final Class<T> runnableClass;
44+
private final boolean isCommandGroup;
45+
private final List<SubCommand<?, T>> subCommands;
46+
47+
Command(Class<T> runnableClass, String name, boolean isCommandGroup, List<SubCommand<?, T>> subCommands) {
48+
this.runnableClass = runnableClass;
49+
this.name = name;
50+
this.isCommandGroup = isCommandGroup;
51+
this.subCommands = subCommands;
52+
}
53+
54+
public Context<? extends CommandRunnable> makeContext(CommandSender sender, String[] arguments) {
55+
return ContextGenerator.makeContext(this, sender, arguments, Optional.empty());
56+
}
57+
58+
public String getName() {
59+
return name;
60+
}
61+
62+
public boolean nameMatches(String string) {
63+
return string.equalsIgnoreCase(name);
64+
}
65+
66+
public Class<T> getRunnableClass() {
67+
return runnableClass;
68+
}
69+
70+
public boolean isCommandGroup() {
71+
return isCommandGroup;
72+
}
73+
74+
public List<SubCommand<?, T>> getSubCommands() {
75+
return subCommands;
76+
}
77+
78+
public Optional<SubCommand<?, T>> getSubCommand(String name) {
79+
for(SubCommand<?, T> subCommand : subCommands) {
80+
if(subCommand.getCommand().nameMatches(name)) return Optional.of(subCommand);
81+
}
82+
return Optional.empty();
83+
}
1284
}

src/main/java/fr/zcraft/zlib/components/commands2/CommandData.java

-70
This file was deleted.

src/main/java/fr/zcraft/zlib/components/commands2/CommandException.java

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright or © or Copr. ZLib contributors (2015 - 2016)
3+
*
4+
* This software is governed by the CeCILL-B license under French law and
5+
* abiding by the rules of distribution of free software. You can use,
6+
* modify and/ or redistribute the software under the terms of the CeCILL-B
7+
* license as circulated by CEA, CNRS and INRIA at the following URL
8+
* "http://www.cecill.info".
9+
*
10+
* As a counterpart to the access to the source code and rights to copy,
11+
* modify and redistribute granted by the license, users are provided only
12+
* with a limited warranty and the software's author, the holder of the
13+
* economic rights, and the successive licensors have only limited
14+
* liability.
15+
*
16+
* In this respect, the user's attention is drawn to the risks associated
17+
* with loading, using, modifying and/or developing or reproducing the
18+
* software by the user in light of its specific status of free software,
19+
* that may mean that it is complicated to manipulate, and that also
20+
* therefore means that it is reserved for developers and experienced
21+
* professionals having in-depth computer knowledge. Users are therefore
22+
* encouraged to load and test the software's suitability as regards their
23+
* requirements in conditions enabling the security of their systems and/or
24+
* data to be ensured and, more generally, to use and operate it in the
25+
* same conditions as regards security.
26+
*
27+
* The fact that you are presently reading this means that you have had
28+
* knowledge of the CeCILL-B license and that you accept its terms.
29+
*/
30+
31+
package fr.zcraft.zlib.components.commands2;
32+
33+
import org.bukkit.command.Command;
34+
import org.bukkit.command.CommandSender;
35+
import org.bukkit.command.TabCompleter;
36+
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
37+
38+
import java.util.List;
39+
40+
class CommandExecutor implements TabCompleter, org.bukkit.command.CommandExecutor {
41+
42+
public CommandExecutor(Command command) {
43+
44+
}
45+
46+
@Override
47+
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
48+
throw new NotImplementedException();
49+
}
50+
51+
@Override
52+
public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) {
53+
throw new NotImplementedException();
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright or © or Copr. ZLib contributors (2015 - 2016)
3+
*
4+
* This software is governed by the CeCILL-B license under French law and
5+
* abiding by the rules of distribution of free software. You can use,
6+
* modify and/ or redistribute the software under the terms of the CeCILL-B
7+
* license as circulated by CEA, CNRS and INRIA at the following URL
8+
* "http://www.cecill.info".
9+
*
10+
* As a counterpart to the access to the source code and rights to copy,
11+
* modify and redistribute granted by the license, users are provided only
12+
* with a limited warranty and the software's author, the holder of the
13+
* economic rights, and the successive licensors have only limited
14+
* liability.
15+
*
16+
* In this respect, the user's attention is drawn to the risks associated
17+
* with loading, using, modifying and/or developing or reproducing the
18+
* software by the user in light of its specific status of free software,
19+
* that may mean that it is complicated to manipulate, and that also
20+
* therefore means that it is reserved for developers and experienced
21+
* professionals having in-depth computer knowledge. Users are therefore
22+
* encouraged to load and test the software's suitability as regards their
23+
* requirements in conditions enabling the security of their systems and/or
24+
* data to be ensured and, more generally, to use and operate it in the
25+
* same conditions as regards security.
26+
*
27+
* The fact that you are presently reading this means that you have had
28+
* knowledge of the CeCILL-B license and that you accept its terms.
29+
*/
30+
31+
package fr.zcraft.zlib.components.commands2;
32+
33+
import fr.zcraft.zlib.components.commands2.annotations.Subcommand;
34+
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
35+
36+
import java.lang.reflect.Field;
37+
import java.util.ArrayList;
38+
import java.util.Arrays;
39+
import java.util.List;
40+
import java.util.stream.Collectors;
41+
42+
/**
43+
* This class contains various utilities to generate Command objects from their bound runnable class.
44+
*/
45+
class CommandGenerator {
46+
static public <T extends CommandRunnable> Command<T> fromClass(Class<T> runnableClass, String name) {
47+
if(isCommandGroup(runnableClass)) {
48+
return fromEnumClass(runnableClass, name);
49+
} else {
50+
return fromPlainClass(runnableClass, name);
51+
}
52+
}
53+
54+
static private boolean isCommandGroup(Class<? extends CommandRunnable> runnableClass) {
55+
return Enum.class.isAssignableFrom(runnableClass);
56+
}
57+
58+
static private <T extends CommandRunnable> Command<T> fromEnumClass(Class<T> runnableClass, String name) {
59+
List<SubCommand<?, ?>> subcommands = Arrays.stream(runnableClass.getDeclaredFields())
60+
.filter(Field::isEnumConstant)
61+
.map(CommandGenerator::fromField)
62+
.collect(Collectors.toList());
63+
64+
return new Command(runnableClass, name, true, subcommands);
65+
}
66+
67+
static private <T extends CommandRunnable> Command<T> fromPlainClass(Class<T> runnableClass, String name) {
68+
return new Command(runnableClass, name, false, new ArrayList<>());
69+
}
70+
71+
static private <T extends CommandRunnable> SubCommand<?, ?> fromField(Field field) {
72+
Subcommand subcommand = field.getAnnotation(Subcommand.class);
73+
if(subcommand == null) throw new RuntimeException("No subcommand annotation"); //TODO: Better exception
74+
75+
String commandName = subcommand.name();
76+
if(commandName.isEmpty()) commandName = field.getName().toLowerCase();
77+
Command<?> innerCommand = fromClass(subcommand.value(), commandName);
78+
T parentValue;
79+
try {
80+
parentValue = (T) field.get(null);
81+
} catch (IllegalAccessException e) {
82+
throw new RuntimeException(e); //TODO: Better exception
83+
}
84+
85+
return new SubCommand(innerCommand, parentValue, field);
86+
}
87+
}

src/main/java/fr/zcraft/zlib/components/commands2/CommandGroup.java

-11
This file was deleted.

src/main/java/fr/zcraft/zlib/components/commands2/CommandNode.java

-26
This file was deleted.

0 commit comments

Comments
 (0)