Guest User

Untitled

a guest
Mar 18th, 2017
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.48 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. //This Plugin Is Made For MumboCraft & It's Players & Staff.. Any misuse or re-distribution of this plugin will call for said plugin to be removed
  20. //Plugin by A_Brave_Panda & Jamdoggy
  21.  
  22. public class DeathLocation extends JavaPlugin implements Listener {
  23.  
  24. MySQL MySQLC = null;
  25. Connection c = null;
  26.  
  27. String host = getConfig().getString("Hostname");
  28. String port = getConfig().getString("Port");
  29. String db = getConfig().getString("Database");
  30. String username = getConfig().getString("Username");
  31. String password = getConfig().getString("Password");
  32. String table = getConfig().getString("Table");
  33.            
  34.  
  35.  @Override
  36.  public void onDisable() {
  37.  
  38.  }
  39.  
  40.  @Override
  41.  public void onEnable() {
  42.       getServer().getPluginManager().registerEvents(this, this);
  43.       registerConfig();
  44.       MySQL MySQLC = new MySQL(host, port, db, username, password);
  45.   try {
  46.     c = MySQLC.openConnection();
  47. } catch (ClassNotFoundException e) {
  48.     e.printStackTrace();
  49. } catch (SQLException e) {
  50.  
  51.     e.printStackTrace();
  52. }
  53.  
  54. }
  55.  
  56.  public void registerConfig() {
  57.     saveDefaultConfig();
  58.    
  59.        
  60.     }
  61.  
  62.  @EventHandler
  63.  public void onPlayerDeath(PlayerDeathEvent e) {
  64.   if (e.getEntity() instanceof Player) {
  65.    Player p = e.getEntity();
  66.    Location loc = e.getEntity().getLocation();
  67.    double x = loc.getBlockX() + 0.5;
  68.    double y = loc.getBlockY();
  69.    double z = loc.getBlockZ() + 0.5;
  70.    String dr = e.getDeathMessage();
  71.    String world = loc.getWorld().getName();
  72.    java.util.Date dt = new java.util.Date();
  73.  
  74.    java.text.SimpleDateFormat sdf =
  75.         new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  76.  
  77.    String time = sdf.format(dt);
  78.    
  79.    
  80.         String statementstring = "SELECT * FROM " + table + " WHERE PlayerName = '" + p.getName() +"';";
  81.         String statementstring2 = "INSERT INTO "+ table + " (id, PlayerName, X, Y, Z, DeathReason, world,Time) VALUES (NULL, '" + p.getName() + "', '" + x + "', '" + y + "', '" + z + "', '" + dr + "', '" + world + "', '" + time + "');";
  82.        
  83.     try {
  84.         Statement statement = c.createStatement();
  85.         ResultSet res = statement.executeQuery(statementstring);
  86.         if(res.next()) {
  87.             String statementstring3 = "UPDATE " + table + " SET X='" + x + "', Y='" + y + "', Z='" + z + "', DeathReason='" + dr + "', world='" + world + "',Time='" + time + "' WHERE id='" + res.getInt("id") + "';";
  88.             statement.executeUpdate(statementstring3);
  89.             } else {
  90.                 statement.executeUpdate(statementstring2);
  91.             }
  92.     } catch (SQLException e2) {
  93.        
  94.         e2.printStackTrace();
  95.     }
  96.   }
  97.  }
  98.    
  99.  
  100.   public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  101.     Player p = (Player)sender;
  102.     boolean tp = false;
  103.     String statementstring4 = null;
  104.    
  105.     if(cmd.getLabel().equalsIgnoreCase("death")) {
  106.       if(args.length != 0) {
  107.         if(args[0].equals("tp")) {
  108.           if(p.hasPermission("death.teleport")) {  
  109.               String sanitised_arg = args[1].replace("\\","");
  110.               sanitised_arg = sanitised_arg.replace("'","");
  111.             statementstring4 = "SELECT * FROM " + table + " WHERE PlayerName = '" + sanitised_arg.toString() +"';";
  112.             try {
  113.                 Statement statement = c.createStatement();
  114.                 ResultSet res = statement.executeQuery(statementstring4);
  115.                 if(res.next()) {
  116.                     Location l = new Location(Bukkit.getWorld(res.getString("world")), res.getDouble("X"), res.getInt("Y"), res.getDouble("Z"));
  117.                     p.teleport(l);
  118.                     p.sendMessage(ChatColor.GRAY +  "You have been teleported to " + ChatColor.GOLD + "" + ChatColor.BOLD + res.getString("PlayerName") + ChatColor.GRAY + "'s death location at [" +  ChatColor.GOLD + "" +
  119.                             res.getString("world") + ": " + res.getString("X") + ", " + res.getString("Y") + ", " + res.getString("Z") + ChatColor.GRAY + "] at: " + ChatColor.GOLD + "" +
  120.                             res.getString("time") + ChatColor.GRAY + ".");
  121.                     p.sendMessage(ChatColor.GRAY + "Cause of Death: " + ChatColor.GOLD + res.getString("DeathReason") + ChatColor.GRAY + "." );
  122.                 } else {
  123.                     p.sendMessage(ChatColor.RED + args[1] + " hasn't died! (yet :D)");
  124.                 }
  125.             } catch (SQLException e) {
  126.                 e.printStackTrace();
  127.             }
  128.           } else {
  129.             p.sendMessage(ChatColor.RED + "Panda & Jam Say You Have No Permission!");
  130.           }
  131.           tp = true;
  132.         } else if (p.hasPermission("death.others")) {
  133.           // Player used /death <name> and has permission to do so
  134.               String sanitised_arg = args[0].replace("\\","");
  135.               sanitised_arg = sanitised_arg.replace("'","");
  136.           statementstring4 = "SELECT * FROM " + table + " WHERE PlayerName = '" + sanitised_arg +"';";
  137.         } else {
  138.           // Player used /death <name> but doesn't have permission to do so
  139.           statementstring4 = "SELECT * FROM " + table + " WHERE PlayerName = '" + p.getName() +"';";
  140.         }
  141.       } else {
  142.         // Player just used /death with no args - use their own name
  143.         statementstring4 = "SELECT * FROM " + table + " WHERE PlayerName = '" + p.getName() +"';";
  144.       }
  145.      
  146.       // If 'tp' wasn't used...
  147.       if (!tp) {
  148.           try {
  149.             Statement statement = c.createStatement();
  150.             ResultSet res = statement.executeQuery(statementstring4);
  151.             if(res.next()) {
  152.                
  153.                 p.sendMessage(ChatColor.GRAY +  "Player " + ChatColor.GOLD + "" + ChatColor.BOLD + res.getString("PlayerName") + ChatColor.GRAY + " died at [" +  ChatColor.GOLD + "" +
  154.                         res.getString("world") + ": " + res.getString("X") + ", " + res.getString("Y") + ", " + res.getString("Z") + ChatColor.GRAY + "] at: " + ChatColor.GOLD + "" +
  155.                         res.getString("time") + ChatColor.GRAY + ".");
  156.                 p.sendMessage(ChatColor.GRAY + "Cause of Death: " + ChatColor.GOLD + res.getString("DeathReason") + ChatColor.GRAY + "." );
  157.                
  158.             } else if(args.length != 0) {
  159.                     p.sendMessage(ChatColor.RED + args[0] + " hasn't died! (yet :D)");
  160.                   } else {
  161.                     p.sendMessage(ChatColor.RED + " You haven't died! (yet :D)");
  162.                   }
  163.           } catch (SQLException e) {
  164.            
  165.             e.printStackTrace();
  166.           }
  167.       }
  168.     }
  169.     return true;
  170.   }
  171. }
Add Comment
Please, Sign In to add comment