Why my command lag?

Discussion in 'Spigot Plugin Development' started by denisee, Mar 13, 2018.

  1. The database with value and itens is in mysql.
    Acessed by
    Coins.getDurability(Coins.getRank(player.getName()));
    is causing so much lag
    0.27% 287.32% 10.92 s 143.66 ms 0.0 0.1k Command: sell

    Code (Text):
    @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(label.equalsIgnoreCase("sell")) {
            if (sender instanceof Player) {
                Player player = (Player) sender;

                Inventory inventory = player.getInventory();

                ItemStack[] contents = inventory.getContents();
               
                Coins.getDurability(Coins.getRank(player.getName()));
               
               
                String i = Coins.getDurability(Coins.getRank(player.getName()));
                String blocks= Coins.getItem(Coins.getRank(player.getName()));
               
                int itemsTotal = 0;
                int valueTotal = 0;
                for (ItemStack item : contents) {
                    if (item != null && item.getType() != Material.AIR) {
                        if(i != null && blocks != null) {
                        int id = item.getTypeId();
                        short durability = item.getDurability();
                        if(Coins.itemExists(player.getName(), Coins.getRank(player.getName()),  String.valueOf(id), Short.toString(durabilidade))) {
                        if(Coins.getItemValorCerto(player.getName(), Coins.getRank(player.getName()), String.valueOf(id), Short.toString(durabilidade)) != null) {
                   
                        Integer Lapis = Coins.getItemExactValue(player.getName(), Coins.getRank(player.getName()), String.valueOf(id), Short.toString(durabilidade));
                       
                        if(Lapis != null) {
                            int Value = Lapis;
                        //if(inventory.contains(MaterialInteger.parseInt(blocos), (short) s)) {
                            itemsTotal += item.getAmount();
                            Value *= item.getAmount();
                            valueTotal += value;
                            inventory.remove(item);
                           
                        //}
                        }
                        }
                        }
                        }
                    }
                        }
                if (itemsTotal > 0) {
                    sell.economy.getEconomy().depositPlayer(player, valorTotal);
                    sender.sendMessage(sell.configManager.getConfig("config").getYaml().getString("messages.yes-sell")
                            .replace("{i}", "" + itemsTotal)
                            .replace("{$}", sell.economy.getEconomy().format(valorTotal)).replace("&", "§"));
                } else {
                    sender.sendMessage(sell.configManager.getConfig("config").getYaml().getString("messages.no-sell")

                            .replace("{i}", "" + itemsTotal)
                            .replace("{$}", sell.economy.getEconomy().format(valorTotal)).replace("&", "§"));
                }
                    }
               
            }
            return false;
     
  2. I would assume you are doing database queries on the main thread.
     
  3. Sorry I do not understand.
     
  4. mysql's methods should be placed in async task
    check in googl how to create it, and place there your acctualy code responsible for connection etc
     
    • Like Like x 1
  5. look please
    That is my mysql.class

    Code (Text):
       public void update(String qry) {
            try {
                PreparedStatement stt = con.prepareStatement(qry);
                stt.executeUpdate(qry);
                stt.close();
            } catch (SQLException e) {
                connect();
                System.err.println(e);
            }
        }
    Thats my Coins.class

    Code (Text):
        public static String getDurability(String Rank) {
            String i = null;
              try {
                ResultSet rs = sell.mysql.query("SELECT * FROM ValuesRank WHERE Rank= '" +
                        Rank + "'");
                if ((rs.next()) &&
                  (String.valueOf(rs.getString("Durability")) == null));
                i = rs.getString("Durability");
              } catch (SQLException e) {
                e.printStackTrace();
              }
            return i;
        }
    Thats my sell class
    Code (Text):
     public static MySQL mysql;
    onEnable{
    ConnectMySQL();
    }
     
     
  6. Read up on basic multithreading and database usage in spigot plugins before you attempt this
     

Share This Page