Find Word in config and replace it

Discussion in 'Plugin Development' started by Jabbers9999, Jan 30, 2016.

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

    Jabbers9999

    So, I am trying to make a CustomJoinMessage Plugin, with the help of config files.
    In my config there is:
    JoinMessage: Welcome to the server %player%
    Just for now, I would like it so it broadcasts it if you do a command, just for now to keep things simple.
    So when it broadcasts, it will change %player% into the players name.
    P.S. I have not made the broadcast script yet
    Code:Java
    1.  
    2. package me.jabbers99.bukkittest3;
    3.  
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.player.PlayerJoinEvent;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Main extends JavaPlugin {
    10. public void onEnable() {
    11. loadConfiguration();
    12. System.out.print("Bukkit Test 3 Enabled!");
    13. }
    14.  
    15. public void loadConfiguration() {
    16. // See "Creating you're defaults"
    17. getConfig().options().copyDefaults(true);
    18. // Save the config whenever you manipulate it
    19. saveConfig();
    20. String path = "JoinMessage";
    21. getConfig().addDefault(path, "Welcome, %player% to the server.");
    22. getConfig().options().copyDefaults(true);
    23. saveConfig();
    24. }
    25. @EventHandler
    26. public void PlayerJoin(PlayerJoinEvent event) {
    27. Player player = event.getPlayer();
    28. event.setJoinMessage(getConfig().getString("JoinMessage").replace("%player%", player.getName()));
    29. }
    30. }
    31.  
    32.  
    33.  
     
    Last edited: Jan 31, 2016
  2. Offline

    teej107

    Use String#replace(). You don't need to check if the String contains the word being replaced. The method will just return the same String if it didn't replace anything.
     
  3. Offline

    Jabbers9999

    So would I do
    Code:
    getConfig().replace(player.getPlayer())
    
     
  4. Offline

    teej107

    @Jabbers9999 No.
    FileConfiguration doesn't have the replace method. String does and it has 2 parameters, not one.
     
  5. Offline

    Coopah

    @Jabbers9999
    If I understand correctly, you're just trying to broadcast a message when a player joins with their name? If this is correct, you're over complicating something simple.

    In the PlayerJoinEvent, you can just get the player and hence their name.

    However, I think you want to be able to manipulate the join message via a configuration file and have the player's name anywhere in the message? Could you explain what you're trying to achieve a little more?
     
  6. Offline

    Jabbers9999

    Yes, I just want to get the player, replaced with player.getPlayer()


    @teej107 look, I have changed me code above. How would I import it from the config file.
     
    Last edited: Jan 31, 2016
  7. Offline

    Coopah

    @Jabbers9999
    In the set message, do config.getString(path); As for the %playername%, you want to create a string which contains what's in the config (config.getString(path);) then use the replace(); method.
     
  8. Offline

    teej107

    If you need help with the config, read this. Once you get the String from the config, just pass the value returned by the replace() method to the setDeathMessage() method.
     
  9. Offline

    Jabbers9999

    @teej107, look, at the updated code. Is that correct? because it does not seem to be working
     
  10. Offline

    JoaoBM

    @Jabbers9999 You didnt implement Listener and also didnt register the events
     
  11. Offline

    Coopah

    @JoaoBM
    Listener is correct, however there is no need to register your events. They're already registered since it's his main class!
     
  12. Offline

    teej107

    No, you still need to register events no matter what class they're in.
     
  13. Offline

    Coopah

    @teej107
    Why? He's already calling the class with the onEnable method. His event will run...
     
  14. Offline

    teej107

    He isn't "calling the class". His class is just overriding the onEnable method. You still need to register events. It doesn't matter where the event listener is.
     
  15. Offline

    Jabbers9999

    Thanks for all the support! Been very busy past few days, but hopefully will get round to doing some more java today.
     
  16. Offline

    boomboompower

    @Jabbers9999
    Put this in onEnable()
    Code:
    Bukkit.getPluginManager().registerEvents(this, this);
     
Thread Status:
Not open for further replies.

Share This Page