mirror of
https://github.com/Grasscutters/PluginTemplate.git
synced 2024-11-23 03:59:44 +00:00
Initial commit
This commit is contained in:
commit
c733f2d1bc
38
.gitignore
vendored
Normal file
38
.gitignore
vendored
Normal file
@ -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
|
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
@ -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
|
7
.idea/discord.xml
Normal file
7
.idea/discord.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DiscordProjectSettings">
|
||||
<option name="show" value="PROJECT_FILES" />
|
||||
<option name="description" value="" />
|
||||
</component>
|
||||
</project>
|
19
.idea/misc.xml
Normal file
19
.idea/misc.xml
Normal file
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
<component name="SwUserDefinedSpecifications">
|
||||
<option name="specTypeByUrl">
|
||||
<map />
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
34
pom.xml
Normal file
34
pom.xml
Normal file
@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>xyz.grasscutters</groupId> <!-- Replace with your groupId -->
|
||||
<artifactId>PluginTemplate</artifactId> <!-- Replace with your plugin name. -->
|
||||
<version>1.0.3-dev</version> <!-- The template's version corresponds to the latest API version. -->
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
<!--
|
||||
While Grasscutter is on Maven Central, we include Sonatype to update quickly.
|
||||
Usually, builds take an hour to go from Sonatype to Maven Central.
|
||||
-->
|
||||
<repository>
|
||||
<id>sonatype</id>
|
||||
<url>https://s01.oss.sonatype.org/content/groups/public/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>xyz.grasscutters</groupId> <!-- Versions below 1.0.3-dev are on 'tech.xigam' -->
|
||||
<artifactId>grasscutter</artifactId>
|
||||
<version>1.0.3-dev</version> <!-- Replace with the latest version of the API. -->
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
23
src/main/java/xyz/grasscutters/pltm/EventListeners.java
Normal file
23
src/main/java/xyz/grasscutters/pltm/EventListeners.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
96
src/main/java/xyz/grasscutters/pltm/PluginTemplate.java
Normal file
96
src/main/java/xyz/grasscutters/pltm/PluginTemplate.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
4
src/main/resources/config.json
Normal file
4
src/main/resources/config.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"sendJoinMessage": true,
|
||||
"joinMessage": "This is the example Grasscutter plugin!"
|
||||
}
|
14
src/main/resources/plugin.json
Normal file
14
src/main/resources/plugin.json
Normal file
@ -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'."
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue
Block a user