Advertisement
Guest User

Ponyplgn - class Main

a guest
Nov 16th, 2014
1,243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.33 KB | None | 0 0
  1. package iwcs.pony;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.util.logging.Logger;
  7.  
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.ChatColor;
  10. import org.bukkit.GameMode;
  11. import org.bukkit.command.Command;
  12. import org.bukkit.command.CommandSender;
  13. import org.bukkit.configuration.InvalidConfigurationException;
  14. import org.bukkit.configuration.file.FileConfiguration;
  15. import org.bukkit.configuration.file.YamlConfiguration;
  16. import org.bukkit.entity.Player;
  17. import org.bukkit.event.EventHandler;
  18. import org.bukkit.event.Listener;
  19. import org.bukkit.event.player.*;
  20. import org.bukkit.event.entity.*;
  21. import org.bukkit.plugin.java.JavaPlugin;
  22. import org.bukkit.potion.PotionEffect;
  23. import org.bukkit.potion.PotionEffectType;
  24.  
  25. import iwcs.pony.Database;
  26. import iwcs.pony.Util;
  27.  
  28. /* Errorlist:
  29.  * 0 - onEnable() > Unable to load config
  30.  */
  31.  
  32. public class Main extends JavaPlugin implements Listener {
  33.    
  34.     private Database Database = new Database();
  35.     private Util Util = new Util();
  36.    
  37.     //Variables
  38.     private double random = 0; //A variable for random numbers
  39.     private File configFile = new File(getDataFolder(), "config.yml"); //File for config
  40.    
  41.     public File databaseFile = new File(getDataFolder(), "database.yml"); //File for database
  42.     public int permissionPlugin = 0; //Type of permission plugin in charge
  43.     public Logger console = getLogger(); //Console (to use instead of getLogger())
  44.     public FileConfiguration config = getConfig(); //Config file
  45.     public FileConfiguration database = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "database.yml")); //Database
  46.    
  47.     //String[]'s
  48.     public String[] helpForPony = {
  49.             ChatColor.GRAY + "Not ready =\\"
  50.     };
  51.    
  52.     public String[] helpForPlgn = {};
  53.     public String[] helpForCast = {};
  54.    
  55.    
  56.    
  57.     @Override
  58.     public void onEnable() {
  59.         Bukkit.getPluginManager().registerEvents(this, this);
  60.         if (configFile.exists()) {
  61.             try {
  62.                 config.load(configFile);
  63.             } catch (IOException | InvalidConfigurationException e) {
  64.                 Util.throwException(e, 0); //* * * ERROR 0 * * *
  65.             }
  66.         } else {
  67.             this.saveDefaultConfig();
  68.         }
  69.        
  70.         if (databaseFile.exists()) {
  71.             try {
  72.                 database.load(databaseFile);
  73.             } catch (IOException | InvalidConfigurationException e) {
  74.                 Util.throwException(e, 0); //* * * ERROR 4 * * * (a)
  75.             }
  76.         } else {
  77.             try {
  78.                 database.loadFromString("");
  79.                 database.save(databaseFile);
  80.             } catch (InvalidConfigurationException | IOException e) {
  81.                 Util.throwException(e, 0); //* * * ERROR 4 * * * (b)
  82.             }
  83.         }
  84.        
  85.         if (Bukkit.getPluginManager().getPlugin("PermissionsEx") != null) {
  86.             permissionPlugin = 1;
  87.             getLogger().info("Your PermissionsEx doesn't like ponies! I swear it!");
  88.         } else if (Bukkit.getPluginManager().getPlugin("PermissionsBukkit") != null) {
  89.             getLogger().info("This PermissionsBukkit is a ponyhater. Info 100%.");
  90.             permissionPlugin = 2;
  91.         } else {
  92.             getLogger().warning("I don't see a pemission plugin... Anarchy?");
  93.             permissionPlugin = 3;
  94.         }
  95.     }
  96.    
  97.     @Override
  98.     public void onLoad() {
  99.         random = Math.random();
  100.         if (random < 0.33) {
  101.             console.info("org.pinkiePie.party(new Server getServer());");
  102.         } else if (random < 0.66) {
  103.             console.info("Twilight asks permission to enter the server...");
  104.         } else {
  105.             console.info("Rainbow Dash thinks your server is now 20% cooler");
  106.         }
  107.     }
  108.    
  109.     //Command processing
  110.     private void commandPony(CommandSender sender, String[] args) {
  111.         if (args.length < 1) {
  112.             Util.help(0, sender);
  113.         } else {
  114.             if (sender instanceof Player) {
  115.                 Player pony = (Player) sender;
  116.                 switch (args[0].toLowerCase()) {
  117.                     case "earth":
  118.                         if (Util.hasPerm(pony, "pony.earth", true)) {
  119.                             Database.setRace(pony, "earth");
  120.                             Database.save();
  121.                             pony.sendMessage(config.getString("msgEarthSet", ChatColor.GOLD + "You are an earth pony now!"));
  122.                         } else {
  123.                             Util.noPerm(pony);
  124.                         }
  125.                         break;
  126.                     case "pegasus":
  127.                         if (Util.hasPerm(pony, "pony.pegasus", true)) {
  128.                             Database.setRace(pony, "pegasus");
  129.                             Database.save();
  130.                             pony.sendMessage(config.getString("msgPegasusSet", ChatColor.GOLD + "You are a pegasus now!"));
  131.                         } else {
  132.                             Util.noPerm(pony);
  133.                         }
  134.                         break;
  135.                     case "unicorn":
  136.                         if (Util.hasPerm(pony, "pony.unicorn", true)) {
  137.                             Database.setRace(pony, "unicorn");
  138.                             Database.save();
  139.                             pony.sendMessage(config.getString("msgUnicornSet", ChatColor.GOLD + "You are a unicorn now!"));
  140.                         } else {
  141.                             Util.noPerm(pony);
  142.                         }
  143.                         break;
  144.                     case "alicorn":
  145.                         if (Util.hasPerm(pony, "pony.alicorn", false)) {
  146.                             Database.setRace(pony, "alicorn");
  147.                             Database.save();
  148.                             pony.sendMessage(config.getString("msgAlicornSet", ChatColor.GOLD + "You are an alicorn now!"));
  149.                         } else {
  150.                             Util.noPerm(pony);
  151.                         }
  152.                         break;
  153.                     case "human":
  154.                         if (Util.hasPerm(pony, "pony.human", false)) {
  155.                             Database.setRace(pony, "human");
  156.                             Database.save();
  157.                             pony.sendMessage(config.getString("msgHumanSet", ChatColor.GOLD + "You are a human now!"));
  158.                         } else {
  159.                             Util.noPerm(pony);
  160.                         }
  161.                         break;
  162.                     case "current":
  163.                         if (Util.hasPerm(pony, "pony.current", true)) {
  164.                             switch (Database.getRace(pony)) {
  165.                             case "earth":
  166.                                 pony.sendMessage(config.getString("msgEarthCurrMe", ChatColor.GOLD + "You are an earth pony"));
  167.                                 break;
  168.                             case "pegasus":
  169.                                 pony.sendMessage(config.getString("msgPegasusCurrMe", ChatColor.GOLD + "You are a pegasus"));
  170.                                 break;
  171.                             case "unicorn":
  172.                                 pony.sendMessage(config.getString("msgUnicornCurrMe", ChatColor.GOLD + "You are a uncorn"));
  173.                                 break;
  174.                             case "alicorn":
  175.                                 pony.sendMessage(config.getString("msgAlicornCurrMe", ChatColor.GOLD + "You are an alicorn"));
  176.                                 break;
  177.                             case "human":
  178.                                 pony.sendMessage(config.getString("msgHumanCurrMe", ChatColor.GOLD + "You are a human"));
  179.                                 break;
  180.                             default:
  181.                                 pony.sendMessage(config.getString("msgUnknownCurrMe", ChatColor.RED + "Wait, who are you?.. You better tell admins 'bout this"));
  182.                                 Util.throwException(null, 10); //* * * ERROR 10 * * *
  183.                             }
  184.                         } else {
  185.                             Util.noPerm(pony);
  186.                         }
  187.                         break;
  188.                     case "default":
  189.                         if (Util.hasPerm(pony, "pony.default", true)) {
  190.                             Database.setDefault(pony);
  191.                         }
  192.                         break;
  193.                     default:
  194.                         pony.sendMessage(config.getString("msgUnknownArgPony", ChatColor.GRAY + "Argument was not recognized; run " + ChatColor.GOLD + "/pony help" + ChatColor.GRAY + " for help"));
  195.                     }
  196.                
  197.             } else {
  198.                 getLogger().info("This command can only be performed by a player, use /ponyplgn instead");
  199.             }
  200.         }
  201.     }
  202.    
  203.     private void commandPlgn(CommandSender sender, String[] args) {
  204.         //get|set|reset|gerror|getraw|setraw|totalreset|getconfig|savedata
  205.         switch (args[0].toLowerCase()) {
  206.         case "get":
  207.             if (sender instanceof Player) {
  208.                 Player pony = (Player) sender;
  209.                 if (Util.hasPerm(pony, "pony.current.others", false)) {
  210.                     if (args.length > 2) {
  211.                         switch (database.getString(args[1])) {
  212.                         case "earth":
  213.                             pony.sendMessage(config.getString("msgEarthCurrOth", ChatColor.GOLD + "That's an earth pony"));
  214.                             break;
  215.                         case "pegasus":
  216.                             pony.sendMessage(config.getString("msgPegasusCurrOth", ChatColor.GOLD + "That's a pegasus"));
  217.                             break;
  218.                         case "unicorn":
  219.                             pony.sendMessage(config.getString("msgUnicornCurrOth", ChatColor.GOLD + "That's a unicorn"));
  220.                             break;
  221.                         case "alicorn":
  222.                             pony.sendMessage(config.getString("msgAlicornCurrOth", ChatColor.GOLD + "That's an alicorn"));
  223.                             break;
  224.                         case "human":
  225.                             pony.sendMessage(config.getString("msgHumanCurrOth", ChatColor.GOLD + "That's a human"));
  226.                             break;
  227.                         default:
  228.                             pony.sendMessage(config.getString("msgUnknownCurrOth", ChatColor.RED + "Erm... I HAVE NO IDEA! >:D"));
  229.                             Util.throwException(null, 11); //* * * ERROR 11 * * *
  230.                         }
  231.                     } else {
  232.                         pony.sendMessage(ChatColor.RED + "Usage: /ponyplgn get <nickname>");
  233.                     }
  234.                 } else {
  235.                     Util.noPerm(pony);
  236.                 }
  237.             } else {
  238.                 if (args.length > 2) {
  239.                     switch (database.getString(args[1])) {
  240.                     case "earth":
  241.                         console.info(args[1] + " is an earth pony");
  242.                         break;
  243.                     case "pegasus":
  244.                         console.info(args[1] + " is a pegasus");
  245.                         break;
  246.                     case "unicorn":
  247.                         console.info(args[1] + " is a unicorn");
  248.                         break;
  249.                     case "alicorn":
  250.                         console.info(args[1] + " is an alicorn");
  251.                         break;
  252.                     case "human":
  253.                         console.info(args[1] + " is a human");
  254.                         break;
  255.                     default:
  256.                         console.info(args[1] + " is not in the database");
  257.                         Util.throwException(null, 11); //* * * ERROR 11 * * *
  258.                     }
  259.                 } else {
  260.                     console.info("Usage: /ponyplgn get <nickname>");
  261.                 }
  262.             }
  263.             break;
  264.         case "set":
  265.             if (sender instanceof Player) {
  266.                 Player pony = (Player) sender;
  267.                 if (Util.hasPerm(pony, "pony.set", false)) {
  268.                     if (args.length > 3) {
  269.                         String r = args[2].toLowerCase();
  270.                         if (r == "earth" || r == "pegasus" || r == "unicorn" || r == "alicorn" || r == "human") {
  271.                             Database.setRace(getServer().getOfflinePlayer(args[1]).getPlayer(), r);
  272.                             pony.sendMessage(ChatColor.GOLD + args[1] + "'s race set to " + r);
  273.                         } else
  274.                             sender.sendMessage(new String[] {ChatColor.RED + "Unknown race type.", ChatColor.GRAY + "Avaliable types: earth, pegasus, unicorn, alicorn, human"});
  275.                     } else {
  276.                         sender.sendMessage(ChatColor.RED + "Usage: /ponyplgn set <nickname> <newRace>");
  277.                     }
  278.                 } else {
  279.                     Util.noPerm(pony);
  280.                 }
  281.             } else {
  282.                 if (args.length > 3) {
  283.                     String r = args[2].toLowerCase();
  284.                     if (r == "earth" || r == "pegasus" || r == "unicorn" || r == "alicorn" || r == "human") {
  285.                         Database.setRace(getServer().getOfflinePlayer(args[1]).getPlayer(), r);
  286.                         console.info(args[1] + "'s race set to " + r);
  287.                     } else
  288.                         console.info("Unknown race type. Avaliable types: earth, pegasus, unicorn, alicorn, human");
  289.                 } else {
  290.                     console.info("Usage: /ponyplgn set <nickname> <newRace>");
  291.                 }
  292.             }
  293.             break;
  294.         case "reset":
  295.             if (sender instanceof Player) {
  296.                 Player pony = (Player) sender;
  297.                 if (Util.hasPerm(pony, "pony.reset", false)) {
  298.                     if (args.length > 2) {
  299.                         Database.setDefault(getServer().getOfflinePlayer(args[1]).getPlayer());
  300.                         pony.sendMessage(ChatColor.GOLD + args[1] + "'s race reset to default");
  301.                     } else {
  302.                         pony.sendMessage(ChatColor.RED + "Usage: /ponyplgn reset <nickname>");
  303.                     }
  304.                 } else {
  305.                     Util.noPerm(pony);
  306.                 }
  307.             } else {
  308.                 if (args.length > 2) {
  309.                     Database.setDefault(getServer().getOfflinePlayer(args[1]).getPlayer());
  310.                     console.info(args[1] + "'s race reset to default");
  311.                 } else {
  312.                     console.info("Usage: /ponyplgn reset <nickname>");
  313.                 }
  314.             }
  315.             break;
  316.         case "gerror":
  317.             if (sender instanceof Player) {
  318.                 if (args.length > 2) {
  319.                     try {
  320.                         sender.sendMessage(Util.gerror(new Integer(args[1])));
  321.                     } catch (NumberFormatException e) {
  322.                         sender.sendMessage(ChatColor.RED + args[1] + " is not numeric");
  323.                     }
  324.                 } else {
  325.                     sender.sendMessage(ChatColor.RED + "Usage: /ponyplgn gerror <errorNumber>");
  326.                 }
  327.             } else {
  328.                 if (args.length > 2) {
  329.                     try {
  330.                         console.info(Util.gerror(new Integer(args[1])));
  331.                     } catch (NumberFormatException e) {
  332.                         console.info(args[1] + " is not numeric");
  333.                     }
  334.                 } else {
  335.                     console.info("Usage: /ponyplgn gerror <errorNumber>");
  336.                 }
  337.             }
  338.             break;
  339.         case "getraw":
  340.             if (sender instanceof Player) {
  341.                 if (Util.hasPerm((Player) sender, "pony.current.raw", false)) {
  342.                     if (args.length < 2) {
  343.                         if (database.contains(args[1])) {
  344.                             sender.sendMessage(ChatColor.GOLD + args[1] + ": " + database.getString(args[1]));
  345.                         } else {
  346.                             sender.sendMessage(ChatColor.GOLD + args[1] + " is not in the database");
  347.                         }
  348.                     } else {
  349.                         sender.sendMessage(ChatColor.RED + "Usage: /ponyplgn getraw <nickname>");
  350.                     }
  351.                 } else {
  352.                     Util.noPerm((Player) sender);
  353.                 }
  354.             } else {
  355.                 if (args.length < 2) {
  356.                     if (database.contains(args[1])) {
  357.                         console.info(args[1] + ": " + database.getString(args[1]));
  358.                     } else {
  359.                         console.info(args[1] + " is not in the database");
  360.                     }
  361.                 } else {
  362.                     console.info("Usage: /ponyplgn getraw <nickname>");
  363.                 }
  364.             }
  365.         case "setraw":
  366.             if (sender instanceof Player) {
  367.                 Player pony = (Player) sender;
  368.                 if (Util.hasPerm(pony, "pony.set.raw", false)) {
  369.                     if (args.length > 3) {
  370.                         database.set(args[1], args[2]);
  371.                         pony.sendMessage(ChatColor.GOLD + args[1] + "'s race set to " + args[2]);
  372.                     } else {
  373.                         pony.sendMessage(ChatColor.RED + "Usage: /ponyplgn setraw <nickname> <newRace>");
  374.                     }
  375.                 } else {
  376.                     Util.noPerm(pony);
  377.                 }
  378.             } else {
  379.                 if (args.length > 3) {
  380.                     database.set(args[1], args[2]);
  381.                     console.info(ChatColor.GOLD + args[1] + "'s race set to " + args[2]);
  382.                 } else {
  383.                     console.info(ChatColor.RED + "Usage: /ponyplgn setraw <nickname> <newRace>");
  384.                 }
  385.             }
  386.             break;
  387.         case "totalreset":
  388.             if (sender instanceof Player) {
  389.                 if (Util.hasPerm((Player) sender, "pony.reset.all", false)) {
  390.                     try {
  391.                         FileConfiguration database2 = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "database_old.yml"));
  392.                         database2.loadFromString(database.saveToString());
  393.                         database2.save(new File(getDataFolder(), "database_old.yml"));
  394.                         database.loadFromString("");
  395.                         database.save(databaseFile);
  396.                         sender.sendMessage(ChatColor.GOLD + "Database cleaned." + ChatColor.GRAY + "Copy saved to database_old.yml");
  397.                     } catch (InvalidConfigurationException | IOException e) {
  398.                         Util.throwException(e, 0); //* * * ERROR 4 * * * (c)
  399.                         sender.sendMessage(ChatColor.GOLD + "Caught some exception, see console for details");
  400.                     }
  401.                 } else {
  402.                     Util.noPerm((Player) sender);
  403.                 }
  404.             } else {
  405.                 try {
  406.                     FileConfiguration database2 = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "database_old.yml"));
  407.                     database2.loadFromString(database.saveToString());
  408.                     database2.save(new File(getDataFolder(), "database_old.yml"));
  409.                     database.loadFromString("");
  410.                     database.save(databaseFile);
  411.                     console.info(ChatColor.GOLD + "Database cleaned." + ChatColor.GRAY + "Copy saved to database_old.yml");
  412.                 } catch (InvalidConfigurationException | IOException e) {
  413.                     Util.throwException(e, 0); //* * * ERROR 4 * * * (c)
  414.                 }
  415.             }
  416.         case "savedata":
  417.             if (sender instanceof Player) {
  418.                 if (Util.hasPerm((Player) sender, "pony.save", false)) {
  419.                     Database.save();
  420.                     sender.sendMessage(ChatColor.GOLD + "Database saved");
  421.                 } else {
  422.                     Util.noPerm((Player) sender);
  423.                 }
  424.             } else {
  425.                 Database.save();
  426.                 console.info(ChatColor.GOLD + "Database saved");
  427.             }
  428.         case "reload":
  429.             if (sender instanceof Player) {
  430.                 if (Util.hasPerm((Player) sender, "pony.reload", false)) {
  431.                     Database.save();
  432.                     sender.sendMessage(ChatColor.GOLD + "Database saved");
  433.                     onEnable();
  434.                     sender.sendMessage(ChatColor.GOLD + "Database & config reloaded");
  435.                 } else {
  436.                     Util.noPerm((Player) sender);
  437.                 }
  438.             } else {
  439.                 Database.save();
  440.                 console.info(ChatColor.GOLD + "Database saved");
  441.                 onEnable();
  442.                 console.info(ChatColor.GOLD + "Database & config reloaded");
  443.             }
  444.         default:
  445.         }
  446.     }
  447.    
  448.     private void commandCast(CommandSender sender, String[] args) {
  449.         if (sender instanceof Player) {
  450.             Player pony = (Player) sender;
  451.             if (Util.hasPerm(pony, "pony.cast", true /*, of course true!*/)) {
  452.                 if (Database.getRace(pony) == "unicorn" || Database.getRace(pony) == "alicorn") {
  453.                     switch (args[0].toLowerCase()) {
  454.                     case "hjump":
  455.                         pony.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 1200, 0));
  456.                         pony.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 1200, 0));
  457.                         break;
  458.                     case "regen":
  459.                         pony.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 300, 0));
  460.                         pony.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 600, 1));
  461.                         break;
  462.                     case "haste":
  463.                         pony.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 1200, 0));
  464.                         pony.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 1200, 0));
  465.                         break;
  466.                     case "nregen":
  467.                         if (pony.getFoodLevel() > 6) {
  468.                             pony.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 1, 0));
  469.                             pony.setFoodLevel(pony.getFoodLevel() - 6);
  470.                         } else {
  471.                             pony.setFoodLevel(0);
  472.                         }
  473.                     case "clear":
  474.                         for (int i = 0; i < pony.getActivePotionEffects().toArray().length; i++) {
  475.                             pony.removePotionEffect((PotionEffectType) pony.getActivePotionEffects().toArray()[i]);
  476.                         }
  477.                         pony.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 100, 0));
  478.                     }
  479.                 } else {
  480.                     pony.sendMessage(config.getString("msgCastWrongRace", ChatColor.GOLD + "Sorry, your magical skills are too bad to cast spells!"));
  481.                 }
  482.             }
  483.         } else {
  484.             getLogger().info("This command can only be performed by a player, use /ponyplgn instead");
  485.         }
  486.     }
  487.    
  488.    
  489.    
  490.     @EventHandler
  491.     public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  492.         switch (command.getName()) {
  493.         case "pony":
  494.             commandPony(sender, args);
  495.             break;
  496.         case "ponyplgn":
  497.             commandPlgn(sender, args);
  498.             break;
  499.         case "cast":
  500.             commandCast(sender, args);
  501.         }
  502.         return true;
  503.     }
  504.    
  505.     @EventHandler
  506.     public void onJoin(PlayerJoinEvent event) {
  507.         Database.checkRace(event.getPlayer());
  508.     }
  509.    
  510.     @EventHandler
  511.     public void onRespawn(PlayerRespawnEvent event) {
  512.         Database.checkRace(event.getPlayer());
  513.     }
  514.    
  515.     @EventHandler
  516.     public void onGamemodeChange(PlayerGameModeChangeEvent event) {
  517.         Database.pegasusCheck(event.getPlayer());
  518.     }
  519.    
  520.     @EventHandler
  521.     public void onFoodLevelChange(FoodLevelChangeEvent event) {
  522.         Database.pegasusCheck((Player) event.getEntity());
  523.     }
  524.    
  525.     @EventHandler
  526.     public void onAttack(EntityDamageByEntityEvent event) {
  527.         if (event.getDamager() instanceof Player) {
  528.             if (Database.getRace((Player) event.getDamager()) == "unicorn" && !(((Player) event.getDamager()).getGameMode() == GameMode.valueOf("CREATIVE"))) {
  529.                 if (event.getDamage() - 4.0 < 0) {
  530.                     event.setCancelled(true);
  531.                 } else {
  532.                     event.setDamage(event.getDamage() - 4.0);
  533.                 }
  534.             }
  535.         }
  536.     }
  537. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement