Resource [Tutorial] Adding custom patches to Spigot

Discussion in 'Spigot Plugin Development' started by snowyCoder, Jun 25, 2016.

  1. Prerequisites:

    What will we do in this tutorial?
    We'll modify spigot/craftbukkit to add some code using patches.
    As example i'll add the method
    Code (Text):
    that will send an "hello world" message to the player

    The patch injection system is a powerful system that allows us to easily customize our spigot.
    It is used mainly to add patches to bukkit/craftbukkit but we can aswell use it for our own purpose.

    How to:
    First you need to put your BuildTools.jar in a folder and make it compile with the git bash
    It will take a while, It's downloading and compiling spigot!
    In order to do so It must download and decompile craftbuikkit, apply the spigot patches and recompile itself.
    After the process ended it will leave all the work dirs.
    The one that we're intrested in is the Spigot dir.
    We can close the terminal and explore what did the BuildTools left for us.
    In the directory there are a lot of other dirs and some files.
    We're instrsted mainly in those dirs: "Bukkit-Patches", "CraftBukkit-Patches", "Spigot-API", "Spigot-Server".
    The first two dirs contain the patches that were applied to spigot: the first speifies what to apply to the bukkit api, the latter specifies the ones to add to craftbukkit.
    The other two dirs contain the source code of spigot but the first contains only the Spigot API (bukkit) and the second contains only the stuff not essential for the api (craftbukkit & other)
    We need to create a patch but, to make the process more simple (no one wants to create a .patch file directly) we will modify the source and let Maven do the dirty work.
    You can modify the source with every text editor but in this tutorial I'll use Intellij idea.
    First we need to Open the Maven project:
    Click next until it asks the name of the project, write "Spigot" or something similar and finish the process.
    It will give a warning related to maven but we only need to modify the code so it doesn't matter.
    Here comes the fun part, develop what you want.
    I made the helloWorld() method:
    I could make it easier writing a default method in the Player class but that's too easy:
    Ok, once it's all written save and exit, we don't need no IDE anymore.
    We need to open the console and add every file we modified with "git add path\to\"
    Once we're done we need to write "git commit -m <message>" where the message arguments specifies what has the path modified.
    WARNING: You need to do the git add/commit two times: once for Spigot-API and the other for Spigot-Server
    Note: the messagges can be different, I take them equal for order.
    Once this is done we just need to use the "" and the .patch file will be created.
    To build the project and obtain the custon spigot.jar we need to write "mvn clean install"
    The jar created will be in "Spgiot-Server\target", it's the one without original in its name.
    Here's the jar, now we can import and use it!
    Remember to replace it with the original one in the server dir!
    • Winner x 40
    • Like x 13
    • Informative x 13
    • Useful x 4
    • Friendly x 3
    • Agree x 1
    • Creative x 1
  2. Thanks for the amazing tutorial :O
    I will definitely be using this soon.
    • Agree Agree x 1
    • Winner Winner x 1
  3. You're welcome :)
    • Agree Agree x 1
  4. Nice tutorial, really helpful! I think that applying custom patches to the server will help Spigot developers in many ways. Thank you! :)
  5. Bookmarked, thanks a lot :)
    • Friendly Friendly x 1
  6. Editing the jar file opens some incredible capabilities, things that plugins would not be able to achieve (or at least would take a lot of reflections and potentially messing with the ClassLoader). This is good stuff if you know your java!
    • Like Like x 1
  7. And with this you can mess with original classes (like creating some really cool-looking Apis
  8. And what to do if Minecraft gets updated? What happens to your changes?
  9. Just plop them in the new patches folder.
  10. How to run .sh files on Windows?
    #10 aLTeR, Feb 17, 2017
    Last edited: Feb 17, 2017
  11. I personally use git bash
    • Like Like x 2
  12. This topic needs a big extension... Barely explains anything. I couldn't even import Spigot platform properly while following this tutorial (missing imports/classes). Also, no information about patches at all. Do you even have to write all those directories manually on CMD? Why not just create patch with Intellij?
    #12 aLTeR, Feb 18, 2017
    Last edited: Feb 18, 2017
    • Funny Funny x 2
    • Agree Agree x 1
  13. I don't work with minecraft from a lot, but the best thing to do if you want to understand how does the patch system works and how to add the files to the patch is to study Git
  14. This is really really helpful, I got a lot of plans with this :D thanks man, great resource
    • Agree Agree x 1
  15. or you could just open spigot.jar in eclipse and have fun with it XD
    • Funny Funny x 3
  16. Can I add kotlin patches?
  17. Choco


    Most likely not considering that the entire project is written in Java...
  18. Is there a way to add patches after compilation?
  19. Kotlin and Java are completely interpolate, you can write a class in Kotlin and utilize it from java as well. You can most certainly do this, as long as you understand both the languages enough.
    • Agree Agree x 2
  20. This is dope. Bumping so others can see how amazing this is. :p