H. Muhammad
H. Muhammad

Reputation: 3

Minecraft Plugin Prefix (Java)

I just want to create simple prefix plugin for minecraft server,which is shows each player points in chatbox.

The API i used = PlayerPoints & Spigot 1.9.4 shaded. About PlayerPoints API : Click here

As console Show problem is here on PlayerListener.java:

package points.prefix;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.black_ixx.playerpoints.PlayerPoints;


public class PlayerListener implements Listener {

Main plugin;
public PlayerListener(Main instance){
     this.plugin = instance;
}  

public PlayerPoints getPlayerPoints() {
    return getPlayerPoints();
}

//OnPlayer Join
@EventHandler
public void playerjoin(PlayerJoinEvent e){
    Player p = e.getPlayer();
    String pname = p.getName();
    int points = getPlayerPoints().getAPI().look("Player");

    //Begin
    if (p.hasPermission("prefix.point")){
        String member = "" + ChatColor.WHITE + "[" + ChatColor.GREEN + points + ChatColor.WHITE + "]" + ChatColor.RESET + ChatColor.WHITE + pname + ChatColor.RESET + "";
        p.setDisplayName(member);
    }
} }

error log from spigot console:

points.prefix.PlayerListener.getPlayerPoints(PlayerListener.java:19) ~[?:?] [20:57:40]

error log from eclipse:

The method look(String) from the type PlayerPointsAPI is deprecated

Here an other note: In PlayerpointsAPI page mentioned to use:

int balance = playerPoints.getAPI().look("Player");

for showing balance! but it is not working!

Any one know what's wrong?

Thank U.

Upvotes: 0

Views: 1168

Answers (1)

Alpvax
Alpvax

Reputation: 172

In future please let people know what your error is as well as what line it is on. In this case you have a stack overflow error because your getPlayerPoints method calls itself recursively, without ever actually doing anything to break out of the infinite loop!

The page you linked tells you exactly what you are missing. It says "During enable, you need to grab the PlayerPoints plugin instance and save that reference somewhere as you will be using the API through it."

So, use the sample code that they supplied (copied to here for simplicity): copy the two methods into your plugin class (presumeably Main.java in your case) and change the getPlayerPoints() method in your listener to plugin.getPlayerPoints(). Alternatively, you could just make a reference to the playerPoints instance in your constructor instead of having a reference to your plugin, if the plugin isn't used anywhere else in your listener.

private PlayerPoints playerPoints;

/**
 * Validate that we have access to PlayerPoints
 *
 * @return True if we have PlayerPoints, else false.
 */
private boolean hookPlayerPoints() {
    final Plugin plugin = this.getServer().getPluginManager().getPlugin("PlayerPoints");
    playerPoints = PlayerPoints.class.cast(plugin);
    return playerPoints != null; 
}

/**
 * Accessor for other parts of your plugin to retrieve PlayerPoints.
 *
 * @return PlayerPoints plugin instance
 */
public PlayerPoints getPlayerPoints() {
    return playerPoints;
}

Upvotes: 1

Related Questions