Guest User

Untitled

a guest
May 14th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. package me.agil.plugins.chestwatcher;
  2.  
  3. import com.mysql.jdbc.Statement;
  4. import org.bukkit.plugin.java.JavaPlugin;
  5. import org.bukkit.plugin.PluginManager;
  6. import org.bukkit.block.Block;
  7. import org.bukkit.block.ContainerBlock;
  8. import org.bukkit.event.Event;
  9. import org.bukkit.inventory.ItemStack;
  10. import org.bukkit.util.config.Configuration;
  11. import java.io.File;
  12. import java.io.IOException;
  13. import java.util.Calendar;
  14. import java.util.HashMap;
  15. import java.util.logging.Logger;
  16. import java.sql.Connection;
  17. import java.sql.DriverManager;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.util.logging.Level;
  21. import javax.persistence.Version;
  22. import org.bukkit.configuration.file.FileConfiguration;
  23. import java.io.File;
  24. import java.io.FileOutputStream;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.util.List;
  28.  
  29. /**
  30. * ChestWatcher for Bukkit
  31. *
  32. * @author robin0van0der0v
  33. */
  34. public class Chestwatcher extends JavaPlugin {
  35.  
  36. public static final Logger log = Logger.getLogger("Minecraft");
  37. private final ChestwatcherBlockListener blockListener = new ChestwatcherBlockListener(this);
  38. private final ChestwatcherEntityListener entityListener = new ChestwatcherEntityListener(this);
  39. private final ChestwatcherPlayerListener playerListener = new ChestwatcherPlayerListener(this);
  40. private final ChestwatcherInventoryListener inventoryListener = new ChestwatcherInventoryListener(this);
  41. public Boolean usingpermissions = false;
  42. public static HashMap<String, Block> lastBlock = new HashMap<String, Block>();
  43. public String pluginDirPath;
  44. public File configFile;
  45. public ChestWatcherConfig config;
  46.  
  47. public void onEnable() {
  48. log.info(getDescription().getName() + " version " + getDescription().getVersion() + " is enabled!");
  49. PluginManager pm = getServer().getPluginManager();
  50. pm.registerEvent(Event.Type.BLOCK_BREAK, blockListener, Event.Priority.Normal, this);
  51. pm.registerEvent(Event.Type.BLOCK_BURN, blockListener, Event.Priority.Normal, this);
  52. pm.registerEvent(Event.Type.BLOCK_FADE, blockListener, Event.Priority.Normal, this);
  53. pm.registerEvent(Event.Type.ENTITY_EXPLODE, entityListener, Event.Priority.Normal, this);
  54. pm.registerEvent(Event.Type.PLAYER_CHAT, playerListener, Event.Priority.Normal, this);
  55. pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, playerListener, Event.Priority.Normal, this);
  56. pm.registerEvent(Event.Type.PLAYER_INTERACT, playerListener, Event.Priority.Normal, this);
  57. pm.registerEvent(Event.Type.PLAYER_QUIT, playerListener, Event.Priority.Normal, this);
  58. pm.registerEvent(Event.Type.FURNACE_BURN, playerListener, Event.Priority.Normal, this);
  59. pm.registerEvent(Event.Type.FURNACE_SMELT, inventoryListener, Event.Priority.Normal, this);
  60.  
  61. this.pluginDirPath = this.getDataFolder().getAbsolutePath();
  62. this.configFile = new File(this.pluginDirPath + File.separator + "config.yml");
  63. this.config = new ChestWatcherConfig(this.configFile);
  64.  
  65. Connection con = null;
  66. Statement st = null;
  67. ResultSet rs = null;
  68.  
  69. String url = config.getString("mysql.mysqlurl");
  70. String user = config.getString("mysql.mysqluser");
  71. String password= config.getString("mysql.mysqlpassword");
  72.  
  73. try {
  74. con = DriverManager.getConnection(url, user, password);
  75. st = (Statement) con.createStatement();
  76. rs = st.executeQuery("SELECT VERSION()");
  77.  
  78. if (rs.next()) {
  79. System.out.println(rs.getString(1));
  80. }
  81.  
  82. } catch (SQLException ex) {
  83. Logger lgr = Logger.getLogger(Version.class.getName());
  84. lgr.log(Level.SEVERE, ex.getMessage(), ex);
  85.  
  86. } finally {
  87. try {
  88. if (rs != null) {
  89. rs.close();
  90. }
  91. if (st != null) {
  92. st.close();
  93. }
  94. if (con != null) {
  95. con.close();
  96. }
  97.  
  98. } catch (SQLException ex) {
  99. Logger lgr = Logger.getLogger(Version.class.getName());
  100. lgr.log(Level.WARNING, ex.getMessage(), ex);
  101. }
  102. }
  103.  
  104.  
  105. }
  106.  
  107. public void onDisable() {
  108. log.info(getDescription().getName() + " version " + getDescription().getVersion() + " is disabled!");
  109. }
  110.  
  111.  
  112.  
  113. public String getTimestamp() {
  114. Calendar calendar = Calendar.getInstance();
  115. return calendar.get(Calendar.DAY_OF_MONTH) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.HOUR_OF_DAY) + "-" + calendar.get(Calendar.MINUTE) + "-" + calendar.get(Calendar.SECOND);
  116. }
  117.  
  118. public void updateContainer(Block block, Boolean clear, String lastAccessed) {
  119. if (block.getState() instanceof ContainerBlock) {
  120. try {
  121. File dir = new File(getDataFolder().getAbsolutePath());
  122. File file = new File(getDataFolder().getAbsolutePath() + File.separator + "container.log");
  123.  
  124. if (!dir.exists()) {
  125. dir.mkdirs();
  126. }
  127.  
  128. if (!file.exists()) {
  129. file.createNewFile();
  130. }
  131.  
  132. Configuration configuration = new Configuration(file);
  133. configuration.load();
  134.  
  135. if (clear) {
  136. configuration.setProperty(block.getX() + ":" + block.getY() + ":" + block.getZ() + ":" + block.getWorld().getName(), "");
  137. } else {
  138. ItemStack[] items = ((ContainerBlock) block.getState()).getInventory().getContents();
  139.  
  140. String itemstring = "";
  141. for (ItemStack item : items) {
  142. if (item != null) {
  143. itemstring = itemstring + ":" + item.getTypeId() + "," + item.getDurability() + "," + item.getAmount();
  144. } else {
  145. itemstring = itemstring + ":" + 0 + "," + 0 + "," + 0;
  146. }
  147. }
  148.  
  149. if (lastAccessed == "") {
  150. lastAccessed = configuration.getString(block.getX() + ":" + block.getY() + ":" + block.getZ() + ":" + block.getWorld().getName(), "");
  151.  
  152. if (lastAccessed != "" && lastAccessed != null) {
  153. lastAccessed = lastAccessed.split(":")[0];
  154.  
  155. if (lastAccessed == "" || lastAccessed == null) {
  156. lastAccessed = "*";
  157. }
  158. } else {
  159. lastAccessed = "*";
  160. }
  161. }
  162.  
  163. configuration.setProperty(block.getX() + ":" + block.getY() + ":" + block.getZ() + ":" + block.getWorld().getName(), lastAccessed + ":" + block.getTypeId() + itemstring);
  164. }
  165.  
  166. configuration.save();
  167. } catch (IOException e) {
  168. e.printStackTrace();
  169. }
  170. }
  171. }
  172. }
Add Comment
Please, Sign In to add comment