How to add permissions to crafting

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

Thread Status:
Not open for further replies.
  1. 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 (open)

    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. }


    listener (open)

    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 ?
     
  2. 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.
    Show Spoiler

    Code (Text):
    1. public class DCListener implements Listener
    2.     {
    3.     FileConfiguration config;
    4.     List<String> permDisabledPermanently    = new ArrayList<String>();
    5.  
    6.     public DCListener(DCMain p)
    7.         {
    8.         this.config                        = p.config;
    9. //        this.permDisabledPermanently    = this.config.getStringList("permDisabledPermanently");//ÂÍÈÌÀÍÈÅ!!! Ïåðåìåííàÿ èíèöèàëèçèðóåòñÿ â êîíñòðóêòîðå, ò.å. âîçìîæíî, òîëüêî 1 ðàç
    10.         this.DCLReloadConfig();
    11.         }
    12.     public void DCLReloadConfig()
    13.         {
    14.         this.permDisabledPermanently    = this.config.getStringList("permDisabledPermanently");
    15.         for (int i=0;i<this.permDisabledPermanently.size();i++)
    16.             {
    17.             Log.debug("[DCDebug] found item in config:"+this.permDisabledPermanently.get(i));
    18.             }
    19.         }
    20.     @EventHandler
    21.     public void onCraftItem(CraftItemEvent e)
    22.         {
    23. //        Logger log                        = Logger.getLogger("Minecraft");
    24.         ItemStack item                    = e.getRecipe().getResult();
    25.         HumanEntity who                    = e.getWhoClicked();
    26.         //TODO: ãàäñòâî êàêîå-òî
    27.         String itemString                = item.getTypeId()+"."+item.getData().getData();
    28.         String itemString1                = item.getTypeId()+".-1";
    29.         String itemString2                = item.getTypeId()+"::"+item.getData().getData();
    30.         String itemString21                = item.getTypeId()+"::-1";
    31.         String worldName                = who.getLocation().getWorld().getName();
    32.         boolean cancelAction            = true;
    33. //TODO ??? ÷òî-òî íàäî ñäåëàòü....
    34.         if (this.permDisabledPermanently.contains(itemString2) ||
    35.                 this.permDisabledPermanently.contains(itemString21))
    36.             {
    37.             Log.debug("item disabled permanently!");
    38.             cancelAction    = true;
    39.             }
    40.         else
    41.             {
    42.             cancelAction    = false;
    43.             Log.debug("5.crafting allowed");
    44.             }
    45.  
    46.         if (who.hasPermission("disablecraft.enabled."+itemString) ||
    47.                 who.hasPermission("disablecraft.enabled."+itemString1)
    48.                 )
    49.             {
    50. //            log.info("[DC] crafting enabled, allowing");
    51.             cancelAction    = false;
    52.             Log.debug("1.crafting allowed");
    53.             }
    54.         else if (who.hasPermission("disablecraft.disabled."+itemString) ||
    55.                 who.hasPermission("disablecraft.disabled."+itemString1)
    56.                 )
    57.             {
    58.             cancelAction    = true;
    59. //            e.setCancelled(true);
    60. //            log.info("[DC] crafting stopped");
    61.             Log.debug("2.crafting disallowed");
    62.             }
    63.         else if (who.hasPermission("disablecraft."+worldName+".enabled."+itemString) ||
    64.                 who.hasPermission("disablecraft."+worldName+".enabled."+itemString1)
    65.                 )
    66.             {
    67.             Log.debug("3.crafting allowed");
    68.             cancelAction    = false;
    69.             }
    70.         else if (who.hasPermission("disablecraft."+worldName+".disabled."+itemString) ||
    71.                 who.hasPermission("disablecraft."+worldName+".disabled."+itemString1)
    72.                 )
    73.             {
    74.             cancelAction    = true;
    75. //            e.setCancelled(true);
    76.             Log.debug("4.crafting stopped in world "+worldName);
    77.             }
    78.         if (cancelAction)
    79.             {
    80.             e.setCancelled(cancelAction);
    81.             Log.detailed("crafting disallowed");
    82.             }
    83.         else
    84.             Log.detailed("crafting allowed");
    85.         //e.setCancelled(cancelAction);
    86.         }
    87.     }


    EDIT: Also thought I'd mention that your website looks pretty slick. I like it.
     
    JOPHESTUS likes this.
  3. I don't understand any of that :p
     
  4. ItemStack item = e.getRecipe().getResult();
    String itemString = item.getTypeId()+"."+item.getData().getData();
    if (who.hasPermission("disablecraft.enabled."+itemString)
     
Thread Status:
Not open for further replies.

Share This Page