Solved Adding permissions

Discussion in 'Plugin Development' started by mrdude123, Jul 11, 2015.

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

    mrdude123

    How would I add permissions to this class? This is a class that is running off of command executor.
    Code:
    package commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import tangster.vampyr.Vampyr;
    
    public class CommandVampyr
      implements CommandExecutor, Listener
    {
      private String swordName;
    
      public CommandVampyr(Vampyr plugin)
      {
        String color = plugin.getConfig().getString("Vampyr.swordColor");
        try {
          this.swordName = (ChatColor.valueOf(color.toUpperCase()) + plugin.getConfig().getString("Vampyr.swordName"));
        } catch (IllegalArgumentException e) {
          Bukkit.getConsoleSender().sendMessage("[Vampyr] " + color + " is not a valid color in the config file");
        }
      }
    
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
      {
        if (!(sender instanceof Player)) {
          sender.sendMessage(ChatColor.RED + "[Vampyr] Player only.");
          return false;
        }
    
        Player player = (Player)sender;
        if (label.equalsIgnoreCase("vampyr"))
        {
          ItemStack itemInHand = player.getItemInHand();
          ItemMeta meta = itemInHand.getItemMeta();
          if (itemInHand.getType().equals(Material.IRON_SWORD))
          {
            if ((meta.getDisplayName() != null) && (meta.getDisplayName().equals(this.swordName))) {
              player.sendMessage(ChatColor.RED + "[Vampyr] This sword is already a " + this.swordName);
            } else {
              meta.setDisplayName(this.swordName);
              itemInHand.setItemMeta(meta);
            }
          }
          else {
            player.sendMessage(ChatColor.RED + "[Vampyr] Command only works with an Iron Sword");
          }
        }
        return false;
      }
    }
     
  2. Offline

    Zombie_Striker

    getServer().getPluginManager().addPermission(new Permission("Perm.perm"));
     
  3. Offline

    Evaluations

    player.hasPermission(string)
     
  4. Offline

    mrdude123

    Disregard what I said, i;m now trying to make it so only OPs can do it. Why is this still not working?
    Code:
    package commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    import tangster.vampyr.Vampyr;
    
    public class CommandVampyr
      implements CommandExecutor
    {
      private String swordName;
    
      public CommandVampyr(Vampyr plugin)
      {
        String color = plugin.getConfig().getString("Vampyr.swordColor");
        try {
          this.swordName = (ChatColor.valueOf(color.toUpperCase()) + plugin.getConfig().getString("Vampyr.swordName"));
        } catch (IllegalArgumentException e) {
          Bukkit.getConsoleSender().sendMessage("[Vampyr] " + color + " is not a valid color in the config file");
        }
      }
    
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
      {
        if (!(sender instanceof Player)) {
          sender.sendMessage(ChatColor.RED + "[Vampyr] Player only.");
          return false;
        }
    
        Player player = (Player)sender;
        if (label.equalsIgnoreCase("vampyr"))
        {
            if(sender.isOp()){
          ItemStack itemInHand = player.getItemInHand();
          ItemMeta meta = itemInHand.getItemMeta();
          if (itemInHand.getType().equals(Material.IRON_SWORD))
          {
            if ((meta.getDisplayName() != null) && (meta.getDisplayName().equals(this.swordName))) {
              player.sendMessage(ChatColor.RED + "[Vampyr] This sword is already a " + this.swordName);
            } else {
              meta.setDisplayName(this.swordName);
              itemInHand.setItemMeta(meta);
            }
          }
          else {
            player.sendMessage(ChatColor.RED + "[Vampyr] Command only works with an Iron Sword");
          }
        }
        }
     
        return false;
    }
    }
    @Evaluations @Zombie_Striker

    Nevermind, I've fixed it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
Thread Status:
Not open for further replies.

Share This Page