getLore() related

Discussion in 'Plugin Development' started by ImaTimelord7, Mar 6, 2015.

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

    ImaTimelord7

    Code:
        public void wand(PlayerInteractEvent wand){
            Player player = wand.getPlayer();
            if(wand.getAction().equals(Action.RIGHT_CLICK_AIR) && player.getItemInHand().getType() == Material.STICK){
                if(((ItemMeta) player.getItemInHand()).getLore() == (ChatColor.stripColor(s).contains("This is your wand."))){
                    player.sendMessage("You swapped wand for Aguamenti");}
    So, I need to be able to get a "Right-Click" from a stick, but not just any stick, one that is renamed "Wand" and has the lore "This is your wand." and then some more lore on more lines, I have this stick/wand as an item stack with that name + lore but I'm not sure how to check the lore, Google has so far told me to use ".getLore()" and this is what I came up with, but it doesn't work. If someone knows why then that would be awesome, thanks in advance.

    Also my code does have a problem on this line:
    Code:
    if(((ItemMeta) player.getItemInHand()).getLore() == (ChatColor.stripColor(s).contains("This is your wand."))){
    It underlines the "s" inside of the brackets on "ChatColor.stripColor(s)" then says "s cannot be resolved to a variable". So that might also be the problem, however what I found on Google was to use that.

    And here is the ItemStack for the Stick/Wand:
    Code:
        public ItemStack wand() { //This is default wand.
            ItemStack wand = new ItemStack(Material.STICK, 1);
            ItemMeta meta = wand.getItemMeta();
            meta.setDisplayName(ChatColor.AQUA + "Wand");
            ArrayList<String> lore = new ArrayList<String>();
            lore.add(ChatColor.DARK_PURPLE + "This is your wand.");
            lore.add(ChatColor.DARK_PURPLE + "Right click to cycle spells.");
            lore.add(ChatColor.DARK_PURPLE + "Or use /spells select [spellname]");
            meta.setLore(lore);
            wand.setItemMeta(meta);
            return wand;}
     
  2. Online

    timtower Administrator Administrator Moderator

    @ImaTimelord7 Your check won't work. A list won't be equal to a boolean.
    You want to use getLore().contains(your wand)
     
  3. Offline

    Hex_27

    @ImaTimelord7 That's not how you use ItemMeta. You need to do this:
    player.getItemInHand().getItemMeta().getLore()
    Normally I don't say this, but it would be a good idea to search up events/objects and the like in the bukkit javadocs.

    PS: Remember to check if lore is NOT null first.
     
    mine-care likes this.
  4. Offline

    ImaTimelord7

    Ok, I changed to this:
    player.getItemInHand().getItemMeta().getLore()
    And I think I added the null check, here is my code, it doesn't throw errors, but it doesn't work either.
    Code:
    public void wand(PlayerInteractEvent wand){
            Player player = wand.getPlayer();
            if(wand.getAction().equals(Action.RIGHT_CLICK_AIR) && player.getItemInHand().getType() == Material.STICK){
                if(player.getItemInHand().getItemMeta().getLore() != null) {
                if(player.getItemInHand().getItemMeta().getLore().contains("This is your wand.")) {
                    player.sendMessage("You swapped wand for Aguamenti");}
    And here is the ItemStack for the stick/wand with the lore. The item that should say "You swapped wand for Aguamenti" in chat on right click.

    Code:
        public ItemStack wand() { //This is default wand.
            ItemStack wand = new ItemStack(Material.STICK, 1);
            ItemMeta meta = wand.getItemMeta();
            meta.setDisplayName(ChatColor.AQUA + "Wand");
            ArrayList<String> lore = new ArrayList<String>();
            lore.add(ChatColor.DARK_PURPLE + "This is your wand.");
            lore.add(ChatColor.DARK_PURPLE + "Right click to cycle spells.");
            lore.add(ChatColor.DARK_PURPLE + "Or use /spells select [spellname]");
            meta.setLore(lore);
            wand.setItemMeta(meta);
            return wand;}
     
  5. Offline

    Hex_27

    @ImaTimelord7 did you remember the @EventHandler? Also, your lore is
    (ChatColor.DARK_PURPLE+"This is your wand.")
    not ("This is your wand")

    It may be why it doesn't work
     
  6. Offline

    ImaTimelord7

    Ok, added the @EventHandler, then I tried removing the lore part so I have:

    Code:
    @EventHandler
        public void wandrightclick(PlayerInteractEvent wand){
            Player player = wand.getPlayer();
            if(wand.getAction().equals(Action.RIGHT_CLICK_AIR) && player.getItemInHand().getType() == Material.STICK){
                    player.sendMessage("You swapped wand for Aguamenti");}
    But even that doesn't work, I think it is because I need to put it here:

    Code:
        public void onEnable(){
            Bukkit.getPluginManager().registerEvents(wand(), this);
            Bukkit.getPluginManager().registerEvents(new Aguamenti(), this); }
    But I don't know how to add it here, thanks in advance.
     
  7. Offline

    Hex_27

    @ImaTimelord7 no you don't. wand() is not a class file. And no I didn't ask you to remove the lore. I said change it.

    Code:
    @EventHandler
    public void wand(PlayerInteractEvent wand){
            Player player = wand.getPlayer();
            if(wand.getAction().equals(Action.RIGHT_CLICK_AIR) && player.getItemInHand().getType() == Material.STICK){
                if(player.getItemInHand().getItemMeta().getLore() != null) {
                if(player.getItemInHand().getItemMeta().getLore().contains(ChatColor.DARK_PURPLE + "This is your wand.")) {
                    player.sendMessage("You swapped wand for Aguamenti");}
    there. Like this.
     
  8. Offline

    ImaTimelord7

    Yeah, I have this:
    Code:
    @EventHandler
        public void wand(PlayerInteractEvent wand){
                Player player = wand.getPlayer();
                if(wand.getAction().equals(Action.RIGHT_CLICK_AIR) && player.getItemInHand().getType() == Material.STICK){
                    if(player.getItemInHand().getItemMeta().getLore() != null) {
                    if(player.getItemInHand().getItemMeta().getLore().contains(ChatColor.DARK_PURPLE + "This is your wand.")) {
                        player.sendMessage("You swapped wand for Aguamenti");}
    And it doesn't work, I don't get the message "You swapped wand for Aguamenti" when I right click with a stick from the ItemStack as follows:
    Code:
    public ItemStack wand() { //This is default wand.
            ItemStack wand = new ItemStack(Material.STICK, 1);
            ItemMeta meta = wand.getItemMeta();
            meta.setDisplayName(ChatColor.AQUA + "Wand");
            ArrayList<String> lore = new ArrayList<String>();
            lore.add(ChatColor.DARK_PURPLE + "This is your wand.");
            meta.setLore(lore);
            wand.setItemMeta(meta);
            return wand;}
    Btw Both of these codes are in the same class.
     
Thread Status:
Not open for further replies.

Share This Page