#map doesnt contain itemstacks even i added them

1 messages · Page 1 of 1 (latest)

red glade
#

Map<Integer, ItemStack> newSort = new HashMap<Integer, ItemStack>();

@Override
    public boolean onCommand(CommandSender s, Command c, String l, String[] args) {
        Player player = (Player) s;
        KitManager.givePlayerKit(player, KitManager.getKit("tank"));
        player.sendMessage("Sort test");
        
        for(int i = 0; i < p.getInventory().size(); i++) {
            newSort.put(i, player.getInventory().getItem(i));
        }
        
        return false;
    }
    @EventHandler
    void onChat(PlayerChatEvent e) throws IOException {
        System.out.println(newSort);
        
        KitManager.givePlayerKitWithCustomSort(e.getPlayer(), KitManager.getKit("tank"));
        Main.yamlConfiguration.set(e.getPlayer().getName()+ "tankkit", newSort);
        Main.yamlConfiguration.save(Main.file);
    }``` it doesnt register the itemstacks idk why
little geyser
#

In that for loop, check to make sure that the item you are getting isn't null or of type air. Then put a clone of that item into the hashmap.

red glade
#

still doesnt work

little geyser
#

Where is newSort being defined?

red glade
#

before the on command method

little geyser
#

Does givePlayerKit clear the player's inventory?

red glade
#

no i tried to clear the inventory too but doesnt work

little geyser
#

After the for loop, can you make sure that newSort's size is the amount of items that you wanted to add by printing it? Then in PlayerChatEvent print out the size of newSort at that point.

red glade
#

wait i noticed that the for loop doesnt get played

little geyser
#

For that p.getInventory().size(), shouldn't it be p.getInventory().getSize()?

red glade
#

yea i just noticed it, but it still just skips the for loop

little geyser
#

So if you put print statements inside of the for loop, it wouldn't print?

red glade
#

yes, idk why bukkit just skips these for me

little geyser
#

Is it printing out "Sort test" beforehand?

red glade
#

yes it does

#

if i put the for loop in a seperate method it runs

little geyser
#

Try printing out the size of the player's inventory inside of the onCommand method.

red glade
#

it works perfectly fine

little geyser
#

If you make another loop inside of that method that just does a print, does it work? Put it before the other for loop

red glade
#

yes a simple loop like this

    System.out.println("ASD");``` just works completly fine
#

now the other loop works too, but it doesnt add it to the map

little geyser
#

Where is the newSort map being created?

red glade
#

    @Override
    public boolean onCommand(CommandSender s, Command c, String l, String[] args) {```
before the oncommand method
little geyser
#

Just to make sure, after that loop that's supposed to add the items to the hashmap, if you did a print immediately after that does newSort.size() it would return 0?

red glade
#

it returns 36

little geyser
#

Ok, Instead of doing PlayerChatEvent, try doing something like EntityDamageEvent, check to make sure it's a player, and then run the code.

red glade
#

still doesnt work

little geyser
#

Can you post the code you have now?

red glade
#
    @EventHandler
    void onChat(EntityDamageEvent e) throws IOException {
        System.out.println(newSort);

        if(e.getEntity() instanceof Player) {
            Player player = (Player) e.getEntity();
            KitManager.givePlayerKitWithCustomSort(player, KitManager.getKit("tank"));
        Main.yamlConfiguration.set(player.getName() + "tankkit", newSort);
        Main.yamlConfiguration.save(Main.file);
        }
        
        
    }```
little geyser
#

Instead of just printing newSort, try printing newSort.size(). Is that 0?

red glade
#

no its 9

little geyser
#

Does anything happen in between that modifies newSort?

red glade
#

no its just ends the method

#

after that is just the end

little geyser
#

Can you show what you have for onCommand?

red glade
#
 Map<Integer, ItemStack> newSort = new HashMap<Integer, ItemStack>();

    @Override
    public boolean onCommand(CommandSender s, Command c, String l, String[] args) {
        Player player = (Player) s;
        player.getInventory().clear();

        KitManager.givePlayerKit(player, KitManager.getKit("tank"));
        player.sendMessage("Sort test");

        for (int i = 0; i < player.getInventory().getSize(); i++) {
            if(player.getInventory().getItem(i) != null) {
                newSort.put(i, player.getInventory().getItem(i));
                System.out.println(newSort.size());
            }
        }
        
        
        return false;
    }```
little geyser
#

for (int i = 0; i < player.getInventory().getSize(); i++) {
ItemStack item = player.getInventory().getItem(i);
if(item == null || item.getType() == Material.AIR)
{
continue;
}
newSort.put(i, item.clone());
}

#

Sorry, not 100% sure on how to post it with the code style

red glade
#

just three `java code then again the thing

little geyser
#
            ItemStack item = player.getInventory().getItem(i);
            if(item == null || item.getType() == Material.AIR)
            {
                continue;
            }
            newSort.put(i, item.clone());
        }```
#

ah, got it

red glade
#

still nothubng

little geyser
#

For this loop it will likely make it so that newSort no longer has a size of 36 since it'll skip the items that are null or air. Inside of that entitydamageevent, print out all of the items and make sure they are the right ones instead.

red glade
little geyser
#

Show the code

red glade
#

i changed back to the player chat event because in entitydamage event it said the same

 Map<Integer, ItemStack> newSort = new HashMap<Integer, ItemStack>();

    @Override
    public boolean onCommand(CommandSender s, Command c, String l, String[] args) {
        Player player = (Player) s;
        player.getInventory().clear();

        KitManager.givePlayerKit(player, KitManager.getKit("tank"));
        player.sendMessage("Sort test");

        for (int i = 0; i < player.getInventory().getSize(); i++) {
            ItemStack item = player.getInventory().getItem(i);
            if (item == null || item.getType() == Material.AIR) {
                continue;
            }
            newSort.put(i, item.clone());
        }
        return false;
    }

    @EventHandler
    void onChat(PlayerChatEvent e) throws IOException {
        System.out.println(newSort);

        KitManager.givePlayerKitWithCustomSort(e.getPlayer(), KitManager.getKit("tank"));
        Main.yamlConfiguration.set(e.getPlayer().getName() + "tankkit", newSort);
        Main.yamlConfiguration.save(Main.file);

    }```
little geyser
#

Instead of just doing println(newSort), try this instead.

for(int slot : newSort.keySet())
{
    ItemStack item = newSort.get(slot);
    Bukkit.getLogger().info("Slot: " + slot + " ItemType: " + item.getType());
}
red glade
#

doesnt send anything in tzhe console

little geyser
#

Try changing back to the EntityDamageEvent

red glade
#

doesnt do anything

little geyser
#

If you just print something, like "Test", does it output that to the console?

red glade
#

yes

little geyser
#

In the damageevent comment out the line including, and everything after KitManager. In the onCommand, comment out the givePlayerKit.

#

Wait I just realized, you cleared the inventory in your onCommand

#

comment out that line

red glade
#

i already did it

little geyser
#

What code do you have now?

red glade
#
Map<Integer, ItemStack> newSort = new HashMap<Integer, ItemStack>();

    @Override
    public boolean onCommand(CommandSender s, Command c, String l, String[] args) {
        Player player = (Player) s;
        player.getInventory().clear();

        KitManager.givePlayerKit(player, KitManager.getKit("tank"));
        player.sendMessage("Sort test");

        for (int i = 0; i < player.getInventory().getSize(); i++) {
            ItemStack item = player.getInventory().getItem(i);
            if (item == null || item.getType() == Material.AIR) {
                continue;
            }
            newSort.put(i, item.clone());
        }
        
        return false;
    }

    @EventHandler
    void onChat(PlayerChatEvent e) throws IOException {
        for(int slot : newSort.keySet())
        {
            ItemStack item = newSort.get(slot);
            Bukkit.getLogger().info("test");
        }
        //e.getPlayer().getInventory().clear();
        KitManager.givePlayerKitWithCustomSort(e.getPlayer(), KitManager.getKit("tank"));
        Main.yamlConfiguration.set(e.getPlayer().getName() + "tankkit", newSort);
        Main.yamlConfiguration.save(Main.file);

    }```
little geyser
#

remove player.getInventory().clear(); in the onCommand

red glade
#

doesnt work

#

still empty

little geyser
#
@Override
    public boolean onCommand(CommandSender s, Command c, String l, String[] args) {
        Player player = (Player) s;
      
        for (int i = 0; i < player.getInventory().getSize(); i++) {
            ItemStack item = player.getInventory().getItem(i);
            if (item == null || item.getType() == Material.AIR) {
                continue;
            }
            newSort.put(i, item.clone());
        }
        
        return true;
    }

    @EventHandler
    void onChat(PlayerChatEvent e) throws IOException {
        for(int slot : newSort.keySet())
        {
            ItemStack item = newSort.get(slot);
            Bukkit.getLogger().info("Slot: " + slot);
            Bukkit.getLogger().info("Type: " + item.getType());
            Bukkit.getLogger().info("test");
        }
    }
``` If you run just this, does it work?
red glade
#

no it prints empty lines

little geyser
#

Can you show what it's outputting?

red glade
#

>```
#

only this