Command is ignoring some checks...

Discussion in 'Plugin Development' started by JBAvodas, Aug 31, 2015.

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

    JBAvodas

    Please understand that I am learning java right now and just telling me to add in something won't help, please give me an example of what you are saying eg, If you say use a loop show me how a loop looks as I will be able to problem solve it that way.
    For some reason the following code which is a command isn't checking what I am asking. It is doing the first If statement:
    Code:
    if (plugin.getConfig().getString("Guild." + guildName) == null) {
    but it is failing to check this line:
    Code:
    if (Guild.contains(sender.getName())) {
                       sender.sendMessage("You are already in another guild!");
                } else {
    Also it will check the first else if statement checking if there is a guild with that name already but it won't tell me I don't have enough gold nuggets.

    Code:
    } else if (!(plugin.getConfig().getString("Guild." + guildName) == null)) {
                        sender.sendMessage(ChatColor.RED + "There is already a Guild with this name!");
                    } else if (!(player.getInventory().containsAtLeast(new ItemStack(Material.GOLD_NUGGET), guildcost))) {
                        sender.sendMessage(
                                ChatColor.RED + "You do not have enough gold nuggets to purchase a guild! It costs 256!");
                    }

    Code:
    if (plugin.getConfig().getString("Guild." + guildName) == null) {
                        if (Guild.contains(sender.getName())) {
                            sender.sendMessage("You are already in another guild!");
                        } else {
                            plugin.getConfig().addDefault("Guild." + guildName, Arrays.asList(Guild));
                            Guild.add(player.getName());
                            plugin.saveConfig();
                            player.getInventory().removeItem(new ItemStack(Material.GOLD_NUGGET, 64));
                            player.getInventory().removeItem(new ItemStack(Material.GOLD_NUGGET, 64));
                            player.getInventory().removeItem(new ItemStack(Material.GOLD_NUGGET, 64));
                            player.getInventory().removeItem(new ItemStack(Material.GOLD_NUGGET, 64));
    
                            sender.sendMessage(ChatColor.GREEN + "You have just created a Guild for 256 Golden Nuggets!");
    
                            Bukkit.broadcastMessage(ChatColor.GOLD + "The guild " + ChatColor.RED + (args[1].toUpperCase())
                                    + ChatColor.GOLD + " has just been created!");
                            plugin.reloadConfig();
                        }
                    } else if (!(plugin.getConfig().getString("Guild." + guildName) == null)) {
                        sender.sendMessage(ChatColor.RED + "There is already a Guild with this name!");
                    } else if (!(player.getInventory().containsAtLeast(new ItemStack(Material.GOLD_NUGGET), guildcost))) {
                        sender.sendMessage(
                                ChatColor.RED + "You do not have enough gold nuggets to purchase a guild! It costs 256!");
                    } else {
                        sender.sendMessage(ChatColor.RED + "[ERROR] Unknown error, Contact administrator!");
                    }
     
  2. Offline

    NullChips

    You need to use return statements, otherwise, the command will still just continue through.

    EDIT: Also, why are you not using HashMaps for the guilds? Why not just have it save into a seperate Guilds.yml file in the onDisable() method, and then load the players and their guilds into a HashMap in the onEnable method?
     
  3. Offline

    JBAvodas

    so I have to return false on every else if? and also the main if @NullChips
     
    Last edited: Aug 31, 2015
  4. Offline

    teej107

    @JBAvodas What is "Guild"? Is is an actual Object or is the #contains() a static method you made?
     
  5. Offline

    JBAvodas

    @teej107 Guild is the name of the Config File
    However it is also the StringList

    Alright so I have added in return statements to everything set the main one to true and the other ones to false, however the commands still are not working, I have changed the position of this else if command
    Code:
    } else if (!player.getInventory().contains(new ItemStack(Material.GOLD_NUGGET), guildcost)) {
                        sender.sendMessage(
                                ChatColor.RED + "You do not have enough gold nuggets to purchase a guild! It costs 256!");
                        return false;
                    } else if (!(plugin.getConfig().getString("Guild." + guildName) == null)) {
                        sender.sendMessage(ChatColor.RED + "There is already a Guild with this name!");
                        return false;
                    }
    To see if setting it so that the order of else commands effect it but it doesn't the guild with this name still works but the check inventory doesn't.
     
    Last edited by a moderator: Sep 1, 2015
  6. Offline

    xMrPoi

    Try adding debug messages through your if statements to see what is and isn't getting a called. Also, is the whole command not getting called or just certain things?
     
  7. Offline

    Oxyorum

    @JBAvodas The issue lies in your use of the contains() method, I think. Look at this. The method says that it will check to see how many ItemStacks that match the ItemStack you supplied are present, and ItemStacks of the same material and amount are what count. Since you are not specifying an amount in that ItemStack of gold nuggets, the amount defaults to 1, so the contains method will try to look for 256 ItemStacks of gold nuggets that are of size 1. There aren't nearly that many slots in an inventory, so that will never be true. xD

    Someone is free to call me an idiot if I'm wrong, by the way.

    I suspect that if you used the contains(Material m, int amount) method instead, that will solve your problem, since that method does not have such strict limitations.

    Tl:dr: Read the docs so you are sure of what everything is doing.
     
  8. Offline

    JBAvodas

    @xMrPoi @Oxyorum As of right now this is my class code:

    Code:
    package com.avairu.guilds;
    
    import java.util.Arrays;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    
    public class GuildCommands implements CommandExecutor, Listener {
    
        Main plugin;
    
        public GuildCommands(Main instance) {
            plugin = instance;
        }
    
        int guildcost = 256;
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
            Player player = (Player) sender;
            if (commandLabel.equalsIgnoreCase("Guild")) {
    
                if (args.length < 1) {
                    sender.sendMessage(ChatColor.RED + "You need to use one of the following:");
    
                    sender.sendMessage(ChatColor.GOLD + "/guild create <name>");
    
                    sender.sendMessage(ChatColor.GOLD + "/guild invite <name>");
    
                    sender.sendMessage(ChatColor.GOLD + "/guild remove <name>");
    
                } else if (args[0].equalsIgnoreCase("create")
                        && player.getInventory().containsAtLeast(new ItemStack(Material.GOLD_NUGGET), guildcost)) {
    
                    String guildName = args[1].toString();
                    List<String> Guild = plugin.getConfig().getStringList("Guild." + guildName);
    
                    if (plugin.getConfig().getString("Guild." + guildName) == null && (!Guild.contains(sender.getName()))) {
                        plugin.getConfig().addDefault("Guild." + guildName, Arrays.asList(Guild));
                        Guild.add(player.getName());
                        plugin.saveConfig();
                        player.getInventory().removeItem(new ItemStack(Material.GOLD_NUGGET, 64));
                        player.getInventory().removeItem(new ItemStack(Material.GOLD_NUGGET, 64));
                        player.getInventory().removeItem(new ItemStack(Material.GOLD_NUGGET, 64));
                        player.getInventory().removeItem(new ItemStack(Material.GOLD_NUGGET, 64));
    
                        sender.sendMessage(ChatColor.GREEN + "You have just created a Guild for 256 Golden Nuggets!");
    
                        Bukkit.broadcastMessage(ChatColor.GOLD + "The guild " + ChatColor.RED + (args[1].toUpperCase())
                                + ChatColor.GOLD + " has just been created!");
                        plugin.reloadConfig();
    
                    } else if (!(player.getInventory().containsAtLeast(new ItemStack(Material.GOLD_NUGGET), guildcost))) {
                        sender.sendMessage(
                                ChatColor.RED + "You do not have enough gold nuggets to purchase a guild! It costs 256!");
                    } else if (!(plugin.getConfig().getString("Guild." + guildName) == null)) {
                        sender.sendMessage(ChatColor.RED + "There is already a Guild with this name!");
    
                    } else if (Guild.contains(player.getName())) {
                        sender.sendMessage(ChatColor.RED + "You are already in a guild!");
                    } else {
                        sender.sendMessage(ChatColor.RED + "[ERROR] Unknown error, Contact administrator!");
    
                    }
                }
                return true;
            }
            return false;
        }
    }
    
    Now for some reason, Only the part that is checking for if the player doesn't have 256 gold nuggets and the You are already in a guild part is not working!
    Prior to this when I had just an else in that said you are already in a guild that had worked but now since the else if statements none of it works. I've been changing all of it constantly to see if I have written something wrong but I get no errors etc and the commands are going through just not replying with any errors.

    The code is also allowing me to make multiple guilds when I am already involved with one which is a serious error but I am unable to find the problem

    Note: The command is checking and removing the 256 with the first containsAtLeast method but not the second.
     
  9. Offline

    Oxyorum

    @JBAvodas You need to rewrite those if statements. Your code will never check to see if there are not enough nuggets because you have that check inside an if statement that only executes if they do have enough. Look below:

    Code:
    if(args.length < 1){
      //do stuff
    }
    else if(args[0].equalsIgnoreCase("create") && player.getInventory().contains(Material.GOLD_NUGGET, guildcost){
      //lets do things if the create command is run, and if the player has 256 nuggets in his inventory
      if(!(player.getInventory().containsAtLeast(new ItemStack(Material.GOLD_NUGGET), guildcost)){
        //lets do things if the player does not have 256 nuggets....what?
      }
    }
    
    Also, you need to have a way to check if the player in question is in other guild that are not the one you are trying to create. Your code is checking if the player is a member of the guild you are trying to create, which either doesn't exist yet, or does exist and doesn't guarantee they aren't a member of another guild.

    Finally, PLEASE check to make sure the sender is a player before you do anything, or your code may throw a ClassCastException.
     
Thread Status:
Not open for further replies.

Share This Page