Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.70 KB | None | 0 0
  1. package Maquinas.alex;
  2.  
  3. import java.io.File;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.util.Arrays;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Set;
  10. import org.bukkit.Bukkit;
  11. import org.bukkit.Location;
  12. import org.bukkit.Material;
  13. import org.bukkit.block.Block;
  14. import org.bukkit.command.Command;
  15. import org.bukkit.command.CommandSender;
  16. import org.bukkit.enchantments.Enchantment;
  17. import org.bukkit.entity.Player;
  18. import org.bukkit.event.EventHandler;
  19. import org.bukkit.event.Listener;
  20. import org.bukkit.event.block.BlockBreakEvent;
  21. import org.bukkit.event.block.BlockPlaceEvent;
  22. import org.bukkit.event.player.PlayerInteractEvent;
  23. import org.bukkit.inventory.ItemStack;
  24. import org.bukkit.inventory.meta.ItemMeta;
  25. import org.bukkit.inventory.meta.PotionMeta;
  26. import org.bukkit.plugin.PluginManager;
  27. import org.bukkit.plugin.java.JavaPlugin;
  28. import org.bukkit.potion.PotionEffect;
  29. import org.bukkit.potion.PotionEffectType;
  30.  
  31. import java.sql.Connection;
  32. import java.sql.Statement;
  33.  
  34. public class Main extends JavaPlugin implements Listener{
  35.  
  36. Connection c = null;
  37. Statement sta = null;
  38. String table_prefix = null;
  39. Main main = this;
  40. public HashMap<String, List<Location>> Maquinas = new HashMap<>();
  41. Integer maquinasloop = null;
  42.  
  43. public void onEnable() {
  44. main = this;
  45. PluginManager pm = getServer().getPluginManager();
  46. pm.registerEvents(this, this);
  47. Dados();
  48. saveDefaultConfig();
  49. startmaquinasloop();
  50. }
  51. public void onDisable() {
  52. try {
  53. c.close();
  54. } catch (Exception e) {}
  55. try {
  56. sta.close();
  57. } catch (Exception e) {}
  58. }
  59.  
  60. private void Dados() {
  61. try {
  62. if (getConfig().getString("MySQL.Ativo").equalsIgnoreCase("Sim")) {
  63. c = DriverManager.getConnection("jdbc:mysql://" + getConfig().getString("MySQL.Hostname") + ":" + getConfig().getString("MySQL.Port") + "/" + getConfig().getString("MySQL.Database"), getConfig().getString("MySQL.Username"), getConfig().getString("MySQL.Password"));
  64. table_prefix = getConfig().getString("MySQL.Prefix");
  65. } else {
  66. File db = new File("plugins/" + getDescription().getName() + "/Dados.db");
  67. if (!db.exists()) {
  68. db.createNewFile();
  69. }
  70. Class.forName("org.sqlite.JDBC");
  71. c = DriverManager.getConnection("jdbc:sqlite:plugins/" + getDescription().getName() + "/Dados.db");
  72. table_prefix = "Maquinas";
  73.  
  74. }
  75. sta = c.createStatement();
  76. sta.executeUpdate("CREATE TABLE IF NOT EXISTS " + table_prefix + "_maquina (player VARCHAR(64), world VARCHAR(64), x VARCHAR(64), y VARCHAR(64), z VARCHAR(64), combu VARCHAR(64), lvl VARCHAR(64), tipo VARCHAR(64));");
  77. } catch (Exception e) {
  78. Bukkit.getPluginManager().disablePlugin(this);
  79. }
  80.  
  81. }
  82. @Override
  83. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  84. if (!(sender instanceof Player)) {
  85. return true;
  86. }
  87. Player p = (Player) sender;
  88. if (cmd.getName().equalsIgnoreCase("Maquinas")) {
  89. for (String maquinas : getConfig().getConfigurationSection("Maquinas").getKeys(false)) {
  90. for (String item : getConfig().getStringList("Maquinas." + maquinas + ".item")) {
  91. ItemStack m = new ItemStack(ItemGet(item.split("\\\\")[0], 1, item.split("\\\\")[1]));
  92. p.getPlayer().getInventory().addItem(m);
  93. p.getPlayer().getInventory().addItem(ItemGet("264", 64, "[N]{TEST}[N]"));
  94. p.sendMessage("EAE");
  95. }
  96. }
  97. }
  98. if (cmd.getName().equalsIgnoreCase("test")) {
  99. try {
  100. ResultSet rs = c.createStatement().executeQuery("SELECT * FROM " + table_prefix + "_maquina");
  101. while (rs.next()) {
  102. Bukkit.getConsoleSender().sendMessage("[" + rs.getInt("combu") + "]" + "[" + rs.getInt("lvl") + "]" + "[" + rs.getString("x") + "]" + "[" + rs.getString("y") + "]" + "[" + rs.getString("z") + "]" + "[" + rs.getString("world") + "]" + "[" + rs.getString("tipo") + "]");
  103. if (rs.getInt("combu") > 0) {
  104. Bukkit.getConsoleSender().sendMessage("4");
  105. sta.executeUpdate("UPDATE " + table_prefix + "_maquina SET combu = '"+ (rs.getInt("combu") - 1) +"' WHERE x = '"+ rs.getString("x") +"' AND y = '"+ rs.getString("y") +"' AND z = '"+ rs.getString("z") +"' AND world = '"+ rs.getString("world") +"';");
  106. Integer Drops = 0;
  107. for (String lvls : getConfig().getStringList("Levels")) {
  108. if (rs.getInt("lvl") == Integer.parseInt(lvls.split("\\\\")[0])) {
  109. Drops = Integer.parseInt(lvls.split("\\\\")[1]);
  110. break;
  111. }
  112. }
  113. for (String drop : getConfig().getStringList("Maquinas." + rs.getString("tipo") + ".drops")) {
  114. Bukkit.getServer().getWorld(rs.getString("world")).dropItemNaturally(new Location(Bukkit.getServer().getWorld(rs.getString("world")), Double.parseDouble(rs.getString("x")), Double.parseDouble(rs.getString("y")), Double.parseDouble(rs.getString("z"))), ItemGet(drop.split("\\\\")[0], Drops, drop.split("\\\\")[1]));
  115. }
  116. }
  117. }
  118. }catch (Exception e2) {
  119. e2.printStackTrace();
  120. }
  121. }
  122. return false;
  123. }
  124. @EventHandler
  125. public void ColocaMaquina(BlockPlaceEvent e) {
  126. if (e.getPlayer() instanceof Player) {
  127. Player p = e.getPlayer();
  128. for (String maquinas : getConfig().getConfigurationSection("Maquinas").getKeys(false)) {
  129. for (String item : getConfig().getStringList("Maquinas." + maquinas + ".item")) {
  130. ItemStack m = new ItemStack(ItemGet(item.split("\\\\")[0], 1, item.split("\\\\")[1]));
  131. if (e.getPlayer().getItemInHand().isSimilar(m)) {
  132. try {
  133. p.sendMessage("Maquina de " + maquinas + " colocada com sucesso");
  134. Block bloco = e.getBlockPlaced();
  135. sta.executeUpdate("INSERT INTO " + table_prefix + "_maquina (player, world, x, y, z, combu, lvl, tipo) VALUES ('"+ e.getPlayer().getName() +"', '"+ bloco.getWorld().getName() +"', '"+ bloco.getLocation().getBlockX() +"', '"+ bloco.getLocation().getBlockY() +"', '"+ bloco.getLocation().getBlockZ() +"', '0', '1', '"+ maquinas +"')");
  136. }catch (Exception e2) {
  137. e2.printStackTrace();
  138. }
  139. }
  140. }
  141. }
  142. }
  143. }
  144. @EventHandler
  145. public void QuebraMaquina(BlockBreakEvent e) {
  146. if (e.getPlayer() instanceof Player) {
  147. Player p = e.getPlayer();
  148. Block bloco = e.getBlock();
  149. try {
  150. ResultSet rs = sta.executeQuery("SELECT * FROM " + table_prefix + "_maquina WHERE x = '"+ bloco.getLocation().getBlockX() +"' AND y = '"+ bloco.getLocation().getBlockY() +"' AND z = '"+ bloco.getLocation().getBlockZ() +"' AND world = '"+ bloco.getLocation().getWorld().getName() +"';");
  151. if (rs.next()) {
  152. sta.executeUpdate("DELETE FROM " + table_prefix + "_maquina WHERE x = '"+ bloco.getLocation().getBlockX() +"' AND y = '"+ bloco.getLocation().getBlockY() +"' AND z = '"+ bloco.getLocation().getBlockZ() +"' AND world = '"+ bloco.getLocation().getWorld().getName() +"';");
  153. p.sendMessage("Está maquina foi removida com sucesso");
  154. }
  155. }catch (Exception e2) {
  156. e2.printStackTrace();
  157. }
  158. }
  159. }
  160. @EventHandler
  161. public void Combus(PlayerInteractEvent e) {
  162. Player p = e.getPlayer();
  163. Block bloco = p.getTargetBlock((Set<Material>)null, 5);
  164. if (e.getPlayer().getItemInHand().isSimilar(ItemGet("264", 1, "[N]{TEST}[N]"))) {
  165. try {
  166. ResultSet rs = sta.executeQuery("SELECT * FROM " + table_prefix + "_maquina WHERE x = '"+ bloco.getLocation().getBlockX() +"' AND y = '"+ bloco.getLocation().getBlockY() +"' AND z = '"+ bloco.getLocation().getBlockZ() +"' AND world = '"+ bloco.getLocation().getWorld().getName() +"';");
  167. if (rs.next()) {
  168. Integer combu = rs.getInt("combu") + 10;
  169. sta.executeUpdate("UPDATE " + table_prefix + "_maquina SET combu = '"+ combu +"' WHERE x = '"+ bloco.getLocation().getBlockX() +"' AND y = '"+ bloco.getLocation().getBlockY() +"' AND z = '"+ bloco.getLocation().getBlockZ() +"' AND world = '"+ bloco.getLocation().getWorld().getName() +"';");
  170. p.sendMessage("Você colocou combustivel com sucesso " + combu);
  171. }
  172. }catch (Exception e2) {
  173. e2.printStackTrace();
  174. }
  175. }
  176. }
  177. private void startmaquinasloop() {
  178. maquinasloop = (Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
  179. public void run() {
  180. try {
  181. ResultSet rs = c.createStatement().executeQuery("SELECT * FROM " + table_prefix + "_maquina");
  182. while (rs.next()) {
  183. Integer combu = rs.getInt("combu");
  184. Integer lvl = rs.getInt("lvl");
  185. String x = rs.getString("x");
  186. String y = rs.getString("y");
  187. String z = rs.getString("z");
  188. String tipo = rs.getString("tipo");
  189. String world = rs.getString("world");
  190. if (combu > 0) {
  191. c.createStatement().executeUpdate("UPDATE " + table_prefix + "_maquina SET combu = '"+ (combu - 1) +"' WHERE x = '"+ x +"' AND y = '"+ y +"' AND z = '"+ z +"' AND world = '"+ world +"';");
  192. Integer Drops = 0;
  193. for (String lvls : getConfig().getStringList("Levels")) {
  194. if (lvl == Integer.parseInt(lvls.split("\\\\")[0])) {
  195. Drops = Integer.parseInt(lvls.split("\\\\")[1]);
  196. break;
  197. }
  198. }
  199. for (String drop : getConfig().getStringList("Maquinas." + tipo + ".drops")) {
  200. Bukkit.getServer().getWorld(world).dropItemNaturally(new Location(Bukkit.getServer().getWorld(world), Double.parseDouble(x), Double.parseDouble(y), Double.parseDouble(z)), ItemGet(drop.split("\\\\")[0], Drops, drop.split("\\\\")[1]));
  201. }
  202. }
  203. }
  204. }catch (Exception e2) {
  205. e2.printStackTrace();
  206. }
  207. }}, 0L, 1 * 20L));
  208. }
  209. @SuppressWarnings("deprecation")
  210. public ItemStack ItemGet (String id, Integer quantidade, String meta) {
  211. Short ID = 0;
  212. Short Data = 0;
  213. if (id.contains(":")) {
  214. String[] IDData = id.split(":");
  215. ID = Short.valueOf(IDData[0]);
  216. Data = Short.valueOf(IDData[1]);
  217. } else {
  218. ID = Short.valueOf(id);
  219. Data = 0;
  220. }
  221. ItemStack Item = new ItemStack(ID);
  222. ItemMeta Meta = Item.getItemMeta();
  223. if (meta.contains("[E]{") && meta.contains("}[E]")) {
  224. for (String EncaBase: Arrays.asList(meta.split("\\[E]\\{")[1].split("\\}\\[E]")[0].split(","))) {
  225. String[] EncaPut = EncaBase.split(":");
  226. Meta.addEnchant(Enchantment.getById(Integer.parseInt(EncaPut[0])), Integer.parseInt(EncaPut[1]), true);
  227. }
  228. }
  229. if (meta.contains("[L]{") && meta.contains("}[L]")) {
  230. Meta.setLore(Arrays.asList(meta.split("\\[L]\\{")[1].split("\\}\\[L]")[0].replace("&", "§").split("(\\|\\|)")));
  231. }
  232. if (meta.contains("[N]{") && meta.contains("}[N]")) {
  233. Meta.setDisplayName(meta.split("\\[N]\\{")[1].split("\\}\\[N]")[0].replace("&", "§"));
  234. }
  235. if (meta.contains("[P]{") && meta.contains("}[P]") && (ID == 373 || ID == 438 || ID == 441)) {
  236. PotionMeta PMeta = (PotionMeta) Meta;
  237. for (String PosinBase: Arrays.asList(meta.split("\\[P]\\{")[1].split("\\}\\[P]")[0].split(","))) {
  238. String[] PosinPut = PosinBase.split(":");
  239. PMeta.addCustomEffect(new PotionEffect(PotionEffectType.getById(Integer.parseInt(PosinPut[0])), Integer.parseInt(PosinPut[1]), Integer.parseInt(PosinPut[2])), true);
  240. }
  241. Item.setItemMeta(PMeta);
  242. } else {
  243. Item.setItemMeta(Meta);
  244. }
  245. Item.setDurability(Data);
  246. Item.setAmount(quantidade);
  247. return (Item);
  248. }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement