{name}

Discussion in 'Plugin Development' started by __ImTheMoney__, Sep 26, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    __ImTheMoney__

    how to make it so that if one would put {name} into the config it will be seen by the plugin as p.getName() so like in the essentials config {PlayerName} but then with {name}
     
  2. Offline

    The_Doctor_123

    myString.replaceAll("{name}", p.getName());
     
    amhokies likes this.
  3. Offline

    ZeusAllMighty11

    It would depend on what exactly you were doing..

    Example:

    Code:
    @EventHandler
    public void onJoin(PlayerJoinEvent e){
        Player p = e.getPlayer();
        Bukkit.broadcastMessage(getConfig().getString("join-message").replace("&", "ยง").replace("{player}", p.getName());
    }
     
  4. Offline

    __ImTheMoney__

    messages like join quit death etc
     
  5. Offline

    amhokies

    I'm going to make a really short example of how this should go.

    Here is the config:
    Code:
    messages:
      join: {player} joined the server!
      death: Unfortunately, {player} is no longer with us.
    Here is the main class:
    Code:java
    1.  
    2. public class MyPlugin extends JavaPlugin
    3. {
    4. public void onEnable()
    5. {
    6. getServer().getPluginManager().registerEvents(new EventListener(this), this);
    7. }
    8. }

    Here is the listener class:
    Code:java
    1.  
    2. public class EventListener implements Listener
    3. {
    4. private MyPlugin plugin;
    5.  
    6. public EventListener(MyPlugin instance)
    7. {
    8. plugin = instance;
    9. }
    10.  
    11. @EventHandler
    12. public void onJoin(PlayerJoinEvent event)
    13. {
    14. String joinMsg = plugin.getConfig().getString("messages.join");
    15.  
    16. event.setJoinMessage(joinMsg.replace("{player}", event.getPlayer().getName()));
    17. }
    18.  
    19. @EventHandler
    20. public void onDeath(PlayerDeathEvent event)
    21. {
    22. String deathMsg = plugin.getConfig().getString("messages.death");
    23.  
    24. event.setDeathMessage(deathMsg.replace("{player}", event.getEntity().getName()));
    25. }
    26. }


    Basically you're getting the String from the config, then replacing every instance of {player} with the player's name that is involved in the event. I haven't tested this out, but it should work.
     
Thread Status:
Not open for further replies.

Share This Page