Started #29, tried compile-time check, finished #29

Damn, annotation processors are complicated
This commit is contained in:
Norbi Peti 2017-04-22 02:17:52 +02:00
parent 3bab090fc7
commit 3af6063c99
15 changed files with 128 additions and 81 deletions

2
.factorypath Normal file
View file

@ -0,0 +1,2 @@
<factorypath>
</factorypath>

1
.gitignore vendored
View file

@ -224,3 +224,4 @@ TheButtonAutoFlair/out/artifacts/Autoflair/Autoflair.jar
*.xml *.xml
TBMC/ TBMC/
/.apt_generated/

View file

@ -1,12 +1,14 @@
eclipse.preferences.version=1 eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8 org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.processAnnotations=enabled
org.eclipse.jdt.core.compiler.source=1.8

View file

@ -0,0 +1 @@
buttondevteam.lib.AnnotationProcessor

View file

@ -8,6 +8,8 @@ import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.PlayerCommandBase;
import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.chat.TBMCCommandBase; import buttondevteam.lib.chat.TBMCCommandBase;
@ -69,11 +71,12 @@ public class CommandCaller implements CommandExecutor {
} }
return true; return true;
} }
if (cmd.GetModOnly() && !MainPlugin.permission.has(sender, "tbmc.admin")) { if (cmd.getClass().getAnnotation(CommandClass.class).modOnly()
&& !MainPlugin.permission.has(sender, "tbmc.admin")) {
sender.sendMessage("§cYou need to be a mod to use this command."); sender.sendMessage("§cYou need to be a mod to use this command.");
return true; return true;
} }
if (cmd.GetPlayerOnly() && !(sender instanceof Player)) { if (cmd instanceof PlayerCommandBase && !(sender instanceof Player)) {
sender.sendMessage("§cOnly ingame players can use this command."); sender.sendMessage("§cOnly ingame players can use this command.");
return true; return true;
} }

View file

@ -9,8 +9,10 @@ import org.bukkit.command.CommandSender;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import buttondevteam.lib.ScheduledServerRestartEvent; import buttondevteam.lib.ScheduledServerRestartEvent;
import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.TBMCCommandBase; import buttondevteam.lib.chat.TBMCCommandBase;
@CommandClass(modOnly = true, path = "schrestart")
public class ScheduledRestartCommand extends TBMCCommandBase { public class ScheduledRestartCommand extends TBMCCommandBase {
private static volatile int restartcounter; private static volatile int restartcounter;
private static volatile BukkitTask restarttask; private static volatile BukkitTask restarttask;
@ -66,19 +68,4 @@ public class ScheduledRestartCommand extends TBMCCommandBase {
"You can optionally set the amount of ticks to wait before the restart." // "You can optionally set the amount of ticks to wait before the restart." //
}; };
} }
@Override
public boolean GetPlayerOnly() {
return false;
}
@Override
public boolean GetModOnly() {
return true;
}
@Override
public String GetCommandPath() {
return "schrestart";
}
} }

View file

@ -4,8 +4,10 @@ import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.TBMCCommandBase; import buttondevteam.lib.chat.TBMCCommandBase;
@CommandClass(modOnly = true)
public class UpdatePluginCommand extends TBMCCommandBase { public class UpdatePluginCommand extends TBMCCommandBase {
@Override @Override
public boolean OnCommand(CommandSender sender, String alias, String[] args) { public boolean OnCommand(CommandSender sender, String alias, String[] args) {
@ -37,14 +39,4 @@ public class UpdatePluginCommand extends TBMCCommandBase {
"To list the plugin names: /" + alias // "To list the plugin names: /" + alias //
}; };
} }
@Override
public boolean GetPlayerOnly() {
return false;
}
@Override
public boolean GetModOnly() {
return true;
}
} }

View file

@ -0,0 +1,33 @@
package buttondevteam.lib;
import java.util.List;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import buttondevteam.lib.player.ChromaGamerEnforcer;
/** * A simple session bean type annotation processor. The implementation * is based on the standard annotation processing API in Java 6. */
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class AnnotationProcessor extends AbstractProcessor {
/** * Check if both @Stateful and @Stateless are present in an * session bean. If so, emits a warning message. */
@Override
public boolean process(Set<? extends TypeElement> typeElements, RoundEnvironment roundEnv) { // TODO: SEparate JAR
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(ChromaGamerEnforcer.class);
for (Element element : elements) {
System.out.println("Processing " + element);
List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors();
System.out.println("Annotations: " + annotationMirrors);
for (AnnotationMirror annotation : annotationMirrors) {
String type = annotation.getAnnotationType().toString();
System.out.println("Type: " + type);
}
}
return true; // claim the annotations
}
}

