Solved Not Giving Respawning player Items?

Discussion in 'Plugin Development' started by kayc01, Oct 25, 2016.

Thread Status:
Not open for further replies.
  1. Hi, for some reason when I respawn a player when they die it does not give them the items I set when I force respawn them. It actually keeps them with the same items even though I set the items to null and then fill them again.

    Same with if I die as vampire, using the same stuff it is not clearing my inventory it is dropping them and then picking them up instead of setting the items to the chest and helm.

    Code:

    Code:
    @EventHandler
        public void onKill(PlayerDeathEvent e) {
           
            Player p = (Player) e.getEntity();
           
           
            if (inArena2.contains(p)) {
                System.out.println(p + " is in the arena, removing him");
                System.out.println(inArena2.size());
                inArena2.remove(p);
                System.out.println("Removed player from arena.");
                System.out.println(inArena2.size());
    
                Random random = new Random();
                int randomNum = random.nextInt(2 - 1) + 1;
               
                if (randomNum == 1) {
                   
                    System.out.println("They are vampire");
                   
                    vampire.add(p);
                    p.setHealth(20);
                    p.setGameMode(GameMode.SURVIVAL);
                    p.getInventory().clear();
                    p.getInventory().setArmorContents(null);
                    ItemStack WSkull = new ItemStack(Material.SKULL_ITEM, 1);
                     SkullMeta meta = (SkullMeta) WSkull.getItemMeta();
                     meta.setOwner("MHF_Enderman");
                     meta.setDisplayName(ChatColor.DARK_PURPLE + "Vampire " + p.getName());
                     WSkull.setItemMeta(meta);
                    p.getInventory().setHelmet(WSkull);
                   
                    ItemStack chest = new ItemStack(Material.LEATHER_CHESTPLATE);
                    LeatherArmorMeta meta2 = (LeatherArmorMeta) chest.getItemMeta();
                    meta2.setColor(Color.BLACK);
                    chest.setItemMeta(meta2);
                    p.getInventory().setChestplate(chest);
                   
                    ItemStack sword = new ItemStack(Material.STONE_SWORD, 1);
                     sword.addEnchantment(Enchantment.DAMAGE_ALL, 2);
                     ItemMeta itemMeta4 = sword.getItemMeta();
                     itemMeta4.setDisplayName(ChatColor.BLUE + (ChatColor.BOLD + "Sharpeness Stone Sword"));
                     itemMeta4.setLore(Arrays.asList(ChatColor.GREEN + "Hunt for Blood!!"));
                     sword.setItemMeta(itemMeta4);
                    
                     ItemStack compass = new ItemStack(Material.COMPASS, 1);
                     ItemMeta itemMeta2 = compass.getItemMeta();
                     itemMeta2.setDisplayName(ChatColor.BLUE + (ChatColor.BOLD + "Track Player's"));
                     itemMeta2.setLore(Arrays.asList(ChatColor.GREEN + "Right click to track the nearest player to you!"));
                     compass.setItemMeta(itemMeta2);
                    
                     if (!p.getInventory().contains(compass)) {
                         p.getInventory().addItem(compass);
                           
                         }
                    
                    
                     if (!p.getInventory().contains(sword)) {
                         p.getInventory().addItem(sword);
                         }
                    
                     System.out.println("Gave Items");
                    World w = Bukkit.getServer().getWorld(settings.getData().getString("warps.vampire.world"));
                    double x = settings.getData().getDouble("warps.vampire.x");
                    double y = settings.getData().getDouble("warps.vampire.y");
                    double z = settings.getData().getDouble("warps.vampire.z");
                    p.teleport(new Location(w, x, y, z));
                    Bukkit.broadcastMessage(ChatColor.RED + p.getName() + " has been killed! And has become a Vampire!.");
                   
                    System.out.println("TP'd");
                }
    Console:

    Code:
    25.10 23:48:07 [Server] INFO poqdavid was slain by ABkayCkay using [§9§lSharpeness Stone Sword]
    25.10 23:48:07 [Server] INFO poqdavid has been slain by ABkayCkay with a STONE_SWORD
    25.10 23:48:07 [Server] INFO TP'd
    25.10 23:48:07 [Server] INFO poqdavid has been killed! And has become a Vampire!.
    25.10 23:48:07 [Server] INFO Gave Items
    25.10 23:48:07 [Server] INFO They are vampire
    25.10 23:48:07 [Server] INFO 2
    25.10 23:48:07 [Server] INFO Removed player from arena.
    25.10 23:48:07 [Server] INFO 3
    25.10 23:48:07 [Server] INFO CraftPlayer{name=poqdavid} is in the arena, removing him
    Any Idea why it is not removing items when they die and not setting them when I put there health to 20 to force respawn.

    Thanks
     
  2. Offline

    Zombie_Striker

    @kayc01
    Unless you have it set so the player keeps their inventory on death (Using GameRules, not your own) setting items when the player dies will not do anything. If you did not set the gamerules, you need to set all the items once they respawn.
     
  3. I sort of get what you are saying, however I force respawn them before setting the items by setting there health to 20.
    Then add the items. What would be the issue there? As they are no longer dead when I give the items.

    Also keeping inventory is not what I want, I want when a player dies, they lose the player items and are given zombie items or vampire items (the random).

    Any recommendation on this or are you saying I cannot respawn someone efficiently in a OnDeathEvent?

    Using another method of a respawn event (meaning I don't force respawn them) it gives them the items correctly.
    Just trying to figure out TP'ing as its not working as wanted. (set highest priority on the event to see if it works in a minute).

    Also, using in context these don't seem to do anything...

    Code:
    @EventHandler
        public void ItemDrop(PlayerDropItemEvent event){
                event.setCancelled(true);
        }
    And


    Code:
    @EventHandler
         public void noPickup(PlayerPickupItemEvent e){
                e.setCancelled(true);
            }
    Gonna set priority to highest now to see if that works.

    Thanks
     
  4. Offline

    Zombie_Striker

    The player and the server still treats the player as though they died, even though you set those values. even though PlayerDeathEvent is triggered before the player dies, the server believes the player is already dead, so it drops all the items in the player's inventory. Either use a different event (by using EntityDamgeEvent and checking if their health - damage would be less than 0) or add the the item using PlayRespawnEvent.
     
  5. Yeah using the respawn event seems to work.
    However, when the player dies or vampire dies that is then meant to TP them to the vampire spawn point (in a cave).
    Instead it is teleporting them to the waiting area (spawn).

    I set the priority to highest on it the event.
    Essentials is set to lowest and not use home as respawn point.


    Code:
    @EventHandler(priority = EventPriority.HIGHEST)
            public void onPlayerRespawn(PlayerRespawnEvent pre) {
             Player p = (Player) pre.getPlayer();
            
             System.out.println("Got to repsawn Event");
             if (vampire.contains(p)) {
                
                 System.out.println("Is Vampire");
                
                 p.getInventory().clear();
                 p.getInventory().setArmorContents(null);
                
                 p.setFoodLevel(20);
                 p.setHealth(20);
             
                 ItemStack WSkull = new ItemStack(Material.SKULL_ITEM, 1);
                 SkullMeta meta = (SkullMeta) WSkull.getItemMeta();
                 meta.setOwner("MHF_Enderman");
                 meta.setDisplayName(ChatColor.DARK_PURPLE + "Vampire " + p.getName());
                 WSkull.setItemMeta(meta);
                p.getInventory().setHelmet(WSkull);
               
                ItemStack chest = new ItemStack(Material.LEATHER_CHESTPLATE);
                LeatherArmorMeta meta2 = (LeatherArmorMeta) chest.getItemMeta();
                meta2.setColor(Color.BLACK);
                chest.setItemMeta(meta2);
                p.getInventory().setChestplate(chest);
               
                ItemStack sword = new ItemStack(Material.STONE_SWORD, 1);
                 sword.addEnchantment(Enchantment.DAMAGE_ALL, 2);
                 ItemMeta itemMeta4 = sword.getItemMeta();
                 itemMeta4.setDisplayName(ChatColor.BLUE + (ChatColor.BOLD + "Sharpeness Stone Sword"));
                 itemMeta4.setLore(Arrays.asList(ChatColor.GREEN + "Hunt for Blood!!"));
                 sword.setItemMeta(itemMeta4);
                
                 ItemStack compass = new ItemStack(Material.COMPASS, 1);
                 ItemMeta itemMeta2 = compass.getItemMeta();
                 itemMeta2.setDisplayName(ChatColor.BLUE + (ChatColor.BOLD + "Track Player's"));
                 itemMeta2.setLore(Arrays.asList(ChatColor.GREEN + "Right click to track the nearest player to you!"));
                 compass.setItemMeta(itemMeta2);
                
                 if (!p.getInventory().contains(compass)) {
                     p.getInventory().addItem(compass);
                       
                     }
                
                
                 if (!p.getInventory().contains(sword)) {
                     p.getInventory().addItem(sword);
                     }
                
                 System.out.println("Gave Items");
                 //vampireEffect(p);
               
                World w = Bukkit.getServer().getWorld(settings.getData().getString("warps.vampire.world"));
                double x = settings.getData().getDouble("warps.vampire.x");
                double y = settings.getData().getDouble("warps.vampire.y");
                double z = settings.getData().getDouble("warps.vampire.z");
                p.teleport(new Location(w, x, y, z));
             }
            
         }
    Which should TP it to the Cords of my vampire set warp.
    But it seems to be overwritten by something.

    My warp system does work (I can "/hunt warp vampire" to go to the correct place).
    Just does not take me there on death/respawn.
     
  6. Offline

    Zombie_Striker

    @kayc01
    Don't use the .teleport method for this. use Event#setSpawnLocation(Location)
     
  7. That did it, thanks!
     
Thread Status:
Not open for further replies.

Share This Page