Is there a way to...

Discussion in 'Plugin Development' started by DREAM_MACHINE, Mar 19, 2013.

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

    DREAM_MACHINE

    Is it possible to check whats in an array and compare everything to one string?
     
  2. Offline

    Rprrr

    Loops.
     
  3. Offline

    DREAM_MACHINE

    I was using one but i kept getting a NullPointerException on a line a used an if statement with it.
     
  4. Probably because the current element you're comparing is set to null and you can't call methods on null pointers :p just add a var != null check before using methods.
     
    DREAM_MACHINE likes this.
  5. Offline

    DREAM_MACHINE

    Thanks for the help. Also, for some reason if i add multiple quizzes for some reason it executes the first if statement instead of the second(I left a note at the place where they are). Do you know what might be causing this?
    Code:
    Code:
    package me.m277772.BQuiz;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class BQuizMain extends JavaPlugin {
     
        public static BQuizMain plugin;
        public final Logger logger = Logger.getLogger("BQuiz");
        public final String[] quizName = new String[100];
        public final String[] quizQuestion1 = new String[30];
        int quizCounter = 0;
     
     
        @Override
        public void onDisable()
        {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " " + pdfFile.getVersion() + " Has been disabled!");
        }
     
        @Override
        public void onEnable()
        {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " " + pdfFile.getVersion() + " Has been enabled!");
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
            boolean isNumber;
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("bq") || commandLabel.equalsIgnoreCase("basicquiz"))
            {
                if(args.length == 0)
                {
                    player.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "[BasicQuiz] " + ChatColor.GREEN + "Test.");
                }
                if(args.length > 0)
                {
                    if(args[0].equalsIgnoreCase("add"))
                    {
                        if(args.length > 1 && quizCounter < 101 && args.length < 3)
                        {
                            for(int i = 0; i < quizName.length; i++)
                            {
                                String quizNames = quizName[i];
                                if(quizNames == null)
                                {
                                    quizName[quizCounter] = args[1];
                                    quizCounter++;
                                    player.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "[BasicQuiz] " + ChatColor.GREEN + "A quiz named: " + ChatColor.ITALIC + args[1] + ChatColor.GREEN + " has been added.");
                                    break;
                                }else
                                { // Right here. Both of these execute for some reason
                                    if(!(quizNames.equalsIgnoreCase(args[1])))
                                    {
                                        quizName[quizCounter] = args[1];
                                        quizCounter++;
                                        player.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "[BasicQuiz] " + ChatColor.GREEN + "A quiz named: " + ChatColor.ITALIC + args[1] + ChatColor.GREEN + " has been added.");
                                        break;
                                    }
                                    if(quizNames.equalsIgnoreCase(args[1]))
                                    {
                                        player.sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "[BasicQuiz] " + ChatColor.GREEN + "That quiz already exist!");
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if(args[0].equalsIgnoreCase("sq") || args[0].equalsIgnoreCase("setquestion"))
                    {
                        if(args.length > 1)
                        {
                            isNumber = true;
                            try
                            {
                                Integer.parseInt(args[1]);
                            }
                            catch(NumberFormatException e)
                            {
                                isNumber = false;
                            }
                                if(args.length > 2 && isNumber == true)
                                {
                                player.sendMessage(":D");
                                }
                        }
                    }
                }
            }
       
            return false;
        }
    }
    
     
Thread Status:
Not open for further replies.

Share This Page