Solved modifying permissions with the PEX API

Discussion in 'Plugin Development' started by Kyorax, Aug 28, 2015.

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

    Kyorax

    Hi everyone,
    I'm currently developing a minigame for my server.
    I need players who wear a golden helmet to be ignored by monsters.
    My current solution is to iterate through all players (I know, it's not optimal performance wise)
    and check for their helmets. If they wear a golden helmet, I give them the permission
    "-modifyworld.mobtarget.*", which makes all monsters ignore them.
    I also check if they no longer wear a helmet and remove the permission if they still have it.

    When a player wears a golden helmet, the permission is added (viewed the permissions.yml file),
    but it is not removed once the player takes the helmet off.

    Code:
    for(Player p: Bukkit.getOnlinePlayers()) {
         PermissionUser user = PermissionsEx.getUser(p);
                 
                    if(p.getInventory().getHelmet() != null) {
                        if(p.getInventory().getHelmet().getType() == Material.GOLD_HELMET) {
                     
                            if(!user.has("-modifyworld.mobtarget.*"))
                                user.addPermission("-modifyworld.mobtarget.*");
                     
                        } else {
                         
                            if(user.has("-modifyworld.mobtarget.*"))
                                user.removePermission("-modifyworld.mobtarget.*");
                         
                        }
                 
                    } else {
                     
                        if(user.has("-modifyworld.mobtarget.*"))
                            user.removePermission("-modifyworld.mobtarget.*");
                     
                    }
    }
    This is ran in a Bukkit runnable with 20 tick interval.

    Does anyone know of a better way to achieve this than by permissions
    or does anyone see what I'm doing wrong?
    Thanks a lot in advance.
     
  2. Offline

    au2001

    @Kyorax Well, you can still use permissions but only check when the inventory click, and armor break.
    This probably use packets to make player not "see" you, so I recommend you just use the plugin you already have.
    If there was an API it would be better though.
     
  3. Offline

    Kyorax

    @au2001 I think I just found a better solution: I use the "EntityTargetEvent" (didn't know that existed)
    and check if the targeted player is wearing a gold helmet.
    I'll try that out now.

    EDIT: Code (this should be much better performance wise)
    Code:
    @EventHandler
        public void onEntityTarget(EntityTargetEvent event) {
           
            if(event.getTarget() instanceof Player && event.getEntity() instanceof Monster) {
                Player p = (Player)event.getTarget();
                if(p.getInventory().getHelmet() != null) {
                    if(p.getInventory().getHelmet().getType() == Material.GOLD_HELMET)
                        event.setCancelled(true);
                }
            }
           
        }
     
  4. Offline

    au2001

    @Kyorax Probably the best solution, nice event ;)
    I used it already, just didn't think about it... :p
     
Thread Status:
Not open for further replies.

Share This Page