Solved For loop not working?

Discussion in 'Plugin Development' started by iFamasssxD, Jul 1, 2013.

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

    iFamasssxD

    So I am trying to make a plugin that equips the kit and does some other things. This is the for loop.
    Code:
    @SuppressWarnings("deprecation")
        public void equipKit(Player player) {   
            EntityType mobTypes = players.get(player.getName());
            String mobType = String.valueOf(mobTypes.getName()).toUpperCase();
            for (String cmd : getConfig().getStringList("Kit." + mobType + ".Commands")) {
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replace("%d", player.getName()));
                player.updateInventory();
            }
           
        }
    This is the code that calls the this method.
    Code:
    plugin.players.put(player.getName(), type);
    plugin.equipKit(player);
    The mobType that i am calling works for other things i am using it for and the config is setup correctly so it should be reading it right.

    Bump?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
  2. Offline

    AmShaegar

    Error, stack trace? What did you expect? What actually happens?
     
  3. Offline

    iFamasssxD

    Nothing happens when I right click the block that is supposed the activate the for loop. It is supposed to run a list of commands in the config and it isnt reading them or reading any commands. There is no errors.
     
  4. Offline

    AmShaegar

    Add some debug messages to see how far it goes, please. Did you register your Listener/Event properly?
     
  5. Offline

    iFamasssxD

    Yes everything is registered everything works except for the for loop in the main class.

    Ive tested it and when the code is like this:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. public void equipKit(Player player) {
    3. EntityType mobTypes = players.get(player.getName());
    4. String mobType = String.valueOf(mobTypes.getName()).toUpperCase();
    5. for (String cmd : getConfig().getStringList("Kit." + mobType + ".Commands")) {
    6. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replace("%d", player.getName()));
    7. player.updateInventory();
    8. player.sendMessage(mobType);
    9. }
    10.  
    11. }

    The message is not sent to the player so it seems the loop isnt working.
    But when the code is like this:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. public void equipKit(Player player) {
    3. EntityType mobTypes = players.get(player.getName());
    4. String mobType = String.valueOf(mobTypes.getName()).toUpperCase();
    5. for (String cmd : getConfig().getStringList("Kit." + mobType + ".Commands")) {
    6. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replace("%d", player.getName()));
    7. player.updateInventory();
    8. }
    9. player.sendMessage(mobType);
    10. }

    The message sends the player with the right name and all.
     
  6. Offline

    AmShaegar

    How does your config look like? Please add the following before the for loop. What is send?

    Code:java
    1. player.sendMessage(getConfig().getStringList("Kit." + mobType + ".Commands").size());
     
  7. Offline

    iFamasssxD

  8. Offline

    AmShaegar

    Well, you are doing it wrong. Let's take this example:
    Code:
    Kit:
      CREEPER:
        Commands: effect &d 11 100000
    You cannot do getStringList("Kit.CREEPER.Commands") because effect &d 11 100000 is not a list but a simple String.

    You need your config to look like this:
    Code:
    Kit:
      CREEPER:
        Commands:
          - effect &d 11 100000
          - command2
          - command3
     
  9. Offline

    iFamasssxD

    Thank you so much! Thats exactly what I needed and you have saved me some more headaches!

    One more thing :p AmShaegar how can I make it replace %d with player.getName()? When I do it in the console it is telling me Player, %d, not found. Current code:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. public void equipKit(Player player) {
    3. EntityType mobTypes = players.get(player.getName());
    4. String mobType = String.valueOf(mobTypes.getName()).toUpperCase();
    5. for (String cmd : getConfig().getStringList("Kit." + mobType + ".Commands")) {
    6. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replace("%d", player.getName()));
    7. player.updateInventory();
    8. }
    9. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
  10. Offline

    AmShaegar

    Well... look at your config. Did you mean &d or %d? :p
     
  11. Offline

    iFamasssxD

    OMG... Thanks so much for putting up with me.
     
  12. Offline

    AmShaegar

    You are welcome.
     
Thread Status:
Not open for further replies.

Share This Page