View file

@ -0,0 +1,29 @@
package buttondevteam.lib.chat;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface CommandClass {
/**
* Determines whether the command can only be used by mods and above or regular players can use it as well.
*
* @return If the command is mod only
*/
public boolean modOnly();
/**
* The command's path, or name if top-level command.<br>
* For example:<br>
* "u admin updateplugin" or "u" for the top level one<br>
* <u>The path must be lowercase!</u><br>
*
* @return The command path, <i>which is the command class name by default</i> (removing any "command" from it)
*/
public String path() default "";
}

View file

@ -1,24 +0,0 @@
package buttondevteam.lib.chat;
public enum Format implements TellrawSerializableEnum {
Bold("bold"), Underlined("underlined"), Italic("italic"), Strikethrough("strikethrough"), Obfuscated(
"obfuscated");
// TODO: Add format codes to /u c <mode>
private String name;
Format(String name) {
this.name = name;
this.flag = 1 << this.ordinal();
}
@Override
public String getName() {
return name;
}
private final int flag;
public int getFlag() {
return flag;
}
}

View file

@ -0,0 +1,16 @@
package buttondevteam.lib.chat;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public abstract class PlayerCommandBase extends TBMCCommandBase {
public abstract boolean OnCommand(Player player, String alias, String[] args);
@Override
public boolean OnCommand(CommandSender sender, String alias, String[] args) {
if (sender instanceof Player)
return OnCommand((Player) sender, alias, args);
sender.sendMessage("§cYou need to be a player to use this command.");
return true;
}
}

View file

@ -61,9 +61,10 @@ public class TBMCChatAPI {
}; };
for (TBMCCommandBase cmd : TBMCChatAPI.GetCommands().values()) { for (TBMCCommandBase cmd : TBMCChatAPI.GetCommands().values()) {
if (cmd.GetCommandPath().startsWith(command + " ")) { if (cmd.GetCommandPath().startsWith(command + " ")) {
if (cmd.GetPlayerOnly() && !(sender instanceof Player)) if (cmd instanceof PlayerCommandBase && !(sender instanceof Player))
continue; continue;
if (cmd.GetModOnly() && !MainPlugin.permission.has(sender, "tbmc.admin")) if (cmd.getClass().getAnnotation(CommandClass.class).modOnly()
&& !MainPlugin.permission.has(sender, "tbmc.admin"))
continue; continue;
int ind = cmd.GetCommandPath().indexOf(' ', command.length() + 2); int ind = cmd.GetCommandPath().indexOf(' ', command.length() + 2);
if (ind >= 0) { if (ind >= 0) {

View file

@ -27,24 +27,13 @@ public abstract class TBMCCommandBase {
* *
* @return The command path, <i>which is the command class name by default</i> (removing any "command" from it) * @return The command path, <i>which is the command class name by default</i> (removing any "command" from it)
*/ */
public String GetCommandPath() { public final String GetCommandPath() {
return getClass().getSimpleName().toLowerCase().replace("command", ""); if (!getClass().isAnnotationPresent(CommandClass.class))
throw new RuntimeException("No @Command annotation on command class " + getClass().getSimpleName() + "!");
String path = getClass().getAnnotation(CommandClass.class).path();
return path.length() == 0 ? getClass().getSimpleName().toLowerCase().replace("command", "") : path;
} }
/**
* Determines whether the command can only be used as a player, or command blocks or the console can use it as well.
*
* @return If the command is player only
*/
public abstract boolean GetPlayerOnly();
/**
* Determines whether the command can only be used by mods or regular players can use it as well.
*
* @return If the command is mod only
*/
public abstract boolean GetModOnly();
Plugin plugin; // Used By TBMCChatAPI Plugin plugin; // Used By TBMCChatAPI
public final Plugin getPlugin() { // Used by CommandCaller (ButtonChat) public final Plugin getPlugin() { // Used by CommandCaller (ButtonChat)

View file

@ -9,6 +9,7 @@ import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
@ChromaGamerEnforcer
public abstract class ChromaGamerBase implements AutoCloseable { public abstract class ChromaGamerBase implements AutoCloseable {
public static final String TBMC_PLAYERS_DIR = "TBMC/players/"; public static final String TBMC_PLAYERS_DIR = "TBMC/players/";

View file

@ -0,0 +1,14 @@
package buttondevteam.lib.player;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
@Inherited
public @interface ChromaGamerEnforcer {
}