I. How to build up a MineCraft Plugin

Preparing

1. Java

Setup 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 template to create Maven Project

Eclipse003

3. Config the Maven Project

Eclipse004

Group Id is the name of Java Package
Artifact Id is the name of 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

<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 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
    <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>
    
    Tag Description
    groupId The domain of maven
    artifactId The union id of maven
    version The version of maven
    scope The scope of using maven
    systemPath The 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
    Eclipse008Then add your package name
  • Create the Java main class
    package com.github.yinquan.testplugin;
    
    import org.bukkit.plugin.java.JavaPlugin;
    public class PluginMain extends JavaPlugin {
    }
    
  • Create the plugin.ymlThis file is needed when loading the plugin in Bukkit.
    This file contains the basic information of plugin
    • Right click in src/main/resources to choose New -> FileEclipse009
    • Add plugin information
      name: {Plugin Name}
      main: {Package Name}.{Main Class Name}
      version: {Version Number}
      
  • Add API doc to Eclipse ID
    It is convenient to view the information of API that adding API to Eclipse ID
    • 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
    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!"):
        }
    }
    
    • onEnableCalled when this plugin is enabled
    • onDisableCalled when this plugin in disabled
    • getLooger().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 As -> Maven install
    Eclipse012
  • Finish Build, the Console will print the information
    Eclipse013
  • The achieve file ({ProjectName}.jar) will be got.
    Eclipse014

Deploy the Plugin to MineCraft

  • Copy the achieve file built by Eclipse to plugins folder in Spigot Server Folder

    Eclipse015

  • Run the Spigot Sever
    When server start running, the text are printed in console of server
    Eclipse016

    Check the loaded plugin:

    Eclipse017

GitHub Link:


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

Recent commits: