Disable Crafting for adventure / loot based servers?

Hey, so I saw this while I was looking for a solution to disable all crafting. Its a very simple plugin that disables players ability to right click a workbench. Not sure if that is really what it does, but if someone wanted to convert this i think a lot of people would benefit from it. As i could see crafting ruining adventure and loot based servers, or trading based servers in general.

Source Code:

package com.gerov.no;

import org.bukkit.Bukkit;
import org.bukkit.Material;
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.plugin.java.JavaPlugin;

public class main extends JavaPlugin implements Listener {

    public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
    }
   
    public void onDisable(){
           
    }
   
    @EventHandler
    public void onRightClick(PlayerInteractEvent e){
            if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK)||e.getAction().equals(Action.RIGHT_CLICK_AIR)){
                    if(e.getClickedBlock().getType() == Material.WORKBENCH){
                            e.setCancelled(true);
                    }
            }
    }

}

With that they can still craft using the 2x2 grid in their inventory, but I’ll port it over I guess.

I kinda forgot about that, it isn’t a big deal i guess. I think removing or deactivating the 2x2 grid would be alot more complicated since its not a simple right click it involves the gui and what not. I know there is a mod that manages recipes, i could just try and disable the 2x2 ones, or just then ones that would impact adventure / rpg games the most.

I appreciate you helping me, hopefully people will fine this useful :D.

This is how I did it in ProtectionPerms, handles both workbench and player inventories :smiley:

@Listener
public void onItemCraft(ClickInventoryEvent event, @Root Player player, @Getter("getTargetInventory") Inventory inventory) {

    if (inventory.getArchetype() == InventoryArchetypes.PLAYER || inventory.getArchetype() == InventoryArchetypes.WORKBENCH) {

            Inventory craftingOutputs = inventory.query(CraftingOutput.class);

            craftingOutputs.slots().forEach(slot -> slot.peek().ifPresent(itemStack -> {
                String itemId = itemStack.getItem().getType().getId();
                if (!player.hasPermission("protectionperms.item.craft." + itemId)) {
                    event.setCancelled(true);
                    player.sendMessage(ChatTypes.ACTION_BAR, Text.of(TextColors.RED, "You don't have permission to craft " + itemId + '!'));
                }
            }));
        }
    }

Hmm I guess I could put in something like that to the ported plugin but using protectionperms might be better :stuck_out_tongue:

Apparently CraftingOutput is any possible crafting output - it doesn’t mean that the player actually crafted the item. I have yet to find any way to figure out if the player actually crafted anything, which is just completely ridiculous.

I would love to hear from someone knowledgeable about this as to how you’re supposed to listen for crafting events

To my knowledge there are no specific crafting events, the only way to do it is to listen to the inventory. Listening to player and workbench inventory clicks is the best you can get as far as I know, though that should catch every time a player crafts something I think. This may of course however not work properly for modded inventories.