Solved "If" problem

Discussion in 'Plugin Development' started by StellarFX, Feb 18, 2019.

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

    StellarFX

    Hello,

    Before starting, I want to say that I'm french, so maybe my thread will have some spelling mistakes.

    I don't understand why my code doesn't want to work. It only works in one way.
    Here is my code :

    Code:
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
           
            Player player = e.getPlayer();
           
            ScoreboardWrapper sc_wrapper = new ScoreboardWrapper(this.getConfig().getString("scoreboard.title").replace("&", "§"));
           
           
            if(this.getConfig().getString("scoreboard.lines.1").length() == 0 ) { // THIS ONE WORKS PERFECTLY
                player.sendMessage(prefix + "Ligne 1 absente.");
            }
           
            if(this.getConfig().getString("scoreboard.lines.1").length() >= 1 ) { // CODE THAT DOESN'T WORK
                sc_wrapper.setLine(1, this.getConfig().getString("scoreboard.lines.1").toString()); //
                player.sendMessage(prefix + "Ligne 1 présente."); //
            } // END OF THE CODE THAT DOESN'T WORK
           
            player.setScoreboard(sc_wrapper.getScoreboard());
           
            }
        public void loadConfiguration() {
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
        }
    So if the line "1" in my config.yml is empty, it shows "Ligne 1 absente.", but if the line "1" is not empty then it doesn't do anything.

    Thanks for reading,
    Have a nice day !
     
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    StellarFX

    Else doesn't work too. I tried :/
     
  4. Online

    timtower Administrator Administrator Moderator

    Why don't you use getConfig().contains(path) instead?
     
  5. Offline

    StellarFX

    I just figured out that there was an error (if it can help) :

    Code:
    Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
            at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_202]
            at java.util.ArrayList.get(Unknown Source) ~[?:1.8.0_202]
            at me.johnnykpl.scoreboardwrapper.ScoreboardWrapper.setLine(ScoreboardWrapper.java:77) ~[?:?]
            at fr.stellarfx.test.Main.onJoin(Main.java:51) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_202]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_202]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_202]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_202]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.12.2.jar:git-Spigot-e8ded36-acbc348]
            ... 14 more
    I don't understand from where is the error..

    PS : Tried "getConfig().contains("scoreboard.lines.1"), doesn't work too.

    EDIT : It comes from "getConfig().contains("scoreboard.lines.1"), the first line that worked doesn't even work anymore. I put it back to my first method.
     
  6. Online

    timtower Administrator Administrator Moderator

    @StellarFX
    sc_wrapper.setLine(
    Post that method.
    And always check your console.
     
  7. Offline

    StellarFX

    As I said, it came from "getConfig().contains("scoreboard.lines.1")", so I put it back to the first version.
    And here is the method (This API is not mine) :

    Code:
       
        public void setLine(int index, String line) {
            if (index < 0 || index >= MAX_LINES)
                throw new IndexOutOfBoundsException("The index cannot be negative or higher than 15.");
            String oldModified = modifies.get(index);
            scoreboard.resetScores(oldModified);
            String modified = getLineCoded(line);
            modifies.set(index, modified);
            objective.getScore(modified).setScore(-(index + 1));
        }
    
    EDIT : Modified code
     
    Last edited: Feb 18, 2019
  8. Online

    timtower Administrator Administrator Moderator

    @StellarFX That is addLine, not setLine.
    java.lang.IndexOutOfBoundsException Does error does not come from getConfig().contains("scoreboard.lines.1") though.
    at me.johnnykpl.scoreboardwrapper.ScoreboardWrapper.setLine(ScoreboardWrapper.java:77) ~[?:?]
     
  9. Offline

    StellarFX

    Oh yes my bad, I just edited.
     
  10. Online

    timtower Administrator Administrator Moderator

    @StellarFX Make sure that you have a line in there first, then you can start to change it.
     
  11. Offline

    StellarFX

    Omg thank you, it was so logic. Now it works.
    Passage of the thread to "Solved".
     
Thread Status:
Not open for further replies.

Share This Page