Buying Permission Nodes

Discussion in 'Plugin Development' started by Nerdfuryz, Aug 21, 2015.

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

    Nerdfuryz

    I am trying to make it so when you click a block in a gui you buy a permission with some in game currency. Everything works great till here:
    Code:
            switch(event.getCurrentItem().getType()){
            case DIAMOND_BLOCK:
                  if(player.hasPermission("essentials.lightning")){
                      player.sendMessage("You already have this permission");
                  }else if(econ.has(player.getName(), 2000000)){
                      econ.withdrawPlayer(player.getName(), 2000000);
                      perms.playerAdd(player, "essentials.lightning");
                      player.sendMessage(ChatColor.GOLD + "[DranNodes] " +
                        ChatColor.GREEN +
                        "You have sucessfully bought " + "/lightning!");
                     
                  }

    Here is the rest of the code:
    Code:
    package me.NerdFuryz.commandshop;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    import net.milkbowl.vault.economy.Economy;
    
    public class commandshop extends JavaPlugin implements Listener{
        public final Logger logger = Logger.getLogger("Minecraft");
       
        public static commandshop plugin;
        public static Permission perms = null;
        public static Economy econ = null;
        @Override
        public void onDisable(){
            PluginDescriptionFile pdfFile = getDescription();
            logger.info(pdfFile.getName() + " Has Been Disabled!");
        }
       
        @Override
        public void onEnable(){
               if (!setupEconomy())
                {
                  this.logger.info(String.format(
                    "[%s] - Disabled due to no Vault dependency found!", new Object[] {
                    getDescription().getName() }));
                  getServer().getPluginManager().disablePlugin(this);
                  return;
                }
              
            PluginDescriptionFile pdfFile = getDescription();
            logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + "Has Been Enabled!");
            getServer().getPluginManager().registerEvents(this, this);
            setupPermissions();
        }
    
        private void openGUI(Player player){
            Inventory inv = Bukkit.createInventory(null, 9, ChatColor.DARK_GREEN + "Commandshop");
            ItemStack commandOne = new ItemStack(Material.DIAMOND_BLOCK);
            ItemMeta commandOneMeta = commandOne.getItemMeta();
            ItemStack commandTwo = new ItemStack (Material.DIAMOND_HELMET);
            ItemMeta commandTwoMeta = commandTwo.getItemMeta();
            ItemStack commandThree = new ItemStack(Material.DIAMOND_CHESTPLATE);
            ItemMeta commandThreeMeta = commandThree.getItemMeta();
            ItemStack commandFour = new ItemStack (Material.DIAMOND_LEGGINGS);
            ItemMeta commandFourMeta = commandFour.getItemMeta();
            ItemStack commandFive = new ItemStack(Material.DIAMOND_BOOTS);
            ItemMeta commandFiveMeta = commandFive.getItemMeta();
            ItemStack commandSix = new ItemStack (Material.DIAMOND_SPADE);
            ItemMeta commandSixMeta = commandSix.getItemMeta();
            ItemStack commandSeven = new ItemStack(Material.DIAMOND_AXE);
            ItemMeta commandSevenMeta = commandSeven.getItemMeta();
            ItemStack commandEight = new ItemStack (Material.DIAMOND_HOE);
            ItemMeta commandEightMeta = commandEight.getItemMeta();
            ItemStack commandNine = new ItemStack (Material.DIAMOND_SWORD);
            ItemMeta commandNineMeta = commandNine.getItemMeta();
           
           
            commandOneMeta.setDisplayName(ChatColor.DARK_RED + "/Lightning");
            commandOne.setItemMeta(commandOneMeta);
           
            commandTwoMeta.setDisplayName(ChatColor.GREEN + "/Enderchest");
            commandTwo.setItemMeta(commandTwoMeta);
           
            commandThreeMeta.setDisplayName(ChatColor.GREEN + "/Ptime");
            commandThree.setItemMeta(commandThreeMeta);
           
            commandFourMeta.setDisplayName(ChatColor.GREEN + "/Pweather");
            commandFour.setItemMeta(commandFourMeta);
           
            commandFiveMeta.setDisplayName(ChatColor.GREEN + "/Top");
            commandFive.setItemMeta(commandFiveMeta);
           
            commandSixMeta.setDisplayName(ChatColor.GREEN + "/Invsee");
            commandSix.setItemMeta(commandSixMeta);
           
            commandSevenMeta.setDisplayName(ChatColor.GREEN + "/Feed");
            commandSeven.setItemMeta(commandSevenMeta);
           
            commandEightMeta.setDisplayName(ChatColor.GREEN + "/Nick");
            commandEight.setItemMeta(commandEightMeta);
            commandNineMeta.setDisplayName(ChatColor.GREEN + "/Kit combat");
            commandNine.setItemMeta(commandNineMeta);
           
            inv.setItem(0, commandOne);
            inv.setItem(1, commandTwo);
            inv.setItem(2, commandThree);
            inv.setItem(3, commandFour);
            inv.setItem(4, commandFive);
            inv.setItem(5, commandSix);
            inv.setItem(6, commandSeven);
            inv.setItem(7, commandEight);
            inv.setItem(8, commandNine);
            player.openInventory(inv);
           
        }
       
       
        //This manages what happens when they click one of the items
        @SuppressWarnings("incomplete-switch")
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event){
            //Stripping color allows it to test the text we made earlier and if it matches
            if(!ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("Commandshop"))
                return;
            //Gets who clicked the block
            Player player = (Player) event.getWhoClicked();
            //Stops them from taking a block
            event.setCancelled(true);
            //Stops them from clicking blocks that aren't valid
            if(event.getCurrentItem()==null
                    || event.getCurrentItem().getType()==Material.AIR
                    ||!event.getCurrentItem().hasItemMeta()){
                player.closeInventory();
                return;
            }
           
           
            // What actually happens when they click a specific block
            switch(event.getCurrentItem().getType()){
            case DIAMOND_BLOCK:
                  if(player.hasPermission("essentials.lightning")){
                      player.sendMessage("You already have this permission");
                  }else if(econ.has(player.getName(), 2000000)){
                      econ.withdrawPlayer(player.getName(), 2000000);
                      perms.playerAdd(player, "essentials.lightning");
                      player.sendMessage(ChatColor.GOLD + "[DranNodes] " +
                        ChatColor.GREEN +
                        "You have sucessfully bought " + "/lightning!");
                     
                  }
                 
            case DIAMOND_HELMET:
               
            case DIAMOND_CHESTPLATE:
           
            case DIAMOND_LEGGINGS:
           
            case DIAMOND_BOOTS:
               
            case DIAMOND_SPADE:
               
            case DIAMOND_AXE:
               
            case DIAMOND_HOE:
               
            case DIAMOND_SWORD:
               
            }
        }
       
       
        //Gives them paper on join
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event){
            event.getPlayer().getInventory().addItem(new ItemStack(Material.BOOK));
        }
       
       
        //Allows them to interact with paper
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
            Action a = event.getAction();
            ItemStack is = event.getItem();
           
            if(a == Action.PHYSICAL || is == null || is.getType() == Material.AIR)
                return;
           
            if(is.getType() == Material.BOOK)
                openGUI(event.getPlayer());
           
        }
        private boolean setupPermissions()
        {
          RegisteredServiceProvider<Permission> rsp = getServer()
            .getServicesManager().getRegistration(Permission.class);
          perms = (Permission)rsp.getProvider();
          return perms != null;
        }
       
       
       
        private boolean setupEconomy()
        {
          if (getServer().getPluginManager().getPlugin("Vault") == null) {
            return false;
          }
          RegisteredServiceProvider<Economy> rsp = getServer()
            .getServicesManager().getRegistration(Economy.class);
          if (rsp == null) {
            return false;
          }
          econ = (Economy)rsp.getProvider();
          return econ != null;
        }
    }
     
  2. Offline

    mythbusterma

  3. Offline

    Nerdfuryz

    @mythbusterma What do you mean? You buy a permission with in game currency and then you can use that command. That is the purpose of the plugin. Unfortunately, I can't seem to get it to actually give the person the permission. I am having trouble figuring it out.


    Edit: It seems worth mentioning I have only tested it with the first case in the switch. The rest I simply haven't put in yet because it isn't working in the first.
     
  4. Offline

    john12345brown

    Maybe this might sound silly but what about making it so on click event or something or ever just make the plugin detect who is clicking it and then make console send the command /pex user p add permissionnode so you save the players name under the p variable and also make it execute /manuaddp p permissionnode that way if someoen using your plugin and they have pex or if they have groupmanager then it will still add because it executes command for both so you dont have to worry about thme having to use one specific permission plugin. again it may sound silly but it would work in theory.
     
  5. Offline

    Zombie_Striker

    @Nerdfuryz
    Did you debug? What do you mean "it isn't working"? Do you receive any errors?

    @john12345brown
    That is bed because you are using another unnecessarily plugin to do something that can be achieved with just bukki.
     
    Last edited: Aug 21, 2015
  6. Offline

    Nerdfuryz

    Oh sorry, "perms.playerAdd(player, "essentials.lightning");" the playerAdd is underlined. I just put it there as a placeholder. I don't know how to actually add the permission.
    @Zombie_Striker

    Yeah you could do that too. I was thinking about it, but I wanted to know if it's possible to do it this way.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  7. Offline

    john12345brown

    I never knew bukkit had its own permission setup to tell you the truth @Zombie_Striker and @Nerdfuryz never realized that bukkit can do anything like that.
     
  8. Offline

    567legodude

    @john12345brown You need to get a PermissionAttachment from the player. Then from the permission attachment you can set a permission value to true.

    I made a class to do this and it works for me, so...:
    Easy Permission Class (open)
    Code:
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.permissions.PermissionAttachment;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class EPerm {
     
        private JavaPlugin plugin;
        private HashMap<UUID, PermissionAttachment> map = new HashMap<UUID, PermissionAttachment>();
     
        public EPerm(JavaPlugin pl) {
            this.plugin = pl;
        }
     
        private PermissionAttachment get(Player player) {
            if (map.containsKey(player.getUniqueId())) return map.get(player.getUniqueId());
            PermissionAttachment pa = player.addAttachment(plugin);
            map.put(player.getUniqueId(), pa);
            return pa;
        }
     
        public void addPermission(Player player, String perm) {
            PermissionAttachment perma = get(player);
            perma.setPermission(perm, true);
            player.recalculatePermissions();
            map.put(player.getUniqueId(), perma);
        }
     
        public void removePermission(Player player, String perm) {
            PermissionAttachment perma = get(player);
            perma.unsetPermission(perm);
            player.recalculatePermissions();
            map.put(player.getUniqueId(), perma);
        }
     
        public void unattach(Player player) {
            if (map.containsKey(player.getUniqueId())) {
                player.removeAttachment(map.get(player.getUniqueId()));
                map.remove(player.getUniqueId());
            }
        }
     
        public void clear() {
            Iterator<UUID> it = map.keySet().iterator();
            while (it.hasNext()) {
                UUID id = it.next();
                if (Bukkit.getPlayer(id) != null) unattach(Bukkit.getPlayer(id));
            }
        }
    
    }
    To use it, start by creating an instance of it and storing it somewhere (It needs to store the data until the plugin unloads)
    Code:
    EPerm perm = new EPerm(JavaPlugin plugin);
    // plugin is the class that extends JavaPlugin
    Then you can add/remove permissions:
    Code:
    perm.addPermission(Player player, String permission);
    // player is the player....
    // permission is the permission to add
    // should have no effect if they already have the permission
    Code:
    perm.removePermission(Player player, String permission);
    // player is the player
    // permission is the permission to remove
    // does nothing if player does not have permission
    And in your onDisable() you should call this to unattach the players.
    Code:
    perm.clear();
    // To unattach every player
     
    Nerdfuryz likes this.
  9. Offline

    Nerdfuryz

    Very cool. That's very helpful, thanks!

    @567legodude One more thing. I am getting a null pointer exception with this part:
    Code:
        public static Permission perms = null;
        public static Economy econ = null;
    I don't really understand how to fix those errors

    @567legodude Nevermind, I fixed it. Now I just don't understand how to get this portion working:
    1. EPerm perm = new EPerm(JavaPlugin plugin);
    I get an underline under plugin saying syntax error.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  10. Offline

    567legodude

    @Nerdfuryz I didn't give you code that you could copy/paste, I gave you examples, and you must figure out how to use them.
     
  11. Offline

    Nerdfuryz

  12. Offline

    Zombie_Striker

Thread Status:
Not open for further replies.

Share This Page