1.14.4 Food increases itself after adjusting

Discussion in 'Spigot Plugin Development' started by NoKi_Le_Previam, Apr 18, 2020.

Thread Status:
Not open for further replies.
  1. Hello hello ! :D

    My problem is simple to explain :D

    I do that :

    Code (Java):

    player.setSaturation(0f);
    player.setFoodLevel(0);
    The food level of the player is well set to 0. But directly it increase itself 1 by 1 to maximum.

    Why? I don't touch the food level anywhere in my plugin. I run the server with only my plugin.

    Please, help me.

    Code (Java):
    public void loadInventory(Player player, int player_id)
    {
        int group_id = groupManager.getGroupId(player.getWorld().getName());

        Connection bdd = null;
        ResultSet resultat = null;
        PreparedStatement ps = null;
        String req = null;

        try
        {
            bdd = DatabaseManager.getConnection();

            req = "SELECT * FROM " + DatabaseManager.table.INV + " WHERE group_id = ? AND player_id = ? AND type = ?";
            ps = bdd.prepareStatement(req);
            ps.setInt(1, group_id);
            ps.setInt(2, player_id);
            ps.setString(3, player.getGameMode().name());
            resultat = ps.executeQuery();

            if(resultat.next())
            {
                YamlConfiguration restoreInv = new YamlConfiguration();

                restoreInv.loadFromString(resultat.getString("inventory"));
                ArrayList invData = (ArrayList) restoreInv.get("inventory", ItemStack[].class);
                player.getInventory().setContents((ItemStack[]) invData.toArray(new ItemStack[invData.size()]));

                restoreInv.loadFromString(resultat.getString("enderchest"));
                ArrayList ecData = (ArrayList) restoreInv.get("enderchest", ItemStack[].class);
                player.getEnderChest().setContents((ItemStack[]) ecData.toArray(new ItemStack[ecData.size()]));

                player.setHealth(resultat.getDouble("health"));
                player.setSaturation((float) resultat.getDouble("food_saturation"));
                player.setFoodLevel(resultat.getInt("food"));
                setTotalExperience(player, resultat.getInt("experience"));
            }
            else
            {
                ps.close();
                resultat.close();

                YamlConfiguration invData = new YamlConfiguration();
                invData.set("inventory", player.getInventory().getContents());

                YamlConfiguration ecData = new YamlConfiguration();
                ecData.set("enderchest", player.getEnderChest().getContents());

                req = "INSERT INTO " + DatabaseManager.table.INV
                        + " ( group_id, player_id , type, inventory, enderchest, health, food, food_saturation, experience ) VALUES ( ? , ? , ? , ? , ? , ? , ? , ? , ? )";
                ps = bdd.prepareStatement(req);
                ps.setInt(1, group_id);
                ps.setInt(2, player_id);
                ps.setString(3, player.getGameMode().name());
                ps.setString(4, invData.saveToString());
                ps.setString(5, ecData.saveToString());
                ps.setDouble(6, player.getHealth());
                ps.setDouble(7, player.getFoodLevel());
                ps.setDouble(8, player.getSaturation());
                ps.setDouble(9, getTotalExperience(player));

                ps.execute();
                ps.close();
            }

            ps.close();
            resultat.close();
        }
        catch(SQLException | InvalidConfigurationException e)
        {
            Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_RED + NKbridge.PNAME + " Error");
            e.printStackTrace();
        }
    }
     
    #1 NoKi_Le_Previam, Apr 18, 2020
    Last edited: Apr 18, 2020
  2. I was the only one with this problem.

    I deleted my player data file and all is working fine ...

    I don't understand.
     
Thread Status:
Not open for further replies.