Solved Listener Class not working properly

Discussion in 'Spigot Plugin Development' started by ShadowMasterG23, Apr 24, 2017.

  1. Hey guys, I'm making a CustomJoin plugin, but I'm having trouble trying getting my code to work properly. My problem lies in the plugins' Listener Class. As you could probably tell, I'm new to Java, so I apologize in advance to those who cringe at my code. However, I'm not a complete newbie. I've developed a plugin that's on Spigot if you want to get a better look at how I code:

    Anyways, I've tried multiple methods from other forum posts and Google searches, but nothing is meeting my requirements. I've left other bits of code that I have tried and they came close to what I wanted, but they weren't quite there. I feel like there is an easier way to do this, but at the same time, it's not so simple. Here's what I have.

    Code (Text):

        public void onPlayerJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();

            if (player.hasPermission("customjoin.join")) {
              //Ended up making a message like this "[ , <join_message> , ]"
                //String mts = configGetter.getConfig().getString("join_message").replace("%name%", player.getName());
                //Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', mts));

              //Had the results I was looking for but fell short because it lacked color codes.
                //configGetter.getConfig().getStringList("join_message").forEach(string -> Bukkit.broadcastMessage(string.replace("%name%", player.getName())));
              //Ended up making a message like this "[ , <join_message> , ]"
                //String mts = configGetter.getConfig().getStringList("join_message").toString().replaceAll("%name%", player.getName());

              //Only produces the first line of the join_message (w/ color codes)
                String mts2 = configGetter.getConfig().getStringList("join_message").iterator().next();
                Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', mts2));
            } else {
    Here is the part of my config.yml that I am dealing with. I want users to have the ability to add and remove lines, use color codes, and have player names show up as well.
    Code (Text):
      - '&8&m--------------------'
      - ''
      - '&c%name% joined the server!'
      - ''
      - '&8&m--------------------'
    I think that a for loop would help, but I've tried a few of those (none of them worked) and I don't really understand those, even with the help of Google. Again, sorry if my code is messy, I'm new to Java. Could someone please explain what I'm doing wrong?

    Just a side note: I've seen people talking about Java versions 7 & 8 in their replies. I'm using Java 8 for this plugin if that helps any.
  2. Sounds like you could use some more brushing up on Java.
  3. Hehe, you're definitely right about that, but do you have any suggestions on how I could fix this? Perhaps a link to a resource? Or maybe something that could help me better understand for loops? :p
  4. For 'for' loops you can do one of these:

    Code (Text):
    for (Object object : someArrayOrListVariable) {
        // code
    Code (Text):
    for (int i = 0; i < 10; i++) {
        // Run code 10 times, can use variable i
    changing 'i < 10' to however many times you want the loop to run.
    #4 iShadey, Apr 24, 2017
    Last edited: Apr 24, 2017
  5. What you need is a loop through the list to translate the colorcodes in every line and replace your placeholder(s). I'll try showing you some code without completely spoon feeding you :p

    Code (Java):
    // get the list from config (add a check for null)
    List<String> yourList = config.getStringList(path);

    // go through each line and do your work with it
    for(int i = 0; i<yourList.size(); i++){
        yourList.set(i, ChatColor.translateAlternateColorCodes('&',yourList.get(i)));
    The above code will translate the ChatColor in each line. I will leave it to you to add the replace functionality :p
    • Agree Agree x 1
  6. Thank you @TrollStar12345 for explaining for loops. I don't know why everywhere else I looked didn't make sense to me, but this is understandable. Maybe I'm more of a newbie than I thought. :p

    Thank you @Nikl for the semi pseudo code. I got the code to work properly thanks to you. :) Although, the only thing that I'm still a little confused about is this section of code:
    Code (Text):
    If I am understanding correctly from TrollStar's explanation, what it does is gets the "join_message", then since "i" is set to zero and therefore less than what is defined in the config, it will run through the code while adding one to "i"'s value. If that's not correct, then oh well, I'll grasp the concept later on, but again, thank you for your help with this.