From 9c15d0ffff3e9804e57d768de09fe57e41af77b9 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 9 Mar 2019 22:57:38 +0100 Subject: [PATCH] Chat handler improvements and config doc gen Configuration documentation generation --- .../java/buttondevteam/core/MainPlugin.java | 14 ++++++ .../buttondevteam/core/PlayerListener.java | 22 ++++++++-- .../component/channel/ChannelComponent.java | 4 ++ .../component/members/MemberComponent.java | 24 ++++++++++- .../component/randomtp/RandomTPComponent.java | 4 ++ .../component/restart/RestartComponent.java | 3 ++ .../core/component/towny/TownyComponent.java | 3 ++ .../updater/PluginUpdaterComponent.java | 5 ++- .../component/votifier/VotifierComponent.java | 5 ++- .../lib/architecture/ButtonPlugin.java | 1 + .../lib/architecture/Component.java | 1 + .../lib/architecture/ComponentMetadata.java | 2 +- .../lib/architecture/HasConfig.java | 13 ++++++ .../buttonproc/ButtonProcessor.java | 24 +++-------- .../buttonproc/ConfigProcessor.java | 43 +++++++++++++++++++ 15 files changed, 141 insertions(+), 27 deletions(-) create mode 100644 ButtonCore/src/main/java/buttondevteam/lib/architecture/HasConfig.java create mode 100644 ButtonProcessor/src/main/java/buttondevteam/buttonproc/ConfigProcessor.java diff --git a/ButtonCore/src/main/java/buttondevteam/core/MainPlugin.java b/ButtonCore/src/main/java/buttondevteam/core/MainPlugin.java index b6c78f4..f46a85c 100755 --- a/ButtonCore/src/main/java/buttondevteam/core/MainPlugin.java +++ b/ButtonCore/src/main/java/buttondevteam/core/MainPlugin.java @@ -20,6 +20,8 @@ import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.TBMCPlayerBase; import com.earth2me.essentials.Essentials; +import lombok.Getter; +import lombok.Setter; import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.permission.Permission; import org.bukkit.Bukkit; @@ -50,11 +52,23 @@ public class MainPlugin extends ButtonPlugin { private Logger logger; @Nullable private Economy economy; + /** + * Whether the Core's chat handler should be enabled. + * Other chat plugins handling messages from other platforms should set this to false. + */ + @Getter + @Setter + private boolean chatHandlerEnabled = true; private ConfigData writePluginList() { return getIConfig().getData("writePluginList", false); } + ConfigData chatFormat() { + return getIConfig().getData("chatFormat", "[{origin}|" + + "{channel}] <{name}> {message}"); + } + @Override public void pluginEnable() { // Logs "Plugin Enabled", registers commands diff --git a/ButtonCore/src/main/java/buttondevteam/core/PlayerListener.java b/ButtonCore/src/main/java/buttondevteam/core/PlayerListener.java index 0ea7ac4..097b90a 100755 --- a/ButtonCore/src/main/java/buttondevteam/core/PlayerListener.java +++ b/ButtonCore/src/main/java/buttondevteam/core/PlayerListener.java @@ -1,8 +1,6 @@ package buttondevteam.core; -import buttondevteam.lib.TBMCCommandPreprocessEvent; -import buttondevteam.lib.TBMCCoreAPI; -import buttondevteam.lib.TBMCSystemChatEvent; +import buttondevteam.lib.*; import buttondevteam.lib.architecture.ButtonPlugin; import buttondevteam.lib.chat.ChatMessage; import buttondevteam.lib.chat.Command2MCSender; @@ -12,6 +10,7 @@ import buttondevteam.lib.player.TBMCPlayerBase; import lombok.val; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -84,4 +83,21 @@ public class PlayerListener implements Listener { //Not cancelling the original event here, it's cancelled in the chat plugin //This way other plugins can deal with the MC formatting if the chat plugin isn't present, but other platforms still get the message } + + @EventHandler(priority = EventPriority.HIGH) //The one in the chat plugin is set to highest + public void onPlayerChat(TBMCChatEvent event) { + if (event.isCancelled()) + return; + if (!MainPlugin.Instance.isChatHandlerEnabled()) return; + if (event.getOrigin().equals("Minecraft")) return; //Let other plugins handle MC messages + String msg = MainPlugin.Instance.chatFormat().get() + .replace("{channel}", event.getChannel().DisplayName().get()) + .replace("{origin}", event.getOrigin().substring(0, 1)) + .replace("{name}", ThorpeUtils.getDisplayName(event.getSender())) + .replace("{message}", event.getMessage()); + for (Player player : Bukkit.getOnlinePlayers()) + if (event.shouldSendTo(player)) + player.sendMessage(msg); + Bukkit.getConsoleSender().sendMessage(msg); + } } \ No newline at end of file diff --git a/ButtonCore/src/main/java/buttondevteam/core/component/channel/ChannelComponent.java b/ButtonCore/src/main/java/buttondevteam/core/component/channel/ChannelComponent.java index 14aff26..5e19ea0 100644 --- a/ButtonCore/src/main/java/buttondevteam/core/component/channel/ChannelComponent.java +++ b/ButtonCore/src/main/java/buttondevteam/core/component/channel/ChannelComponent.java @@ -4,8 +4,12 @@ import buttondevteam.lib.TBMCSystemChatEvent; import buttondevteam.lib.architecture.Component; import org.bukkit.plugin.java.JavaPlugin; +/** + * Manages chat channels. If disabled, only global channels will be registered. + */ public class ChannelComponent extends Component { static TBMCSystemChatEvent.BroadcastTarget roomJoinLeave; + @Override protected void register(JavaPlugin plugin) { super.register(plugin); diff --git a/ButtonCore/src/main/java/buttondevteam/core/component/members/MemberComponent.java b/ButtonCore/src/main/java/buttondevteam/core/component/members/MemberComponent.java index 6cc9536..b5bab6e 100644 --- a/ButtonCore/src/main/java/buttondevteam/core/component/members/MemberComponent.java +++ b/ButtonCore/src/main/java/buttondevteam/core/component/members/MemberComponent.java @@ -14,11 +14,31 @@ import java.util.Date; import static buttondevteam.core.MainPlugin.permission; +/** + * Allows giving a 'member' group over some time elapsed OR played. + */ public class MemberComponent extends Component implements Listener { + /** + * The permission group to give to the player + */ ConfigData memberGroup() { return getConfig().getData("memberGroup", "member"); } + /** + * The amount of hours needed to play before promotion + */ + private ConfigData playedHours() { + return getConfig().getData("playedHours", 12); + } + + /** + * The amount of days passed since first login + */ + private ConfigData registeredForDays() { + return getConfig().getData("registeredForDays", 7); + } + @Override protected void enable() { registerListener(this); @@ -32,8 +52,8 @@ public class MemberComponent extends Component implements Listener { @EventHandler public void onPlayerJoin(PlayerJoinEvent event) { if (permission != null && !permission.playerInGroup(event.getPlayer(), memberGroup().get()) - && (new Date(event.getPlayer().getFirstPlayed()).toInstant().plus(7, ChronoUnit.DAYS).isBefore(Instant.now()) - || event.getPlayer().getStatistic(Statistic.PLAY_ONE_TICK) > 20 * 3600 * 12)) { + && (new Date(event.getPlayer().getFirstPlayed()).toInstant().plus(registeredForDays().get(), ChronoUnit.DAYS).isBefore(Instant.now()) + || event.getPlayer().getStatistic(Statistic.PLAY_ONE_TICK) > 20 * 3600 * playedHours().get())) { permission.playerAddGroup(null, event.getPlayer(), memberGroup().get()); event.getPlayer().sendMessage("§bYou are a member now. YEEHAW"); MainPlugin.Instance.getLogger().info("Added " + event.getPlayer().getName() + " as a member."); diff --git a/ButtonCore/src/main/java/buttondevteam/core/component/randomtp/RandomTPComponent.java b/ButtonCore/src/main/java/buttondevteam/core/component/randomtp/RandomTPComponent.java index e7df49d..0f1fb5e 100644 --- a/ButtonCore/src/main/java/buttondevteam/core/component/randomtp/RandomTPComponent.java +++ b/ButtonCore/src/main/java/buttondevteam/core/component/randomtp/RandomTPComponent.java @@ -3,6 +3,10 @@ package buttondevteam.core.component.randomtp; import buttondevteam.core.MainPlugin; import buttondevteam.lib.architecture.Component; +/** + * Teleport player to random location within world border. + * Every five players teleport to the same general area, and then a new general area is randomly selected for the next five players. + */ public class RandomTPComponent extends Component { @Override protected void enable() { diff --git a/ButtonCore/src/main/java/buttondevteam/core/component/restart/RestartComponent.java b/ButtonCore/src/main/java/buttondevteam/core/component/restart/RestartComponent.java index dd58a0c..6b431bb 100644 --- a/ButtonCore/src/main/java/buttondevteam/core/component/restart/RestartComponent.java +++ b/ButtonCore/src/main/java/buttondevteam/core/component/restart/RestartComponent.java @@ -13,6 +13,9 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerQuitEvent; +/** + * Provides commands such as /schrestart (restart after a countdown) and /primerestart (restart when nobody is online) + */ public class RestartComponent extends Component implements Listener { @Override public void enable() { diff --git a/ButtonCore/src/main/java/buttondevteam/core/component/towny/TownyComponent.java b/ButtonCore/src/main/java/buttondevteam/core/component/towny/TownyComponent.java index 0397c8a..42c857d 100644 --- a/ButtonCore/src/main/java/buttondevteam/core/component/towny/TownyComponent.java +++ b/ButtonCore/src/main/java/buttondevteam/core/component/towny/TownyComponent.java @@ -11,6 +11,9 @@ import com.palmergames.bukkit.towny.object.Resident; import com.palmergames.bukkit.towny.object.TownyUniverse; import org.bukkit.Bukkit; +/** + * Automatically renames Towny players if they changed their Minecraft name + */ public class TownyComponent extends Component { @Override protected void enable() { diff --git a/ButtonCore/src/main/java/buttondevteam/core/component/updater/PluginUpdaterComponent.java b/ButtonCore/src/main/java/buttondevteam/core/component/updater/PluginUpdaterComponent.java index 0adef98..3ea44c8 100644 --- a/ButtonCore/src/main/java/buttondevteam/core/component/updater/PluginUpdaterComponent.java +++ b/ButtonCore/src/main/java/buttondevteam/core/component/updater/PluginUpdaterComponent.java @@ -4,7 +4,10 @@ import buttondevteam.core.MainPlugin; import buttondevteam.lib.architecture.Component; import buttondevteam.lib.chat.TBMCChatAPI; -public class PluginUpdaterComponent extends Component { +/** + * Downloads plugin updates built from their source using JitPack - older code + */ +public class PluginUpdaterComponent extends Component { //TODO: Config @Override public void enable() { TBMCChatAPI.AddCommand(this, new UpdatePluginCommand()); diff --git a/ButtonCore/src/main/java/buttondevteam/core/component/votifier/VotifierComponent.java b/ButtonCore/src/main/java/buttondevteam/core/component/votifier/VotifierComponent.java index 839ea17..94ffe55 100644 --- a/ButtonCore/src/main/java/buttondevteam/core/component/votifier/VotifierComponent.java +++ b/ButtonCore/src/main/java/buttondevteam/core/component/votifier/VotifierComponent.java @@ -11,12 +11,15 @@ import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +/** + * Do not use (EULA) + */ @RequiredArgsConstructor public class VotifierComponent extends Component { private final Economy economy; private ConfigData rewardAmount() { - return getConfig().getData("rewardAmount", 50.0); + return getConfig().getData("rewardAmount", 0.0); } @Override diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java index a4b5bdf..602aefa 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java @@ -11,6 +11,7 @@ import org.bukkit.plugin.java.JavaPlugin; import java.util.Stack; +@HasConfig public abstract class ButtonPlugin extends JavaPlugin { @Getter private static Command2MC command2MC = new Command2MC(); diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java index 3090a07..d9a15c3 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java @@ -23,6 +23,7 @@ import java.util.stream.Collectors; /** * Configuration is based on class name */ +@HasConfig //Used for obtaining javadoc public abstract class Component { private static HashMap, Component> components = new HashMap<>(); diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ComponentMetadata.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ComponentMetadata.java index 6519fca..a182df8 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ComponentMetadata.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ComponentMetadata.java @@ -8,5 +8,5 @@ import java.lang.annotation.Target; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface ComponentMetadata { - Class[] depends(); + Class[] depends() default {}; } diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/HasConfig.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/HasConfig.java new file mode 100644 index 0000000..8e1e63a --- /dev/null +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/HasConfig.java @@ -0,0 +1,13 @@ +package buttondevteam.lib.architecture; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Target; + +/** + * Used to generate documentation for the config + */ +@Target(ElementType.TYPE) +@Inherited +public @interface HasConfig { +} diff --git a/ButtonProcessor/src/main/java/buttondevteam/buttonproc/ButtonProcessor.java b/ButtonProcessor/src/main/java/buttondevteam/buttonproc/ButtonProcessor.java index 1fda004..50e1939 100755 --- a/ButtonProcessor/src/main/java/buttondevteam/buttonproc/ButtonProcessor.java +++ b/ButtonProcessor/src/main/java/buttondevteam/buttonproc/ButtonProcessor.java @@ -8,14 +8,11 @@ import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.SupportedAnnotationTypes; import javax.lang.model.SourceVersion; import javax.lang.model.element.*; -import javax.tools.Diagnostic; import javax.tools.Diagnostic.Kind; import javax.tools.FileObject; -import javax.tools.JavaFileObject; import javax.tools.StandardLocation; import java.io.File; import java.io.IOException; -import java.io.Writer; import java.util.List; import java.util.Set; import java.util.function.Function; @@ -25,6 +22,8 @@ import java.util.stream.Collectors; public class ButtonProcessor extends AbstractProcessor { @Override public boolean process(Set annotations, RoundEnvironment roundEnv) { + if (configProcessor == null) + configProcessor = new ConfigProcessor(processingEnv); for (TypeElement te : annotations) { Set classes = roundEnv.getElementsAnnotatedWith(te); for (Element targetcl : classes) { @@ -47,6 +46,8 @@ public class ButtonProcessor extends AbstractProcessor { //System.out.println("Type: " + type); } processSubcommands(targetcl, annotationMirrors); + if (hasAnnotation.apply("HasConfig")) + configProcessor.process(targetcl); } } try { @@ -63,6 +64,7 @@ public class ButtonProcessor extends AbstractProcessor { private YamlConfiguration yc = new YamlConfiguration(); private boolean found = false; + private ConfigProcessor configProcessor; private void processSubcommands(Element targetcl, List annotationMirrors) { if (!(targetcl instanceof ExecutableElement)) @@ -91,20 +93,4 @@ public class ButtonProcessor extends AbstractProcessor { public SourceVersion getSupportedSourceVersion() { return SourceVersion.latestSupported(); } - - private String fetchSourcePath() { - try { - JavaFileObject generationForPath = processingEnv.getFiler().createSourceFile("PathFor" + getClass().getSimpleName()); - Writer writer = generationForPath.openWriter(); - String sourcePath = generationForPath.toUri().getPath(); - writer.close(); - generationForPath.delete(); - - return sourcePath; - } catch (IOException e) { - processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Unable to determine source file path!"); - } - - return ""; - } } diff --git a/ButtonProcessor/src/main/java/buttondevteam/buttonproc/ConfigProcessor.java b/ButtonProcessor/src/main/java/buttondevteam/buttonproc/ConfigProcessor.java new file mode 100644 index 0000000..f244553 --- /dev/null +++ b/ButtonProcessor/src/main/java/buttondevteam/buttonproc/ConfigProcessor.java @@ -0,0 +1,43 @@ +package buttondevteam.buttonproc; + +import org.bukkit.configuration.file.YamlConfiguration; + +import javax.annotation.processing.ProcessingEnvironment; +import javax.lang.model.element.Element; +import javax.lang.model.element.Modifier; +import javax.tools.FileObject; +import javax.tools.StandardLocation; +import java.io.File; +import java.io.IOException; + +public class ConfigProcessor { + private final ProcessingEnvironment procEnv; + private final YamlConfiguration yaml; + private final File file; + + public ConfigProcessor(ProcessingEnvironment procEnv) { + this.procEnv = procEnv; + FileObject file = null; + try { + file = procEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", "config.yml"); + } catch (IOException e) { + e.printStackTrace(); + } + yaml = new YamlConfiguration(); + this.file = new File(file.toUri()); + } + + public void process(Element targetcl) { + if (targetcl.getModifiers().contains(Modifier.ABSTRACT)) return; + String javadoc = procEnv.getElementUtils().getDocComment(targetcl); + if (javadoc == null) return; + System.out.println("JAVADOC"); //TODO: Config methods + System.out.println(javadoc); + yaml.set("components." + targetcl.getSimpleName() + "._doc", javadoc); + try { + yaml.save(file); + } catch (IOException e) { + e.printStackTrace(); + } + } +}