Advertisement
Guest User

Untitled

a guest
Mar 18th, 2017
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. package me.zachbears27;
  2. import java.sql.Connection;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.Location;
  7. import org.bukkit.command.Command;
  8. import org.bukkit.command.CommandSender;
  9. import org.bukkit.entity.Player;
  10. import org.bukkit.event.EventHandler;
  11. import org.bukkit.event.Listener;
  12. import org.bukkit.event.entity.PlayerDeathEvent;
  13. import org.bukkit.plugin.java.JavaPlugin;
  14. import java.sql.Statement;
  15.  
  16. import me.zachbears27.MySQL;
  17. import net.md_5.bungee.api.ChatColor;
  18.  
  19. public class DeathLocation extends JavaPlugin implements Listener {
  20.  
  21.  
  22. MySQL MySQLC = null;
  23. Connection c = null;
  24. String host = getConfig().getString("Hostname");
  25. String port = getConfig().getString("Port");
  26. String db = getConfig().getString("Database");
  27. String username = getConfig().getString("Username");
  28. String password = getConfig().getString("Password");
  29. String table = getConfig().getString("Table");
  30.  
  31.  
  32. @Override
  33. public void onDisable() {
  34.  
  35. }
  36.  
  37. @Override
  38. public void onEnable() {
  39. getServer().getPluginManager().registerEvents(this, this);
  40. registerConfig();
  41. MySQL MySQLC = new MySQL(host, port, db, username, password);
  42. try {
  43. c = MySQLC.openConnection();
  44. } catch (ClassNotFoundException e) {
  45. e.printStackTrace();
  46. } catch (SQLException e) {
  47.  
  48. e.printStackTrace();
  49. }
  50.  
  51. }
  52.  
  53. public void registerConfig() {
  54. saveDefaultConfig();
  55.  
  56.  
  57. }
  58.  
  59. @EventHandler
  60. public void onPlayerDeath(PlayerDeathEvent e) {
  61. if (e.getEntity() instanceof Player) {
  62. Player p = e.getEntity();
  63. Location loc = e.getEntity().getLocation();
  64. double x = loc.getBlockX() + 0.5;
  65. double y = loc.getBlockY();
  66. double z = loc.getBlockZ() + 0.5;
  67. String dr = e.getDeathMessage();
  68. String world = loc.getWorld().getName();
  69. java.util.Date dt = new java.util.Date();
  70.  
  71. java.text.SimpleDateFormat sdf =
  72. new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  73.  
  74. String time = sdf.format(dt);
  75.  
  76.  
  77. String statementstring = "SELECT * FROM " + table + " WHERE PlayerName = '" + p.getName() +"';";
  78. String statementstring2 = "INSERT INTO "+ table + " (id, PlayerName, X, Y, Z, DeathReason, world,Time) VALUES (NULL, '" + p.getName() + "', '" + x + "', '" + y + "', '" + z + "', '" + dr + "', '" + world + "', '" + time + "');";
  79.  
  80. try {
  81. Statement statement = c.createStatement();
  82. ResultSet res = statement.executeQuery(statementstring);
  83. if(res.next()) {
  84. String statementstring3 = "UPDATE " + table + " SET X='" + x + "', Y='" + y + "', Z='" + z + "', DeathReason='" + dr + "', world='" + world + "',Time='" + time + "' WHERE id='" + res.getInt("id") + "';";
  85. statement.executeUpdate(statementstring3);
  86. } else {
  87. statement.executeUpdate(statementstring2);
  88. }
  89. } catch (SQLException e2) {
  90.  
  91. e2.printStackTrace();
  92. }
  93. }
  94. }
  95.  
  96.  
  97. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  98. Player p = (Player)sender;
  99. if(cmd.getLabel().equalsIgnoreCase("death")) {
  100. if(args.length != 0) {
  101. if(args[0].equals("tp")) {
  102. if(p.hasPermission("death.deathlocation")) {
  103. String statementstring = "SELECT * FROM " + table + " WHERE PlayerName = '" + args[1].toString() +"';";
  104. try {
  105. Statement statement = c.createStatement();
  106. ResultSet res = statement.executeQuery(statementstring);
  107. if(res.next()) {
  108.  
  109. Location l = new Location(Bukkit.getWorld(res.getString("world")), res.getDouble("X"), res.getInt("Y"), res.getDouble("Z"));
  110. p.teleport(l);
  111. p.sendMessage(ChatColor.GRAY + "You have been teleported to " + ChatColor.GOLD + "" + ChatColor.BOLD + res.getString("PlayerName") + ChatColor.GRAY + "'s death location at [" + ChatColor.GOLD + "" +
  112. res.getString("world") + ": " + res.getString("X") + ", " + res.getString("Y") + ", " + res.getString("Z") + ChatColor.GRAY + "] at: " + ChatColor.GOLD + "" +
  113. res.getString("time") + ChatColor.GRAY + ".");
  114. p.sendMessage(ChatColor.GRAY + "Cause of Death: " + ChatColor.GOLD + res.getString("DeathReason") + ChatColor.GRAY + "." );
  115. } else {
  116. p.sendMessage(ChatColor.RED + args[1] + " hasn't died! (yet :D)");
  117. }
  118. } catch (SQLException e) {
  119.  
  120. e.printStackTrace();
  121. }
  122.  
  123. } else {
  124. p.sendMessage(ChatColor.RED + "Panda & Jam Say You Have No Permission!");
  125. }
  126.  
  127. } else
  128. {
  129.  
  130. String statementstring = "SELECT * FROM " + table + " WHERE PlayerName = '" + args[0] +"';";
  131. try {
  132. Statement statement = c.createStatement();
  133. ResultSet res = statement.executeQuery(statementstring);
  134. if(res.next()) {
  135. if (p.getName() == args[1]) {
  136. p.sendMessage(ChatColor.GRAY + "Player " + ChatColor.GOLD + "" + ChatColor.BOLD + res.getString("PlayerName") + ChatColor.GRAY + " died at [" + ChatColor.GOLD + "" +
  137. res.getString("world") + ": " + res.getString("X") + ", " + res.getString("Y") + ", " + res.getString("Z") + ChatColor.GRAY + "] at: " + ChatColor.GOLD + "" +
  138. res.getString("time") + ChatColor.GRAY + ".");
  139. p.sendMessage(ChatColor.GRAY + "Cause of Death: " + ChatColor.GOLD + res.getString("DeathReason") + ChatColor.GRAY + "." );
  140. } else {
  141. p.sendMessage("Please Use Your Own Name");
  142. }
  143. } else {
  144. p.sendMessage(ChatColor.RED + args[0] + " hasn't died! (yet :D)");
  145. }
  146. } catch (SQLException e) {
  147.  
  148. e.printStackTrace();
  149. }
  150. }
  151.  
  152. }
  153. else
  154. {
  155. if(p.hasPermission("death.deathlocation")) {
  156. p.sendMessage(ChatColor.RED + "Please Specify A Player or Add TP Then A Player Name");
  157. } else
  158. {
  159. p.sendMessage(ChatColor.RED + "Please Specify A Player");
  160. }
  161. }
  162. }
  163. return true;
  164. }
  165.  
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement