HELP - Quests API - Getting Player's Quest Point

Discussion in 'Spigot Plugin Help' started by morningstarIWI, Jan 14, 2020.

  1. Hello, I'm trying to get the player's quest point by using the quests API but I have a problem that I do not understand. I think it should work but it doesn't

    Code (Java):
    Player player=(Player)sender;
    String i=player.getName();
    UUID uid=player.getUniqueId();
    String uuid=uid.toString();
    Quester quester=null;
    int questpuan=quester.getQuestPoints();
    The problem here is "Quester quester=null;" part.

    Error log is in the spoiler;

    Code (YAML):
    [19:26:03] [Server thread/ERROR]: null
    org.bukkit.command.CommandException
    : Unhandled exception executing command 'gorev' in plugin TLGNDdfgorev v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_131]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_131]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by
    : java.lang.NullPointerException
        at tr.thelegend.gorev.cmds.GorevCMD.onCommand(GorevCMD.java:35) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
        ... 15 more

    I really need help right now. Because I do not understand why it doesn't work.
     
  2. Well obviously you're getting a null exception because Quester is null. It hasn't been instantiated yet(it basically doesn't exist yet).
    You can't do quester.getQuestPoints() if quester doesn't have any instances of itself, you can't slice a loaf of bread if there is no bread to slice.
    I suggest you look into plugin hooks to find exactly what you're looking for.
    I don't know what the rest of your code looks like, but here's an example of how you might be able to get your Quester object.
    Code (Java):

        public Quester getQuester() {
           Quester qstr = null;
            RegisteredServiceProvider<Quester> rsp = plugin.getServer().getServicesManager().getRegistration(Quester.class);
            if (rsp == null) {
                return null;
            }
            qstr = rsp.getProvider();
            return qstr;
       }
     
    Followed by
    Code (Java):

    quester = getQuester();
     
    Don't forget that "plugin" in "plugin.getServer().getServicesManager()..etc" is your main class

    Disclaimer: I'm not that experienced with spigotapi so things that I say here could be flawed/wrong
     
  3. @Athlaeos Thanks for trying to assist. I am the developer of Quests and I've already reached out to the OP via private message.

    The issue with the original code is two-fold: it's looking up a Quester by name using a UUID, and Quests was never initialized.

    Since I know of at least one other person trying to use morningstar's code, I'll post the correct solution here:
    Code (Java):
    Quests quests = (Quests)Bukkit.getPluginManager().getPlugin("Quests");

    UUID uuid = (Player)sender.getUniqueId();
    Quester quester = quests.getQuester(uuid);
    Edit: This is also the wrong forum. Questions about plugin development should be posted in https://www.spigotmc.org/forums/spigot-plugin-development.52/
     
    #3 PikaMug, Jan 15, 2020
    Last edited: Jan 15, 2020
  4. I tried this but it didn't work.

    Type mismatch: cannot convert from Plugin to Quests
     
  5. Forgot to cast. Updated my reply.
     
  6. Now I tried the new way but the problem is still the same.

    Code (Java):
    00:07:10 [SEVERE] null
    00:07:10 org.bukkit.command.CommandException: Unhandled exception executing command 'gorev' in plugin TLGNDdfgorev v1.0
    00:07:10     at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    00:07:10     at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    00:07:10     at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    00:07:10     at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    00:07:10     at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    00:07:10     at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    00:07:10     at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    00:07:10     at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    00:07:10     at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_131]
    00:07:10     at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_131]
    00:07:10     at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    00:07:10     at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    00:07:10     at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    00:07:10     at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    00:07:10     at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    00:07:10     at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    00:07:10 Caused by: java.lang.NullPointerException
    00:07:10     at tr.thelegend.gorev.cmds.GorevCMD.onCommand(GorevCMD.java:35) ~[?:?]
    00:07:10     at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8-R0.1-SNAPSHOT-latest.jar:git-Spigot-db6de12-18fbb24]
    00:07:10     ... 15 more
     
    My code is;
    Code (Java):
    Player player=(Player)sender;
    String i=player.getName();
    UUID uuid=player.getUniqueId();
    Quester quester=plugin.quests.getQuester(i);
    int questpuan=quester.getQuestPoints();
    (I also tried to get the quester by UUID, still same)

    İn my main class;
    Code (Java):
    public Quests quests = (Quests)Bukkit.getPluginManager().getPlugin("Quests");
     
  7. Post your entire onCommand method, please. Also, what's at line 35 now?
     
    #7 PikaMug, Jan 15, 2020
    Last edited: Jan 15, 2020
  8. Code (Java):
    public boolean onCommand(CommandSender gonderen, Command komut, String label, String[] args) {
            if (komut.getName().equalsIgnoreCase("gorev")) {
                if (!(gonderen instanceof Player)) {
                    gonderen.sendMessage("Sadece oyun ici sayin mr thelegend.");
                    return true;
                }
                Player oyuncu=(Player)gonderen;
                String i=oyuncu.getName();
                UUID uuid=oyuncu.getUniqueId();
                Quester quester=plugin.quests.getQuester(i);
                int questpuan=quester.getQuestPoints();
                if (args.length==0) {
                    List<String> liste = plugin.c.getStringList("gorev-args0-msj");
                      int xx = Bukkit.getOnlinePlayers().size();
                      String xxx = Integer.toString(xx);
                      for (String hgmsj : liste) {
                          if(Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null){
                              String avecpapi=PlaceholderAPI.setPlaceholders(oyuncu, hgmsj);
                              oyuncu.sendMessage(ChatColor.translateAlternateColorCodes('&', avecpapi.replace("%online%", xxx).replace("%oyuncu%", oyuncu.getName()).replace("%siradakigorev%", questpuan+1+"").replace("%questpuan%", questpuan+"")));
                          } else {
                          oyuncu.sendMessage(ChatColor.translateAlternateColorCodes('&', hgmsj.replace("%online%", xxx).replace("%oyuncu%", oyuncu.getName()).replace("%siradakigorev%", questpuan+1+"").replace("%questpuan%", questpuan+"")));
                          }
                }
            }
                if (args.length==1) {
                    int sayi;
                    try {
                          sayi = Integer.parseInt(args[0]);
                        }
                        catch (Exception e) {
                            gonderen.sendMessage("§8[§3TLGND§8] §7Gecerli bir gorev sayisi girin.");
                          return true;
                        }
                    if (quester.getQuestPoints()<sayi) {
                        gonderen.sendMessage("§8[§3TLGND§8] §7Gorev puanınınız bu göreve gitmek için yetersiz.");
                        gonderen.sendMessage("§7Şuan "+quester.getQuestPoints()+" §7puana sahipsiniz yani, "+quester.getQuestPoints()+"§7. goreve gidebilirsiniz.");
                        gonderen.sendMessage("§7Gorevleri tamamlayarak puan kazanırsınız ve bu şekilde yeni görevler açarsınız.");
                        return true;
                    }
                    Player o=(Player)gonderen;
                    World w=Bukkit.getServer().getWorld(plugin.c2.getString(sayi+".w"));
                    float yaw=(float)plugin.c2.getDouble(sayi+".yaw");
                    float pitch=(float)plugin.c2.getDouble(sayi+".pitch");
                    double x=plugin.c2.getDouble(sayi+".x");
                    double y=plugin.c2.getDouble(sayi+".y");
                    double z=plugin.c2.getDouble(sayi+".z");
                    Location loc=new Location(w,x,y,z);
                    loc.setYaw(yaw);
                    o.getLocation().setPitch(pitch);
                    o.teleport(loc);
                    o.sendMessage("§8§l* §7"+sayi+"§7. goreve ışınlandın!");
                    return true;
                }
        }
            return false;
    }
    Line 35. is "Quester quester=plugin.quests.getQuester(i);"
     
  9. Just tested your code (had to move "args.length==1" block, looks like it was pasted in the wrong spot) and it works fine for me. If you already have Quests as a depend/softdepend in plugin.yml then I imagine either your server is running an old version of the plugin (make sure it's running the latest version from here) or something prevented Quests from loading properly. Try the following instead:
    Code (Java):
        @Override
        public boolean onCommand(CommandSender gonderen, Command komut, String label, String[] args) {
            if (komut.getName().equalsIgnoreCase("gorev")) {
                if (!(gonderen instanceof Player)) {
                    gonderen.sendMessage("Sadece oyun ici sayin mr thelegend.");
                    return true;
                }
                if (plugin.quests == null) {
                    plugin.getLogger().warning("Quests did not load properly. Check startup log!");
                    return true;
                }
                Player oyuncu=(Player)gonderen;
                if (plugin.quests.getQuester(oyuncu.getUniqueId()) == null) {
                    plugin.getLogger().warning("Unable to find Quester: " + oyuncu.getName() + " " + oyuncu.getUniqueId());
                    return true;
                }
                int questpuan=plugin.quests.getQuester(oyuncu.getUniqueId()).getQuestPoints();
                if (args.length==0) {
                    List<String> liste = null;
                      int xx = Bukkit.getOnlinePlayers().size();
                      String xxx = Integer.toString(xx);
                      for (String hgmsj : liste) {
                          if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null){
                              String avecpapi=PlaceholderAPI.setPlaceholders(oyuncu, hgmsj);
                              gonderen.sendMessage(ChatColor.translateAlternateColorCodes('&', avecpapi.replace("%online%", xxx).replace("%oyuncu%", oyuncu.getName()).replace("%siradakigorev%", questpuan+1+"").replace("%questpuan%", questpuan+"")));
                          } else {
                              gonderen.sendMessage(ChatColor.translateAlternateColorCodes('&', hgmsj.replace("%online%", xxx).replace("%oyuncu%", oyuncu.getName()).replace("%siradakigorev%", questpuan+1+"").replace("%questpuan%", questpuan+"")));
                          }
                      }
                }
                if (args.length==1) {
                    int sayi;
                    try {
                          sayi = Integer.parseInt(args[0]);
                    }
                    catch (Exception e) {
                          gonderen.sendMessage("§8[§3TLGND§8] §7Gecerli bir gorev sayisi girin.");
                          return true;
                    }
                    if (questpuan<sayi) {
                        gonderen.sendMessage("§8[§3TLGND§8] §7Gorev puanınınız bu göreve gitmek için yetersiz.");
                        gonderen.sendMessage("§7Şuan "+questpuan+" §7puana sahipsiniz yani, "+questpuan+"§7. goreve gidebilirsiniz.");
                        gonderen.sendMessage("§7Gorevleri tamamlayarak puan kazanırsınız ve bu şekilde yeni görevler açarsınız.");
                        return true;
                    }
                    World w=Bukkit.getServer().getWorld(plugin.c2.getString(sayi+".w"));
                    float yaw=(float)plugin.c2.getDouble(sayi+".yaw");
                    float pitch=(float)plugin.c2.getDouble(sayi+".pitch");
                    double x=plugin.c2.getDouble(sayi+".x");
                    double y=plugin.c2.getDouble(sayi+".y");
                    double z=plugin.c2.getDouble(sayi+".z");
                    Location loc=new Location(w,x,y,z);
                    loc.setYaw(yaw);
                    oyuncu.getLocation().setPitch(pitch);
                    oyuncu.teleport(loc);
                    oyuncu.sendMessage("§8§l* §7"+sayi+"§7. goreve ışınlandın!");
                    return true;
                }
            }
            return true;
        }
     
    Based on what you're trying to do, I should also mention PlaceholderExpansion-Quests which can get a player's quest points for you instead.
     
  10. My problem is solved, thanks to @PikaMug .