[JAVA] Creating Spigot plugins using Eclipse

things to do

スクリーンショット 2019-08-12 23.00.57.png Like this, we will implement a super-simple plugin that displays the log when the plugin is enabled. If you do it properly, it will take less than 15 minutes.

Target audience

  1. You have already built a minecraft spigot server.

Writer's environment

macOS Mojave Version: 10.14.6

Macbook Pro (15-inch, 2018) Processor: 2.2GHz Intel Core i7 Memory: 16GB 2400MHz DDR4 Boot disk: Macintosh HD Graphics: Radeon Pro 555X 4GB Intel UHD Graphics 630 1536MB

Introduction of comprehensive development environment (eclipse)

Access the Download Site. Then you should be able to access the following sites. For the time being, I chose the latest version. スクリーンショット 2019-08-12 18.15.35.png

Next, install the full Edition of Java in this. It seems that there is no problem with Standard Edition, but I used full Edition because there is no reason to select it. スクリーンショット 2019-08-12 18.17.15.png

However, for some reason it was played by Mac security, so I solved it by referring to the following site. (Maybe the latest version of Eclipse wasn't authenticated to the Mac?) Mac-Allow all applications to run (https://pc-karuma.net/macos-sierra-allow-apps-from-anywhere/)

Creating a project

First, select File-> New-> Maven Project. スクリーンショット 2019-08-12 18.31.41.png

Then, the following screen will be displayed. Put a chuck in "Create a single project (skip architecture selection)" and click "Next>". スクリーンショット 2019-08-12 18.37.27.png

Next is the screen for entering project information. (Qiita is just my typo ...) スクリーンショット 2019-08-12 18.42.35.png "Group Id" is the name of the creator. "Artif Id" is the project name = plugin name. You can name it as you like. "Version" is the version number. You can name this as you like. Set these three. Others should not need to be changed.

This completes the project creation!

Setting pon.xml

Next, we will make the settings for setting the plug-in. Here, prepare "what you need (dependency)". I think this is the initial state.

<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>com.github.kubota</groupId>
  <artifactId>QiitaSample</artifactId>
  <version>1.0.0</version>
</project>

The settings related to spigot are described here. You can use'Bukkit API'by adding spigot-repository and Spigot-API as shown below.

<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>com.github.kubota</groupId>
  <artifactId>QiitaSample</artifactId>
  <version>1.0.0</version>

  <!-- ↓ Spigot-repository -->
  <repositories>
    <repository>
      <id>spigot-repo</id>
      <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
    </repository>
  </repositories>

  <!-- ↓ Spigot-API -->
  <dependencies>
    <dependency>
      <groupId>org.spigotmc</groupId>
      <artifactId>spigot-api</artifactId>

      <!-- ↓ (Minecraft version)-R0.1-SNAPSHOT -->
      <version>1.13.2-R0.1-SNAPSHOT</version>

      <scope>provided</scope>
    </dependency>
  </dependencies>
</project>

However, this alone will not work depending on the version of Minecraft, so describe the properties as follows.

<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>com.github.kubota</groupId>
  <artifactId>QiitaSample</artifactId>
  <version>1.0.0</version>

  <!-- ↓ Spigot-repository -->
  <repositories>
    <repository>
      <id>spigot-repo</id>
      <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
    </repository>
  </repositories>

  <!-- ↓ Spigot-API -->
  <dependencies>
    <dependency>
      <groupId>org.spigotmc</groupId>
      <artifactId>spigot-api</artifactId>

      <!-- ↓ (Minecraft version)-R0.1-SNAPSHOT -->
      <version>1.13.2-R0.1-SNAPSHOT</version>

      <scope>provided</scope>
    </dependency>
  </dependencies>

  <!--↓ Property setting-->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- ↓ Java version 12 -->
    <maven.compiler.source>12</maven.compiler.source>
    <maven.compiler.target>12</maven.compiler.target>
  </properties>
</project>

Also, at present, change as follows to facilitate cooperation with plugin.yml which will be added later.

<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>com.github.kubota</groupId>
  <artifactId>QiitaSample</artifactId>
  <version>1.0.0</version>

  <!-- ↓ Spigot-repository -->
  <repositories>
    <repository>
      <id>spigot-repo</id>
      <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
    </repository>
  </repositories>

  <!-- ↓ Spigot-API -->
  <dependencies>
    <dependency>
      <groupId>org.spigotmc</groupId>
      <artifactId>spigot-api</artifactId>

      <!-- ↓ (Minecraft version)-R0.1-SNAPSHOT -->
      <version>1.13.2-R0.1-SNAPSHOT</version>

      <scope>provided</scope>
    </dependency>
  </dependencies>

  <!--↓ Property setting-->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- ↓ Java version 12 -->
    <maven.compiler.source>12</maven.compiler.source>
    <maven.compiler.target>12</maven.compiler.target>
  </properties>

  <!-- plugin.yml pom.Link with xml-->
  <build>
    <resources>
      <resource>
        <targetPath>.</targetPath>
        <filtering>true</filtering>
        <directory>src/main/resources/</directory>
      </resource>
    </resources>
  </build>

</project>

This completes the change of pom.xml.

Project update

The dependency has changed a little due to the change of pom.xml, so it is necessary to update it. As a method, right-click the created project (QiitaSample is applicable on my screen) and select it in the order of Maven-> Update project. This will start updating the dependencies (it didn't take long). スクリーンショット 2019-08-12 20.07.21.png

Then you will see the following screen. (In the case of the author, there are projects created before, but if it is the first creation, there will be only one) スクリーンショット 2019-08-12 20.12.35.png Chuck the project created this time to this and press OK, This completes the update!

Creating a plugin

Now that the Bukkit API dependencies have been set, you can start creating the Plugin itself.

Creating a main class

Right-click on src / main / java under the created project and select" New "->" Package ". スクリーンショット 2019-08-12 20.16.00.png

Next, let's use the package name ** which is the group ID and the plug-in name connected by. (Dot) **. In the case of the author, the group ID is com.github.kubota and the plugin name is QiitaSample, which is com.gtihub.kubota.qiitasample. Some changes are due to the fact that this package name cannot be used in uppercase letters, so it is replaced with lowercase letters, and since - (hyphen) cannot be used, it is replaced with _ (underscore).

Then right-click on the created package and select "New"-> "Class". スクリーンショット 2019-08-12 20.26.40.png

Then, the following screen will be displayed, so let's enter the class name that will bloom there. The name can be anything, but for the sake of clarity, let's use the current project name or Main. (It is also my typo that Qiita here is QIita ...) スクリーンショット 2019-08-12 20.32.30.png

When you open it, it should look like this:

package com.github.kubota.qiitasample;

public class QIitaPlugin {

}

Let's change this as follows.

package com.github.kubota.qiitasample;
import org.bukkit.plugin.java.JavaPlugin;

public class QIitaPlugin extends JavaPlugin{
	//↓ onEnable is a method that is executed when it is loaded
	@Override
	public void onEnable() {
		//↓ Leave a log on the server
		getLogger().info("Hello, Qiita!");
	}
}

Then, I will explain each changed part one by one.

import org.bukkit.plugin.java.JavaPlugin;

This line imports the functionality needed to create a plugin.

public class QIitaPlugin extends JavaPlugin{

With ʻexetends JavaPlugin` added at this point, it is possible to create by adding functions to the class in which the basic functions of Minecraft are already implemented.

public class QIitaPlugin extends JavaPlugin{
	//↓ onEnable is a method that is executed when it is loaded
	@Override
	public void onEnable() {
		//↓ Leave a log on the server
		getLogger().info("Hello, Qiita!");
	}
}

Here is a brief explanation of the ʻonEnablemethod. The place called@Override indicates that the method in the Javaplugin class is overridden. The overwrite target is the ʻonEnable method. The ʻonEnable` method is a method that is automatically executed when the plugin is enabled. The processing here has no function as long as it is not overwritten.

So, as an overwrite process, add something called "Hello, Qiita!" To the console with the'getLogger (). Info ()' method. By this overwrite process, it became a method with a function called ** that automatically displays "Hello, Qiita!" On the console when the plug-in is enabled.

Creating plugin.yml

Right-click on src / main / resources and select New> File. スクリーンショット 2019-08-12 20.51.55.png Then create a file named plugin.yml. スクリーンショット 2019-08-12 20.53.35.png If the following four are described.

name: "Main class"
version: "version"
main: "Main class"
api-version: "Bukkit API version""

name describes the plug-in name. version is the version of the plugin. However, this version is OK if you write $ {project.version} because plugin.yml is linked with pom.xml this time. main describes the package name of the main class and the class name connected by dots. ʻApi-version` is not strictly required, but it is almost required, so let's mention it.

This lists the supported Bukkit API version. For example, 1.13 or 1.13.2 is 1.13, 1.14 or 1.14.2 is 1.14, and so on.

name: "QiitaSample"
version: "${project.version}"
main: "com.github.kubota.qiitasample.QiitaSample"
api-version: "1.13"

Compiling plugins

This almost completes the implementation. All you have to do is compile. To do this, right-click on the project name and select "Run"-> "Maven install". スクリーンショット 2019-08-12 21.14.55.png

When you do this, the results will be displayed on the console screen. If BUILD SUCCESS is displayed, it is successful.

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.762 s
[INFO] Finished at: 2019-08-12T21:15:44+09:00
[INFO] ------------------------------------------------------------------------

Then the Jar file should be added to terget. スクリーンショット 2019-08-12 21.18.52.png

If you add this Jar file to Plugins of the server directory that was originally created and execute it, Plugin will start working. (The plugin name is QiitaTest due to various reasons, but I confirmed that it works normally by the above procedure.) スクリーンショット 2019-08-12 23.00.57.png

That is the end of this content. I developed the plug-in under the Mac environment, but I think that it will be helpful because the usage of Eclipse itself is the same for Windows. (Although the button layout may be slightly different ...)

Continued

Accept the [MineCraft plugin] command and raise an event

Where I got stuck

name: "SampleProject"
version: "${project.version}"
main: "com.github.kubota.sampleproject.SampleProject"
api-version: "1.13"

Although it is described in plugin.yml of the main part. I misunderstood that there is no problem with only the package name in this main place, and described it as follows.

name: "SampleProject"
version: "${project.version}"
main: "com.github.kubota.sampleproject"
api-version: "1.13"

As a result, the SampleProject file could not be referenced and the following error was thrown.

[16:11:39 ERROR]: Could not load 'plugins/SampleProject' in folder 'plugins'
org.bukkit.plugin.InvalidPluginException: Cannot find main class `com.github.kubota.sampleproject'
	at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:62) ~[spigot-1.13.2.jar:git-Spigot-1a3504a-dfa7583]
	at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:135) ~[spigot-1.13.2.jar:git-Spigot-1a3504a-dfa7583]
	at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:332) ~[spigot-1.13.2.jar:git-Spigot-1a3504a-dfa7583]
	at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:252) [spigot-1.13.2.jar:git-Spigot-1a3504a-dfa7583]
	at org.bukkit.craftbukkit.v1_13_R2.CraftServer.loadPlugins(CraftServer.java:325) [spigot-1.13.2.jar:git-Spigot-1a3504a-dfa7583]
	at net.minecraft.server.v1_13_R2.DedicatedServer.init(DedicatedServer.java:213) [spigot-1.13.2.jar:git-Spigot-1a3504a-dfa7583]
	at net.minecraft.server.v1_13_R2.MinecraftServer.run(MinecraftServer.java:698) [spigot-1.13.2.jar:git-Spigot-1a3504a-dfa7583]
	at java.lang.Thread.run(Thread.java:835) [?:?]
