Permissions

Discussion in 'Spigot Plugin Development' started by MrHardyCZ, Apr 18, 2016.

  1. Hi. How i can add permissions to craft to this code? :) Please help me. Thanks.

    Code (Text):
    private void Elytra() {
            if (getConfig().getString("Elytra").equals("true")) {
      ItemStack item = new ItemStack(Material.ELYTRA, (short)1);
      ShapedRecipe r = new ShapedRecipe(item);
      r.shape(new String[] { "***", "*%*", "*%*" });
      r.setIngredient('*', Material.IRON_INGOT);
      r.setIngredient('%', Material.LEATHER);
      getServer().addRecipe(r);
        }
      }
     
  2. PlayerCraftEvent i hope im not wrong.
     
  3. Literally do:

    if(player.hasPermission("your.Permission"){
    //do stuff
    }

    Hope I am not wrong :/
     
  4. Something like this I believe :p:rolleyes: hope i helped!
     
  5. NOPE, this is adding the recipe to the server, it doesnt make the player craft it or not
     
  6. Oops, darn, you're right. Hopefully I can redeem with this :oops::D

    Code (Text):
    PrepareItemCraftEvent
    use getRecipe(), check if it's instance of your recipe type and check if it's your recipe... then get the player by using (Player)getView().getPlayer() and then check if he doesn't have the permission, set the result to null and you're done !
     
  7. Thanks. Please, do you write it in code? :-D
     
  8. Get booleans from the config with getConfig#getBoolean("Elytra");
     
    • Like Like x 1
  9. This is a terrible way to check if its true (Well not terrible, but its not good either)
    Do as @Grossen said and use #getBoolean
     
  10. Ok, thanks! But i asking you with help with permissions :D
     
  11. Surround it with that

    or

    if player doesn't have the permission return
     
  12. i'll try this.
     
  13. I have player red lined. PS: I'm Noob. :D
     
  14. May you show me your code?
     
  15. If you're a noob should you maybe try to learn a bit more Java before you start making plugins with the Bukkit API..

    And it doesn't help that you say that you're a noob, we won't spoonfeed you with code.
     
  16. package cz.MrHardy.example;

    import java.io.File;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin
    implements Listener
    {
    public void onEnable()
    {
    getServer().getPluginManager().registerEvents(this, this);
    File file = new File(getDataFolder(), "config.yml");
    if (!file.exists()) {
    saveDefaultConfig();
    }
    getLogger().info(ChatColor.GREEN + "Craftables has been enabled");
    }

    public void onDisable() {
    getLogger().info(ChatColor.RED + "Craftables has been disabled");
    }

    public void addRecipes() { Elytra(); }
    private void Elytra() {
    if (getConfig().getString("Elytra").equals("true")) {
    ItemStack item = new ItemStack(Material.ELYTRA, (short)1);
    ShapedRecipe rc = new ShapedRecipe(item);
    rc.shape(new String[] { "***", "*%*", "*%*" });
    rc.setIngredient('*', Material.IRON_INGOT);
    rc.setIngredient('%', Material.LEATHER);
    getServer().addRecipe(rc);
    }
    }
    }
     
  17. private void Elytra() {
    if (getConfig().getString("Elytra").equals("true")) {
    if(player.hasPermission("elytra.use"){
    ItemStack item = new ItemStack(Material.ELYTRA, (short)1);
    ShapedRecipe r = new ShapedRecipe(item);
    r.shape(new String[] { "***", "*%*", "*%*" });
    r.setIngredient('*', Material.IRON_INGOT);
    r.setIngredient('%', Material.LEATHER);
    getServer().addRecipe(r);
    }
    }
    }



    I hope i'm not wrong!
     
  18. The if (!file.exists()) is not needed as this is what the saveDefaultConfig method does already
    Its good except your still not getting the boolean from the config.
     

  19. Ok.....