Need help making NPC drop a certain Player's inventory when killed.

Discussion in 'Plugin Development' started by Calebizzthaman, Aug 4, 2017.

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

    Calebizzthaman

    I'm trying to make a plugin to where when a player is hit and tries to log out it creates an NPC that allows the person they were fighting to kill. I've gotten the NPC to spawn but I'm not quite sure how I would make the NPC contain the player that logged out inventory and how to make them drop it. I'm using Citizens API.

    Code:
    public class Listeners implements Listener{
    
        private HashMap<Player, Player> tag = new HashMap<Player, Player>();
       
        @EventHandler
        public void onEntityDamage(EntityDamageByEntityEvent e) {
            if(e.getEntity() instanceof Player && e.getDamager() instanceof Player) {
                Player hit = (Player) e.getEntity();
                Player hitter = (Player) e.getDamager();
               
                if(tag.get(hit) != null) {
                    return;
                } else {
                    tag.put(hit, hitter);
                    hit.sendMessage(ChatColor.DARK_RED + "You have been combat tagged do not log out!");
                }
            }
        }
       
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent e) {
            if(tag.containsKey(e.getEntity())) {
                tag.remove(e.getEntity());
            }
        }
       
        @EventHandler
        public void CreateNPC(PlayerQuitEvent e) {
            Player p = e.getPlayer();
            String name = p.getName();
            NPCRegistry registry = CitizensAPI.getNPCRegistry();
            NPC npc = registry.createNPC(EntityType.PLAYER,  ChatColor.DARK_RED + ChatColor.BOLD +  "[CombatLogged] " + ChatColor.GRAY +name);
    
           
            if(tag.containsKey(p)) {
                npc.spawn(p.getLocation());
                npc.setProtected(false);
    
            }
        }
       
       
    }
     
  2. Offline

    MrGriefer_

    Store player's inventory in hashmap, and then spawn your npc then listen for
    EntityDeathEvent then simply add to the drops player's items:
    PHP:
    @EventHandler
    public void onEntityDeathEvent(EntityDeathEvent event) {
      
    event.getDrops().clear();
      
    event.getDrops().addAll(drops);
    }
     
  3. Offline

    Calebizzthaman

    How would I call the inventory from the Listener that creates the NPC and use it in the Listener for when it dies.
     
  4. Offline

    MrGriefer_

    Listen for PlayerQuitEvent, check if he was tagged if he is then store his inventory items in the HashMap after that do the rest as I said above.
     
    Last edited: Aug 4, 2017
  5. Offline

    Calebizzthaman

    Okay cool. But how would I be able to use inventory when it says I need to cast ItemStack. When i do that it tells me ItemStack and Inventory shouldn't be cast together and gives me errors.


    Code:
        private HashMap<Player, Player> tag = new HashMap<Player, Player>();
        private ArrayList<ItemStack> inv = new ArrayList<ItemStack>();
        private ArrayList<NPC> npcs = new ArrayList<NPC>();
      
        @EventHandler
        public void onEntityDamage(EntityDamageByEntityEvent e) {
            if(e.getEntity() instanceof Player && e.getDamager() instanceof Player) {
                Player hit = (Player) e.getEntity();
                Player hitter = (Player) e.getDamager();
              
                if(tag.get(hit) != null) {
                    return;
                } else {
                    tag.put(hit, hitter);
                    hit.sendMessage(ChatColor.DARK_RED + "You have been combat tagged.. "+ ChatColor.BOLD + "Do not log out!");
                    hitter.sendMessage(ChatColor.DARK_RED + "You have engaged in combat.. " + ChatColor.BOLD +  "Do not log out!");
                }
            }
        }
      
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent e) {
            if(tag.containsKey(e.getEntity())) {
                tag.remove(e.getEntity());
            }
        }
      
        @EventHandler
        public void CreateNPC(PlayerQuitEvent e) {
            Player p = e.getPlayer();
            String name = p.getName();
            NPCRegistry registry = CitizensAPI.getNPCRegistry();
            NPC npc = registry.createNPC(EntityType.VILLAGER, "[Sleeper] "+name);
            if(tag.containsKey(p)) {
    
                npc.spawn(p.getLocation());
                npc.setProtected(false);
                npcs.add(npc);
    
            }
          
        }
      
        @EventHandler
        public void PlayerLeft(PlayerQuitEvent e) {
            Player p = e.getPlayer();
            ItemStack inventory = (ItemStack) p.getInventory();
            if(tag.containsKey(p)) {
                inv.add(inventory);
            }
                  
        }
      
        @EventHandler
        public void PlayerKilled(EntityDeathEvent e){
          
            if(tag.containsKey(e.getEntity())){
                tag.remove(e.getEntity());
    
            }   
        }
      
        @EventHandler
        public void NPCKilled(NPCDeathEvent e)
        {
            NPC killed = e.getNPC();
            if(npcs.contains(killed)) {
                e.getDrops().clear();
                e.getDrops().addAll(inv);
              
            }
        }
    
     
    Last edited: Aug 5, 2017
  6. Offline

    MrGriefer_

    First - Create a HashMap (Not an ArrayList) storing the NPC as a key and itemstack as a value. This will store all player's inventory contents.
    PHP:
    private HashMap<NPCItemStack[]> drops = new HashMap<>();
    Second - Listen for PlayerQuitEvent and check if the player was tagged if he is, then spawn your NPC and add his inventory contents to the HashMap.
    PHP:
    drops.put(npcplayer.getInventory().getContents());
    Third - Listen for NPCDeathEvent and check if the HashMap contains the NPC if it does then clear the drops and add all the contents.
    Not 100% sure if this is going to work. :p
     
    Last edited: Aug 5, 2017
  7. Offline

    Calebizzthaman

    @MrGriefer_
    You're not supposed to cast ItemStack and Inventory together. It still gives the same errors...


    Errors say: java.util.HashMap cannot be cast to java.util.Collection
     
    Last edited: Aug 5, 2017
Thread Status:
Not open for further replies.

Share This Page