Solved Help Please

Discussion in 'Plugin Development' started by IcyRelic, Oct 21, 2012.

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

    IcyRelic

    how do i give players a list of items LIKE a kit

    here is what i have but its not working



    Code:
        public static void giveItems(Player p, List<?> items){
            int x = 0;
            while(x < items.size()){
             
                String ids = items.get(x).toString();
                String[] idss = ids.split(":");
                int id = Integer.parseInt(idss[0]);
                int n = Integer.parseInt(idss[1]);
             
                p.getInventory().addItem(new ItemStack(id, n));
             
             
            }
        }
    here is how i call it

    Code:
            List<?> items = getConfig().getList("Rewards.Items.List");
            Methods.giveItems(Bukkit.getPlayer("IcyRelic"), items);
    it doesnt even give an error just crashes server
     
  2. Offline

    travja

    Use List<ItemStack> and use a foreach for adding the items.
     
  3. Offline

    IcyRelic

    what? the list looks like this
    Rewards
    Items
    List:
    - 1:5
    - 10:64

    <itemid>:<amount>
     
  4. Offline

    nathanaelps

    Icyrelic

    Here's what I'd try. It seemed to work for me, but I haven't tested it really well.
    Also, I changed a few of the variables for my own ease of use.

    Code:
    if(this.getConfig().isList("Rewards.Items.List")) { //Are you crashing because the config doesn't look right?
        List<String> items = this.getConfig().getStringList("Rewards.Items.List"); //Let's get the list as a List<String>
        giveItems(server.getPlayer("IcyRelic"), items); //I set server = this.getServer() earlier.
    } else {
        log("getConfig failed."); //make your own log() function
    }
    
    Code:
    public void giveItems(Player player, List<String> items){
        for(String item: items){ //Handy format for reading through a list.
            String[] parsedItem = item.split(":"); //Here on out is just your code, I altered the variables to read it easier (for me)
            int id = 0;
            int count = 0;
            try{ //Maybe you're not actually able to parse the integers?
                id = Integer.parseInt(parsedItem[0]);
            } catch (NumberFormatException e) {
                log("Error! tried to parse "+parsedItem[0]+" as an int!");
            }
            try{
                count = Integer.parseInt(parsedItem[1]);
            } catch (NumberFormatException e) {
                log("Error! tried to parse "+parsedItem[1]+" as an int!");
            }
            if(id>0 && count>0) { //Maybe you have an unfortunate 0 in the config?
                player.getInventory().addItem(new ItemStack(id, count));
            } else {
                log("Could not give player item.");
            }
        }
    }
    If that doesn't work, it should at least let you know what's happening.
     
  5. Offline

    travja

    Use for(List<String> list: config.getStringList("list directory")){
    String[] name = list.split(":");
    then
    List<ItemStack> items = List<ItemStack>();
    then
    items.add(new ItemStack(name[0], name[1]);
    }

    That's how I would do it.
     
  6. Offline

    the_merciless

    Your not changing the value of x, its just repeating over and over again.

    Try changing this...
    while(x < items.size())
    to...
    while(x < items.size(); x++)
     
  7. Offline

    IcyRelic

    java.lang.ArrayIndexOutOfBoundsException: 1
    at me.icyrelic.com.Methods.giveItems(Methods.java:50)
    at me.icyrelic.com.LegendaryVoting.onEnable(LegendaryVoting.java:35)
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:374)
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
    at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:270)
    at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:252)
    at org.bukkit.craftbukkit.CraftServer.reload(CraftServer.java:578)
    at org.bukkit.Bukkit.reload(Bukkit.java:183)
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:22)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180)
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:502)
    at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:915)
    at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:828)
    at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:810)
    at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
    at net.minecraft.server.NetworkManager.b(NetworkManager.java:282)
    at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:111)
    at net.minecraft.server.ServerConnection.b(SourceFile:35)
    at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:561)
    at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:213)
    at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)

    Methods Line 50: count = Integer.parseInt(parsedItem[1]);
    LegendaryVoting Line 35: Methods.giveItems(getServer().getPlayer("IcyRelic"), items); //I set server = this.getServer() earlier.
     
  8. parsedItem.lenght may be 1 or 0
     
  9. Offline

    nathanaelps

    It looks like the items that you're passing the function aren't in the ID:COUNT format. Maybe you've just put the ID? Now, naturally, I should have coded in the failsafe that if there's no COUNT specified that it should default to 1.

    In short: Do a
    Code:
    parsedItem.length
    check before checking the COUNT.

    Incidentally, I don't know what would happen if you tried to pass a COUNT that's greater than 64. You might want to check for that, too.
     
  10. Offline

    IcyRelic

    i see what is happening my list looks like this until it creates the config.yml file
    List:
    - 1:10
    - 2:10
    - 3:10

    but when it creates the config it gives a number like

    List:
    -8374663

    and idk what is going on

    I got it working now i added '' around the numbers in the list thanks to everyone that helped

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

    nathanaelps

    Ah! Good thing! That'd do it!
     
Thread Status:
Not open for further replies.

Share This Page