Error in the cicle For

Discussion in 'Plugin Development' started by Ancetras, Feb 10, 2012.

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

    Ancetras

    Hi everyone!

    I'm trying to create a simple plugin and I get some "error" in the cicle for.

    Here my code:
    Code:
    public class Settings {
        public static File file;
        public static Configuration config;
        public static MyPlugin plugin;
     
        public static String itemsSetting = "340:5,381:1,348:10";
        public static List<Integer> itemsID = new ArrayList<Integer>();
        public static List<Integer> itemsAmount = new ArrayList<Integer>();
        public static Recipe finalRecipe = new Recipe();
     
     
    public static void loadRecipes(){
            String text;
            String[] split;
     
            plugin.log.info("[MyPlugin] Implementing Materials ...");
            text = itemsSetting;
            split = text.split("[,]");
     
            for (int i = 0; i < split.length; i++) {
     
                if (split[i].contains(":")) {
     
                    String[] item = split[i].split("[:]");
     
                    if (((item[0].matches("[0-9]*") && item[1].matches("[0-9]*")))){
     
                        for (int u = 1; u <= Integer.parseInt(item[0]); u++) {
                             itemsID.add(Integer.valueOf(item[0]));
                        }
     
                        for (int u = 1; u <= Integer.parseInt(item[1]); u++) {
                            itemsAmount.add(Integer.valueOf(item[1]));
                        }
                    }
                }
            }
           
            for (int i = 0; i < itemsID.size(); i++) {
                Material material = Material.getMaterial(itemsID.get(i));
                int quantity = 1;
                try {
                    quantity = (itemsAmount.get(i)).intValue();
                } catch (Exception e) {
                    e.printStackTrace();
                    plugin.log.info("[MyPlugin] ERROR: Invalid Materials Amount!");
                }
                finalRecipe.materialQuantities.put(material, Integer.valueOf(quantity));
            }
           
            plugin.log.info("[MyPlugin] Implementation complete.");
     
        }
    }
    When i start my private server with this code the console log keep giving infinite strings that i can't understand. They are too fast ... Maybe the strings are the info message and then the cicle for is infinite.

    I just want to add all the items i write in the string itemsSetting and the amount of each items in two separated arrays.

    Can someone help me? Maybe by editing this code i wrote T.T
     
  2. Why do you do:
    Code:
    split = text.split("[,]");
    ?
    shouldn't it at least be
    Code:
    split = text.split(",");
    ?

    Second:
    Why do you do
    Code:
    Integer.valueOf(quantity)
    where quantity is already an int :confused:?
     
  3. Offline

    Lolmewn

    You can't split at a [ or ], since they are Special Characters in Java. I recommend splitting at , ; : <-- one of these 3.
     
  4. Offline

    Njol

    Because that's more efficient.
    You can, as they are not "special Java chars", but Regex syntax chars. You'll have to escape them if you want to use them (\\[ or \\] respectively, same with .+*?(){} and more, depending on where they occur in the regex.)

    Post at least one of the errors please. You can find them in you server.log or you can copy them directly from the console.

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

    Ancetras

    Ok ... i found the errors so i post you:

    Code:
    2012-02-11 02:44:26 [INFO] [MyPlugin] Implementing Materials ...
    2012-02-11 02:44:26 [SEVERE] java.lang.IndexOutOfBoundsException: Index: 16, Size: 16
    2012-02-11 02:44:26 [SEVERE]    at java.util.ArrayList.rangeCheck(Unknown Source)
    2012-02-11 02:44:26 [SEVERE]    at java.util.ArrayList.get(Unknown Source)
    2012-02-11 02:44:26 [SEVERE]    at ancetras.main.configs.Settings.loadRecipes(Settings.java:156)
    2012-02-11 02:44:26 [SEVERE]    at ancetras.main.MyPlugin.onEnable(MyPlugin.java:52)
    2012-02-11 02:44:26 [SEVERE]    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:231)
    2012-02-11 02:44:26 [SEVERE]    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:1057)
    2012-02-11 02:44:26 [SEVERE]    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:379)
    2012-02-11 02:44:26 [SEVERE]    at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:191)
    2012-02-11 02:44:26 [SEVERE]    at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:174)
    2012-02-11 02:44:26 [SEVERE]    at net.minecraft.server.MinecraftServer.t(MinecraftServer.java:357)
    2012-02-11 02:44:26 [SEVERE]    at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:344)
    2012-02-11 02:44:26 [SEVERE]    at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:175)
    2012-02-11 02:44:26 [SEVERE]    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:408)
    2012-02-11 02:44:26 [SEVERE]    at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
    2012-02-11 02:44:26 [INFO] [MyPlugin] ERROR: Invalid Materials Amount!
    By the way, he doesn't recognize this string of code:
    Code:
    quantity = (itemsAmount.get(i)).intValue();

    EDIT:

    When I change the code like this:

    Code:
    public class Settings {
        public static File file;
        public static Configuration config;
        public static MyPlugin plugin;
     
        public static String itemsSetting = "340:5,381:1,348:10";
        public static List<Integer> itemsID = new ArrayList<Integer>();
        public static List<Integer> itemsAmount = new ArrayList<Integer>();
        public static Recipe finalRecipe = new Recipe();
     
     
    public static void loadRecipes(){
            String text;
            String[] split;
     
            plugin.log.info("[MyPlugin] Implementing Materials ...");
            text = itemsSetting;
            split = text.split(",");
     
            for (int i = 0; i < split.length; i++) {
     
                if (split[i].contains(":")) {
     
                    String[] item = split[i].split(":");
     
                    if (((item[0].matches("[0-9]*") && item[1].matches("[0-9]*")))){
     
                        for (int u = 1; u <= Integer.parseInt(item[0]); u++) {
                            itemsID.add(Integer.valueOf(item[0]));
                        }
     
                        for (int u = 1; u <= Integer.parseInt(item[1]); u++) {
                            itemsAmount.add(Integer.valueOf(item[1]));
                        }
                    }
                }
            }
     
            for (int i = 0; i < itemsID.size(); i++) {
                Material material = Material.getMaterial(itemsID.get(i));
                int quantity = 1;
                try {
                      for (int u = 0; u < itemsAmount.size(); u++) {
                        quantity = (itemsAmount.get(u)).intValue();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    plugin.log.info("[MyPlugin] ERROR: Invalid Materials Amount!");
                }
                finalRecipe.materialQuantities.put(material, Integer.valueOf(quantity));
            }
     
            plugin.log.info("[MyPlugin] Implementation complete.");
     
        }
    }
    it works without any error, but all the items have the same amount! >.<
     
  6. Offline

    Njol

    I think this part of the code causes the problems:
    Code:java
    1.  
    2. for (int u = 1; u <= Integer.parseInt(item[0]); u++) {
    3. itemsID.add(Integer.valueOf(item[0]));
    4. }
    5.  
    6. for (int u = 1; u <= Integer.parseInt(item[1]); u++) {
    7. itemsAmount.add(Integer.valueOf(item[1]));
    8. }
    9.  

    If an item e.g. has ID 100 and amount 16, it adds the item 100 times and the amount 16 times, but you only want to add the item & amount ONCE each:
    Code:java
    1.  
    2. itemsID.add(Integer.valueOf(item[0]));
    3. itemsAmount.add(Integer.valueOf(item[1]));
    4.  
     
  7. Offline

    Ancetras

    I didn't think about that ...

    ç_ç now it works
    Thank you very much ç_ç
     
Thread Status:
Not open for further replies.

Share This Page