Help with ItemStacks and editing them.

Discussion in 'Plugin Development' started by jurosic_park, Jan 8, 2022.

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

    jurosic_park

    So what im trying to do, is create an ItemStack with
    Code:
    ItemStack is = player.getItemInHand();
    and then remove one item with
    Code:
    is.setAmount(is.getAmount()-1)
    so that i can use it later, the only problem is that when i set that ItemStacks amount to remove one item, it also removes it in game,

    How would i do it in such a way, that i can "Keep it" or more like duplicate it, so i can use it later, but not actually editing the players inventory? Preferably without config files. Ive already tried using "final" but that doesn't work.

    Thanks in advance!
     
  2. Offline

    timtower Administrator Administrator Moderator

    @jurosic_park Why do you remove 1 from the stack though if you are gonna use it later?
     
  3. Offline

    jurosic_park

    What i want to do, is learn plugin making the hard way lmao, now i know that may sound dumb, but thats how i like to do stuff.

    Basically what i want to do is give an item limited uses, for example 4 and put it into the lore, then every time the item is used, i want to remove -1 from the lore, the problem is i also want the item to be stackable, and so what i do is copy the meta from the stack that the player is holding, remove -1 from the stack and put it in his inventory, remove the ItemStack he has in his hand, and replace it with the same item except the lore now has -1 uses from the ItemStack i extracted from his hand, pls dont hate me for this code.

    Code:
    if (interChkReqHand.equals(cusLoreFajka1) || interChkReqHand.equals(cusLoreFajka2) || interChkReqHand.equals(cusLoreFajka3) || interChkReqHand.equals(cusLoreFajka4) && interChk.getAction() == Action.RIGHT_CLICK_AIR) {
                  
                    interChkReq.giveExp(20);
                  
                    interChkReq.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 2000, 2, false));
                    interChkReq.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 500, 4, false));
                    interChkReq.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 500, 7, false));
                    interChkReq.addPotionEffect(new PotionEffect(PotionEffectType.HARM, 5, 1, false));
                  
                    final ItemStack interChkReqItem = interChkReq.getItemInHand();
                  
                    if ((interChkReqItem.getAmount() >= 0)) {
    
                        interChkReq.getInventory().removeItem(interChkReqItem);
                      
                        String interChkReqItemLore = String.valueOf(interChkReqItem.getItemMeta().getLore());
                        Integer interChkReqItemUsage = Integer.valueOf(interChkReqItemLore.split("Pouzitia: ")[1].split("/4]")[0]);
                      
                        if (!(interChkReqItemUsage <= 1)) {
                          
                            Integer newFajkaUsage = interChkReqItemUsage-1;
                            final ItemStack newFajka = interChkReqItem;
                            final ItemMeta newFajkaMeta = interChkReqItem.getItemMeta();
                            final ItemStack oldFajka = interChkReqItem;
                            oldFajka.setAmount(oldFajka.getAmount()-1);
                          
                            ArrayList<String> newFajkaLore = new ArrayList<String>();
                            newFajkaLore.add("Fajka lepsia ako od dvanastky ;))");
                            newFajkaLore.add("Pouzitia: %s/4".formatted(String.valueOf(newFajkaUsage)));
                          
                            newFajkaMeta.setLore(newFajkaLore);
                            newFajka.setItemMeta(newFajkaMeta);
                            newFajka.setAmount(1);
                          
                            interChkReq.getInventory().addItem(oldFajka);
                            interChkReq.setItemInHand(newFajka);
                                              
                        } else {
                            interChkReqItem.setAmount(interChkReqItem.getAmount()-1);
                        }
                      
                    }
                  
                } else {
                    interChk.setCancelled(true);
                }
    The problem is, when i remove the whole ItemStack now has only 1 item, theres some other problems, but i plan to fix those when i fix this.

    Ive thought of, if possible, make the item not stackable, or make it not be able to be used if its more than one item in the stack, but the solution for this may come handy in the future. Im also fairly new to making plugins, and this can probably be done in a better fashion, but i prefer to learn the hard way.

    EDIT: the lore is written in slovak, but "Pouzitia" means "uses".
     
  4. Offline

    timtower Administrator Administrator Moderator

    @jurosic_park Why not check if the item is getting stacked and merge the uses if they do so?
     
  5. Offline

    jurosic_park

    Sorry, im not quite sure i understand, but if i do, i dont want to merge uses, i want the items to be separate if they dont have the same number of uses
     
  6. Offline

    Strahan

    Then you cannot let them merge, and needs must cancel that. The only way I could see to get around that would be that when they merge, you can create a collection to track each of their uses then restore the lore. You'd have to listen on any event that involves a change of the stack qty and restore lores once that happens.

    Personally, it sounds like a pain in the ass that isn't worth it. I'd just not merge them.
     
  7. Offline

    jurosic_park

    Right, so youre saying making them not stackable would be the best option? I'm going to look into that, as i dont know how to do that exactly lol. But thanks a bunch!

    Also ive thought of making a separate ItemStack in the code, and giving it the same properties as the players one would have, and just setting the amount of it with getAmount();
     
Thread Status:
Not open for further replies.

Share This Page