QNick is Kore’s very much advanced nickname plugin for Minecraft servers. So what does it do? Note: this tutorial has SQL stripped out of it as this makes the code very complex

Here is a run down of what QNick does in some simple code steps:

Step 1

First is the Nickname class

public class Nickname {
    private static void setDisplayName(@NotNull Player player, @NotNull Component nick) {
        player.displayName(nick);
    }

    private static NamespacedKey getKey() {
        return QNick.getNickKey();
    }


    public static Component get(@NotNull Player player) {
        if (player.getPersistentDataContainer().has(getKey(), PersistentDataType.STRING)) {
            return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(player.getPersistentDataContainer().get(getKey(), PersistentDataType.STRING)));
        }
        return null;
    }

    public static void set(@NotNull Player player, @NotNull Component nick) {
        player.getPersistentDataContainer().set(getKey(), PersistentDataType.STRING, MiniMessage.miniMessage().serialize(nick));
        setDisplayName(player, nick);
    }

    public static void remove(@NotNull Player player) {
        player.getPersistentDataContainer().remove(getKey());
        player.displayName(Component.text(player.getName()));
    }

    public static boolean has(@NotNull Player player) {
        return player.getPersistentDataContainer().has(getKey(), PersistentDataType.STRING);
    }
}

Let’s break this down further into the functions

public static Component get(@NotNull Player player) {
    if (player.getPersistentDataContainer().has(getKey(), PersistentDataType.STRING)) {
        return MiniMessage.miniMessage().deserialize(Objects.requireNonNull(player.getPersistentDataContainer().get(getKey(), PersistentDataType.STRING)));
    }
    return null;
}

In this function we are getting the user’s nickname by first checking if the user has a nickname, note that the function “getKey()” returns

new NamespacedKey(this/*This is the Plugin instance*/, "nick");

from the main plugin. The PersistentDataType.STRING shows this value is a text value.

If the user is found to have a nickname it will return a Component from the AdventureAPI which is from a MiniMessage formatted string, you will see that this is how nicknames are stored in the next function explaination.

Next let’s focus on setting a nickname

public static void set(@NotNull Player player, @NotNull Component nick) {
    player.getPersistentDataContainer().set(getKey(), PersistentDataType.STRING, MiniMessage.miniMessage().serialize(nick));
    setDisplayName(player, nick);
}

Here it is a little simpler, we set the PersistentDataContainer’s key to the nickname in a MiniMessage formatted string. Then it calls the setDisplayName() function which just makes the nickname visible everywhere.

Now removing a nickname is a simple process

public static void remove(@NotNull Player player) {
    player.getPersistentDataContainer().remove(getKey());
    player.displayName(Component.text(player.getName()));
}

Gets the PDC and removes any reference to the key, then set the user’s display name to their username

Checking for a nickname is very very simple

public static boolean has(@NotNull Player player) {
    return player.getPersistentDataContainer().has(getKey(), PersistentDataType.STRING);
}

Simpily checks if the PDC has the key, not much more to say

Step 2

Applying the nickname on join

@EventHandler(priority = EventPriority.LOWEST)
public void onJoin(PlayerJoinEvent event) {
    if (Nickname.has(event.getPlayer())) {
        event.getPlayer().displayName(Nickname.get(event.getPlayer()));
    }
}

This checks if a player has a nickname on the PlayerJoinEvent then if a nickname is found, it applys it by getting the nickname using the get function which we covered eariler

Those are the basics of QNick but here is something you may be able to use in your Gradle plugin. I’m not sure how to do this in Maven

First in your build.gradle file add the following

jar {
    manifest {
        attributes 'Main-Class': 'your.package.path.NotRunnable'
    }
}

Then make a new class at the location you put above with the following code

package your.classes.path;

import javax.swing.JOptionPane;

public class NotRunnable {
    public static void main(String[] args){
        JOptionPane.showMessageDialog(null, "This jar file is not runnable, please put it in your server's plugin folder to use the plugin", "Not Runnable!", JOptionPane.INFORMATION_MESSAGE);
    }
}

Now when you build your gradle plugin and try run it as a program you’ll see this

Thanks for reading, that was a short look at how QNick works, goodbye now!

Edit: Can’t use your.package.path as package was a keyword, fixed it up and it’s fine


<
Previous Post
Dev Log 1
>
Next Post
Java Guide