Removing Permission Through Armor

Discussion in 'Plugin Development' started by Mnky281, Jan 7, 2017.

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

    Mnky281

    Hi, this is my first post, so don't sue me if I do something wrong...
    So I'm making a plugin that makes it so that if you wear an enderman head, you get a permission that allows you to teleport at will through right clicking.
    So this works. But now I have to make it so that when the head is removed, the permission is taken away.

    Here's what I have:

    Enderman (The class that teleports the player, not sure if this is relevant)

    Code:
    @EventHandler
        public void onClick(PlayerInteractEvent e) {
            if (e.getPlayer().hasPermission("mobpowers.enderman")) {
                e.getPlayer().sendMessage("You have teleported");
                if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                    if (e.getPlayer().isSneaking()) {
    
                        Block b = e.getPlayer().getTargetBlock((Set<Material>) null, 5);
    
                        World w = e.getPlayer().getWorld();
                        Location l = new Location(w, b.getLocation().getX(), b.getLocation().getY(), b.getLocation().getZ(),
                                e.getPlayer().getLocation().getYaw(), e.getPlayer().getLocation().getPitch());
    
                        if (b != null) {
                            e.getPlayer().teleport(l);
                        }
                    }
                }
    
            }
        }
    MobTransformation (The class that sees when the head is equipped and then gives the player the permission, I know its messy, I'm not sure anymore what I was trying to do...)

    Code:
        public void getHead(final Player player) {
    
            PermissionAttachment attachment = player.addAttachment(plugin);
            plugin.attachments.put(player.getUniqueId(), attachment);
    
            ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) SkullType.PLAYER.ordinal());
            SkullMeta meta = (SkullMeta) skull.getItemMeta();
    
            if (player.getInventory().getHelmet().getType() == skull.getType()) {
                if (meta.getOwner().equals("MHF_Enderman")) {
    
                    PermissionAttachment pperms = plugin.attachments.get(player.getUniqueId());
                    pperms.setPermission("mobpowers.enderman", true);
                }
            }
    
            if (!(player.getInventory().getHelmet().getType() == skull.getType())) {
                plugin.attachments.get(player.getUniqueId()).unsetPermission("mobpowers.enderman");
            }
    
        }
    Main Class OnEnable

    Code:
    public void onEnable() {
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = getLogger();
    
            registerEvents();
            registerConfig();
            registerCommands();
            registerPermissions();
    
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    for (Player all : Bukkit.getOnlinePlayers()) {
    
                        ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) SkullType.PLAYER.ordinal());
                        SkullMeta meta = (SkullMeta) skull.getItemMeta();
    
                        if (all.getInventory().getHelmet().getType() == skull.getType()) {
                            if (meta.getOwner().equals("MHF_Enderman")) {
                                all.sendMessage("You have the Enderman Mob Power");
                            }
    
                        }
                    }
                }
            }, 0L, 20L);
    
            logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " has been enabled!");
    
        }
    EDIT: plugin.yml permissions

    Code:
    permissions:
      mobpowers.*:
        description: Allows transformation
        children:
          mobpowers.enderman: true
      mobpowers.enderman:
        description: Allows enderman transformation
        default: false
    EDIT 2: I'm trying to do this without Vault, PEX, etc.
     
    Last edited: Jan 9, 2017
  2. Offline

    Zombie_Striker

    @Mnky281
    1. On InventoryClickEvent.
    2. If the clicked item is the "helmet" slot, and the player is currently wearing the enderman helmet
    3. Remove the permission.
     
  3. Offline

    Mnky281

    Noob question, main class or MobTransformation?
     
  4. Offline

    Mnky281

    Also, are the permissions in my plugin.yml done correctly? I haven't found a lot about permissions and this is what I could gather...

    Alright, I made the MobTransformation a InventoryClickEvent listener, but it doesn't seem to do anything...

    Code:
    @EventHandler
        public void getHead(InventoryClickEvent e) {
           
            Player player = (Player) e.getWhoClicked();
    
            PermissionAttachment attachment = player.addAttachment(plugin);
            plugin.attachments.put(player.getUniqueId(), attachment);
    
            ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) SkullType.PLAYER.ordinal());
            SkullMeta meta = (SkullMeta) skull.getItemMeta();
    
            if (player.getInventory().getHelmet().getType() == skull.getType()) {
                if (meta.getOwner().equals("MHF_Enderman")) {
    
                    Bukkit.broadcastMessage("Testing");
                    PermissionAttachment pperms = plugin.attachments.get(player.getUniqueId());
                    pperms.setPermission("mobpowers.enderman", true);
                }
            }
    
            if (!(player.getInventory().getHelmet().getType() == skull.getType())) {
                plugin.attachments.get(player.getUniqueId()).unsetPermission("mobpowers.enderman");
            }
    
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 8, 2017
  5. Offline

    CrazyLukeHD

    Here's ONE solution...

    Install a permissions plugin, like PermissionsEX or Essentials GroupManager and replace the line that removes or adds the permission with this:

    PEX:
    Code:
    player.getServer().dispatchCommand(player.getServer().getConsoleSender(), "pex user " + player.getName() + " add mobpowers.enderman");
    // Change "add" to "remove" to remove the permission
    GroupManager:
    Code:
    player.getServer().dispatchCommand(player.getServer().getConsoleSender(), "manuaddp " + player.getName() + " mobpowers.enderman");
    // change "manuaddp" to "manudelp" to remove the permission
     
  6. Online

    timtower Administrator Administrator Moderator

  7. Offline

    Mnky281

    I am trying to do my permissions without plugins like PEX or Vault.
     
  8. Online

    timtower Administrator Administrator Moderator

    @Mnky281 Could you also tell why? As Vault pretty much exists for this task.
     
  9. Offline

    DoggyCode™

    Well, what is the reason for this? If you have a permissions plugin (like GroupManager or PEX), chances are that 1. Vault support them. and 2. Any server that uses any permission-based or economic system has Vault installed.
     
  10. Offline

    Mnky281

    I want to know how to do permissions without plugins because I know it's possible. Also, I like to try to code my own stuff. Also, I don't plan to publish it or anything like that.
     
    Last edited: Jan 14, 2017
  11. Offline

    Mnky281

  12. Offline

    JanTuck

    I know this is a silly question but in the PlayerInteractEvent, why do you not just check if the player has your custom head and of he has he can teleport?
     
  13. Offline

    Mnky281

    To be honest, I kind of wanted to know how to use permissions... besides I feel like it is more organized
     
  14. Offline

    JanTuck

  15. Offline

    Mnky281

    Honestly, I can't understand a thing in this
     
Thread Status:
Not open for further replies.

Share This Page