#Inventory.clear

69 messages · Page 1 of 1 (latest)

acoustic yacht
#

Does someone here know how to limit inventory.clear to only clear a certaint amount?
I tried js player.inventory.clear(Item.of(payment.id,amount,nbt)) player.inventory.clear(Ingredient.of(Item.of(payment.id,nbt),amount))
Then I looked at mojand did it for the /clear command, it seems to be```java
private static int clearInventory(CommandSourceStack p_136706_, Collection<ServerPlayer> p_136707_, Predicate<ItemStack> p_136708_, int p_136709_) throws CommandSyntaxException {
int i = 0;

  for(ServerPlayer serverplayer : p_136707_) {
     i += serverplayer.getInventory().clearOrCountMatchingItems(p_136708_, p_136709_, serverplayer.inventoryMenu.getCraftSlots());
     serverplayer.containerMenu.broadcastChanges();
     serverplayer.inventoryMenu.slotsChanged(serverplayer.getInventory());
  }

  if (i == 0) {
     if (p_136707_.size() == 1) {
        throw ERROR_SINGLE.create(p_136707_.iterator().next().getName());
     } else {
        throw ERROR_MULTIPLE.create(p_136707_.size());
     }
  } else {
     int j = i;
     if (p_136709_ == 0) {
        if (p_136707_.size() == 1) {
           p_136706_.sendSuccess(() -> Component.translatable("commands.clear.test.single", j, p_136707_.iterator().next().getDisplayName()), true);
        } else {
           p_136706_.sendSuccess(() -> Component.translatable("commands.clear.test.multiple", j, p_136707_.size()), true);
        }
     } else if (p_136707_.size() == 1) {
        p_136706_.sendSuccess(() -> Component.translatable("commands.clear.success.single", j, p_136707_.iterator().next().getDisplayName()), true);
     } else {
        p_136706_.sendSuccess(() -> Component.translatable("commands.clear.success.multiple", j, p_136707_.size()), true);
     }

     return i;
  }

}so I triedjs
player.inventory.clearOrCountMatchingItems(Item.of(payment.id,nbt),amount)```
But that tells me it couldn't find the methode

[10:16:58] [ERROR] ! …rhino.EvaluatorException: Can't find method net.minecraft.world.entity.player.Inventory.m_36022_(net.minecraft.world.item.ItemStack,number). (server_scripts:guis.js#2874)```
rancid inletBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

gloomy tree
#

the method doesnt take an ItemStack, number

#

look at the types of p_136708_ and p_136709_ and see they are passing serverplayer.inventoryMenu.getCraftSlots()

acoustic yacht
#

I tried that, it gives some weird error

#

Do you have an idea how I could get it to work?

gloomy tree
#

you need to check the type of the first parameter

#

as i said before

fossil hazel
#

player.inventory probably returns some KubeJS Inventory wrapper and clearOrCountMatchingItems only exist on the vanilla inventory

acoustic yacht
#

How do I get the vanilla one then?

gloomy tree
#

its not a wrapper, its managing to reobf the name

acoustic yacht
#

But no idea what that is

fossil hazel
#

interesting, because kubejs wraps the inventory on getInventory()

fossil hazel
#

so like if you would .filter on an array

acoustic yacht
#

How can I get that in kubejs

fossil hazel
#

wdym?

acoustic yacht
#

How I can get an item predicat

#

So the function works

fossil hazel
#

you create it. Its a function u pass

#

like in .filter()

acoustic yacht
#

There is no predicate thing here

fossil hazel
#

pls ._.

#

YOu just create the function yourself and pass it into clearOrCountMatchingItems if clearOrCountMatchingItems wants the predicate

#
clearOrCountMatchingItems((item) => {
  return true;
})
acoustic yacht
#

And that makes it work

#

?

#

How weird...

fossil hazel
#

why is that weird?

#

predicates are there to filter data

acoustic yacht
fossil hazel
#

why?

acoustic yacht
#

Vscode shouts at me

fossil hazel
#

show the code

acoustic yacht
fossil hazel
#

because thats not valid javascript. Why do you declare a function called clearOrCountMatchingItems

#

its the function you want to call on player

acoustic yacht
#

Because you told me so...

fossil hazel
#

No. I told you to call clearOrCountMatchingItems with the predicate. Because you said that clearOrCountMatchingItems takes a predicate as argument

acoustic yacht
#
                        player.inventory.clearOrCountMatchingItems((Item.of(payment.id,nbt)) => {
                            return true
                        },amount)```Like this?
#

It also doesn't like that

fossil hazel
#

no item.of, why you want to use item of still

acoustic yacht
#

Why not?

#

What else do I have to use

#

Oooh

#

Do I have to write the logic myself?

#

That detects if its the item I want?

fossil hazel
#

yes

#

its like you would filter an array

#

or use some .test methods on some objects

acoustic yacht
#

(I never do that)

fossil hazel
#

I already helped u with array filtering some weeks ago

#

:P

acoustic yacht
#

Yeah

#

but not the .test

#
[12:55:17] [ERROR] ! …rhino.EvaluatorException: Can't find method net.minecraft.world.entity.player.Inventory.m_36022_(function,number). (server_scripts:guis.js#2874)```
#
                        player.inventory.clearOrCountMatchingItems((item) => {
                            if(item.id == payment.id) {
                                let itemNbt = {}
                                if(item.nbt) {
                                    itemNbt = item.nbt
                                }
                                if(JSON.stringify(JSON.parse(NBT.toJson(itemNbt))) == JSON.stringify(nbt)) {
                                    return true
                                } else return false
                            } else return false
                        },amount)```
gloomy tree
#

count the number of arguments that minecraft is passing to that method, then count the number that you are passing.

acoustic yacht
#

What should I use for the last argument?

gloomy tree
#

what. does. minecraft. use.

acoustic yacht
#

It works

#

I just added ,player.inventoryMenu.getCraftSlots()

#

Thanks a lot

#

Im just really stupid sometimes

fossil hazel
#

learning process. I just hope that you remember these stuff

acoustic yacht
#

Me too 😄