How to add permissions to crafting

Discussion in 'Plugin Development' started by JOPHESTUS, Aug 6, 2012.

  1. Online

    JOPHESTUS

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Hey guys,

    I want to add to my plugin permissions to craft certain recipes, I'v tried to use the code on this page Help!!!! | Bukkit Forums. Here's all my code, please help meh


    Code:java
    1.  
    2. package me.jophestus.JOPHCraft;
    3.  
    4. import java.util.logging.Logger;
    5.  
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Material;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.inventory.ItemStack;
    12. import org.bukkit.inventory.ShapedRecipe;
    13. import org.bukkit.plugin.PluginDescriptionFile;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16.  
    17. public class JOPHCraft extends JavaPlugin {
    18. Logger log = Logger.getLogger("Minecraft");
    19. PluginDescriptionFile pdfFile;
    20. public final JOPHCraftListener Listener = new JOPHCraftListener(this);
    21. public static JOPHCraft plugin;
    22.  
    23.  
    24. public void onEnable() {
    25. pdfFile = this.getDescription();
    26. this.log.info(pdfFile.getName() + " is now enabled!.");
    27. setupConfig();
    28. if (getConfig().getBoolean("enable.ice", true)){
    29. ShapedRecipe ice = new ShapedRecipe(new ItemStack(Material.ICE, 2));
    30. ice.shape(new String[] { "ABA" });
    31. ice.setIngredient('A', Material.SNOW_BLOCK);
    32. ice.setIngredient('B', Material.WATER_BUCKET);
    33. getServer().addRecipe(ice);
    34. }
    35. if (getConfig().getBoolean("enable.grass", true)){
    36. ShapedRecipe grass = new ShapedRecipe(new ItemStack(Material.GRASS, 1));
    37. grass.shape(new String[] { "A", "B" });
    38. grass.setIngredient('A', Material.LEAVES);
    39. grass.setIngredient('B', Material.DIRT);
    40. getServer().addRecipe(grass);
    41. }
    42. if (getConfig().getBoolean("enable.web", true)){
    43. ShapedRecipe cobweb = new ShapedRecipe(new ItemStack(Material.WEB, 2));
    44. cobweb.shape(new String[] { "A A", " B ", "A A" });
    45. cobweb.setIngredient('A', Material.STRING);
    46. cobweb.setIngredient('B', Material.SPIDER_EYE);
    47. getServer().addRecipe(cobweb);
    48. }
    49. if (getConfig().getBoolean("enable.blazerod", true)){
    50. ShapedRecipe blaze = new ShapedRecipe(new ItemStack(Material.BLAZE_ROD, 1));
    51. blaze.shape(new String[] { "A", "B", "B" });
    52. blaze.setIngredient('A', Material.LAVA_BUCKET);
    53. blaze.setIngredient('B', Material.GOLD_INGOT);
    54. getServer().addRecipe(blaze);
    55. }
    56. if (getConfig().getBoolean("enable.sponge", true)){
    57. ShapedRecipe sponge = new ShapedRecipe(new ItemStack(Material.SPONGE, 2));
    58. sponge.shape(new String[] { "ABA", "BAB", "ABA" });
    59. sponge.setIngredient('A', Material.STRING);
    60. sponge.setIngredient('B', Material.WOOL, 4);
    61. getServer().addRecipe(sponge);
    62. }
    63. if (getConfig().getBoolean("enable.slimeball", true)){
    64. ShapedRecipe slimeball = new ShapedRecipe(new ItemStack(Material.SLIME_BALL, 1));
    65. slimeball.shape(new String[] { "ABC", " D " });
    66. slimeball.setIngredient('A', Material.WATER_BUCKET);
    67. slimeball.setIngredient('B', Material.SAPLING);
    68. slimeball.setIngredient('C', Material.INK_SACK, 15);
    69. slimeball.setIngredient('D', Material.INK_SACK, 2);
    70. getServer().addRecipe(slimeball);
    71. }
    72. if (getConfig().getBoolean("enable.ghasttear", true )){
    73. ShapedRecipe ghasttear = new ShapedRecipe(new ItemStack(Material.GHAST_TEAR, 1));
    74. ghasttear.shape(new String[] { "ABA" });
    75. ghasttear.setIngredient('A', Material.SOUL_SAND);
    76. ghasttear.setIngredient('B', Material.WATER_BUCKET);
    77. getServer().addRecipe(ghasttear);
    78. }
    79. if (getConfig().getBoolean("enable.mobspawner", true)){
    80. ShapedRecipe spawner = new ShapedRecipe(new ItemStack(Material.MOB_SPAWNER, 1));
    81. spawner.shape(new String[] { "ABA", "ACA", "ABA" });
    82. spawner.setIngredient('A', Material.IRON_INGOT);
    83. spawner.setIngredient('B', Material.LAVA_BUCKET);
    84. spawner.setIngredient('C', Material.DIAMOND);
    85. getServer().addRecipe(spawner);
    86. }
    87. if (getConfig().getBoolean("enable.mossycobble", true)){
    88. ShapedRecipe mossy = new ShapedRecipe(new ItemStack(Material.MOSSY_COBBLESTONE, 1));
    89. mossy.shape(new String[] { "A", "B" });
    90. mossy.setIngredient('A', Material.VINE);
    91. mossy.setIngredient('B', Material.COBBLESTONE);
    92. getServer().addRecipe(mossy);
    93. }
    94. if (getConfig().getBoolean("enable.netherwart", true)){
    95. ShapedRecipe wart = new ShapedRecipe(new ItemStack(Material.NETHER_WARTS, 2));
    96. wart.shape(new String[] { "AAA", "ABA", "CCC"});
    97. wart.setIngredient('A', Material.NETHERRACK);
    98. wart.setIngredient('B', Material.IRON_INGOT);
    99. wart.setIngredient('C', Material.SOUL_SAND);
    100. getServer().addRecipe(wart);
    101. }
    102. if (getConfig().getBoolean("enable.helmetcrafting", true)){
    103. ShapedRecipe JOPHhelmet = new ShapedRecipe(new ItemStack(Material.IRON_INGOT, 5));
    104. JOPHhelmet.shape(new String[] { "B"});
    105. JOPHhelmet.setIngredient('B', Material.IRON_HELMET);
    106. getServer().addRecipe(JOPHhelmet);
    107. }
    108. if (getConfig().getBoolean("enable.swordcrafting", true)){
    109. ShapedRecipe JOPHSword = new ShapedRecipe(new ItemStack(Material.IRON_INGOT, 2));
    110. JOPHSword.shape(new String[] { "B"});
    111. JOPHSword.setIngredient('B', Material.IRON_SWORD);
    112. getServer().addRecipe(JOPHSword);
    113. }
    114. if (getConfig().getBoolean("enable.mossybrick", true)){
    115. ShapedRecipe mossystone = new ShapedRecipe(new ItemStack(Material.SMOOTH_BRICK, 1, (byte)1));
    116. mossystone.shape(new String[] { "A", "B"});
    117. mossystone.setIngredient('B', Material.SMOOTH_BRICK);
    118. mossystone.setIngredient('A', Material.VINE);
    119. getServer().addRecipe(mossystone);
    120. }
    121.  
    122. }
    123.  
    124. private void setupConfig() {
    125. // TODO Auto-generated method stub
    126. getConfig().options().copyDefaults(true);
    127. saveDefaultConfig();
    128. }
    129.  
    130. @Override
    131. public boolean onCommand(CommandSender sender, Command command,
    132. String label, String[] args) {
    133. // TODO Auto-generated method stub
    134.  
    135. if (command.getName().equalsIgnoreCase("JOPHCraft")) {
    136. sender.sendMessage(ChatColor.DARK_GREEN
    137. + "+------------------------------+");
    138. sender.sendMessage(ChatColor.DARK_AQUA + "JOPHCraft: Custom recipes plugin.");
    139. sender.sendMessage(ChatColor.GREEN + "By JOPHESTUS");
    140. sender.sendMessage(ChatColor.YELLOW + "Version:" + " 1.3" );
    141. sender.sendMessage(ChatColor.DARK_GREEN
    142. + "+------------------------------+");
    143. }
    144.  
    145.  
    146.  
    147.  
    148.  
    149. if (command.getName().equalsIgnoreCase("jchelp")) {
    150. sender.sendMessage(ChatColor.DARK_GREEN + "+------------------------------+");
    151. sender.sendMessage(ChatColor.DARK_AQUA + "JOPHCraft: To view recipies, please read bukkitdev page.");
    152. sender.sendMessage(ChatColor.DARK_GREEN + "+------------------------------+"); }
    153. return super.onCommand(sender, command, label, args);
    154.  
    155. }
    156.  
    157. public void onDisable() {
    158.  
    159. pdfFile = this.getDescription();
    160. this.log.info(pdfFile.getName() + " is now disabled!");
    161. }
    162. }



    Code:java
    1.  
    2.  
    3. package me.jophestus.JOPHCraft;
    4.  
    5.  
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.inventory.CraftItemEvent;
    9.  
    10. public class JOPHCraftListener
    11. implements Listener
    12. {
    13. public static JOPHCraft plugin;
    14.  
    15. public JOPHCraftListener(JOPHCraft instance)
    16. {
    17. plugin = instance;
    18. }
    19.  
    20. @EventHandler
    21. public void onPlayerCraft(CraftItemEvent e){
    22. if(e.getRecipe().equals("ice") && !e.getWhoClicked().hasPermission("jophcraft.ice")){
    23. e.setCancelled(true);
    24.  
    25. }
    26. }
    27.  
    28.  
    29.  
    30.  
    31. }
    32.  
    33.  


    @evilmidget38 ?

    This post has been edited 2 times. It was last edited by JOPHESTUS Aug 6, 2012.
  2. Offline

    evilmidget38 BukkitDev Staff BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    This code is from the plugin Ban Recipe, or something like that. Source was taken from the .jar file. It should show you want you want.

    Code:
    public class DCListener implements Listener
        {
        FileConfiguration config;
        List<String> permDisabledPermanently    = new ArrayList<String>();
     
        public DCListener(DCMain p)
            {
            this.config                        = p.config;
    //        this.permDisabledPermanently    = this.config.getStringList("permDisabledPermanently");//ÂÍÈÌÀÍÈÅ!!! Ïåðåìåííàÿ èíèöèàëèçèðóåòñÿ â êîíñòðóêòîðå, ò.å. âîçìîæíî, òîëüêî 1 ðàç
            this.DCLReloadConfig();
            }
        public void DCLReloadConfig()
            {
            this.permDisabledPermanently    = this.config.getStringList("permDisabledPermanently");
            for (int i=0;i<this.permDisabledPermanently.size();i++)
                {
                Log.debug("[DCDebug] found item in config:"+this.permDisabledPermanently.get(i));
                }
            }
        @EventHandler
        public void onCraftItem(CraftItemEvent e)
            {
    //        Logger log                        = Logger.getLogger("Minecraft");
            ItemStack item                    = e.getRecipe().getResult();
            HumanEntity who                    = e.getWhoClicked();
            //TODO: ãàäñòâî êàêîå-òî
            String itemString                = item.getTypeId()+"."+item.getData().getData();
            String itemString1                = item.getTypeId()+".-1";
            String itemString2                = item.getTypeId()+"::"+item.getData().getData();
            String itemString21                = item.getTypeId()+"::-1";
            String worldName                = who.getLocation().getWorld().getName();
            boolean cancelAction            = true;
    //TODO ??? ÷òî-òî íàäî ñäåëàòü....
            if (this.permDisabledPermanently.contains(itemString2) ||
                    this.permDisabledPermanently.contains(itemString21))
                {
                Log.debug("item disabled permanently!");
                cancelAction    = true;
                }
            else
                {
                cancelAction    = false;
                Log.debug("5.crafting allowed");
                }
     
            if (who.hasPermission("disablecraft.enabled."+itemString) ||
                    who.hasPermission("disablecraft.enabled."+itemString1)
                    )
                {
    //            log.info("[DC] crafting enabled, allowing");
                cancelAction    = false;
                Log.debug("1.crafting allowed");
                }
            else if (who.hasPermission("disablecraft.disabled."+itemString) ||
                    who.hasPermission("disablecraft.disabled."+itemString1)
                    )
                {
                cancelAction    = true;
    //            e.setCancelled(true);
    //            log.info("[DC] crafting stopped");
                Log.debug("2.crafting disallowed");
                }
            else if (who.hasPermission("disablecraft."+worldName+".enabled."+itemString) ||
                    who.hasPermission("disablecraft."+worldName+".enabled."+itemString1)
                    )
                {
                Log.debug("3.crafting allowed");
                cancelAction    = false;
                }
            else if (who.hasPermission("disablecraft."+worldName+".disabled."+itemString) ||
                    who.hasPermission("disablecraft."+worldName+".disabled."+itemString1)
                    )
                {
                cancelAction    = true;
    //            e.setCancelled(true);
                Log.debug("4.crafting stopped in world "+worldName);
                }
            if (cancelAction)
                {
                e.setCancelled(cancelAction);
                Log.detailed("crafting disallowed");
                }
            else
                Log.detailed("crafting allowed");
            //e.setCancelled(cancelAction);
            }
        }


    EDIT: Also thought I'd mention that your website looks pretty slick. I like it.

    This post has been edited 1 time. It was last edited by evilmidget38 Aug 6, 2012.
    JOPHESTUS likes this.
  3. Online

    JOPHESTUS

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    I don't understand any of that :p
  4. Offline

    evilmidget38 BukkitDev Staff BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    ItemStack item = e.getRecipe().getResult();
    String itemString = item.getTypeId()+"."+item.getData().getData();
    if (who.hasPermission("disablecraft.enabled."+itemString)

Share This Page