Solved String cannot be cast to ItemStack

Discussion in 'Plugin Development' started by AnonyBart, Apr 25, 2016.

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

    AnonyBart

    LOOK AT THE BOTTOM OF T3


    Hello guys,
    I am trying to do a list of ItemStack but i got an error:
    The left-hand side of an assignment must be a variable

    I don't know how to solve it, can you help me? Thanks :)

    Code:
              ItemStack[] list = {(128 , 64), (130, 1), (4, 5), (1, 10)};
    
     
    Last edited: Apr 25, 2016
  2. Online

    timtower Administrator Administrator Moderator

    @AnonyBart That is not how you initialize an ItemStack array, you need to fill it with Itemstacks, not with numbers.
     
  3. Offline

    AnonyBart

    Sorry but:
    ItemStack(int type, int amount);

    Like you say it will be:

    ItemStack banana = new ItemStack (128, 5);
    ItemStack[] list = {banana, ..};

    Right?

    Inviato dal mio SM-G930F utilizzando Tapatalk
     
  4. Offline

    Lordloss

    Uhm, what exactly are you doing there? Or what do you think are you doing there?
     
  5. Online

    timtower Administrator Administrator Moderator

    @AnonyBart or {new ItemStack(),new ItemStack()};
     
    AnonyBart likes this.
  6. Offline

    mine-care

  7. Offline

    AnonyBart

  8. Offline

    mine-care

  9. Offline

    AnonyBart

  10. Online

    timtower Administrator Administrator Moderator

    @AnonyBart Use the comstructors again, that is not how Bukkit saves an ItemStack.
     
  11. Offline

    AnonyBart

    Can you please explain me what you mean?
     
    Last edited by a moderator: Apr 26, 2016
  12. Online

    timtower Administrator Administrator Moderator

    @AnonyBart It is the same issue as in your first post.
     
  13. Offline

    AnonyBart

    Are you sure? Because I did all like you said.
     
    Last edited by a moderator: Apr 26, 2016
  14. Online

    timtower Administrator Administrator Moderator

    I am sure.
    You are getting a string and assuming that it will cast to an ItemStack.
     
  15. Offline

    mine-care

  16. Offline

    AnonyBart

    When i put in the config:
    Code:
              items.createSection("IDItems");
              ItemStack[] list = {new ItemStack(128 , 64),new ItemStack(130, 1),new ItemStack (4, 5),new ItemStack (1, 10)};
              items.set("IDItems", Arrays.asList(list));
    
    What i have in the config now:
    Code:
    IDItems:
    - 128, 64
    - 130, 1
    - 4, 5
    - 1, 10
    
    When i have to do the for each loop:
    Code:
    static List <ItemStack> Items =  (List<ItemStack>)Main.instance.items.getList("IDItems");
        public static ItemStack Oggetti(){
            randomGenerator = new Random();
            ItemStack Item2 = null;
                for(ItemStack Item:Items)     //THE ERROR IS HERE
                {
                    int index = randomGenerator.nextInt(Items.size());
                    Item2 = Items.get(index);
                }
                return Item2;
        }
    


    In a forum i have found that:
    [​IMG]

    So i am getting the List <ItemStack> Items correctly.
     
  17. Online

    timtower Administrator Administrator Moderator

    @AnonyBart But you aren't setting it through that same method.
    You are setting a single string, not an ItemStack.
     
  18. Offline

    AnonyBart

    I do not understand what you mean ... can you try to tell me the error?

    Inviato dal mio SM-G930F utilizzando Tapatalk
     
  19. Online

    timtower Administrator Administrator Moderator

    @AnonyBart You are trying to turn a string into an ItemStack.
    That will not work.

    You are expecting that the constructor takes a single string argument, it does not.
    You need to transform it first.
     
    Last edited: Apr 30, 2016
  20. Offline

    AnonyBart

    Something like that will work?
    Code:
        public static ItemStack Oggetti(){
            randomGenerator = new Random();
            ItemStack Item2 = null;
            ItemStack[] Items2 = Items.toArray(new ItemStack[0]);
                for(ItemStack Item:Items2)
                {
                    int index = randomGenerator.nextInt(Items2.length);
                    Item2 = Items2[index];
                }
                return Item2;
        }
    
    Here i transformed the List items in an Array. It is what you mean?

    PS: Why you can't give me the code i need :(
     
  21. Online

    timtower Administrator Administrator Moderator

    @AnonyBart That will probably just throw errors.
    And no, that is not what I mean.
    And because I wont spoonfeed, then you just copy paste it.

    you have a list of strings.
    Each needs to be converted to itemstack.
    Split the string, change the 2 strings to numbers.
    Call the constructor.
     
  22. Offline

    AnonyBart

    Really thanks, I thought i did not serve do everything that but probably I was wrong.

    Inviato dal mio SM-G930F utilizzando Tapatalk
     
  23. Offline

    AnonyBart

    Sorry I'm late but I was seriously ill (still am), that code should work? :)

    Code:
    static List <String> Items =  Main.items.getStringList("IDItems");
    
        @SuppressWarnings("deprecation")
        public static ItemStack Oggetti(){
            ItemStack FinalItem = null;
            randomGenerator = new Random();
            int index = randomGenerator.nextInt(Items.size());
            String Item  = Items.get(index);
                String[] parts = Item.split(",");
                String part3 = parts[0];
                String part4 = parts[1];
                int part1 = Integer.parseInt(part3);
                int part2 = Integer.parseInt(part4);
                FinalItem = new ItemStack(part1, part2);
              
              
                return FinalItem;
        }
    



    Ps: Another question without open another thread, can you say me why in this plugin when i write /fly nothing appends? http://paste.md-5.net/taxukudufu.coffee
    PS: The onCommand is in the main, so i must only register the command with getCommand("fly").setExecutor(this);
    Thanks :)
     
  24. Online

    timtower Administrator Administrator Moderator

    @AnonyBart That looks a lot better.

    Add debug messages, just messages that tell what is happening.
     
    AnonyBart likes this.
  25. Offline

    AnonyBart

    And for the second question? (Look the before message at "PS:")

    Inviato dal mio SM-G930F utilizzando Tapatalk
     
  26. Online

    timtower Administrator Administrator Moderator

    @AnonyBart That is the second line of my post.
     
  27. Offline

    AnonyBart

    Ah ok thanks, I will do it.
    But for you the code it's correct or not?

    Inviato dal mio SM-G930F utilizzando Tapatalk
     
  28. Online

    timtower Administrator Administrator Moderator

    @AnonyBart Best way to find out is by trying.
     
  29. Offline

    mcdorli

    Huh, that code has some serious problems, first and foremost is that you're trying to cast a player to a charsequence. You can only cast an object to a different one, if it extends that sifferent object.

    Before casting, ask yourself: "Is <object A> a <class you're extending to> too?"
    Example:
    "Is a player a charsequence (list of characters)?" Answer is: no.

    Please, read this link, it's essential to know java if you're using bukkit.
    https://docs.oracle.com/javase/tutorial/
     
  30. Offline

    AnonyBart

    I also thought it was a problem, I just wanted to try it.

    Inviato dal mio SM-G930F utilizzando Tapatalk
     
Thread Status:
Not open for further replies.

Share This Page