Advertisement
Guest User

Ponyplgn - class Util

a guest
Nov 16th, 2014
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.24 KB | None | 0 0
  1. package iwcs.pony;
  2.  
  3. import java.io.IOException;
  4. import java.util.logging.Logger;
  5.  
  6. import org.bukkit.ChatColor;
  7. import org.bukkit.command.CommandSender;
  8. import org.bukkit.configuration.InvalidConfigurationException;
  9. import org.bukkit.entity.Player;
  10.  
  11. import ru.tehkode.permissions.bukkit.PermissionsEx;
  12. import iwcs.pony.Main;
  13. import iwcs.pony.Database;
  14.  
  15. public class Util {
  16.    
  17.     private Main Main = new Main();
  18.     private Database Database = new Database();
  19.    
  20.     //Variables
  21.     //Importing
  22.     private Logger console = Main.console;
  23.    
  24.     //Own
  25.     public String[] errors = { //MaxError 11
  26.             "Called when config file existed but could not be loaded." + ChatColor.GRAY + " (public void iwcs.pony.Main.onEnable(), IOException or InvalidConfigurationException)",
  27.             "Called when player or permission is not specified when checking for permissions. Most probably an internal error." + ChatColor.GRAY + " (public void iwcs.pony.Util.hasPerm(Player pony, String permission, boolean byDefault), some argument is null)",
  28.             "Called when the plugin has not initialized correctly, reload it." + ChatColor.GRAY + " (public boolean iwcs.pony.Util.hasPerm(Player pony, String permission, boolean byDefault), iwcs.Main.permissionPlugin == 0)",
  29.             "Called when player not specified when sending \"Insufficient permissions\" to someone. Most probably an internal error." + ChatColor.GRAY + " (public void iwcs.pony.Util.noPerm(Player pony), pony == null)",
  30.             "Called when database file existed but could not be loaded." + ChatColor.GRAY + " (public void iwcs.pony.Main.onEnable(), IOException or InvalidConfigurationException)",
  31.             "Called when player's race in the database is unknown." + ChatColor.GRAY + " (public String iwcs.pony.Database.getRace(Player pony), value in database doesn't make any sence)",
  32.             "Called when database could not be saved due to IOException." + ChatColor.GRAY + " (public void iwcs.pony.Database.save(), IOException)",
  33.             "Called when player not specified when saving him to database." + ChatColor.GRAY + " (public void iwcs.pony.Database.save(Player pony, String newValue), pony == null)",
  34.             "Called when the new race is null or unknown when changing race." + ChatColor.GRAY + " (public void iwcs.pony.Database.setRace(Player pony, String newRace), newRace doesn't make any sence)",
  35.             "Called when the race of the command sender is unknown. One of the errors you'll actually see sometimes. :)" + ChatColor.GRAY + " (private void iwcs.pony.Main.commandPony(CommandSender sender, String[] args), public String iwcs.pony.Database.getPony(Player pony) returned \"unset\" or \"unpresent\")",
  36.             "Called when the race of the requested player is unknown. One of the errors you'll actually see sometimes. :)" + ChatColor.GRAY + " (private void iwcs.pony.Main.commandPlgn(CommandSender sender, String[] args), public String config.getString(args[1]) returned \"unset\" or \"unpresent\")",
  37.     };
  38.    
  39.    
  40.     public void throwException(Exception exception, int errorNumber) {
  41.         console.info("Uups! I've just caught " + ((exception !=null) ? exception.toString() : "unknown exception") + "!");
  42.         console.info("To see details, type /ponyplgn gerror " + errorNumber);
  43.         if (!(exception == null)) {exception.printStackTrace();} else {console.info("Unspecified exception");}
  44.         double random = Math.random();
  45.         if (random < 0.33) {
  46.             console.info("Derp-derp-derp!");
  47.         } else if (random < 0.66) {
  48.             console.info("Muffin!");
  49.         } else {
  50.             console.info("I'm a cross-eyed pegasus!");
  51.         }
  52.     }
  53.    
  54.     public String gerror(int errorNumber) {
  55.         if (errorNumber > -1 && errorNumber < errors.length) {
  56.             return "Error #" + errorNumber + ": " + errors[errorNumber];
  57.         } else {
  58.             return "Such error doesn't exist";
  59.         }
  60.     }
  61.    
  62.     public void help(int command, CommandSender sender) {
  63.         if (command == 0) {
  64.             if (sender instanceof Player) {
  65.                 sender.sendMessage(Main.helpForPony);
  66.             } else {
  67.                 for (int i = 0; i < Main.helpForPony.length; i++) {
  68.                     console.info(Main.helpForPony[i]);
  69.                 }
  70.             }
  71.         } else if (command == 1) {
  72.             if (sender instanceof Player) {
  73.                 sender.sendMessage(Main.helpForPlgn);
  74.             } else {
  75.                 for (int i = 0; i < Main.helpForPlgn.length; i++) {
  76.                     console.info(Main.helpForPlgn[i]);
  77.                 }
  78.             }
  79.         } else if (command == 2) {
  80.             if (sender instanceof Player) {
  81.                 sender.sendMessage(Main.helpForCast);
  82.             } else {
  83.                 console.info("This command can only be sent by a player");
  84.             }
  85.         } else {
  86.             throw new IllegalArgumentException();
  87.         }
  88.     }
  89.    
  90.     public boolean hasPerm(Player pony, String permission, boolean byDefault) {
  91.         if (pony == null || permission == null) {
  92.             throwException(new NullPointerException(), 1); //* * * ERROR 1 * * *
  93.             return false;
  94.         } else {
  95.             switch (Main.permissionPlugin) {
  96.             case 1:
  97.                 return PermissionsEx.getUser(pony).has(permission);
  98.             case 2:
  99.                 return pony.hasPermission(permission);
  100.             case 3:
  101.                 if (byDefault | pony.isOp()) {
  102.                     return true;
  103.                 } else {
  104.                     return false;
  105.                 }
  106.             default:
  107.                 throwException(new IllegalArgumentException(), 2); //* * * ERROR 2 * * *
  108.                 return false;
  109.             }
  110.         }
  111.     }
  112.    
  113.     public void noPerm(Player pony) {
  114.         if (pony != null) {
  115.             pony.sendMessage(ChatColor.GRAY + "Insufficient permissions");
  116.         } else {
  117.             throwException(new NullPointerException(), 3); //* * * ERROR 3 * * *
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement