플러그인 디버그

플러그인의 버그 중에는 추적하기 어려운 것이 있습니다. 이 때 코드를 조금씩 바꾸고 매번 다시 컴파일하는 작업은 번거로울 것이며, 코드는 로그 출력문으로 어지럽혀질 것이므로, 디버그 작업은 매우 싫증날 수 있습니다. 따라서, 이 문단은 당신의 플러그인을 설정하여 자바에서 제공하는 디버그 기능을 활용하도록 도와줄 것입니다.

작업공간 준비하기

당신의 IDE에 Sponge와 당신의 플러그인을 동시에 돌릴 것이기 때문에, 당신은 SpongeForge 또는 SpongeVanilla 중 당신이 선호하는 것 하나를 당신의 작업공간에 프로젝트로 불러와야 합니다. 위 링크(Github 프로젝트 페이지) 에서 방법을 확인하고 잘 따라주세요. 작업공간 준비가 끝났다면 아래로 계속 진행해주세요.

당신이 불러온 SpongeForge 또는 SpongeVanilla 프로젝트에서 당신의 플러그인 프로젝트가 보여지도록 만들어야 합니다. 당신이 사용하는 IDE에 따라 방법은 달라집니다.

IntelliJ IDEA

IntelliJ에서, 모든 프로젝트는 각자의 작업공간을 갖고 있습니다. 당신의 프로젝트가 Sponge(Vanilla) 프로젝트에 보여지도록 하려면, 프로젝트가 Module이 되어야 합니다. 당신의 프로젝트가 IntelliJ IDEA 설정하기에서 설명한 방법에 따라 생성되었다고 가정하겠습니다. 프로젝트를 아래 방법으로 불러오세요.

  • SpongeVanilla/SpongeForge 프로젝트를 연다.

  • FileNewModule from Existing Sources... 클릭

  • 플러그인 프로젝트가 있는 폴더로 이동한다.

    • Gradle을 사용하는 경우, build.gradle 파일을 선택하고, 다음 화면에서 Use auto-import를 선택한 후 확인한다.

    • Gradle을 사용하지 않으면, 최상위 폴더를 선택하고 Finish를 누른다.

  • Finish를 누른다.

아직 당신의 플러그인을 만들지 않았다면, Module from Existing Sources... 대신에 Module...을 클릭하고, 플러그인을 위한 프로젝트를 생성하세요.

Eclipse

Just create your project as described here: Eclipse 설정하기. As long as it is in the same workspace as your SpongeVanilla/SpongeForge project, it will be visible.

Sponge classpath에 플러그인 추가하기

이제 당신의 IDE에서 Sponge를 실행시킬 것입니다. 하지만, Sponge classpath에 당신의 플러그인을 추가할 것이 평소와 다른 점입니다. Sponge는 classpath에 존재하는 모든 플러그인들을 기본적으로 불러옵니다. 따라서 당신의 플러그인 프로젝트를 Sponge classpath에 추가한다면, 코드를 수정할 때마다 매번 빌드하여 플러그인 파일을 추출하고 서버 폴더에 추가하는 작업을 할 필요가 없어집니다.

우선 당신의 IDE에 Run/Debug Configuration이 적절히 설정되었는지 Sponge README.md를 참조하여 확인해주세요.

그 다음 Run/Debug Configuration을 수정하여 당신의 프로젝트가 classpath에 추가되도록 하세요. 방법은 당신의 IDE에 따라 달라집니다:

IntelliJ IDEA

  • Project Structure를 연다.

    • FileProject Structure... 클릭

    • 또는, IDE 화면의 오른쪽 모서리에서 Search 옆의 Project Structure를 클릭한다.

  • Modules 클릭. (당신이 선택한 것에 따라) SpongeForge 또는 SpongeVanilla 그룹을 확장한다.

  • SpongeForge_main 또는 SpongeVanilla_main을 선택한다.

  • 오른쪽 열에서 Dependencies 탭을 선택한다.

  • 맨 아랫줄에``+`` (추가) 를 클릭하고 Module Dependency를 선택한다.

  • yourplugin_main (자신의 플러그인 모듈) 을 선택한다.

  • 추가한 모듈에 Export 옵션을 선택하지 마십시오.

경고

Due to a bug in IntelliJ (IDEA-194641), any dependencies that your plugin has (e.g. the Kotlin standard library, or Gson) will not be added to the classpath using the above method. This results in a ClassNotFoundException when your plugin tries to access one of these classes, even though it successfully compiled against that class.

This issue only affects running your plugin from IntelliJ as a module. Your final built plugin jar will run normally in both IntelliJ and a production server.

If your plugin has external dependencies, you’ll need to follow these steps in order to run it directly from IntelliJ:

  • Create a new Java module with File -> New -> Module.... Name it SpongeForgeContainer.

  • Open the module settings for SpongeForgeContainer as described above

  • In the dependency settings for SpongeForgeContainer, add a module dependency on SpongeForge_main

  • Edit your server run configuration. Change Use classpath of module: from SpongeForge_main to SpongeForgeContainer

You can leave all of your settings unchanged, including SpongeForge_main’s dependency on your plugin module. Now, all of your plugin’s dependencies should be added to the classpath when you run the server.

Eclipse

  • Find your Run/Debug Configuration

    • Click Run, followed by Run Configurations...

    • OR, click the drop-down arrow beside the Run/Debug icons and then Run Configurations... or Debug Configurations..., respectively.

  • Select your Run/Debug Configuration for Sponge (Server) on the left side.

  • Switch to the Classpath tab.

  • Select User Entries, followed by the Add Projects... button.

  • Select the appropriate Project for your Plugin.

  • Click the OK button.

  • Click the Apply button on the bottom right corner.

Running the Configuration

After you’ve followed the previous steps, you should be ready to start debugging. If you start your server from your IDE, its working directory will be the run directory in your SpongeForge/SpongeVanilla project. All the files usually created by a server (worlds, configs etc.) will be stored in that run directory and persist over multiple runs of your local test server, just as if you manually copied a server .jar to the run directory and started it from there.

IntelliJ IDEA

Run/Debug Configuration을 실행하는 초록색 오른쪽 화살표를 누르는 대신, 그 오른쪽에 있는 Debug 아이콘을 클릭하세요.

Eclipse

Rather than pressing the green right-pointing arrow to run your Run/Debug configuration, click the drop-down arrow of the Debug icon (the one displaying a bug) and click your Test (Server) configuration. If it doesn’t appear in the drop-down menu, click Debug Configurations. Select Test (Server) configuration and hit the Debug button on the bottom left.

디버거 사용하기

Now that your server (and your Plugin) are running in the Debugger, you can make use of the features it holds. The most prominently used are explained below in short, though they are not features of Sponge, but the Java Debugger your IDE makes use of.

Breakpoints

Breakpoints are a useful tool to take a closer look at the code. A breakpoint can be set at the beginning of a line of code or a function. When reaching a breakpoint, the debugger will halt the code execution and your IDE will open up a view allowing you to inspect the content of all variables in the current scope. Code execution will not resume unless you press the according button in your IDE’s debugging view.

Breakpoints may also be added, removed or temporarily disabled while the debugging is in process.

Once a single server tick takes more than a given amount of time, the watchdog will consider the server crashed and forcefully shut it down. When working with breakpoints this might occur, so it is recommended that you edit your test environments server.properties file and set the value of max-tick-time to either a very large number (the amount of milliseconds a tick may take) or -1 (to disable the Watchdog completely).

IntelliJ IDEA

To add or remove a breakpoint, just left click in the blank space just to the left of your editor.

Alternatively, have your cursor be in the line where you want the breakpoint added or removed and then click Run followed by Toggle Line Breakpoint.

Eclipse

To add or remove a breakpoint, just right click in the blank space just to the left of your editor and click Toggle Breakpoint.

Alternatively, have your cursor be in the line where you want the breakpoint added or removed and then click Run followed by Toggle Breakpoint.

Code Hotswapping

The other major feat of the debugger is that you will not have to restart your server for every small change you make, thanks to code hotswapping. This means that you can just recompile portions of your code while it is running in the debugger. However, there are a couple of limitations, the most important of which are:

  • You cannot create or remove methods.

    • Changes to methods are limited to code within the method. You cannot modify its signature (that means its name, return type and parameter types)

  • You cannot remove classes.

    • You cannot modify a class’ name, superclass or the list of interfaces it implements.

    • You can add classes. However, once it’s been built and hotswapped, the class follows the above rules.

You can test this functionality: Introduce a simple command to your plugin that just writes a word, like Sponge Then save it and start the server as described above. Run the command. It will output Sponge. Now change the command to write a different word to console, save the file. After a change, do as follows to hotswap the changes to the running program:

IntelliJ IDEA

  • Open the Run menu, from the top of the IDE.

  • Below the first category break, click Reload Changed Classes.

Eclipse

No action needed. As soon as you save the file, it will be rebuilt and automatically hotswapped with the currently running debug. Unless you changed this particular default behavior, you will not have to trigger a manual hotswap.