event item

Discussion in 'Plugin Development' started by Subtelny, Dec 10, 2013.

Thread Status:
Not open for further replies.
  1. Hello ! (my english is bad, sorry)

    Code:
        @EventHandler
        public void interact(PlayerInteractEvent event) {
                Player player = event.getPlayer();
                ItemStack diax = player.getItemInHand();
                if(player.hasPermission("swieta")){
                } else {
                    player.sendMessage(ChatColor.GREEN + "Masz juz VIP, chyba nie chcesz go stracic? ^_^");
                    player.sendMessage(ChatColor.AQUA + "Przedmiot mozesz zachowac albo sprzedac.");
                }
                if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                        if (diax.getType() == Material.GOLD_NUGGET) {
                                if (player.getName() != null) {
                                    if(player.hasPermission("swieta")){
                                        event.setCancelled(true);
                                        getServer().dispatchCommand(Bukkit.getConsoleSender(), "azaddgroup " + player.getName() + " vip 7d");
                                        player.sendMessage(ChatColor.GREEN + "Dostales range VIP na 7 dni!");
                                        player.getInventory().removeItem(new ItemStack(Material.GOLD_NUGGET, 1));
                                }
                        }
                }
        }
        }
        }

    How to add meta and lore to
    Code:
    if (diax.getType() == Material.GOLD_NUGGET) {
    And this event work only holding in your hands this item with meta and lore.
     
  2. Offline

    Wolfey

    Hi,

    If you're asking how you can set the lore of an item, you have to get the ItemMeta of the item like this:
    Code:java
    1.  
    2. diax.getItemMeta()
    3.  

    With this, you can set the Display name, and the lore

    To set the display name, you must get the item meta and setDisplayname(String); like this:
    Code:java
    1.  
    2. diax.getItemMeta().setDisplayName(ChatColor.RED + "This is a display name");
    3.  

    To set the lore, you must use a List<String>, so do this:
    Code:java
    1.  
    2. diax.getItemMeta().setLore(Arrays.asList(ChatColor.RED + "This is a lore"));
    3.  

    Now this is important, to set the meta data on the item, you must do this:
    Code:java
    1.  
    2. diax.setItemMeta(diax.getItemMeta());
    3.  

    And then it will set the item meta to whatever you put!
     
  3. OK, but i can use this event with normal GOLD_NUGGET.
    I want to only using this with GOLD_NUGGET with meta and lore.

    Do you understand me?

    And please help me with this
    Code:
    player.getInventory().removeItem(new ItemStack(Material.GOLD_NUGGET, 1));
    This is must remove item with GOLD_NUGGET with meta and lore.

    My code:

    Code:
        @EventHandler
        public void interact(PlayerInteractEvent event) {
                Player player = event.getPlayer();
                ItemStack diax = player.getItemInHand();
                if(player.hasPermission("swieta")){
                } else {
                    player.sendMessage(ChatColor.GREEN + "Masz juz VIP, chyba nie chcesz go stracic? ^_^");
                    player.sendMessage(ChatColor.AQUA + "Przedmiot mozesz zachowac albo sprzedac.");
                }
                if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                        if (diax.getType() == Material.GOLD_NUGGET) {
                            diax.getItemMeta().setDisplayName(ChatColor.GREEN + "Magiczny Przedmiot");
                            diax.getItemMeta().setLore(Arrays.asList(ChatColor.AQUA + "Kliknij PPM aby dostac:", ChatColor.WHITE + "Range " + ChatColor.GREEN + "VIP " + ChatColor.WHITE +  "na 7 dni."));
                            diax.setItemMeta(diax.getItemMeta());
                                if (player.getName() != null) {
                                    if(player.hasPermission("swieta")){
                                        event.setCancelled(true);
                                        getServer().dispatchCommand(Bukkit.getConsoleSender(), "azaddgroup " + player.getName() + " vip 7d");
                                        player.sendMessage(ChatColor.GREEN + "Dostales range VIP na 7 dni!");
                                        player.getInventory().removeItem(new ItemStack(Material.GOLD_NUGGET, 1));
                                }
                        }
                }
        }
        }
        }
    help?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  4. Offline

    NathanWolf

    You can use the code Wolfey provided to also check for the custom name or lore after you've added it- just use getItemMeta and look at the getLore list or getDisplayName string. Compare it to what you set it to when you created your item.

    Unfortunately I'm not too sure what you're trying to do - it sounds like you only want your interact event to work with a custom item, but you're also creating the custom item in the interact event?
     
  5. This is full code plugin:

    Show Spoiler

    Code:
        package pl.subtelny;
     
        import java.util.ArrayList;
     
     
     
     
     
     
    import java.util.Arrays;
     
        import net.milkbowl.vault.economy.Economy;
     
     
     
     
     
     
     
        import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
        public class Crafting extends JavaPlugin implements Listener
        {
        public static Economy economy = null;
     
        public void onEnable()
        {
        getServer().getPluginManager().registerEvents(this, this);
        getServer().getPluginManager().registerEvents(new Drop(this), this);
     
        ItemStack item = new ItemStack(Material.GOLD_NUGGET, 1);
        ItemMeta meta = item.getItemMeta();
     
        meta.setDisplayName(ChatColor.GREEN + "Magiczny Przedmiot");
     
        ArrayList<String> description = new ArrayList<String>();
        description.add(ChatColor.AQUA + "Kliknij PPM aby dostac:");
        description.add(ChatColor.WHITE + "Range " + ChatColor.GREEN + "VIP " + ChatColor.WHITE +  "na 7 dni.");
        meta.setLore(description);
        item.setItemMeta(meta);
     
        ShapedRecipe diax = new ShapedRecipe(item);
        diax.shape(new String[] { "ZZZ", "ZZZ", "ZZZ" });
        diax.setIngredient('Z', Material.GHAST_TEAR);
        getServer().addRecipe(diax);
        }
        @EventHandler
        public void interact(PlayerInteractEvent event) {
                Player player = event.getPlayer();
                ItemStack diax = player.getItemInHand();
                if(player.hasPermission("swieta")){
                } else {
                    player.sendMessage(ChatColor.GREEN + "Masz juz VIP, chyba nie chcesz go stracic? ^_^");
                    player.sendMessage(ChatColor.AQUA + "Przedmiot mozesz zachowac albo sprzedac.");
                }
                if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                        if (diax.getType() == Material.GOLD_NUGGET) {
                            diax.getItemMeta().setDisplayName(ChatColor.GREEN + "Magiczny Przedmiot");
                            diax.getItemMeta().setLore(Arrays.asList(ChatColor.AQUA + "Kliknij PPM aby dostac:", ChatColor.WHITE + "Range " + ChatColor.GREEN + "VIP " + ChatColor.WHITE +  "na 7 dni."));
                            diax.setItemMeta(diax.getItemMeta());
                                if (player.getName() != null) {
                                    if(player.hasPermission("swieta")){
                                        event.setCancelled(true);
                                        getServer().dispatchCommand(Bukkit.getConsoleSender(), "azaddgroup " + player.getName() + " vip 7d");
                                        player.sendMessage(ChatColor.GREEN + "Dostales range VIP na 7 dni!");
                                        player.getInventory().removeItem(new ItemStack(Material.GOLD_NUGGET, 1));
                                }
                        }
                }
        }
        }
        }
        


    I want to create plugin which, you create a item (paper). Click right the right mouse button on the create item to give player vip on 7 days.

    "Crafting item with vip is special paper"

    but if you want to have vip you must have special paper (not create with sugar cane just create with my crafting in code)
     
  6. Offline

    NathanWolf

    OK, I think I get it! So this all looks correct, except in your interact handler you should be *checking* for the item, not setting item properties.

    It might be easiest to just check the item name.. something like:

    Code:java
    1. @EventHandler
    2. public void interact(PlayerInteractEvent event) {
    3. Player player = event.getPlayer();
    4. ItemStack diax = player.getItemInHand();
    5. if(player.hasPermission("swieta")){
    6. } else {
    7. player.sendMessage(ChatColor.GREEN + "Masz juz VIP, chyba nie chcesz go stracic? ^_^");
    8. player.sendMessage(ChatColor.AQUA + "Przedmiot mozesz zachowac albo sprzedac.");
    9. }
    10. if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    11. if (diax.getType() == Material.GOLD_NUGGET) {
    12. ItemMeta itemMeta = diax.getItemMeta();
    13. if (itemMeta != null && itemMeta.getDisplayName().contains("Magiczny Przedmiot")) {
    14. if (player.getName() != null) {
    15. if(player.hasPermission("swieta")){
    16. event.setCancelled(true);
    17. getServer().dispatchCommand(Bukkit.getConsoleSender(), "azaddgroup " + player.getName() + " vip 7d");
    18. player.sendMessage(ChatColor.GREEN + "Dostales range VIP na 7 dni!");
    19. player.getInventory().removeItem(new ItemStack(Material.GOLD_NUGGET, 1));
    20. }
    21. }
    22. }
    23. }
    24. }
    25. }
     
    Subtelny likes this.
  7. OK, its work but.

    Item afer click doesnt destroy. (Item golden_nugget with meta)
    And i try use normal gold_nugget and this...

    Show Spoiler
    [​IMG]
     
  8. Offline

    NathanWolf

    Try player.removeItem(player.getItemInHand());
     
    Subtelny likes this.
  9. I use
    Code:
    event.getPlayer().getInventory().remove(event.getPlayer().getItemInHand());

    I try use normal "golden_nugget" (no meta) and shows errors

    [​IMG]
     
  10. Offline

    NathanWolf

    What's on line 70 of Crafting.java?

    And, actually, I think you can just do player.setItemInHand(null) to clear the item in their hand.
     
    Subtelny likes this.
  11. Crafting.java this is it (up)
     
  12. Offline

    NathanWolf

    Can you change the code tag to java? Or look in your editor for line #'s and see what's on line 70? I don't want to count :)

    It might not matter if you change it to setItemInHand(null), though I'm not sure.
     
  13. I dont see this.

    Code:
    2013-12-11 01:12:13 [SEVERE] Could not pass event PlayerInteractEvent to DG-Swieta v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
        at org.bukkit.craftbukkit.v1_6_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:190)
        at org.bukkit.craftbukkit.v1_6_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:160)
        at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:610)
        at net.minecraft.server.v1_6_R3.Packet15Place.handle(SourceFile:58)
        at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
        at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
        at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
        at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.NullPointerException
        at pl.subtelny.Crafting.interact(Crafting.java:70)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 16 more
     
  14. Offline

    NathanWolf

    This is the root of your issue- whatever is happening at line 70 of Crafting.java is accessing a null variable.

    You have all your code above, but it's inside a code tag inside a spoiler, there's some weird whitespace and no line numbers- so I really have no idea where line 70 would be.
     
Thread Status:
Not open for further replies.

Share This Page