get crafting item and execute block code

Discussion in 'Plugin Development' started by Veyn12, Mar 5, 2021.

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

    Veyn12

    Hello, I am from the Russian community, I am addressing here, tk. I'm sure you have more thoughts and energy in terms of the developer)

    In general, to the point.
    I need the code:
    The player is crafting an item, making a check. If this item == let's say a wooden pickaxe, then a block of code is executed, for example sendMessage ("You crafted a pickaxe");

    I kind of know this is done by the CraftingItemEvent, but all my attempts to do this have been unsuccessful. I hope for your help, preferably all the code from the beginning of the event method to its end)
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Veyn12 Please post what code you have tried before.
     
  3. Offline

    Veyn12

    Yes, it was not in principle, or rather it was, but I picked up checks from the bulldozer)
    Here's what I tried

    @EventHandler
    public void onPlayerCraft(CraftItemEvent e){
    Player p = (Player) e.getWhoClicked();
    if(e.getInventory().contains(Material.WOODEN_PICKAXE)) {
    p.sendMessage("Omg, Pickaxe is crafting!");
    }
    }
     
  4. Offline

    timtower Administrator Administrator Moderator

  5. Offline

    Veyn12

  6. Offline

    timtower Administrator Administrator Moderator

    Not gonna spoonfeed.
     
  7. Offline

    Veyn12

    Please =)
    all the same, knowing myself, I will not understand the documentation, only if the person does not prompt or help
     
  8. Offline

    timtower Administrator Administrator Moderator

    getRecipe() getResult() getType()==wooden_pickaxe?
     
  9. Offline

    Veyn12

    If the code is correct, then it does not work. If not faithful, then what is wrong with it?

    @EventHandler
    public void onPlayerCraft(CraftItemEvent e){
    if(e.getInventory().getRecipe().getResult().getType() == Material.WOODEN_PICKAXE) {
    Bukkit.broadcastMessage("hi");
    }
    }
     
  10. Offline

    timtower Administrator Administrator Moderator

  11. Offline

    Veyn12

    Code:
     package craft.battle;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.CraftItemEvent;
    
    public class CBevent implements Listener {
        private Main main;
        public CBevent(Main main) {
            this.main = main;
        }
       
        @EventHandler
        public void onPlayerCraft(CraftItemEvent e){
           if(e.getInventory().getRecipe().getResult().getType() == Material.WOODEN_PICKAXE) {
              Bukkit.broadcastMessage("hi");
           }
        }
    }
     
    Last edited by a moderator: Mar 5, 2021
  12. Offline

    timtower Administrator Administrator Moderator

    @Veyn12 Do you call registerEvent somewhere?
     
  13. Offline

    Veyn12

    I do not think that the error lies in registering the event, because the same "PlayerJoinEvent" event worked fine for me, displaying messages. But for craft not, here is the registration of the event in the main class
    Code:
    public class Main extends JavaPlugin implements Listener{
       
    
        ConfigArena area;
        private HashMap<Player, Player> invite = new HashMap<Player, Player>();
        public HashMap<Player,Player> getInvite(){
            return invite;
        }
       
    
        public void onEnable() {
            loadConfigArena();
            Bukkit.getServer().getPluginManager().registerEvents(new CBevent(this), this);
            getCommand("cb").setExecutor(new CBcommand(this));
            File config = new File(getDataFolder() + File.separator + "config.yml");
            if(!config.exists()) {
            getConfig().options().copyDefaults(true);
            saveDefaultConfig();
            }
           
        }
       
        public void onDisable() {
            Bukkit.getLogger().info(ChatColor.RED + "Disabled");
           
            saveConfig();
            try {
                area.arenacfg.save(area.arenafile);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public void loadConfigArena() {
            area = new ConfigArena();
            area.setup();
        }
    }
     
    Last edited by a moderator: Mar 5, 2021
  14. Offline

    timtower Administrator Administrator Moderator

    @Veyn12 Then start debugging the event, print message when it should get called, regardless of what you are crafting.
     
  15. Offline

    Veyn12

    Yes, I removed the check for some installed item and everything works. If someone is crafting something - display the text in the chat, everything will work. Crafted a workbench - displayed "hello". It's about the verification itself
     
  16. Offline

    timtower Administrator Administrator Moderator

    Then show what has bee crafted.
     
  17. Offline

    Veyn12

    Weed need to somehow use the validation parameters correctly, because we are not doing something, but I don't know how... =(


    Well, how do you correctly check for a specific item?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 6, 2021
Thread Status:
Not open for further replies.

Share This Page