Setting itemlore on death [COMPLICATED!]

Discussion in 'Plugin Development' started by sonica123, Jun 30, 2016.

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

    sonica123

    So basically I am trying to... On player death, if that player is wearing any type of iron armor, on death, set the itemlore of that ironarmor to PLAYER killed PLAYER. Thats it. I am a rookie in Java and really need help w this!

    @EventHandler //Iron Helmet death lore
    public void onPlayerDeath(PlayerDeathEvent e) {
    if (e.getEntity().getPlayer().getKiller() instanceof Player) {
    Player deathe = (Player) e.getEntity().getPlayer();

    if (deathe.getInventory().getHelmet() == new ItemStack(Material.IRON_HELMET)) {
    ItemStack helm = new ItemStack(Material.IRON_HELMET);
    ItemMeta helmmeta = helm.getItemMeta();
    List<String> lore = new ArrayList<String>();
    lore.add(ChatColor.BOLD + "" + ChatColor.DARK_RED + "Deaths:");
    lore.add(ChatColor.YELLOW + deathe.getName() + ChatColor.GRAY + " killed by " +ChatColor.YELLOW + e.getEntity().getPlayer().getKiller().getName());
    helm.setItemMeta(helmmeta);
    }
    } else {
    return;
    }
    }
    }
     
  2. Offline

    Esophose

    @sonica123
    Looks like you never set the lore to the arraylist of lore lines.
    The "else return" is completely pointless and should be removed.
    Consider putting your code inside code tags in the future so they are formatted nicely.

    Good luck!
     
  3. Offline

    MCnumi

    1. split up
    Code:
    if (e.getEntity().getPlayer().getKiller() instanceof Player)
    into
    Code:
    if (e.getEntity() instanceof Player)
    and
    Code:
    if (e.getKiller() instanceof Player)
    because e#getEntity() will automatically retrieve the damagee/deathee.

    2.
    Code:
    Player deathe = (Player) e.getEntity().getPlayer();
    get rid of getPlayer()

    3.
    Code:
    if (deathe.getInventory().getHelmet() == new ItemStack(Material.IRON_HELMET)) {
    ItemStack helm = new ItemStack(Material.IRON_HELMET); //do this before the if statement
    ItemMeta helmmeta = helm.getItemMeta(); //this is ok
    List<String> lore = new ArrayList<String>(); //I will write how to fix this
    lore.add(ChatColor.BOLD + "" + ChatColor.DARK_RED + "Deaths:");
    lore.add(ChatColor.YELLOW + deathe.getName() + ChatColor.GRAY + " killed by " +ChatColor.YELLOW + e.getEntity().getPlayer().getKiller().getName());
    helm.setItemMeta(helmmeta);
    instead of List<String> lore = new ArrayList<String>(); to store your lore, do
    helmmeta.setLore(Arrays.asList(""Example", "Example")
    then do helm.setItemMeta(helmmeta) to apply it onto the item

    4. Your helmmeta variable should be camelcase

    5. Please learn Java. (check this forums stickies)
     
    Zombie_Striker likes this.
  4. Offline

    Zombie_Striker

    Adding on to what @Esophose said, you also never re-add the item to the player's inventory. You are creating a new helmet and then never do anything with it.
    Also, this is a basic == vs .equals problem. You can't compare objects using ==. What == does is compares memory space. The method .equals compares the values of an object.

    Also, all you are really checking for is if the Types are the same. Get the helmet's type and check if the type is equal to a material instead of comparing itemstacks. (BTW: If the player is not wearing anything, this will thorw an NPE and break this event. Make sure the helmet is not null before getting it's type.)
     
    Last edited: Jun 30, 2016
    MCnumi likes this.
Thread Status:
Not open for further replies.

Share This Page