YAML Bukkit Dev problem

Discussion in 'Plugin Development' started by Azen0xCore, Dec 14, 2015.

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

    Azen0xCore

    Hi !
    I have a problem with my plugin :

    I want this in config.yml :

    MOTD:
    inTimer: '&b&l-&l-&l-&8&l[&l-&l-&r&e&kII&r &4&lBloodTeam &1&lNetwork&r &e&kII&r&8&l-&l-&l]&b&l-&l-&l- &r\n &8>> &aOPEN IN : &b&b%time% &a! &8<<'


    But when I reload the plugin, I have this :

    MOTD:
    inTimer: '&b&l-&l-&l-&8&l[&l-&l-&r&e&kII&r &4&lBloodTeam &1&lNetwork&r &e&kII&r
    &8&l-&l-&l]&b&l-&l-&l- &r\n &8>> &aOPEN IN : &b&b%time% &a! &8<<'


    Between "&e&kII&r" and "&8&l-&l-&l]&b", there is a line return. It destabilize my plugin.

    I don't want this line break, someone can help me ?
     
  2. Offline

    Mrs. bwfctower

  3. Offline

    Zombie_Striker

    @Azen0xCore
    Why not just remove it if it breaks your plugin?
     
  4. Offline

    Azen0xCore

    My code is not very clear but :

    Code:
    package fr.azenox.MOTD;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.server.ServerListPingEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitScheduler;
    
    
    public class MainClass extends JavaPlugin implements Listener {
      
        public int s = getConfig().getInt("Timer.Seconds");
        public int m = getConfig().getInt("Timer.Minutes");
        public int h = getConfig().getInt("Timer.Hours");
        private String motd;
        private boolean inTimer = false;
      
        public String sec;
        public String min;
        public String hou;
      
        @Override
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents(this, this);
          
            final FileConfiguration config = this.getConfig();
            config.options().copyDefaults(true);
          
            if(getConfig().contains("Timer") == false){
                getConfig().set("Timer.Seconds", 00);
                getConfig().set("Timer.Minutes", 00);
                getConfig().set("Timer.Hours", 00);
            }
          
            if(getConfig().contains("MOTD") == false){
                getConfig().set("MOTD.inTimer", "&eIn %timer% !");
            }
          
            saveConfig();
          
            timer();
          
        }
      
        @Override
        public void onDisable() {
            inTimer = false;
        }
      
      
        @EventHandler(priority=EventPriority.HIGHEST)
        public void onPing(final ServerListPingEvent e){
          
          
            if(inTimer == true){
                motd = getConfig().getString("MOTD.inTimer").replaceAll("%timer%", getConfig().getString("Timer.Timer")).replace("&", "§");
                e.setMotd(motd);
            }
          
        }
      
        public void timer(){
          
          
            BukkitScheduler sched = Bukkit.getScheduler();
          
    
            sched.scheduleSyncRepeatingTask(this, new Runnable()
            {
                public void run()
                {
                  
                    if(s <= 0){
                      
                        if(m <= 0){
                          
                            if(h <= 0){
                              
                                inTimer = false;
                              
                            }
                          
                            h = h - 1;
                            m = 60;
    
                            String time = h + ":" + m + ":" + s;
                            getConfig().set("Timer.Timer", time);
                            saveConfig();
                          
                        }
                      
                        m = m - 1;
                        s = 60;
                      
                        String time = h + ":" + m + ":" + s;
                        getConfig().set("Timer.Timer", time);
                        saveConfig();
                    }
    
                    s = s - 1;
                  
                    if(s < 10){
                        sec = "0" + String.valueOf(s);
                    }else if(s >= 10){
                        sec = String.valueOf(s);
                    }
                  
                    if(m < 10){
                        min = "0" + String.valueOf(m);
                    }else if(m >= 10){
                        min = String.valueOf(m);
                    }
                  
                    if(h < 10){
                        hou = "0" + String.valueOf(h);
                    }else if(h >= 10){
                        hou = String.valueOf(h);
                    }
                  
                  
                  
                    String time = hou
                            + ":"
                            + min
                            + ":"
                            + sec;
                    getConfig().set("Timer.Timer", time);
                    saveConfig();
                  
                }
            }
            , 0, 20L);
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
          
            if(Label.equalsIgnoreCase("gotimer") == sender.isOp()){
              
                s = getConfig().getInt("Timer.Seconds");
                m = getConfig().getInt("Timer.Minutes");
                h = getConfig().getInt("Timer.Hours");
              
                inTimer = true;
            }
          
            if(Label.equalsIgnoreCase("stoptimer") == sender.isOp()){
              
                s = getConfig().getInt("Timer.Seconds");
                m = getConfig().getInt("Timer.Minutes");
                h = getConfig().getInt("Timer.Hours");
              
                inTimer = false;
            }
          
          
            return true;
        }
      
    }
     
  5. Offline

    Zombie_Striker

    Keep your package names lowercase.
    Please, Don't call you main class "main", nor have "class" as apart of the name.
    There is a possiblity that the config might have not generated yet (if it was a first-time startup), so this would break your plugin.
    Why does this need to be final?

    This is in the on Disabled. once your plugin is disabled, all processes that plugin was doing would be stopped.
    There is no need to create this variable, since it is only used once.

    It's acceptable now (not really, but most likely you won't change it) to have the names like this, but you should get into the habbit of making each variable name more "Understandable". Someone should be able to look at the name of the variable and know "That stores the hours/minutes"

    Don't use label. Instead, use cmd (or command).getName() to get the command name. cmd.getName() works with aliases.

    I really don't know why you have this line. This is just a huge flaw in the design. If the player is not OP, AND the command is "say", it will STILL RUN, because false = false;

    Return true ONLY when the command has done what you want. If it did not complete successfully, then return false.
     
  6. Offline

    mcdorli

    @Azen0xCore Why do you repeata color codes?
    inTimer: '&b&l-&l-&l-&8&l[&l-&l-&r&e&kII&r &4&lBloodTeam &1&lNetwork&r &e&kII&r&8&l-&l-&l]&b&l-&l-&l- &r\n &8>> &aOPEN IN : &b&b%time% &a! &8<<'
    This could be
    inTimer: '&b&l---&8&l[--&r&e&kII&r &4&lBloodTeam &1&lNetwork&r &e&kII&r&8&l--]&b&l--- &r\n &8>> &aOPEN IN : &b%time% &a! &8<<'
     
  7. Offline

    Azen0xCore


    I updated the code but I doesn't work :
    I cannot put to lowercase the package's name because my project call "MOTD"


    Code:
    package fr.azenox.MOTD;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.server.ServerListPingEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class Main extends JavaPlugin implements Listener {
    
        public int s = getConfig().getInt("Timer.Seconds");
        public int m = getConfig().getInt("Timer.Minutes");
        public int h = getConfig().getInt("Timer.Hours");
        private String motd;
        private boolean inTimer = false;
       
        public String sec;
        public String min;
        public String hou;
       
        @Override
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents(this, this);
           
            FileConfiguration config = this.getConfig();
            config.options().copyDefaults(true);
           
            if(getConfig().contains("Timer") == false){
                getConfig().set("Timer.Seconds", 00);
                getConfig().set("Timer.Minutes", 00);
                getConfig().set("Timer.Hours", 00);
            }
           
            if(getConfig().contains("MOTD") == false){
                getConfig().set("MOTD.inTimer", "&eIn %timer% !");
            }
           
            saveConfig();
           
            timer();
           
        }
       
        @Override
        public void onDisable() {
            inTimer = false;
        }
       
       
        @EventHandler(priority=EventPriority.HIGHEST)
        public void onPing(final ServerListPingEvent e){
           
           
            if(inTimer == true){
                motd = getConfig().getString("MOTD.inTimer").replaceAll("%timer%", getConfig().getString("Timer.Timer")).replace("&", "§");
                e.setMotd(motd);
            }
           
        }
       
        public void timer(){
    
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
            {
                public void run()
                {
                   
                    if(s <= 0){
                       
                        if(m <= 0){
                           
                            if(h <= 0){
                               
                                inTimer = false;
                               
                            }
                           
                            h = h - 1;
                            m = 60;
    
                            String time = h + ":" + m + ":" + s;
                            getConfig().set("Timer.Timer", time);
                            saveConfig();
                           
                        }
                       
                        m = m - 1;
                        s = 60;
                       
                        String time = h + ":" + m + ":" + s;
                        getConfig().set("Timer.Timer", time);
                        saveConfig();
                    }
    
                    s = s - 1;
                   
                    if(s < 10){
                        sec = "0" + String.valueOf(s);
                    }else if(s >= 10){
                        sec = String.valueOf(s);
                    }
                   
                    if(m < 10){
                        min = "0" + String.valueOf(m);
                    }else if(m >= 10){
                        min = String.valueOf(m);
                    }
                   
                    if(h < 10){
                        hou = "0" + String.valueOf(h);
                    }else if(h >= 10){
                        hou = String.valueOf(h);
                    }
                   
                   
                   
                    String time = hou
                            + ":"
                            + min
                            + ":"
                            + sec;
                    getConfig().set("Timer.Timer", time);
                    saveConfig();
                   
                }
            }
            , 0, 20L);
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
           
            if(cmd.getName().equalsIgnoreCase("gotimer") == sender.isOp()){
               
                s = getConfig().getInt("Timer.Seconds");
                m = getConfig().getInt("Timer.Minutes");
                h = getConfig().getInt("Timer.Hours");
               
                inTimer = true;
               
                return true;
            }
           
            if(cmd.getName().equalsIgnoreCase("stoptimer") == sender.isOp()){
               
                s = getConfig().getInt("Timer.Seconds");
                m = getConfig().getInt("Timer.Minutes");
                h = getConfig().getInt("Timer.Hours");
               
                inTimer = false;
               
                return true;
            }
           
           
            return false;
        }
       
    }
     
  8. Offline

    mcdorli

    Then set the packageName to fr.azenox.motd

    errors?
     
  9. Offline

    Azen0xCore

    I rename my projet "motd" and my package "fr.azenox.motd" but It still doesn't work
     
  10. Offline

    mcdorli

    1.: Changing names shouldn't solve problems
    2.: You should use refactoring (provided in eclipse), to rename every reference of it.
    3.: Errors?
     
  11. Offline

    Azen0xCore

    What is refactoring ?
     
  12. Offline

    mcdorli

    It renames the class/variable/field and all of the references of it.
    But again.
    Any errors?
     
  13. Offline

    Azen0xCore

    Errors:

    "Syntax error, annotations are only available if source level is 1.5 or greater" On @Override and @EventHandler
    "The method set(String, Object) in the type MemorySection is not applicable for the arguments (String, int)" On

    FileConfiguration config = this.getConfig();
    config.options().copyDefaults(true);

    if(getConfig().contains("Timer") == false){
    getConfig().set("Timer.Seconds", 0);
    getConfig().set("Timer.Minutes", 0);
    getConfig().set("Timer.Hours", 0);
    }

    if(getConfig().contains("MOTD") == false){
    getConfig().set("MOTD.inTimer", "&eIn %timer% !");
    }

    saveConfig();
     
  14. Offline

    Zombie_Striker

    @Azen0xCore
    What version of Java are you using to make your plugin?
     
  15. Offline

    Azen0xCore

    I use JRE System Library [jre1.8.0_45]
    bukkit-1.8.7-R0.1
     
  16. Offline

    mcdorli

    Check the project settings. You probably use 1.1 őr something.
     
    Zombie_Striker likes this.
  17. Offline

    Azen0xCore

    I have only this one install (jre1.8.0_45). But, if I have 1.1 version, I cannot do other things like Scheduler (I think...)
     
  18. Offline

    mcdorli

    No, I said, that maybe out of accident, you use java 1.x, where x is below 5. Check the project settings.
     
  19. Offline

    Azen0xCore

    Ok that's good, I no longer have errors
     
  20. Offline

    mcdorli

    Mark the thread solved sir.
     
  21. Offline

    Azen0xCore

    But my YAML problem is not resolved...
     
  22. Offline

    mcdorli

    TL;DR what's the problem?
     
  23. Offline

    Azen0xCore

    I want to fill 'inTimer' in my config.yml :
    [​IMG]
    So, I reload the server : /reload

    And my config.yml updates itself :
    [​IMG]

    You can see a break line, that's not that I want because it breaks my plugin
     
  24. Offline

    Mrs. bwfctower

    Is it ever going to refer to another object? No, so why not have it be final?
     
  25. Offline

    Azen0xCore

    Someone have a solution ?
     
  26. Offline

    Zombie_Striker

    @Azen0xCore
    Have you tried using other bits of text? Is it that it is too long? Is it that it has a "bad character"?
     
Thread Status:
Not open for further replies.

Share This Page