I am trying to make it so players can not craft blocks that are banned.

Discussion in 'Plugin Development' started by JustGorilla, Apr 22, 2017.

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

    JustGorilla

    I have made a plugin where you can not place blocks unless you have permission and I made it so you can check the blocks that are banned, while fully configurable.

    My problem is that i want to make it so they cant craft it, I have made every good so far but when i try and do this code
    Code:
                      event.setCancelled(true);
    it give me the error that says:
    Multiple markers at this line
    - The method setCancelled(boolean) is undefined for the type
    PrepareItemCraftEvent
    - Line breakpoint:BlockListener [line: 52] - craftItem(PrepareItemCraftEvent)


    Here is the rest of the code!!! IF you can help that would be great!!
    Code:
        @EventHandler
      public void craftItem(PrepareItemCraftEvent event) {
          Material itemType = event.getRecipe().getResult().getType();
          Byte itemData = event.getRecipe().getResult().getData().getData();
         
            FileConfiguration config = Restriction.plugin.getConfig();
            Player plr = (Player) event.getInventory();
           
            String b = event.getRecipe().getClass().toString().toLowerCase();
           
            if (config.getStringList("bannedblocks").contains(b)) {
                if (!plr.hasPermission("block.allow." + b)) {
                      event.setCancelled(true);
     
  2. Offline

    timtower Administrator Administrator Moderator

    @JustGorilla That event can't be cancelled.
    Set the result to null instead.
     
  3. Offline

    JustGorilla

    Ok I will try it out.

    It gave me no errors but when I craft an item in miencraft that is banned it lets me craft it. That not what i want, and i dont have any permission so it should let me craft it!!!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 22, 2017
  4. Offline

    Zombie_Striker

    @JustGorilla @timtower
    Add debug messages. Is the event being called? Does it get past the first if statement? Are you sure you are no OP?
     
  5. Offline

    JustGorilla

    this is what my code is tell me what im going wrong:

    Code:
        @EventHandler
        public void craftItem(PrepareItemCraftEvent e) {
            Material itemType = e.getRecipe().getResult().getType();
            Byte itemData = e.getRecipe().getResult().getData().getData();
    
            FileConfiguration config = Restriction.plugin.getConfig();
            Player plr = (Player) e.getInventory();
    
            String b = e.getRecipe().getClass().toString().toLowerCase();
    
            if (config.getStringList("bannedblocks").contains(b)) {
                if (!plr.hasPermission("block.allow." + b)) {
                    e = null;
    
                }
            }
        }
    }
     
  6. Offline

    Ilomiswir

    You are setting the event to null, you have to set if im right(not tested) the result from e.getresult() to null
     
  7. Offline

    JustGorilla

    Code:
        @EventHandler
        public void craftItem(PrepareItemCraftEvent e) {
            Material itemType = e.getRecipe().getResult().getType();
            Byte itemData = e.getRecipe().getResult().getData().getData();
            e = null;
           
            FileConfiguration config = Restriction.plugin.getConfig();
            Player plr = (Player) e.getInventory();
    
            String b = e.getRecipe().getClass().toString().toLowerCase();
    
            if (config.getStringList("bannedblocks").contains(b)) {
                if (!plr.hasPermission("block.allow." + b)) {
                    e = null;
    
                }
            }
        }
    }
    this is what it is and still not working
     
  8. Offline

    timtower Administrator Administrator Moderator

    @JustGorilla e.setResult(null)
    Setting the event to null does nothing you want.
     
  9. Offline

    JustGorilla

    @timtower do you know anyway for me to do what i want?
     
  10. Offline

    timtower Administrator Administrator Moderator

  11. Offline

    JustGorilla

    @timtower that doest work it give me this error

    Multiple markers at this line
    - The method setResult(null) is undefined for the type
    PrepareItemCraftEvent
    - Line breakpoint:BlockListener [line: 53] -
    craftItem(PrepareItemCraftEvent)
     
  12. Offline

    timtower Administrator Administrator Moderator

  13. Offline

    JustGorilla

    @timtower
    Code:
        @EventHandler
        public void craftItem(PrepareItemCraftEvent e) {
    
            FileConfiguration config = Restriction.plugin.getConfig();
            Player plr = (Player) e.getInventory();
    
            String b = e.getRecipe().getClass().toString().toLowerCase();
    
            if (config.getStringList("bannedblocks").contains(b)) {
                    Material itemType = e.getRecipe().getResult().getType();
                    Byte itemData = e.getRecipe().getResult().getData().getData();
                    if (!plr.hasPermission("block.allow." + b)) {
                        e.getInventory().setResult(new ItemStack(Material.AIR));
                        for(HumanEntity he:e.getViewers()) {
                            if(he instanceof Player) {
                                ((Player)he).sendMessage(ChatColor.RED+"You cannot craft this!");
                                getInventory().setResult(null)
    
    gives me an error code
     
  14. Offline

    timtower Administrator Administrator Moderator

    @JustGorilla Do not just blindly copy paste what I post.
    It generally has to do with your code.
    So getInventory is a method for the PrepareItemCraftEvent.
     
  15. Offline

    JustGorilla

    @timtower I know that but how would i do about in putting that into the code, I use to code like 2 years ago and i just started getting back into and I dont remember anything, about it, so i need someone help with this.
    thanks you.
     
  16. Offline

    timtower Administrator Administrator Moderator

    @JustGorilla How about e.getInventory().setResult(null)
    And why are you casting an CraftingInventory to a Player?

    I do suggest taking a look at reading Javadocs again though.
     
  17. Offline

    JustGorilla

    @timtower so i can do if player has permission, and it still lets me craft it

    Code:
        @EventHandler
        public void craftItem(PrepareItemCraftEvent e) {
    
            FileConfiguration config = Restriction.plugin.getConfig();
            Player plr = (Player) e.getInventory();
    
            String b = e.getRecipe().getClass().toString().toLowerCase();
    
            if (config.getStringList("bannedblocks").contains(b)) {
                    Material itemType = e.getRecipe().getResult().getType();
                    Byte itemData = e.getRecipe().getResult().getData().getData();
                    if (!plr.hasPermission("block.allow." + b)) {
                        e.getInventory().setResult(new ItemStack(Material.AIR));
                        for(HumanEntity he:e.getViewers()) {
                            if(he instanceof Player) {
                                ((Player)he).sendMessage(ChatColor.RED+"You cannot craft this!");
                                e.getInventory().setResult(null);
    
     
  18. Offline

    Zombie_Striker

    @JustGorilla
    1. Do you know if the event is being called?
    2. Do you know if the the first if statement is true? Are you sure the string is in the string list
    3. Do you know if the second if statement is true? Does not matter if you know you have/don't have the perm, does it say it is true?
     
  19. Offline

    JustGorilla

    @Zombie_Striker yes its being called on with this code
    Code:
            getServer().getPluginManager().registerEvents(this, this);
    and yes its in the config and i dont understand what you mean, i want to check if they dont have the permission then it removes the item

    @timtower hey thanks for the help so far i have a question.
    whats is wrong with this???
    Code:
        @EventHandler
        public void craftItem(PrepareItemCraftEvent e) {
    
            FileConfiguration config = Restriction.plugin.getConfig();
           
            Player plr = (Player) e.getInventory();
            String b = e.getRecipe().getResult().getType().toString().toLowerCase();
       
            if (config.getStringList("bannedblocks").contains(b)) {
                if (!plr.hasPermission("block.allow." + b)) {
                    e.getInventory().setResult(new ItemStack(Material.AIR));
                  
                    ///HAS TO WORK
                    for(HumanEntity he:e.getViewers()) {
                        if(he instanceof Player) {
                            ((Player)he).sendMessage(ChatColor.RED+"You cannot craft this!");
                            e.getInventory().setResult(null);
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 24, 2017
  20. Offline

    timtower Administrator Administrator Moderator

    @JustGorilla The fact that you are still casting an Inventory to a Player.
    Which will throw a ClassCastException
     
  21. Offline

    JustGorilla

    @timtower i know the problem is what do i set the cast to then i cant do .getPlayer that event doest allow me too, what would i set it too.
     
  22. Offline

    timtower Administrator Administrator Moderator

  23. Offline

    JustGorilla

    still letting me craft,

    Code:
        @EventHandler
        public void craftItem(PrepareItemCraftEvent e) {
    
            FileConfiguration config = Restriction.plugin.getConfig();
           
            Player plr = (Player) e.getInventory().getHolder();
            String b = e.getRecipe().getResult().getType().toString().toLowerCase();
       
            if (config.getStringList("bannedblocks").contains(b)) {
                if (!plr.hasPermission("block.allow." + b)) {
                    e.getInventory().setResult(new ItemStack(Material.AIR));
                  
                    ///HAS TO WORK
                    for(HumanEntity he:e.getViewers()) {
                        if(he instanceof Player) {
                            ((Player)he).sendMessage(ChatColor.RED+"You cannot craft this!");
                            e.getInventory().setResult(null);
    
                        }
                    }
                }
            }
        }
    }
     
  24. Offline

    Caderape2

    @JustGorilla you don't need the last part since you have the player.
    did you try to debug your code for where it reaches ?
     
  25. Offline

    JustGorilla

    @Caderape2 I try my best and I had one of my friends look at it really well and we could not figure it out.
    Can you try and help me
     
  26. Offline

    Caderape2

    @JustGorilla add broadcast message after every action, then you will be able to know where the code stop
     
  27. Offline

    JustGorilla

    @Caderape2 I did at the beginning and it didnt work.
     
Thread Status:
Not open for further replies.

Share This Page