Getting the value of a string in config.yml

Discussion in 'Plugin Development' started by Erwy, Sep 22, 2018.

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

    Erwy

    Hi everyone, I was trying to do some stuff inside an if statement just when the value of "open" (in the config.yml) is "yes".
    The code without the if statement should be alright, I have already tried to run it alone.
    The problem is that when I run the code with the if statement, it doesn't work (no errors, just doesn't do anything.)

    Code:
    int conf = getConfig().getInt("open");
    
    if (cmd.getName().equalsIgnoreCase("giveaway")) {
        if (conf == 1) {
                    getServer().broadcastMessage(prefix + ChatColor.BLUE + p.getName() + ChatColor.GRAY + " has entered the giveaway! Type " +
                            ChatColor.BLUE + "/giveaway " + ChatColor.GRAY + "to enter!");
                           
                            players.add(p.getName());
                            saveConfig();
    
     
  2. Online

    timtower Administrator Administrator Moderator

    @Erwy Yes is not an int.
    Try getBoolean with true or false.
     
  3. Offline

    Erwy

    I'm sorry I explained this wrong, I meant that I tried with Strings (yes and no), Ints (1, 0) and booleans but still doesn't work.
     
  4. Online

    timtower Administrator Administrator Moderator

    @Erwy What does the config in the plugins folder contain then?
     
  5. Offline

    Erwy

    The config.yml just contains "open: 0", the fact is that I don't know how to check if that's a "0" or a "1"
     
  6. Online

    timtower Administrator Administrator Moderator

    @Erwy Please post your full class
     
  7. Offline

    Erwy

    Ok, I might have figured it out. Basically, there's something in the code that sets the "open" and "prize" strings to respectively "yes" and "a NFA account", but I can't find what does that. Here's my code, if it can help:


    Code:
    package me.erwy.zaliumgive;
    
    
    import java.util.ArrayList;
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class Main extends JavaPlugin implements Listener {
        String prefix = ChatColor.GRAY + "[" + ChatColor.BLUE + "Zalium" + ChatColor.GRAY + "Giveaway] ";
        ArrayList<String> players = new ArrayList<>();
        Random random = new Random();
    
       
        public void onEnable() {
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "ZaliumGiveaways by Erwy has been enabled!");
            loadConfig();
            saveConfig();
        }
       
        public void onDisable() {
            saveConfig();
        }
       
        public void loadConfig() {
            getConfig().options().copyDefaults(true);
        }
       
        @EventHandler
        public void onQuit(PlayerQuitEvent e) {
            Player disco = e.getPlayer();
            String namedisco = disco.getName();
            players.remove(namedisco);
            }
    
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    
           
            if (!(sender instanceof Player)) {
                sender.sendMessage("You must be a player in order to perform this command!");
            }
           
            Player p = (Player) sender;
           
            if (cmd.getName().equalsIgnoreCase("giveaway")) {
                if (getConfig().getString("open").equalsIgnoreCase("yes")) {
                    if (!(players.contains(p.getName()))) {
                        getServer().broadcastMessage(prefix + ChatColor.BLUE + p.getName() + ChatColor.GRAY + " has entered the giveaway! Type " +
                                ChatColor.BLUE + "/giveaway " + ChatColor.GRAY + "to enter!");
                               
                            players.add(p.getName());   
                           
                    }            
                    else if (players.contains(p.getName())) {
                        p.sendMessage(prefix + ChatColor.RED + "You have already participated in this giveaway!");
                }
               
                }
                else {
                    p.sendMessage(prefix + ChatColor.RED + " There's no active giveaway right now");
           
                }
               
            }
               
    
           
           
            if (cmd.getName().equalsIgnoreCase("giveresult")) {
    
                if (p.hasPermission("zalium.result")) {
                    if (getConfig().getString("open").equalsIgnoreCase("yes")) {
                        @SuppressWarnings("deprecation")
                        Player randomPlayer = Bukkit.getPlayer(players.get(random.nextInt(players.size())));
                        String prize = getConfig().getString("prize");
                        if (randomPlayer.isOnline()) {
                            getServer().broadcastMessage(prefix + ChatColor.GRAY + "Congrats " + ChatColor.BLUE + randomPlayer.getName() + ChatColor.GRAY + ", you just won " + ChatColor.BLUE + prize + ChatColor.GRAY + "!");
                            saveConfig();
                            players.clear();
                        }
                        else if (!(randomPlayer.isOnline())) {
                            getServer().broadcastMessage(ChatColor.RED + randomPlayer.getName() + " disconnected, extracting again");
                            }
                    } else {
                        p.sendMessage(prefix + ChatColor.GRAY + "There's no active giveaway right now.");
                    }
    
                       
                }
                else if (!(p.hasPermission("zalium.result"))) {
                        p.sendMessage(ChatColor.RED + "I'm sorry, you can't perform this command!");
                }
               
               
    
               
            }
            return true;
         }
    }
     
  8. Online

    timtower Administrator Administrator Moderator

    @Erwy I am gonna say loadConfig in this casem
     
Thread Status:
Not open for further replies.

Share This Page