commit c733f2d1bcc25cf241623b70809836e30765d1a5 Author: KingRainbow44 Date: Tue May 3 18:58:04 2022 -0400 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..d8e9561 --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..14a17be --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..6c558ee --- /dev/null +++ b/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + + xyz.grasscutters + PluginTemplate + 1.0.3-dev + + + 17 + 17 + + + + + + sonatype + https://s01.oss.sonatype.org/content/groups/public/ + + + + + + xyz.grasscutters + grasscutter + 1.0.3-dev + + + \ No newline at end of file diff --git a/src/main/java/xyz/grasscutters/pltm/EventListeners.java b/src/main/java/xyz/grasscutters/pltm/EventListeners.java new file mode 100644 index 0000000..666d184 --- /dev/null +++ b/src/main/java/xyz/grasscutters/pltm/EventListeners.java @@ -0,0 +1,23 @@ +package xyz.grasscutters.pltm; + +import emu.grasscutter.game.player.Player; +import emu.grasscutter.server.event.player.PlayerJoinEvent; +import xyz.grasscutters.pltm.objects.PluginConfig; + +/** + * A class containing all event handlers. + */ +public final class EventListeners { + private static final PluginConfig config = PluginTemplate.getInstance().getConfiguration(); + + /** + * Called when the player joins the server. + * @param event PlayerJoinEvent. + */ + public static void onJoin(PlayerJoinEvent event) { + if(!config.sendJoinMessage) return; + + Player player = event.getPlayer(); + player.dropMessage(config.joinMessage); + } +} diff --git a/src/main/java/xyz/grasscutters/pltm/PluginTemplate.java b/src/main/java/xyz/grasscutters/pltm/PluginTemplate.java new file mode 100644 index 0000000..9bd4908 --- /dev/null +++ b/src/main/java/xyz/grasscutters/pltm/PluginTemplate.java @@ -0,0 +1,96 @@ +package xyz.grasscutters.pltm; + +import emu.grasscutter.Grasscutter; +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 org.slf4j.Logger; +import xyz.grasscutters.pltm.objects.PluginConfig; + +import java.io.*; +import java.util.stream.Collectors; + +/** + * The Grasscutter plugin template. + * This is the main class for the plugin. + */ +public final class PluginTemplate extends Plugin { + /* Turn the plugin into a singleton. */ + private static PluginTemplate instance; + + /** + * Gets the plugin instance. + * @return A plugin singleton. + */ + public static PluginTemplate getInstance() { + return instance; + } + + /* A proper plugin logger will be introduced in v1.0.4-dev. */ + private final Logger logger = Grasscutter.getLogger(); + /* The plugin's configuration instance. */ + private PluginConfig configuration; + + /** + * This method is called immediately after the plugin is first loaded into system memory. + */ + @Override public void onLoad() { + // Set the plugin instance. + instance = this; + + // Get the configuration file. + File config = new File(this.getDataFolder(), "config.json"); + + // Load the configuration. + try { + if(!config.exists() && !config.createNewFile()) { + this.logger.error("Failed to create config file."); + } else { + try (FileWriter writer = new FileWriter(config)) { + InputStream configStream = this.getResource("config.json"); + if(configStream == null) { + this.logger.error("Failed to save default config file."); + } else { + writer.write(new BufferedReader( + new InputStreamReader(configStream)).lines().collect(Collectors.joining("\n")) + ); writer.close(); + + this.logger.info("Saved default config file."); + } + } + } + + // Put the configuration into an instance of the config class. + this.configuration = Grasscutter.getGsonFactory().fromJson(new FileReader(config), PluginConfig.class); + } catch (IOException exception) { + this.logger.error("Failed to create config file.", exception); + } + } + + /** + * This method is called before the servers are started, or when the plugin enables. + */ + @Override public void onEnable() { + // Register event listeners. + new EventHandler<>(PlayerJoinEvent.class) + .priority(HandlerPriority.LOW) + .listener(EventListeners::onJoin) + .register(); + } + + /** + * This method is called when the plugin is disabled. + */ + @Override public void onDisable() { + + } + + /** + * Gets the plugin's configuration. + * @return A plugin config instance. + */ + public PluginConfig getConfiguration() { + return this.configuration; + } +} diff --git a/src/main/java/xyz/grasscutters/pltm/objects/PluginConfig.java b/src/main/java/xyz/grasscutters/pltm/objects/PluginConfig.java new file mode 100644 index 0000000..13f2862 --- /dev/null +++ b/src/main/java/xyz/grasscutters/pltm/objects/PluginConfig.java @@ -0,0 +1,9 @@ +package xyz.grasscutters.pltm.objects; + +/** + * A data container for the plugin configuration. + */ +public final class PluginConfig { + public boolean sendJoinMessage; + public String joinMessage; +} diff --git a/src/main/resources/config.json b/src/main/resources/config.json new file mode 100644 index 0000000..a3a46d0 --- /dev/null +++ b/src/main/resources/config.json @@ -0,0 +1,4 @@ +{ + "sendJoinMessage": true, + "joinMessage": "This is the example Grasscutter plugin!" +} \ No newline at end of file diff --git a/src/main/resources/plugin.json b/src/main/resources/plugin.json new file mode 100644 index 0000000..909bc84 --- /dev/null +++ b/src/main/resources/plugin.json @@ -0,0 +1,14 @@ +{ + "name": "PluginTemplate", + "description": "The Grasscutter example plugin, which uses Maven.", + "version": "1.0.2-dev", + "authors": [ "KingRainbow44" ], + + "mainClass": "xyz.grasscutters.pltm.PluginTemplate", + + "comments": [ + "This comments block doesn't need to be in your plugin.json.", + "The mainClass should include the FQDN of the main plugin class.", + "Required fields are: 'name', 'description', and 'mainClass'." + ] +} \ No newline at end of file