Advertisement
Guest User

Ponyplgn - class Database

a guest
Nov 16th, 2014
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.43 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.GameMode;
  8. import org.bukkit.configuration.file.FileConfiguration;
  9. import org.bukkit.entity.Player;
  10.  
  11. import iwcs.pony.Main;
  12. import iwcs.pony.Util;
  13.  
  14.  
  15.  
  16. public class Database {
  17.    
  18.     private Main Main = new Main();
  19.     private Util Util = new Util();
  20.    
  21.     //Variables
  22.     //Importing
  23.     private Logger console = Main.console;
  24.     private FileConfiguration database = Main.database;
  25.    
  26.    
  27.    
  28.     public String getRace(Player pony) { //Returns "unset", "unpresent" or the race in lower case
  29.         if (database.contains(pony.getName()) && database.isString(pony.getName())) {
  30.             if (database.getString(pony.getName(), null).equalsIgnoreCase("earth") || database.getString(pony.getName(), null).equalsIgnoreCase("pegasus") || database.getString(pony.getName(), null).equalsIgnoreCase("unicorn") || database.getString(pony.getName(), null).equalsIgnoreCase("alicorn") || database.getString(pony.getName(), null).equalsIgnoreCase("human")) {
  31.                 return database.getString(pony.getName(), null);
  32.             } else if (database.getString(pony.getName(), null) == null) {
  33.                 return "unset";
  34.             } else {
  35.                 Util.throwException(new IOException(), 0); //* * * ERROR 5 * * *
  36.                 return "unset";
  37.             }
  38.         } else {
  39.             return "unpresent";
  40.         }
  41.     }
  42.    
  43.     public void save() {
  44.         try {
  45.             database.save(Main.databaseFile);
  46.         } catch (IOException e) {
  47.             Util.throwException(e, 6); //* * * ERROR 7 * * *
  48.         }
  49.     }
  50.    
  51.     public void save(Player pony, String newValue) {
  52.         if (pony !=null) {
  53.             database.set(pony.getName(), newValue);
  54.         } else {
  55.             Util.throwException(new NullPointerException(), 0); //* * * ERROR 8 * * *
  56.         }
  57.     }
  58.    
  59.     public void setRace(Player pony, String newRace) {
  60.         if (newRace == "earth" || newRace == "pegasus" || newRace == "unicorn" || newRace == "alicorn" || newRace == "human") {
  61.             save(pony, newRace);
  62.             switch (newRace) {
  63.             case "earth":
  64.                 if (!(pony.getGameMode() == GameMode.valueOf("CREATIVE"))) {pony.setAllowFlight(false);}
  65.                 pony.setWalkSpeed((float) 0.25);
  66.                 pony.setFlySpeed((float) 0.1);
  67.                 pony.setMaxHealth((double) 22.0);
  68.                 break;
  69.             case "pegasus":
  70.                 pony.setAllowFlight(true);
  71.                 pony.setWalkSpeed((float) 0.15);
  72.                 pony.setFlySpeed((float) 0.13);
  73.                 pony.setMaxHealth((double) 20.0);
  74.                 pegasusCheck(pony);
  75.                 break;
  76.             case "unicorn":
  77.                 if (!(pony.getGameMode() == GameMode.valueOf("CREATIVE"))) {pony.setAllowFlight(false);}
  78.                 pony.setWalkSpeed((float) 0.2);
  79.                 pony.setFlySpeed((float) 0.1);
  80.                 pony.setMaxHealth((double) 18.0);
  81.                 break;
  82.             case "alicorn":
  83.                 pony.setAllowFlight(true);
  84.                 pony.setWalkSpeed((float) 0.25);
  85.                 pony.setFlySpeed((float) 0.13);
  86.                 pony.setMaxHealth((double) 22.0);
  87.                 break;
  88.             case "human":
  89.                 if (!(pony.getGameMode() == GameMode.valueOf("CREATIVE"))) {pony.setAllowFlight(false);}
  90.                 pony.setWalkSpeed((float) 0.2);
  91.                 pony.setFlySpeed((float) 0.1);
  92.                 pony.setMaxHealth((double) 20.0);
  93.             }
  94.         } else {
  95.             Util.throwException(new IllegalArgumentException(), 9);
  96.         }
  97.     }
  98.    
  99.     public void setDefault(Player pony) {
  100.         String fjr = Main.config.getString("firstJoinRace", "random-a-h");
  101.         if (fjr == "earth") {
  102.             setRace(pony, "earth");
  103.             pony.sendMessage(Main.config.getString("msgEarthSet", ChatColor.GOLD + "You are an earth pony now!"));
  104.         } else if (fjr == "pegasus") {
  105.             setRace(pony, "pegasus");
  106.             pony.sendMessage(Main.config.getString("msgPegasusSet", ChatColor.GOLD + "You are a pegasus now!"));
  107.         } else if (fjr == "unicorn") {
  108.             setRace(pony, "unicorn");
  109.             pony.sendMessage(Main.config.getString("msgUnicornSet", ChatColor.GOLD + "You are a unicorn now!"));
  110.         } else if (fjr == "alicorn") {
  111.             setRace(pony, "alicorn");
  112.             pony.sendMessage(Main.config.getString("msgAlicornSet", ChatColor.GOLD + "You are an alicorn now!"));
  113.         } else if (fjr == "human") {
  114.             setRace(pony, "human");
  115.             pony.sendMessage(Main.config.getString("msgHumanSet", ChatColor.GOLD + "You are a human now!"));
  116.            
  117.         } else if (fjr == "random") {
  118.             double random = Math.random();
  119.             if (random < 0.2) {
  120.                 setRace(pony, "earth");
  121.                 pony.sendMessage(Main.config.getString("msgEarthSet", ChatColor.GOLD + "You are an earth pony now!"));
  122.             } else if (random < 0.4) {
  123.                 setRace(pony, "pegasus");
  124.                 pony.sendMessage(Main.config.getString("msgPegasusSet", ChatColor.GOLD + "You are a pegasus now!"));
  125.             } else if (random < 0.6) {
  126.                 setRace(pony, "unicorn");
  127.                 pony.sendMessage(Main.config.getString("msgUnicornSet", ChatColor.GOLD + "You are a unicorn now!"));
  128.             } else if (random < 0.8) {
  129.                 setRace(pony, "alicorn");
  130.                 pony.sendMessage(Main.config.getString("msgAlicornSet", ChatColor.GOLD + "You are an alicorn now!"));
  131.             } else {
  132.                 setRace(pony, "human");
  133.                 pony.sendMessage(Main.config.getString("msgHumanSet", ChatColor.GOLD + "You are a human now!"));
  134.             }
  135.            
  136.         } else if (fjr == "random-a") {
  137.             double random = Math.random();
  138.             if (random < 0.25) {
  139.                 setRace(pony, "earth");
  140.                 pony.sendMessage(Main.config.getString("msgEarthSet", ChatColor.GOLD + "You are an earth pony now!"));
  141.             } else if (random < 0.5) {
  142.                 setRace(pony, "pegasus");
  143.                 pony.sendMessage(Main.config.getString("msgPegasusSet", ChatColor.GOLD + "You are a pegasus now!"));
  144.             } else if (random < 0.75) {
  145.                 setRace(pony, "unicorn");
  146.                 pony.sendMessage(Main.config.getString("msgUnicornSet", ChatColor.GOLD + "You are a unicorn now!"));
  147.             } else {
  148.                 setRace(pony, "human");
  149.                 pony.sendMessage(Main.config.getString("msgHumanSet", ChatColor.GOLD + "You are a human now!"));
  150.             }
  151.         } else if (fjr == "random-h") {
  152.             double random = Math.random();
  153.             if (random < 0.25) {
  154.                 setRace(pony, "earth");
  155.                 pony.sendMessage(Main.config.getString("msgEarthSet", ChatColor.GOLD + "You are an earth pony now!"));
  156.             } else if (random < 0.5) {
  157.                 setRace(pony, "pegasus");
  158.                 pony.sendMessage(Main.config.getString("msgPegasusSet", ChatColor.GOLD + "You are a pegasus now!"));
  159.             } else if (random < 0.75) {
  160.                 setRace(pony, "unicorn");
  161.                 pony.sendMessage(Main.config.getString("msgUnicornSet", ChatColor.GOLD + "You are a unicorn now!"));
  162.             } else {
  163.                 setRace(pony, "alicorn");
  164.                 pony.sendMessage(Main.config.getString("msgAlicornSet", ChatColor.GOLD + "You are an alicorn now!"));
  165.             }
  166.         } else {
  167.             double random = Math.random();
  168.             if (random < 1.0 / 3.0) {
  169.                 setRace(pony, "earth");
  170.                 pony.sendMessage(Main.config.getString("msgEarthSet", ChatColor.GOLD + "You are an earth pony now!"));
  171.             } else if (random < 2.0 / 3.0) {
  172.                 setRace(pony, "pegasus");
  173.                 pony.sendMessage(Main.config.getString("msgPegasusSet", ChatColor.GOLD + "You are a pegasus now!"));
  174.             } else {
  175.                 setRace(pony, "unicorn");
  176.                 pony.sendMessage(Main.config.getString("msgUnicornSet", ChatColor.GOLD + "You are a unicorn now!"));
  177.             }
  178.         }
  179.     }
  180.    
  181.     public void checkRace(Player pony) {
  182.         String checkedRace = getRace(pony);
  183.         if (checkedRace == "unset" || checkedRace == "unpresent") {
  184.             setDefault(pony);
  185.             console.info(pony.getName() + "'s race is set to default");
  186.         } else {
  187.             setRace(pony, checkedRace);
  188.         }
  189.     }
  190.    
  191.     public void pegasusCheck(Player pony) {
  192.         if (getRace(pony) == "pegasus") {
  193.             if (pony.getFoodLevel() < 7) {
  194.                 pony.setWalkSpeed((float) 0.1);
  195.                 if (!(pony.getGameMode() == GameMode.valueOf("CREATIVE"))) {pony.setAllowFlight(false);}
  196.             } else {
  197.                 pony.setAllowFlight(true);
  198.                 pony.setWalkSpeed((float) 0.15);
  199.             }
  200.         }
  201.     }
  202.    
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement