sending items to chest

Discussion in 'Spigot Plugin Development' started by William112, May 16, 2019.

  1. So I'm trying to get it so when i kill a mob it sends its loot right to a chest, I have it working but only sometimes, it seems like entitydeathevent isnt getting called everytime. is this the case or am i doing something wrong
  2. Please post source code and, if any exist, stack-traces.
  3. Code (Text):
        public void onKills(EntityDeathEvent e){
            if(e.getEntity().getLastDamageCause().getCause() == DamageCause.ENTITY_ATTACK){
                if(e.getEntity().getLastDamageCause().getEntity() instanceof Player){
                    Player player = e.getEntity().getLastDamageCause().getEntity();
                    for(String key : main.chests.keySet()){   //a hashmap containing players name and chest theyre connected to
                        if(player.getName() == key){
                                Inventory inv = ((Chest)main.chests.get(key)).getInventory();
                                List<ItemStack> getDropped = e.getDrops();
                                for(ItemStack itemToAdd : getDropped){
  4. just posted pretty simple dont think its that and no errors
  5. Did you register the event? Also please format your code
    Code (Text):
    like this
  6. no idea how u did it but yea it works sometimes thats why im confused
  7. 1.
    Code (Text):
    if(player.getName() == key)
    should probably be
    Code (Text):
    2. Make sure your 'chests' arraylist contains what you're trying to get and make sure the chest exists.
    3. I would try testing with debug messages to see what parts of your code works. (Basically just add
    Code (Text):
    every now and then in your code, and test it.)
    • Useful Useful x 1
  8. ill try those changes but i did have some debug messages, they just got me more confused they would sometimes be called sometimes not
  9. Re-add your debug messages, send updated code and show console output please.
  10. the console output is 100% the same as it was before, i dont know if u dont understand or what but it just seems like the debug messages arent being called i put one in every section, they only get called sometimes
  11. Send updated code please.
  12. If you're storing the String of a Player's name, as the key of a HashMap, you can use
    Code (Java):
    to check if the Map contains the Player. You can then use
    Code (Java):
    Chest chest = map.get(player.getName())
    This way you're not looping every single chest in the Map, and if the Player can have multiple Chest's then it's better to use
    Code (Java):
    HashMap<Player, ArrayList<Chest>>
    to further stop looping unnecessary things.

    It's a good idea when looping ItemStack types to add in
    Code (Java):
    if (itemstack == null) continue;
    so that your loop will move onto the next ItemStack in the iterable array or list or whatever without failing if the ItemStack is null - as is the case with inventory.getContents() if the slot is empty.

    You should also check if the target inventory has room, you can use
    Code (Java):
    for this since it will return either the slot index, or -1 if the Inventory is full.

    This should make the code a little more resilient to issue occurring. You can always debug this in testing with
    Code (Java):
    Bukkit.broadcastMessage("some message")
    after each line to ensure each line is running and then you can see where it's failing exactly and check console for any errors.

    Hope this helps
    • Useful Useful x 1

Share This Page