Advertisement
MsXTab

XHeads

Sep 5th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. package cz.sam.plugin;
  2.  
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6.  
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.Location;
  9. import org.bukkit.Material;
  10. import org.bukkit.SkullType;
  11. import org.bukkit.World;
  12. import org.bukkit.block.Block;
  13. import org.bukkit.block.Skull;
  14. import org.bukkit.command.Command;
  15. import org.bukkit.command.CommandSender;
  16. import org.bukkit.entity.Player;
  17. import org.bukkit.plugin.java.JavaPlugin;
  18.  
  19. import com.mysql.jdbc.Connection;
  20. import com.mysql.jdbc.Statement;
  21.  
  22. public class XHeads extends JavaPlugin {
  23.  
  24. public void onEnable() {
  25. Config.log = Bukkit.getServer().getLogger();
  26. Config.log.info("[XHeads] Activated.");
  27.  
  28. this.saveDefaultConfig();
  29. Config.DBActive = this.getConfig().getBoolean("mysql.active");
  30. Config.DBIp = this.getConfig().getString("mysql.server");
  31. Config.DBPort = this.getConfig().getInt("mysql.port");
  32. Config.DBUser = this.getConfig().getString("mysql.user");
  33. Config.DBPass = this.getConfig().getString("mysql.pass");
  34. Config.Databse = this.getConfig().getString("mysql.database");
  35.  
  36. this.Connect();
  37.  
  38. this.UpdateSkull();
  39.  
  40. }
  41.  
  42. public void UpdateSkull() {
  43. Bukkit.getScheduler().runTaskTimer(this, new Runnable() {
  44. public void run() {
  45. try {
  46. ResultSet readHeads = Config.st.executeQuery("SELECT * FROM `sg_heads`");
  47. while(readHeads.next()) {
  48. updateSkull(readHeads.getString("world"), getName(readHeads.getInt("position")), readHeads.getInt("x"), readHeads.getInt("y"), readHeads.getInt("z"));
  49. }
  50. } catch (SQLException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. }, 30, 60);
  55. }
  56.  
  57. public String getName(int Position) {
  58. String name = null;
  59. try {
  60. ResultSet getName = Config.st.executeQuery("SELECT * FROM `sg_playerstats` WHERE `position`='" + Position + "'");
  61. while(getName.next()) {
  62. name = getName.getString("player");
  63. }
  64. } catch (SQLException e) {
  65. e.printStackTrace();
  66. } finally {
  67.  
  68. }
  69. return name;
  70. }
  71.  
  72. public void updateSkull(String World, String PlayerName, int x, int y, int z){
  73. World Sworld = Bukkit.getWorld(World);
  74. Location loc = new Location(Sworld, x, y, z);
  75. Block b = loc.getBlock();
  76. if(b.getType() == Material.SKULL){
  77. Skull skull = (Skull) b.getState();
  78. skull.setSkullType(SkullType.PLAYER);
  79. skull.setOwner(PlayerName);
  80. skull.update(true);
  81. }
  82. }
  83.  
  84. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
  85. Player player = (Player) sender;
  86. if(cmd.getName().equalsIgnoreCase("setskull")) {
  87. if(args[0] != null & args[1] != null & args[2] != null & args[3] != null & args[4] != null) {
  88. try {
  89. Config.st.execute("INSERT INTO `sg_heads` (position, world, x, y, z) values('" + args[0] + "', '" + args[1] + "', '" + args[2] + "', '" + args[3] + "', '" + args[4] + "')");
  90. player.sendMessage("Pozice hlavy ulozena.");
  91. } catch (SQLException e) {
  92. e.printStackTrace();
  93. }
  94. }
  95. }
  96. return false;
  97. }
  98.  
  99. public void Connect() {
  100. if(Config.DBActive) {
  101. try {
  102. Config.con = (Connection) DriverManager.getConnection("jdbc:mysql://" + Config.DBIp + ":" + Config.DBPort + "/" + Config.Databse, Config.DBUser, Config.DBPass);
  103. Config.st = (Statement) Config.con.createStatement();
  104. Config.st.execute("CREATE TABLE IF NOT EXISTS `sg_heads` (`id` int(255) NOT NULL AUTO_INCREMENT, `position` int(255) NOT NULL, `world` varchar(255) NOT NULL, `x` int(255) NOT NULL, `y` int(255) NOT NULL, `z` int(255) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1");
  105. Config.log.info("[XHeads] Plugin se pripojil do database !");
  106. } catch (SQLException e) {
  107. e.printStackTrace();
  108. }
  109. } else {
  110. Config.log.info("[XHeads] Pro pouziti pluginu musi byt nastavena database !");
  111. }
  112. }
  113.  
  114. public void onDisable() {
  115. Config.log.info("[XHeads] Deactivated.");
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement