From 104ea01d259d0f175fe2426d61fd131920a5f0c8 Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Fri, 10 Jun 2022 21:52:05 -0400 Subject: [PATCH] Improve documentation --- .../xyz/grasscutters/pltm/EventListeners.java | 16 ++++++++++--- .../xyz/grasscutters/pltm/PluginTemplate.java | 16 +++++++++++-- .../pltm/objects/PluginConfig.java | 23 +++++++++++++++++-- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/main/java/xyz/grasscutters/pltm/EventListeners.java b/src/main/java/xyz/grasscutters/pltm/EventListeners.java index 666d184..1f72a95 100644 --- a/src/main/java/xyz/grasscutters/pltm/EventListeners.java +++ b/src/main/java/xyz/grasscutters/pltm/EventListeners.java @@ -1,13 +1,23 @@ package xyz.grasscutters.pltm; import emu.grasscutter.game.player.Player; +import emu.grasscutter.server.event.EventHandler; +import emu.grasscutter.server.event.HandlerPriority; import emu.grasscutter.server.event.player.PlayerJoinEvent; import xyz.grasscutters.pltm.objects.PluginConfig; /** * A class containing all event handlers. + * + * Syntax in event handler methods are similar to CraftBukkit. + * To register an event handler, create a new instance of {@link EventHandler}. + * Pass through the event class you want to handle. (ex. `new EventHandler<>(PlayerJoinEvent.class);`) + * From this class, invoke {@link EventHandler#register()} to register the handler. + * You can change the point at which the handler method is invoked with {@link EventHandler#priority(HandlerPriority)}. + * You can set whether the handler method should be invoked when another plugin cancels the event with {@link EventHandler#ignore(boolean)}. */ public final class EventListeners { + /* Saves the configuration to a local variable, reducing calls made to the plugin instance. */ private static final PluginConfig config = PluginTemplate.getInstance().getConfiguration(); /** @@ -15,9 +25,9 @@ public final class EventListeners { * @param event PlayerJoinEvent. */ public static void onJoin(PlayerJoinEvent event) { - if(!config.sendJoinMessage) return; + if(!config.sendJoinMessage) return; // Check if the plugin is configured to send a message when a player joins. - Player player = event.getPlayer(); - player.dropMessage(config.joinMessage); + Player player = event.getPlayer(); // Get the player who joined from the event. + player.dropMessage(config.joinMessage); // "Drop" the player a message. } } diff --git a/src/main/java/xyz/grasscutters/pltm/PluginTemplate.java b/src/main/java/xyz/grasscutters/pltm/PluginTemplate.java index b2daed3..284c5d0 100644 --- a/src/main/java/xyz/grasscutters/pltm/PluginTemplate.java +++ b/src/main/java/xyz/grasscutters/pltm/PluginTemplate.java @@ -5,7 +5,9 @@ import emu.grasscutter.plugin.Plugin; import emu.grasscutter.server.event.EventHandler; import emu.grasscutter.server.event.HandlerPriority; import emu.grasscutter.server.event.player.PlayerJoinEvent; -import xyz.grasscutters.pltm.objects.PluginConfig; + +import xyz.grasscutters.pltm.commands.*; +import xyz.grasscutters.pltm.objects.*; import java.io.*; import java.util.stream.Collectors; @@ -63,6 +65,9 @@ public final class PluginTemplate extends Plugin { } catch (IOException exception) { this.getLogger().error("Failed to create config file.", exception); } + + // Log a plugin status message. + this.getLogger().info("The example plugin has been loaded."); } /** @@ -74,13 +79,20 @@ public final class PluginTemplate extends Plugin { .priority(HandlerPriority.LOW) .listener(EventListeners::onJoin) .register(); + + // Register commands. + this.getHandle().registerCommand(new ExampleCommand()); + + // Log a plugin status message. + this.getLogger().info("The example plugin has been enabled."); } /** * This method is called when the plugin is disabled. */ @Override public void onDisable() { - + // Log a plugin status message. + this.getLogger().info("The example plugin has been disabled."); } /** diff --git a/src/main/java/xyz/grasscutters/pltm/objects/PluginConfig.java b/src/main/java/xyz/grasscutters/pltm/objects/PluginConfig.java index 13f2862..e06b3c4 100644 --- a/src/main/java/xyz/grasscutters/pltm/objects/PluginConfig.java +++ b/src/main/java/xyz/grasscutters/pltm/objects/PluginConfig.java @@ -1,9 +1,28 @@ package xyz.grasscutters.pltm.objects; +import com.google.gson.Gson; + +import java.io.Reader; +import java.lang.reflect.Type; + /** * A data container for the plugin configuration. + * + * This class is used in conjunction with {@link Gson#toJson(Object)} and {@link Gson#fromJson(Reader, Type)}. + * With {@link Gson}, it is possible to save and load configuration values from a JSON file. + * + * You can set property defaults using `public Object property = (default value);`. + * Use {@link Gson#fromJson(Reader, Type)} to load the values set from a reader/string into a new instance of this class. */ public final class PluginConfig { - public boolean sendJoinMessage; - public String joinMessage; + public boolean sendJoinMessage = true; + public String joinMessage = "Welcome to the server!"; + + /** + * When saved with {@link Gson#toJson(Object)}, it produces: + * { + * "sendJoinMessage": true, + * "joinMessage": "Welcome to the server!" + * } + */ }