SHARE
TWEET

Untitled

a guest Jul 21st, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package constitution.permissions;
  2.  
  3. import java.util.Map;
  4. import java.util.UUID;
  5. import java.util.WeakHashMap;
  6.  
  7. import constitution.ConstitutionMain;
  8. import constitution.chat.channels.Channel;
  9. import constitution.commands.engine.CommandManager;
  10. import constitution.commands.engine.CommandTemplate;
  11. import constitution.configuration.ChannelConfig;
  12. import constitution.configuration.GroupConfig;
  13. import constitution.configuration.UserConfig;
  14. import net.minecraft.command.ICommand;
  15. import net.minecraft.command.ICommandSender;
  16. import net.minecraft.entity.player.EntityPlayer;
  17. import net.minecraftforge.fml.common.FMLCommonHandler;
  18. import net.minecraftforge.server.permission.DefaultPermissionLevel;
  19. import net.minecraftforge.server.permission.PermissionAPI;
  20.  
  21. public class PermissionManager {
  22.     public static final String DEFAULT_COMMAND_NODE = "command.";
  23.     public static final String PERM_COMMANDBLOCK = "commandblock";
  24.     private final PermissionProvider permProvider = new PermissionProvider();
  25.     public final Group.Container groups = new Group.Container();
  26.     public final User.Container users = new User.Container();
  27.     public final Channel.Container channels = new Channel.Container();
  28.     private final GroupConfig groupConfig = new GroupConfig(ConstitutionMain.CONFIG_FOLDER + "JSON/Groups.json");
  29.     private final UserConfig userConfig = new UserConfig(ConstitutionMain.CONFIG_FOLDER + "JSON/Users.json");
  30.     private final ChannelConfig channelConfig = new ChannelConfig(ConstitutionMain.CONFIG_FOLDER + "JSON/Channels.json");
  31.     protected static Map<ICommand, String> commandPermissions = new WeakHashMap<ICommand, String>();
  32.  
  33.     public PermissionManager() {
  34.     }
  35.    
  36.     public void preInitialization() {
  37.         PermissionAPI.setPermissionHandler(permProvider);
  38.     }
  39.    
  40.     public void serverLoad() {
  41.         loadConfigs();
  42.     }
  43.    
  44.     public void registerDefaultPermissions() {
  45.         permProvider.registerNode(PERM_COMMANDBLOCK, DefaultPermissionLevel.OP, "CommandBlock");
  46.     }
  47.    
  48.     public void registerPermission(String permission, DefaultPermissionLevel level, String description) {
  49.         permProvider.registerNode(permission, level, description);
  50.     }
  51.    
  52.     public void registerServerCommandPermissions() {
  53.         @SuppressWarnings("unchecked")
  54.         Map<String, ICommand> commands = FMLCommonHandler.instance().getMinecraftServerInstance().getCommandManager().getCommands();
  55.         for (ICommand command : commands.values())
  56.             if (!commandPermissions.containsKey(command))
  57.                 registerCommandPermission(command);
  58.     }
  59.    
  60.     public void registerCommandPermission(ICommand command) {
  61.         registerPermission(getCommandPermission(command), getCommandPermissionLevel(command), CommandManager.getDescriptionForCommand(command.getName()));
  62.     }
  63.    
  64.     public String getCommandPermission(ICommand command) {
  65.         String node = "";
  66.         if (commandPermissions.keySet().contains(command)) {
  67.             node = commandPermissions.get(command);
  68.         } else {
  69.             if (CommandManager.getPermForCommand(command.getName())!=null) {
  70.                 node = CommandManager.getPermForCommand(command.getName());
  71.             } else {
  72.                 node = DEFAULT_COMMAND_NODE + command.getName();
  73.             }
  74.         }
  75.     return node;
  76.     }
  77.    
  78.     public DefaultPermissionLevel getCommandPermissionLevel(ICommand command) {
  79.         if (command instanceof CommandTemplate) {
  80.             CommandTemplate cmd = (CommandTemplate) command;
  81.             if (cmd != null) {
  82.                 return cmd.getPermissionLevel();
  83.             }
  84.         }
  85.         return DefaultPermissionLevel.NONE;
  86.     }
  87.     public Boolean hasPermission(UUID uuid, String node) {
  88.         return null;
  89.     }
  90.     public boolean checkPermission(PermissionContext context, String permission) {
  91.         return permProvider.hasPermission(context.getPlayer().getGameProfile(), permission, context);
  92.     }
  93.  
  94.     public boolean checkPermission(EntityPlayer player, String permission) {
  95.         return checkPermission(new PermissionContext(player), permission);
  96.     }
  97.  
  98.     public boolean checkPermission(ICommandSender sender, ICommand command, String permission) {
  99.         return checkPermission(new PermissionContext(sender, command), permission);
  100.     }
  101.  
  102.     public boolean checkPermission(ICommandSender sender, ICommand command) {
  103.         return checkPermission(new PermissionContext(sender, command), getCommandPermission(command));
  104.     }
  105.  
  106.     public boolean checkPermission(ICommandSender sender, String permission) {
  107.         return checkPermission(new PermissionContext(sender), permission);
  108.     }
  109.  
  110.     public void loadConfigs() {
  111.         groups.clear();
  112.         users.clear();
  113.         channels.clear();
  114.        
  115.         groupConfig.init(groups);
  116.         userConfig.init(users);
  117.         channelConfig.init(channels);
  118.     }
  119.  
  120.     public void saveConfigs() {
  121.         groupConfig.write(groups);
  122.         userConfig.write(users);
  123.         channelConfig.write(channels);
  124.     }
  125.  
  126.     public void saveGroups() {
  127.         groupConfig.write(groups);
  128.     }
  129.     public void saveUsers() {
  130.         userConfig.write(users);
  131.     }
  132.    
  133.     public void saveChannels() {
  134.         channelConfig.write(channels);
  135.     }
  136. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top