Solved Can't squash this intialization

Discussion in 'Plugin Development' started by RealTophatTom, Nov 28, 2019.

  1. Offline

    RealTophatTom

    So for the past good hour or two today I have been trying to fix this plugin of mine that I have been working on as practice and I just can't seem to figure out why when I go to String message = message.replaceAll("%", "%%"); it just gives the error
    "The local variable message may not have been initialized". I have tried almost every way of inserting the code and just can't get it to initialize. So this is my last resort here. This is what I have after giving up. Feel free to give any suggestions and please do not spoon feed me this, just give me a simple or complicated explanation.

    Code:
    package me.RealTophatTom.countrytags.listener;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    
    import me.RealTophatTom.countrytags.Main;
    import me.RealTophatTom.countrytags.utils.Utils;
    
    public class CountryChatListener implements Listener {
       
        boolean check = false;
       
        private static Main plugin;
       
        public CountryChatListener (Main plugin) {
           
            this.plugin = plugin;
           
            Bukkit.getPluginManager().registerEvents(this, plugin);
           
        }
       
        @EventHandler
        public void CountryTags(AsyncPlayerChatEvent e) {
           
            Player p = e.getPlayer();
           
            if (check == false) {
           
                if (p.hasPlayedBefore()) {
                   
                    return;
                   
                } else {
                   
                    for (String words : plugin.getConfig().getStringList("countries")) {
                   
                        if(e.getMessage().contains(words)) {
                           
                            e.setCancelled(true);
                           
                            p.sendMessage(Utils.chat("&8[&4CA&8] &fCountry Tag Given!"));
                           
                            check = true;
                        }
                    }
                   
                    String message; <-- my last resort after trying all placements and even making boolean & the public void static.
                   
                    e.setFormat(p.getDisplayName() + ": " + message.replaceAll("%", "%%") + e.getMessage()); <-- my error
                   
                }
            } else {
               
                return;
               
            }
        }
       
    }
     
  2. Offline

    timtower Moderator Moderator

  3. Offline

    RealTophatTom

    Can't see what you mean here. All I am really trying to do here is actually get it to initialize properly instead of telling me "The local variable message may not have been initialized".
     
  4. Offline

    Strahan

    Also a suggestion on your logic structure. If you know you are going to return on a condition, just return. There is no need for else and you can just continue on a negative match in your loop. Also boolean doesn't need to be checked with "== (value)". e.g. instead of:
    Code:
    @EventHandler
    public void CountryTags(AsyncPlayerChatEvent e) {
        Player p = e.getPlayer();
        if (check == false) {
            if (p.hasPlayedBefore()) {
                return;
            } else {
                for (String words : plugin.getConfig().getStringList("countries")) {
                    if(e.getMessage().contains(words)) {
                        e.setCancelled(true);
                        p.sendMessage(Utils.chat("&8[&4CA&8] &fCountry Tag Given!"));
                        check = true;
                    }
                }
                String message; <-- my last resort after trying all placements and even making boolean & the public void static.
                e.setFormat(p.getDisplayName() + ": " + message.replaceAll("%", "%%") + e.getMessage()); <-- my error
            }
        } else {
            return;
        }
    }
    ...I'd do:
    Code:
    @EventHandler
    public void CountryTags(AsyncPlayerChatEvent e) {
        Player p = e.getPlayer();
        if (!check) return;
        if (p.hasPlayedBefore()) return;
    
        for (String words : plugin.getConfig().getStringList("countries")) {
            if (!e.getMessage().contains(words)) continue;
    
            e.setCancelled(true);
            p.sendMessage(Utils.chat("&8[&4CA&8] &fCountry Tag Given!"));
            check = true;
        }
        String message; <-- my last resort after trying all placements and even making boolean & the public void static.
        e.setFormat(p.getDisplayName() + ": " + message.replaceAll("%", "%%") + e.getMessage()); <-- my error
    }
    Much less indentation, which IMO improves readability.
     
  5. Offline

    RealTophatTom

    I prefer my spacing as I only read my scripts but I thank you for the logic structure fix.
     
  6. Offline

    timtower Moderator Moderator

  7. Offline

    RealTophatTom

    Can you give a quote to what you were referencing here?
     
  8. Offline

    timtower Moderator Moderator

    You are missing that, so can't quote anything.
    You declare message, but you are not initialization it.
     
  9. Offline

    RealTophatTom

    I tried initializing it every way eclipse gave me. I even resorted to old threads on here to find a fix for it but nothing seemed to work, (tried the way they initialized it). Literally at this point I just need somebody to reference WHERE not HOW to initialize it and this should be solved.
     
  10. Offline

    timtower Moderator Moderator

    Before you try to replace stuff in it.
     
  11. Offline

    robertlit

    Initialize means give it a value. You can't replace something from a string that has no value.
    If you can't initialize variables without help from your ide, you should learn Java.
     
    Strahan likes this.
  12. Offline

    yPedx

  13. Offline

    Strahan

    This. You may read this and think "what a jerk, that doesn't help me" but it really does. Trying to make a plugin without at least understanding the language is a recipe for headaches and forum clutter. Your life will be SO much easier if you want to work on plugins if you start already knowing basic Java.
     
  14. Offline

    RealTophatTom

    Alright so this is to all 3. According to the link I read, String message = message.replaceAll("%", "%%"); should work and it didn't when I tried it before making this post. But it errors at (message).replaceAll("%", "%%"); () is where it errors. I do know basic Java I just haven't had eclipse spit these errors out ever when trying to do this exact thing.

    And yes I tried initializing it at the top, I tried creating content for it with the "fixes" eclipse gave me. I tried what that link said, it still spits it may not have been initialized.

    Like I said before, making the class static seems to fix it, but break my boolean values. That's why I came here.

    Update again, String message = ChatColor.translateAlternateColorCodes('&', getConfig().getString("chat-message").replace("%", "%%")); works with the String message = but message.replaceAll("%", "%%"); doesn't
     
    Last edited: Nov 29, 2019
  15. Offline

    yPedx

    Code:
    String message = message.replaceAll("%", "%%");
    This does not work because you're trying to set the value of message to itself. But message never contained any value.

    Let me try to explain;
    Code:
    String message = "Hello world!";        // Here we are INITIALIZING the variable message with the value "Hello world!".
    message = message.replace("world!", "pranked");        // Here we are setting the value of MESSAGE to itself just with some replaced text. The reason this works is because message already has a value!
    
    player.sendMessage(message);
    In your case, just use
    Code:
    String message = ChatColor.translateAlternateColorCodes('&', getConfig().getString("chat-message").replace("%", "%%"));
    
    player.sendMessage(message);
     
    RealTophatTom likes this.
  16. Offline

    RealTophatTom

    I am never trying to send a player a message though. I am trying to set the message formatting. But thank you for explaining the first part. This is solved now :3
     

Share This Page