Caused by: java.lang.ClassNotFoundException: com.github.kubota.projectsample
	at java.net.URLClassLoader.findClass(URLClassLoader.java:436) ~[?:?]
	at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:136) ~[spigot-1.13.2.jar:git-Spigot-1a3504a-dfa7583]
	at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:82) ~[spigot-1.13.2.jar:git-Spigot-1a3504a-dfa7583]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:588) ~[?:?]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[?:?]
	at java.lang.Class.forName0(Native Method) ~[?:?]
	at java.lang.Class.forName(Class.java:415) ~[?:?]
	at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:60) ~[spigot-1.13.2.jar:git-Spigot-1a3504a-dfa7583]
	... 7 more

References

-Creating a Bukkit project Eclipse -[Bukkit plug-in production course-No. 2] Main class creation and build -[For super beginners] Maven super introduction -[Minecraft](for Java inexperienced people) Bukkit / Spigot compatible plug-in making course [Preparation] -Set up a multi-server with Minecraft! (Think IT Books)

Recommended Posts

Creating Spigot plugins using Eclipse
Creating async plugins with neovim
Creating a web application using Flask ②
Creating a simple table using prettytable
Creating a web application using Flask ①
Creating a learning model using MNIST
Creating a web application using Flask ③
Creating a web application using Flask ④