Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Maquinas.alex;
- import java.io.File;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Set;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.block.Block;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.enchantments.Enchantment;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.block.BlockBreakEvent;
- import org.bukkit.event.block.BlockPlaceEvent;
- import org.bukkit.event.player.PlayerInteractEvent;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.meta.ItemMeta;
- import org.bukkit.inventory.meta.PotionMeta;
- import org.bukkit.plugin.PluginManager;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.potion.PotionEffect;
- import org.bukkit.potion.PotionEffectType;
- import java.sql.Connection;
- import java.sql.Statement;
- public class Main extends JavaPlugin implements Listener{
- Connection c = null;
- Statement sta = null;
- String table_prefix = null;
- Main main = this;
- public HashMap<String, List<Location>> Maquinas = new HashMap<>();
- Integer maquinasloop = null;
- public void onEnable() {
- main = this;
- PluginManager pm = getServer().getPluginManager();
- pm.registerEvents(this, this);
- Dados();
- saveDefaultConfig();
- startmaquinasloop();
- }
- public void onDisable() {
- try {
- c.close();
- } catch (Exception e) {}
- try {
- sta.close();
- } catch (Exception e) {}
- }
- private void Dados() {
- try {
- if (getConfig().getString("MySQL.Ativo").equalsIgnoreCase("Sim")) {
- 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"));
- table_prefix = getConfig().getString("MySQL.Prefix");
- } else {
- File db = new File("plugins/" + getDescription().getName() + "/Dados.db");
- if (!db.exists()) {
- db.createNewFile();
- }
- Class.forName("org.sqlite.JDBC");
- c = DriverManager.getConnection("jdbc:sqlite:plugins/" + getDescription().getName() + "/Dados.db");
- table_prefix = "Maquinas";
- }
- sta = c.createStatement();
- 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));");
- } catch (Exception e) {
- Bukkit.getPluginManager().disablePlugin(this);
- }
- }
- @Override
- public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
- if (!(sender instanceof Player)) {
- return true;
- }
- Player p = (Player) sender;
- if (cmd.getName().equalsIgnoreCase("Maquinas")) {
- for (String maquinas : getConfig().getConfigurationSection("Maquinas").getKeys(false)) {
- for (String item : getConfig().getStringList("Maquinas." + maquinas + ".item")) {
- ItemStack m = new ItemStack(ItemGet(item.split("\\\\")[0], 1, item.split("\\\\")[1]));
- p.getPlayer().getInventory().addItem(m);
- p.getPlayer().getInventory().addItem(ItemGet("264", 64, "[N]{TEST}[N]"));
- p.sendMessage("EAE");
- }
- }
- }
- if (cmd.getName().equalsIgnoreCase("test")) {
- try {
- ResultSet rs = c.createStatement().executeQuery("SELECT * FROM " + table_prefix + "_maquina");
- while (rs.next()) {
- Bukkit.getConsoleSender().sendMessage("[" + rs.getInt("combu") + "]" + "[" + rs.getInt("lvl") + "]" + "[" + rs.getString("x") + "]" + "[" + rs.getString("y") + "]" + "[" + rs.getString("z") + "]" + "[" + rs.getString("world") + "]" + "[" + rs.getString("tipo") + "]");
- if (rs.getInt("combu") > 0) {
- Bukkit.getConsoleSender().sendMessage("4");
- 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") +"';");
- Integer Drops = 0;
- for (String lvls : getConfig().getStringList("Levels")) {
- if (rs.getInt("lvl") == Integer.parseInt(lvls.split("\\\\")[0])) {
- Drops = Integer.parseInt(lvls.split("\\\\")[1]);
- break;
- }
- }
- for (String drop : getConfig().getStringList("Maquinas." + rs.getString("tipo") + ".drops")) {
- 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]));
- }
- }
- }
- }catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- return false;
- }
- @EventHandler
- public void ColocaMaquina(BlockPlaceEvent e) {
- if (e.getPlayer() instanceof Player) {
- Player p = e.getPlayer();
- for (String maquinas : getConfig().getConfigurationSection("Maquinas").getKeys(false)) {
- for (String item : getConfig().getStringList("Maquinas." + maquinas + ".item")) {
- ItemStack m = new ItemStack(ItemGet(item.split("\\\\")[0], 1, item.split("\\\\")[1]));
- if (e.getPlayer().getItemInHand().isSimilar(m)) {
- try {
- p.sendMessage("Maquina de " + maquinas + " colocada com sucesso");
- Block bloco = e.getBlockPlaced();
- 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 +"')");
- }catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- }
- }
- }
- }
- @EventHandler
- public void QuebraMaquina(BlockBreakEvent e) {
- if (e.getPlayer() instanceof Player) {
- Player p = e.getPlayer();
- Block bloco = e.getBlock();
- try {
- 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() +"';");
- if (rs.next()) {
- 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() +"';");
- p.sendMessage("Está maquina foi removida com sucesso");
- }
- }catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- }
- @EventHandler
- public void Combus(PlayerInteractEvent e) {
- Player p = e.getPlayer();
- Block bloco = p.getTargetBlock((Set<Material>)null, 5);
- if (e.getPlayer().getItemInHand().isSimilar(ItemGet("264", 1, "[N]{TEST}[N]"))) {
- try {
- 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() +"';");
- if (rs.next()) {
- Integer combu = rs.getInt("combu") + 10;
- 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() +"';");
- p.sendMessage("Você colocou combustivel com sucesso " + combu);
- }
- }catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- }
- private void startmaquinasloop() {
- maquinasloop = (Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
- public void run() {
- try {
- ResultSet rs = c.createStatement().executeQuery("SELECT * FROM " + table_prefix + "_maquina");
- while (rs.next()) {
- Integer combu = rs.getInt("combu");
- Integer lvl = rs.getInt("lvl");
- String x = rs.getString("x");
- String y = rs.getString("y");
- String z = rs.getString("z");
- String tipo = rs.getString("tipo");
- String world = rs.getString("world");
- if (combu > 0) {
- c.createStatement().executeUpdate("UPDATE " + table_prefix + "_maquina SET combu = '"+ (combu - 1) +"' WHERE x = '"+ x +"' AND y = '"+ y +"' AND z = '"+ z +"' AND world = '"+ world +"';");
- Integer Drops = 0;
- for (String lvls : getConfig().getStringList("Levels")) {
- if (lvl == Integer.parseInt(lvls.split("\\\\")[0])) {
- Drops = Integer.parseInt(lvls.split("\\\\")[1]);
- break;
- }
- }
- for (String drop : getConfig().getStringList("Maquinas." + tipo + ".drops")) {
- 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]));
- }
- }
- }
- }catch (Exception e2) {
- e2.printStackTrace();
- }
- }}, 0L, 1 * 20L));
- }
- @SuppressWarnings("deprecation")
- public ItemStack ItemGet (String id, Integer quantidade, String meta) {
- Short ID = 0;
- Short Data = 0;
- if (id.contains(":")) {
- String[] IDData = id.split(":");
- ID = Short.valueOf(IDData[0]);
- Data = Short.valueOf(IDData[1]);
- } else {
- ID = Short.valueOf(id);
- Data = 0;
- }
- ItemStack Item = new ItemStack(ID);
- ItemMeta Meta = Item.getItemMeta();
- if (meta.contains("[E]{") && meta.contains("}[E]")) {
- for (String EncaBase: Arrays.asList(meta.split("\\[E]\\{")[1].split("\\}\\[E]")[0].split(","))) {
- String[] EncaPut = EncaBase.split(":");
- Meta.addEnchant(Enchantment.getById(Integer.parseInt(EncaPut[0])), Integer.parseInt(EncaPut[1]), true);
- }
- }
- if (meta.contains("[L]{") && meta.contains("}[L]")) {
- Meta.setLore(Arrays.asList(meta.split("\\[L]\\{")[1].split("\\}\\[L]")[0].replace("&", "§").split("(\\|\\|)")));
- }
- if (meta.contains("[N]{") && meta.contains("}[N]")) {
- Meta.setDisplayName(meta.split("\\[N]\\{")[1].split("\\}\\[N]")[0].replace("&", "§"));
- }
- if (meta.contains("[P]{") && meta.contains("}[P]") && (ID == 373 || ID == 438 || ID == 441)) {
- PotionMeta PMeta = (PotionMeta) Meta;
- for (String PosinBase: Arrays.asList(meta.split("\\[P]\\{")[1].split("\\}\\[P]")[0].split(","))) {
- String[] PosinPut = PosinBase.split(":");
- PMeta.addCustomEffect(new PotionEffect(PotionEffectType.getById(Integer.parseInt(PosinPut[0])), Integer.parseInt(PosinPut[1]), Integer.parseInt(PosinPut[2])), true);
- }
- Item.setItemMeta(PMeta);
- } else {
- Item.setItemMeta(Meta);
- }
- Item.setDurability(Data);
- Item.setAmount(quantidade);
- return (Item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement