Solved How to override permission checks ;)

Discussion in 'Plugin Development' started by Lightspeed, Feb 15, 2016.

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

    Lightspeed

    >EDIT> Hey visitor the awnser is below enjoy<<

    Just a quick question does CraftPlayer fields change everytime a player reconnects?
    Just to make sure I don't run code when it doesen't need to be.
     
    Last edited: Feb 17, 2016
  2. Offline

    teej107

    @Lightspeed Fields in a class don't change at all. The values may change but not the fields themselves.
     
  3. Offline

    Lightspeed

    So If I cast a player to CraftPlayer and change lets say there permissionbase for overriding it will keep the player with that permission base until there player data gets deleted in the world?
    Or will it be a new instance everytime a player logs back in or somthing.
    @teej107
    Better question will the players CraftPlayer instance be changed everytime he reconnects or somthing.
     
  4. Offline

    teej107

    @Lightspeed Yes, there will be a new CraftPlayer instance each time they connect. When they disconnect, CraftBukkit stops keeping a reference to the CraftPlayer and it will eventually get GC'd.
     
  5. Offline

    Lightspeed

    Bitt offtopic but, anyone know how to get a players CraftEntity?
     
  6. Offline

    teej107

    @Lightspeed CraftPlayer extends from CraftEntity. If you are talking about getting Mojang's entity then it is getHandle() called from the CraftPlayer I believe.
     
  7. Offline

    Lightspeed

    Example or is it
    Code:
    ((CraftPlayer)p).getHandle();
    
    Because this just returns EntityPlayer.
     
  8. Offline

    Xerox262

    @Lightspeed CraftPlayer extends CraftEntity, read what you have, rather than casting to CraftPlayer you can just cast to CraftEntity and you do not need to get the handle. @teej107 was telling you how to get EntityPlayer using getHandle();
     
  9. Offline

    Lightspeed

    @Xerox262 CraftPlayer extends CraftHumanEntity. . .
    Edit: Casting does not work.
    I'm trying to override permission checks by changing private static final PermissibleBase in CraftEntity using reflection.
     
  10. Offline

    Xerox262

    @Lightspeed CraftPlayer extends CraftHumanEntity which extends CraftLivingEntity which extends CraftEntity. Therefore CraftPlayer extends CraftHumanEntity, CraftLivingEntity and CraftEntity and can be cast as such, you do not need to cast to access the methods though, you know that right? You can just call the method from the CraftPlayer instance.
     
  11. Offline

    Lightspeed

    Yes but, when I do
    Code:
    ((CraftEntity)p).getClass();
    it returns CraftPlayer or am I stupid?
    This may not work anyway I just want to get to CraftEntity(Just so everyone knows :p) .
     
  12. Offline

    Xerox262

    @Lightspeed It shouldn't matter, you should be able to access the field through the CraftPlayer class, since it extends it it has it's own that you can access.

    The reason it's returning the CraftPlayer class is because that's what form of the CraftEntity class you have.
     
  13. Offline

    Lightspeed

    No such field exception.
     
  14. Offline

    Xerox262

    @Lightspeed Why can't you just do CraftEntity.class.getField(FieldName).get(Object); You don't need to get the actual class type from the player, remember you have to actually use get(Object) to tell it what instance to get it from.
     
  15. Offline

    Lightspeed

    I have no clue this thread is a mess. XD
    I'm experimenting with reflection and trying to Successfully set permissible base in craftentity to my own class extending permissible base to override it maybe I'm doing the reflection wrong.
     
  16. Offline

    Xerox262

    @Lightspeed Can you post it please? Reflection can be hard to wrap your head around at first but you'll get the hang of it.

    Edit: Just checked, The permission base is final, are you able to change that with reflection?

    Edit 2: You can, just need to setAccessible() to true.
     
  17. Offline

    Lightspeed

    HOLY CRAP I THINK I DID IT
    I randomly noticed I could reload ingame and was op and I also had a simple test if(p.hasPermBlah("POO")) and it said true.

    How to override permission checks.
    Code:
    Field field = CraftEntity.class.getDeclaredField("perm");
    field.setAccessible(true);
    field.get(p);
    Field modifiersField = field.getClass().getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); 
    field.set(null, new PermissibleBase(true));
    PermissionBase
    Code:
    package net.lyght.utils;
    
    import org.bukkit.permissions.Permission;
    import org.bukkit.permissions.ServerOperator;
    
    /**
    * Created Feb 15, 2016
    *   at 11:52:19 PM by Lightspeed
    */
    public class PermissibleBase
    extends org.bukkit.permissions.PermissibleBase
    {
    
        public PermissibleBase(final boolean op)
        {
            super(new ServerOperator()
            {
                @Override
                public boolean isOp()
                {
                    return op;
                }
          
                public void setOp(boolean paramBoolean){}
            });
        }
    
        public boolean hasPermission(String inName)
        {
            return true;
        }
    
        public boolean hasPermission(Permission perm)
        {
            return true;
        }
    }
    
    and now it doesen't work. . .
     
  18. Offline

    Xerox262

    @Lightspeed What is that for exactly? You know you can't release a plugin with that for download? It will get you banned from uploading any more.

    Edit: What doesn't work? You do need to be more specific.
     
  19. Offline

    Lightspeed

    Well first I had all permission and was randomly opped(AKA system working) and then I reloaded and poof it gone yet changed nothing. . .
    ALSO: This is just me messing around and trying to override permissions. XD
     
  20. Offline

    Xerox262

    @Lightspeed And where are you setting it? If you're setting it on join then you need to log out then back in for it to actually apply.

    Edit:
    field.set(null, new PermissibleBase(true));

    Where you passed null here you want to tell it what instance to set the field in, null is only if the field is static.


    Edit: Which it is... Ignore.
     
  21. Offline

    Lightspeed

    I'm setting it on command . . .
    Did I make boo boo?
     
  22. Offline

    Xerox262

  23. Offline

    Lightspeed

    @Xerox262 What do you mean post everything? thats it . . .
     
  24. Offline

    Xerox262

    @Lightspeed After checking, the reason you were unable to get the Field from the CraftPlayer class was because you're using getDeclaredField(), if you used getField() it would've checked all superclasses/implemented interfaces in the order they were declared and returned the first it found.

    Are there any errors when you try to do it?
     
  25. Offline

    Lightspeed

    I can get the field there not even any errors it just doesen't work.
    What happened (open)

    I had an error and forgot I wasen't op somehow and tried to reload and it worked!
    I then tried to do the command to set it and it gave an error again.
    If you want the part of the logs I can post it below.
    I even deoped myself(Welp my alt accidentally) and I never deopped or opped myself.


    but it's a private static final field I can't get it with the normal getField?
    Somthing worked at some point cause I just checked my logs I was never op and never opped myself during the test and was then randomly able to every permission.

    ~Edits~

    I made a little test app and found out it works unless it's final . . . but, I lookup up how to edit private static final fields . . . . . :confused:


    It worked randomly once then stopped on reload . . . so I might to set it at a specific time(I'm setting it by command at the moment)?

    ~Edits~
     
    Last edited: Feb 17, 2016
  26. Offline

    Lightspeed

    My smile is too big for my face . . . welp anyway I was setting the wrong class and it was getting reset to another Permissible later so heres the awnser.
    Code:
    Player p = <YourPlayer>;
    Field field = CraftHumanEntity.class.getDeclaredField("perm");
    field.setAccessible(true);
    Field modifiersField = field.getClass().getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    field.set(p, new PermissionBase(p));
    Has to be done for every player that joined.
    PermissionBase is a class extending PermissibleBase to override the check permission method allowing for super permissions like big permission plugins '*' permission.
    Just add an if (perm == "*") and return true there ya go every plugin now follows this rule(Or so I think. :p)

    Even create a custom event off it if you want easy access for listeners Example: A jail system blocking acces to all commands no matter what :p Not a good example I know sorry for it .XD
     
    Last edited: Feb 18, 2016
Thread Status:
Not open for further replies.

Share This Page