Solved find arg in array with string

Discussion in 'Plugin Development' started by Capby, Aug 9, 2016.

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

    Zombie_Striker

    @Capby
    You can send a message to a player, spawn a block, print something in the console. Basically, figure out a way to know if a big of code is being read.
     
  2. Offline

    Capby

    @AlvinB B does have the perm "rankups.rankup", one sec and I'll get the debug results

    Debug Results:
    Using /rankup in A:
    11.08 21:44:53 [Server] INFO Added user to group
    11.08 21:44:53 [Server] INFO Arrays set
    11.08 21:44:53 [Server] INFO Player has perms
    11.08 21:44:53 [Server] INFO Command run
    Using /rankup in B:
    11.08 21:45:39 [Server] INFO Added user to group
    11.08 21:45:39 [Server] INFO Arrays set
    11.08 21:45:39 [Server] INFO Player has perms
    11.08 21:45:39 [Server] INFO Command run
     
    Last edited: Aug 11, 2016
  3. @Capby
    And could you show us the code, so we can see where you put said debug statements?
     
  4. Offline

    Capby

    @AlvinB
    Code:
    package net.minebloxmc.main;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.logging.Logger;
    
    import org.apache.commons.lang.ArrayUtils;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.permission.Permission;
    
    public class Main extends JavaPlugin {
        private static final Logger log = Logger.getLogger("Minecraft");
        public static Economy econ = null;
        public static Permission perms = null;
        public FileConfiguration config = getConfig();
    
        public void onEnable() {
            if (!setupEconomy()) {
                log.severe(String.format("[%s] - Disabled due to no Vault dependency found!",
                        new Object[] { getDescription().getName() }));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
            setupPermissions();
    
            this.config.options().copyDefaults(true);
            saveConfig();
        }
    
        private boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = (Economy) rsp.getProvider();
            return econ != null;
        }
    
        private boolean setupPermissions() {
            RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
            perms = (Permission) rsp.getProvider();
            return perms != null;
        }
    
        public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
            Player player = (Player) sender;
            if (player.getLocation().getWorld().getName().equals(getConfig().getString("World.World"))) {
                if (commandLabel.equalsIgnoreCase("ranks")) {
                    if (perms.has(player, "rankups.seeranks")) {
                        if ((sender instanceof Player)) {
                            player.sendMessage("[" + ChatColor.GOLD + "Ranks" + ChatColor.WHITE + "]");
                            if (args.length == 0) {
                                String[] classes = getConfig().getString("Ranks.Names").split(",");
                                String[] prices = getConfig().getString("Ranks.Prices").split(",");
                                for (int i = 0; i < classes.length; i++) {
                                    sender.sendMessage("[" + ChatColor.GOLD + classes[i] + ChatColor.WHITE + "] "
                                            + ChatColor.BLUE + "This gives you the " + classes[i] + " rank!" + " Price: $"
                                            + prices[i]);
                                }
                            }
                        }
                    } else {
                        sender.sendMessage("[" + ChatColor.GOLD + "Rankups" + ChatColor.WHITE + "]" + ChatColor.RED
                                + "You don't have the correct permissions to use this command! You need rankups.seeranks to use this command!");
                    }
                } else if (command.getLabel().equals("rankup")) {
                    System.out.print("Command run");
                    if (perms.has(player, "rankups.rankup")) {
                        System.out.print("Player has perms");
                        List<String> ranksy = Arrays.asList(getConfig().getString("Ranks.Names").split(","));
                        String[] ranks = new String[ranksy.size()];
                        ranksy.toArray(ranks);
                        System.out.print("Arrays set");
                        perms.playerAddGroup(getConfig().getString("World.World"), player,
                                ranks[ArrayUtils.indexOf(ranks, perms.getPrimaryGroup(player))+1]);
                        System.out.print("Added user to group");
    
                        return true;
                    } else {
                        System.out.print("Player does not have perms");
                        sender.sendMessage("[" + ChatColor.GOLD + "Rankups" + ChatColor.WHITE + "]" + ChatColor.RED
                                + "You don't have the correct permissions to use this command! You need rankups.rankup to use this command!");
                    }
                }
            }
            return false;
        }
    
        public void onDisable() {
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = Logger.getLogger("Minecraft");
    
            logger.info(pdfFile.getName() + " is now disabled (V." + pdfFile.getVersion() + ")");
        }
    }
    
     
  5. @Capby
    What happens if you print out all the entries of the "ranks" array?
     
  6. Offline

    Capby

    @AlvinB This:
    [​IMG]

    Edit: I gotta go, it's late here. Talk to you tomorrow

    @AlvinB I am back now, though might not reply instantly because I'm in school

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.

    Edit2: @AlvinB I am back now, and able to respond instantly
     
    Last edited: Aug 12, 2016
  7. Offline

    Capby

  8. Offline

    Zombie_Striker

    @Capby
    Can you post what the following values [are equal to]:
    1. ArrayUtils.indexOf(ranks, perms.getPrimaryGroup(player))+1 When you are in group A
    2. ArrayUtils.indexOf(ranks, perms.getPrimaryGroup(player))+1 When you are in group B
    3. ranks[ArrayUtils.indexOf(ranks, perms.getPrimaryGroup(player))+1] When you are in group A
    4. ranks[ArrayUtils.indexOf(ranks, perms.getPrimaryGroup(player))+1] When you are in group B
    These values should be the only values that change between the different ranks. This is most likely where your problem is.
     
    Last edited: Aug 13, 2016
  9. Offline

    Capby

    @Zombie_Striker Not sure what you mean? The first sentence doesn't really make much sense, sorry to say.
     
  10. Offline

    Zombie_Striker

    @Capby
    Edited the main post. "are equal to" was left out.

    Heres is what I am looking for:
    1. If #1 and #2 are equal to 0, then it is not finding either group (I.E the string "perms.getPrimaryGroup(player)" is not in the array)
    2. If #3 and #4 are the same, and the index are not the same, then then there are duplicate values in the ranks array.
    [edit]It may be that "indexOf" does not use ".equals" when comparing, but '=='. That would mean you would have to have the instance of the string in order to use that method (meaning you can't create a new string using "B"). If that is the case, (and if the above is not happening), then you would need to do the following:
    1. Create a for int loop. It will start at 0, and will loop up until it reaches the size/length of the array.
    2. If the string at is equal to the new string (using .equals)
      [*]return the string at [i+1]
     
  11. @Zombie_Striker
    Had a quick look at the sources, and the indexOf method does use .equals() (Why would they add a method to support Object arrays if it will almost never work?).
     
    Zombie_Striker likes this.
  12. Offline

    Capby

    @Zombie_Striker I updated the code, it still doesn't work
    Code:
    package net.minebloxmc.main;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.logging.Logger;
    
    import org.apache.commons.lang.ArrayUtils;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.permission.Permission;
    
    public class Main extends JavaPlugin {
        private static final Logger log = Logger.getLogger("Minecraft");
        public static Economy econ = null;
        public static Permission perms = null;
        public FileConfiguration config = getConfig();
    
        public void onEnable() {
            if (!setupEconomy()) {
                log.severe(String.format("[%s] - Disabled due to no Vault dependency found!",
                        new Object[] { getDescription().getName() }));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
            setupPermissions();
    
            this.config.options().copyDefaults(true);
            saveConfig();
        }
    
        private boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = (Economy) rsp.getProvider();
            return econ != null;
        }
    
        private boolean setupPermissions() {
            RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
            perms = (Permission) rsp.getProvider();
            return perms != null;
        }
    
        public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
            Player player = (Player) sender;
            if (player.getLocation().getWorld().getName().equals(getConfig().getString("World.World"))) {
                if (commandLabel.equalsIgnoreCase("ranks")) {
                    if (perms.has(player, "rankups.seeranks")) {
                        if ((sender instanceof Player)) {
                            player.sendMessage("[" + ChatColor.GOLD + "Ranks" + ChatColor.WHITE + "]");
                            if (args.length == 0) {
                                String[] classes = getConfig().getString("Ranks.Names").split(",");
                                String[] prices = getConfig().getString("Ranks.Prices").split(",");
                                for (int i = 0; i < classes.length; i++) {
                                    sender.sendMessage("[" + ChatColor.GOLD + classes[i] + ChatColor.WHITE + "] "
                                            + ChatColor.BLUE + "This gives you the " + classes[i] + " rank!" + " Price: $"
                                            + prices[i]);
                                }
                            }
                        }
                    } else {
                        sender.sendMessage("[" + ChatColor.GOLD + "Rankups" + ChatColor.WHITE + "]" + ChatColor.RED
                                + "You don't have the correct permissions to use this command! You need rankups.seeranks to use this command!");
                    }
                } else if (command.getLabel().equals("rankup")) {
                    System.out.print("Command run");
                    if (perms.has(player, "rankups.rankup")) {
                        if (player.getLocation().getWorld().getName().equals(getConfig().getString("World.World"))) {
                            System.out.print("Player has perms");
                            List<String> ranksy = Arrays.asList(getConfig().getString("Ranks.Names").split(","));
                            String[] ranks = new String[ranksy.size()];
                            ranksy.toArray(ranks);
                            System.out.print("Arrays set");
                            for (int i = 0; i < ranks.length; i++) {
                                if (perms.getPrimaryGroup(player).equals(ranksy)) {
                                    perms.playerAddGroup(getConfig().getString("World.World"), player,
                                            ranks[ArrayUtils.indexOf(ranks, perms.getPrimaryGroup(player)) + 1]);
                                    System.out.print("Added user to group");
                                    player.sendMessage("" + ranksy);
                                }
                            }
                        } else {
                            player.sendMessage("Wrong world!");
                        }
                        return true;
                    } else {
                        System.out.print("Player does not have perms");
                        sender.sendMessage("[" + ChatColor.GOLD + "Rankups" + ChatColor.WHITE + "]" + ChatColor.RED
                                + "You don't have the correct permissions to use this command! You need rankups.rankup to use this command!");
                    }
                }
            }
            return false;
        }
    
        public void onDisable() {
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = Logger.getLogger("Minecraft");
    
            logger.info(pdfFile.getName() + " is now disabled (V." + pdfFile.getVersion() + ")");
        }
    }
    
     
    Last edited: Aug 14, 2016
  13. Offline

    Capby

  14. @Capby
    Post the answers to the questions @Zombie_Striker asked above, your problem is most likely somewhere in there.
     
  15. Offline

    Capby

    @AlvinB I don't know what he meant
     
  16. @Capby
    Print out said values to console, then copy paste it to here.
     
  17. Offline

    Capby

    @AlvinB Yes, I know that, but which values?
     
  18. @Capby
     
  19. Offline

    Capby

    @AlvinB They aren't equal to anything, all they do is rank you up
     
  20. @Capby
    Print out the mentioned statements, I promise you, they are equal to something.
     
  21. Offline

    Capby

    @AlvinB Never mind, I figured it out... It's a little more inefficient than this way though, but I'd rather finish this now than wait about 2 weeks until I figure it out.
     
Thread Status:
Not open for further replies.

Share This Page