/Bukkit Basiks Teleports/src/me/oa10712/bukkitbasiksteleports/BukkitBasiksTeleports.java

https://github.com/theblackhatman/Bukkit-Basiks · Java · 173 lines · 157 code · 8 blank · 8 comment · 12 complexity · b4cfec2f70fd77e39b7f4f5fb790fc57 MD5 · raw file

  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package me.oa10712.bukkitbasiksteleports;
  6. import java.io.File;
  7. import java.io.FileNotFoundException;
  8. import java.io.IOException;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. import me.oa10712.bukkitbasikscore.BukkitBasiksCore;
  12. import org.bukkit.Bukkit;
  13. import org.bukkit.Server;
  14. import org.bukkit.configuration.InvalidConfigurationException;
  15. import org.bukkit.configuration.file.YamlConfiguration;
  16. import org.bukkit.plugin.Plugin;
  17. import org.bukkit.plugin.java.JavaPlugin;
  18. import me.oa10712.bukkitbasikscore.functions.setConfig;
  19. import me.oa10712.bukkitbasiksteleports.functions.teleport;
  20. import org.bukkit.Location;
  21. import org.bukkit.command.Command;
  22. import org.bukkit.command.CommandSender;
  23. import org.bukkit.entity.Player;
  24. import org.bukkit.event.EventHandler;
  25. import org.bukkit.event.Listener;
  26. import org.bukkit.event.player.PlayerLoginEvent;
  27. /**
  28. *
  29. * @author Oa10712
  30. */
  31. public class BukkitBasiksTeleports extends JavaPlugin implements Listener {
  32. private YamlConfiguration teleportsConfig;
  33. static final Logger log = Logger.getLogger("Minecraft");
  34. Server server = Bukkit.getServer();
  35. File dataFolder = new File(server.getWorldContainer().getPath() + File.separator + "plugins" + File.separator + "Bukkit Basiks");
  36. File teleportsConfigFile = new File(dataFolder.getPath() + File.separator + "bukkitbasiksconfig.yml");
  37. Plugin basiks = server.getPluginManager().getPlugin("Bukkit Basiks Core");
  38. private transient setConfig configset;
  39. private transient teleport tele;
  40. private YamlConfiguration userData;
  41. File userDataFile = new File(dataFolder.getPath() + File.separator + "userData.yml");
  42. @Override
  43. public void onDisable() {
  44. try {
  45. teleportsConfig.load(teleportsConfigFile);
  46. teleportsConfig.set("Teleports.Enabled", false);
  47. teleportsConfig.save(teleportsConfigFile);
  48. } catch (FileNotFoundException ex) {
  49. Logger.getLogger(BukkitBasiksTeleports.class.getName()).log(Level.SEVERE, null, ex);
  50. } catch (IOException ex) {
  51. Logger.getLogger(BukkitBasiksTeleports.class.getName()).log(Level.SEVERE, null, ex);
  52. } catch (InvalidConfigurationException ex) {
  53. Logger.getLogger(BukkitBasiksTeleports.class.getName()).log(Level.SEVERE, null, ex);
  54. }
  55. }
  56. @Override
  57. public void onEnable() {
  58. teleportsConfig = new YamlConfiguration();
  59. this.configset = new setConfig();
  60. this.tele = new teleport();
  61. try {
  62. teleportsConfig.load(teleportsConfigFile);
  63. if ((teleportsConfig.contains("Teleports.Defaults") == false) || (teleportsConfig.getBoolean("Teleports.Default") == true)) {
  64. setupTeleportsConfig();
  65. }
  66. teleportsConfig.load(teleportsConfigFile);
  67. teleportsConfig.set("Teleports.Enabled", true);
  68. teleportsConfig.save(teleportsConfigFile);
  69. } catch (FileNotFoundException ex) {
  70. Logger.getLogger(BukkitBasiksTeleports.class.getName()).log(Level.SEVERE, null, ex);
  71. } catch (IOException ex) {
  72. Logger.getLogger(BukkitBasiksTeleports.class.getName()).log(Level.SEVERE, null, ex);
  73. } catch (InvalidConfigurationException ex) {
  74. Logger.getLogger(BukkitBasiksTeleports.class.getName()).log(Level.SEVERE, null, ex);
  75. }
  76. server.getPluginManager().registerEvents(this, this);
  77. }
  78. @Override
  79. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
  80. Player player = null;
  81. if (sender instanceof Player) {
  82. player = (Player) sender;
  83. }
  84. if (cmd.getName().equalsIgnoreCase("tp")) {
  85. tele.teleport(player, args);
  86. return true;
  87. }
  88. if (cmd.getName().equalsIgnoreCase("sethome")) {
  89. try {
  90. userData = new YamlConfiguration();
  91. Location playerlocat = player.getLocation();
  92. userData.load(userDataFile);
  93. userData.set("Users." + player.getName() + ".Home.X", playerlocat.getX());
  94. userData.set("Users." + player.getName() + ".Home.Y", playerlocat.getY());
  95. userData.set("Users." + player.getName() + ".Home.Z", playerlocat.getZ());
  96. userData.set("Users." + player.getName() + ".Home.World", playerlocat.getWorld().getName());
  97. userData.save(userDataFile);
  98. player.sendMessage("Home set");
  99. } catch (InvalidConfigurationException ex) {
  100. Logger.getLogger(BukkitBasiksTeleports.class.getName()).log(Level.SEVERE, null, ex);
  101. } catch (FileNotFoundException ex) {
  102. Logger.getLogger(BukkitBasiksTeleports.class.getName()).log(Level.SEVERE, null, ex);
  103. } catch (IOException ex) {
  104. Logger.getLogger(BukkitBasiksTeleports.class.getName()).log(Level.SEVERE, null, ex);
  105. }
  106. }
  107. if (cmd.getName().equalsIgnoreCase("home")) {
  108. try {
  109. userData = new YamlConfiguration();
  110. userData.load(userDataFile);
  111. double homex = userData.getDouble("Users." + player.getName() + ".Home.X");
  112. double homey = userData.getDouble("Users." + player.getName() + ".Home.Y");
  113. double homez = userData.getDouble("Users." + player.getName() + ".Home.Z");
  114. String homeworld = userData.getString("Users." + player.getName() + ".Home.World");
  115. Object[] argout = {cmd.getName()};
  116. if (homey != 0.0) {
  117. tele.teleport(player, argout);
  118. } else {
  119. player.sendMessage("You do not have a home yet. Use /sethome to set your home.");
  120. }
  121. return true;
  122. } catch (FileNotFoundException ex) {
  123. Logger.getLogger(BukkitBasiksTeleports.class.getName()).log(Level.SEVERE, null, ex);
  124. } catch (IOException ex) {
  125. Logger.getLogger(BukkitBasiksTeleports.class.getName()).log(Level.SEVERE, null, ex);
  126. } catch (InvalidConfigurationException ex) {
  127. Logger.getLogger(BukkitBasiksTeleports.class.getName()).log(Level.SEVERE, null, ex);
  128. }
  129. }
  130. return false;
  131. }
  132. @EventHandler
  133. public void normalLogin(PlayerLoginEvent event) {
  134. try {
  135. userData = new YamlConfiguration();
  136. Player player = event.getPlayer();
  137. userData.load(userDataFile);
  138. if (userData.contains("Users." + player.getName() + ".Home")) {
  139. userData.set("Users." + player.getName() + ".Home", null);
  140. userData.save(userDataFile);
  141. }
  142. } catch (FileNotFoundException ex) {
  143. Logger.getLogger(BukkitBasiksCore.class.getName()).log(Level.SEVERE, null, ex);
  144. } catch (IOException ex) {
  145. Logger.getLogger(BukkitBasiksCore.class.getName()).log(Level.SEVERE, null, ex);
  146. } catch (InvalidConfigurationException ex) {
  147. Logger.getLogger(BukkitBasiksCore.class.getName()).log(Level.SEVERE, null, ex);
  148. }
  149. }
  150. private void setupTeleportsConfig() {
  151. try {
  152. teleportsConfig = new YamlConfiguration();
  153. teleportsConfig.load(teleportsConfigFile);
  154. teleportsConfig.set("Teleports.Defaults.Wait", 0);
  155. teleportsConfig.save(teleportsConfigFile);
  156. log.info("Teleports config generated");
  157. } catch (FileNotFoundException ex) {
  158. Logger.getLogger(BukkitBasiksTeleports.class.getName()).log(Level.SEVERE, null, ex);
  159. } catch (InvalidConfigurationException ex) {
  160. Logger.getLogger(BukkitBasiksTeleports.class.getName()).log(Level.SEVERE, null, ex);
  161. } catch (IOException ex) {
  162. Logger.getLogger(BukkitBasiksTeleports.class.getName()).log(Level.SEVERE, null, ex);
  163. }
  164. }
  165. }