1.16.5 Using LuckPerms API, How can i get a player's prefix ?

Discussion in 'Spigot Plugin Development' started by Aurexium, Feb 20, 2021.

Thread Status:
Not open for further replies.
  1. Hey!

    I hope you're well,

    I recently started to use Luckperms API and I don't find how can I get a player's prefix with the v5 of the API. I searched on the Developer API Usage but I found nothing... Can someone help me ??

    What I want to do is simple, I want to make my own chat Format for my server:

    Code (Java):
    public class ChatFormat implements Listener {
     
        public LuckPerms lp;
     
        @EventHandler
        public void onChat(AsyncPlayerChatEvent e) {
            Player p = e.getPlayer();
            User user = lp.getUserManager().getUser(p.getUniqueId());
         
            e.setFormat((I WANT THE LP PREFIX HERE) + p.getDisplayName() + " §e§l» " + e.getMessage().replace("&", "§"));
        }

    }
     
  2. You have to use Vault's API, check the GitHub for usage on how to set it up, and then it's something like Chat#getPlayerPrefix()
     
  3. Code (Java):
    Player p = e.getPlayer();
    User user = luckPerms.getPlayerAdapter(Player.class).getUser(p);
    String prefix = user.getCachedData().getMetaData().getPrefix();
    I simply checked the api but this should works
     
  4. Ty! I tried but that didn't work, when I use:

    Code (Java):
            User user = lp.getPlayerAdapter(Player.class).getUser(p);
            String prefix = user.getCachedData().getMetaData().getPrefix();
           
            e.setFormat(prefix + p.getDisplayName() + " §e§l» §7" + e.getMessage());
    But works when I remove every LuckPerms line.
    Here's the error in the console:

    Code (Text):
    [14:00:23 ERROR]: Could not pass event AsyncPlayerChatEvent to MiniBeeGlobal v1.0
    java.lang.NullPointerException: null
            at me.aurexium.minibeeglobal.ChatFormat.onChat(ChatFormat.java:18) ~[?:?]
            at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor18.execute(Unknown Source) ~[?:?]
            at org.bukkit.plugin.EventExecutor.lambda$create$1(EventExecutor.java:69) ~[patched_1.16.5.jar:git-Paper-469]
            at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:76) ~[patched_1.16.5.jar:git-Paper-469]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[patched_1.16.5.jar:git-Paper-469]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:607) ~[patched_1.16.5.jar:git-Paper-469]
            at net.minecraft.server.v1_16_R3.PlayerConnection.chat(PlayerConnection.java:1934) ~[patched_1.16.5.jar:git-Paper-469]
            at net.minecraft.server.v1_16_R3.PlayerConnection.c(PlayerConnection.java:1849) ~[patched_1.16.5.jar:git-Paper-469]
            at net.minecraft.server.v1_16_R3.PlayerConnection.a(PlayerConnection.java:1787) ~[patched_1.16.5.jar:git-Paper-469]
            at net.minecraft.server.v1_16_R3.PacketPlayInChat$1.run(PacketPlayInChat.java:41) ~[patched_1.16.5.jar:git-Paper-469]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_261]
            at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_261]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_261]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_261]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_261]
     
  5. I will try it thx !
     

  6. Code (Java):
            User user = lp.getPlayerAdapter(Player.class).getUser(p);

            if (user.getCachedData().getMetaData().getPrefix() == null)
                return;

            String prefix = user.getCachedData().getMetaData().getPrefix();
            e.setFormat(prefix + p.getDisplayName() + " §e§l» §7" + e.getMessage());
    Can you try this? and if this doesn't work please show me 18th line
     
  7. Thanks but didn't work, I don't understand, I tried to use the Vault API but I have a the same problem.

    Here's my all class with LuckPerms:

    Code (Java):
    package me.aurexium.minibeeglobal;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;

    import net.luckperms.api.LuckPerms;
    import net.luckperms.api.model.user.User;
    import net.milkbowl.vault.chat.Chat;
    import net.milkbowl.vault.permission.Permission;

    public class ChatFormat implements Listener {
       
        public LuckPerms lp;
       
        @EventHandler
        public void onChat(AsyncPlayerChatEvent e) {
            Player p = e.getPlayer();
            User user = lp.getPlayerAdapter(Player.class).getUser(p);

            if (user.getCachedData().getMetaData().getPrefix() == null)
                return;

            String prefix = user.getCachedData().getMetaData().getPrefix();
            e.setFormat(prefix + p.getDisplayName() + " §e§l» §7" + e.getMessage());
        }
    }
     
    Here's my all class with Vault:

    Code (Text):
    package me.aurexium.minibeeglobal;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;

    import net.milkbowl.vault.Vault;
    import net.milkbowl.vault.chat.Chat;
    import net.milkbowl.vault.permission.Permission;

    public class ChatFormat implements Listener {
       
        private Chat chat;
        private Permission perms;

        @EventHandler
        public void onChat(AsyncPlayerChatEvent e) {
            Player p = e.getPlayer();
            String prefix = chat.getGroupPrefix(null, HERE);
           
            e.setFormat( prefix + p.getDisplayName() + "§e§l» §f" + e.getMessage());
        }
    }
    With Vault, I used the getGroupPrefix(), but I don't know what to write between the parentheses like the first string is the world (when null==allworld). And the second String idk I tried p.getName() but same problem as LuckPerms
     
  8. Did you ever initialize the LuckPerms and / or Chat variable?
     
  9. What do you mean by "initialize" ??
     
  10. I guess, that public LuckPerms lp; and private Chat chat; are permanently null, because you have not set them.
     
  11. Oh, and how should I return them I thought like when I write these lines it will be ok?
     
  12. Add ChatFormat:
    Code (Java):
      public ChatFormat(LuckPerms lp) {
        this.lp = lp;
      }
    and in onEnable():
    Code (Text):
    RegisteredServiceProvider<LuckPerms> provider = Bukkit.getServicesManager().getRegistration(LuckPerms.class);
    LuckPerms api;
    if (provider != null) {
        api = provider.getProvider();
        getServer().getPluginManager().registerEvents(new ChatFormat(api), this);
    }
     
     
  13. It works! Thank you so much! I didn't know that I needed to initialize like that...

    You all helped me a lot, thanks! :)
     
Thread Status:
Not open for further replies.