1.17.x Write to server.properties

Discussion in 'Spigot Plugin Development' started by Alcachofraz, Sep 2, 2021.

  1. Hello, fellow developers.

    Does anyone know of a way to write to server.properties? I need to dynamically update resource-pack-sha1, but I can't figure out how to.

    Thanks in advance.
  2. Try
    Code (Text):
    ((DedicatedServer) MinecraftServer.getServer()).propertyManager.setProperty("resource-pack-sha1", value);
    • Useful Useful x 1
  3. My IDE doesn't recognize class DedicatedServer nor MinecraftServer. I'm using Intellij's plugin for Minecraft Spigot Plugin Development. Also can't find anything in java docs https://hub.spigotmc.org/javadocs/bukkit/.
  4. That's because both classes are not included in Spigot, but in NMS. So, to use that code you need to import CraftBukkit ór use Reflection.

    If you want to use Reflection, the last post in this thread includes an example. https://www.spigotmc.org/threads/util-change-server-properties-easily.113868/#post-1222024
  5. To make that work, you need:
    1. Run buildtools for your version on your development computer.
    2. In pom.xml change dependency from spigot-api to just spigot.
  6. I've ran BuildTools and it created these files:
    What's the next step? To import CraftBukkit into my plugin? What's the pom.xml you're talking about?

    Attached Files:

  7. 1. Run buildtools for your desired version, that's it, you don't have to anything more such as moving any of the files, it automatically creates local maven jar.
    2. Open your plugin project and there should be file named pom.xml, open it, scroll down and there will be a dependency tag <dependency> with <artifactId>spigot-api<\artifactId> and change it to <artifactId>spigot<\artifactId>
    3. There should appear little blue M button at top right, click it.
    4. Now you should be able to use the NMS classes from first post.
  8. Oh, I see. pom.yml is a Maven file. I've never really used it. I've been using Gradle. I'll look into it.
    • Like Like x 1
  9. Same for gradle, but then using the gradle syntax.
  10. I'm trying to use:
    ((DedicatedServer) MinecraftServer.getServer()).propertyManager;
    ...to get the propertyManager, but getServer() seems to be deprecated. Do you know what I can use instead? I can't find any BukkitCraft docs.
  11. Why? Spigot has an API for sending players resource packs (and verifying it via a SHA1 checksum).

    See Player#setResourcePack(…)
    • Agree Agree x 2
  12. Was going to say what @FigT said, but I will just add that I use that method to dynamically update resource packs during runtime without a server restart. I also listen to the resource pack event to deny players from logging in if they dont accept it.
  13. Thank you for your help. However, when I try to use setResourcePack(), nothing happens. I found out OnPlayerJoin gets called before the player actually joins. So, before calling setResourcePack(), I wait 1 tick. Here's my code:
    Code (Text):

    public void onJoin(PlayerJoinEvent event) {
                () -> Pack.set(event.getPlayer()),
    And inside Pack:
    Code (Text):

    public static void set(Player player) {
        String url = plugin.getConfig().getString("resource-pack-link");
        // Pack link set:
        System.out.println("[PACK] Pack URL = " + url);
        player.setResourcePack(url, DropboxUtils.getHash());
        System.out.println("[PACK] Player resource pack set");
    In DropboxUtils.getHash() I use the Dropbox API to get a relevant SHA-1 code (byte[20]).

    However, as I said, nothing happens. The game doesn't even freeze or anything. It's like there's no resource pack at all.

    Here's what happens on the console:


    Inside DropboxUtils.getHash() I print the SHA-1 code converted to hexadecimal.
    Everything seems fine. It generates a 20-byte hash code, as intended.[/CODE]

    Attached Files:

    #13 Alcachofraz, Sep 6, 2021
    Last edited: Sep 6, 2021
    • Like Like x 1
  14. Well, I changed the link from https\://www.dropbox.com/...dl\=1 to https://www.dropbox.com/...dl=1 and now something happened. "Resource pack couldn't be applied." I don't know what to do.
  15. Yeah, I'm not sure about how to use a cached version of the pack. I generally don't need this given that once a player has downloaded the resource pack they don't need to again, it will just load.