How to build up a MineCraft Plugin

Preparing

1. Java

Set up the JDK environment:
JDK 1.8.0_161

2. Eclipse IDE

Download Eclipse IDE
Eclipse Oxygen.3 (4.7.3)

3. Spigot Server API

Spigot Server API Doc
Spigot-API_1.12.2

Founded in 2012, SpigotMC.org is home to the community behind the biggest Minecraft server software projects and provides a place for everyone involved with Minecraft servers to connect with each other whether they seeking help and support or sharing and showcasing their work. We provide a web forum, chat room and wiki for providing support as well as project hosting for content creators and hope that you too will become involved in this extensive and growing community of more than 300,000 members.

Create a Maven Project

1. From Eclipse to create the Maven project

Eclipse001
Eclipse002

2. Use a template to create a Maven Project

Eclipse003

3. Configure the Maven Project

Eclipse004

Group Id” is the name of a Java Package
“Artifact Id” is the name of the Plugin run in Minecraft
“Version” is the software version
“Packaging” is the output type

4. Java Configuration in Maven Project

Add the code to pom.xml

XML
<project>
......
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

1.8 is the JDK version used in this project.
And then update the Maven Project (Alt+F5)

Eclipse005

Notice: Please modified the JRE configuration to JDK

Eclipse006

Add the Plugin Code

1. Add Spigot API Dependency Library

  • Copy spigot-api-1.11.2-R0.1-SNAPSHOT.jar to src/lib/
  • Copy spigot-api-1.11.2-R0.1-SNAPSHOT-shaded.jar to src/lib/
  • Open the pom.xml
XML
<project>
...
    <dependencies>
        <dependency>
            <groupId>org.bukkit</groupId>
            <artifactId>bukkit</artifactId>
            <version>1.11.2-R0.1</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/lib/spigot-api-1.11.2-R0.1-SNAPSHOT.jar</systemPath>  
        </dependency>
        <dependency>
            <groupId>org.bukkit.shared</groupId>
            <artifactId>bukkit_shared</artifactId>
            <version>1.11.2-R0.1</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/lib/spigot-api-1.11.2-R0.1-SNAPSHOT-shaded.jar</systemPath>  
        </dependency>
    </dependencies>
</project>
TagDescription
groupIdThe domain of maven
artifactIdThe version of Maven
versionThe scope of using Maven
scopeThe path of the dependency library
systemPathThe path of dependency library

spigot-api-1.11.2-R0.1-SNAPSHOT.jar and spigot-api-1.11.2-R0.1-SNAPSHOT-shaded.jar can be got by compile the source code of Spigot

2. Create the Java files

Right Click src/main/java to create a package

Eclipse008

Then add your package name

Create the Java main class

Java
package com.github.yinquan.testplugin;

import org.bukkit.plugin.java.JavaPlugin;

public class PluginMain extends JavaPlugin {
}

Create the plugin.yml

This file is needed when loading the plugin in Bukkit. This file contains the basic information about the plugin.

  • Right-click in src/main/resources to choose NewFile
Eclipse009

Add plugin information:

JSON
name: {Plugin Name}
main: {Package Name}.{Main Class Name}
version: {Version Number}

Add API doc to the Eclipse IDE

It is convenient to view the API information when adding the API to the Eclipse IDE.

  • Choose the Dependency Library in Maven Dependency
Eclipse011
  • Right-click to choose Properties, and then choose Javadoc Location. Add https://jd.bukkit.org/
Eclipse010

Add onEnable() and onDisable() method

Java
package com.github.yinquan.testplugin;

import org.bukkit.plugin.java.JavaPlugin;
public class PluginMain extends JavaPlugin {
    @Override
    public void onEnable() {
        getLogger().info("onEnable has been invoked!");
    }

    @Override
    public void onDisable() {
        getLogger().info("onDisable has been invoked!");
    }
}
  • onEnable — Called when this plugin is enabled
  • onDisable — Called when this plugin is disabled
  • getLogger().info(String msg) — If the logger is currently enabled for the INFO message level the given message is forwarded to all the registered output Handler objects.

Build the Plugin Project

  • In Eclipse, click Run AsMaven install
Eclipse012
  • Finish building, and the Console will print the information
Eclipse013
  • The archive file ({ProjectName}.jar) will be obtained.
Eclipse014

Deploy the Plugin to Minecraft

  • Copy the archive file built by Eclipse to plugins folder in Spigot Server Folder
Eclipse015
  • Run the Spigot Server

When the server starts running, the text is printed in cthe onsole of the server:

Eclipse016

Check the loaded plugin:

Eclipse017

GitHub Link


https://github.com/yinquan529/mincraftplugin
0 forks.
0 stars.
0 open issues.

Recent commits: