1.16.5 Config returning default value

Discussion in 'Spigot Plugin Development' started by Shadow501pll, Jul 13, 2021.

  1. So i want to make a custom health system and for storing player hp i use configs.
    The problem is:
    I want to get values and save values in the config but it instead gives me 0 (the default value i set)
    What im trying to achieve:
    Make a simmilar to hypixel skyblock health system.

    My variables class (a shortcut type of thing):

    Code (Java):
    package com.gmail.shadow501gamespl.skyblockimproved.utilityclasses;

    import com.gmail.shadow501gamespl.skyblockimproved.SkyblockImproved;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;

    public class Variables {
        public static void setvar(String varname, Player p, Object value) {
            FileConfiguration config = SkyblockImproved.getPlugin(SkyblockImproved.class).getConfig();
            config.set(p.getUniqueId().toString().toLowerCase() + "." + varname, value);
            SkyblockImproved.getPlugin(SkyblockImproved.class).saveConfig();
        }
        public static Integer getInt(String varname, Player p) {
            SkyblockImproved.getPlugin(SkyblockImproved.class).reloadConfig();
            FileConfiguration config = SkyblockImproved.getPlugin(SkyblockImproved.class).getConfig();
            return config.getInt(p.getUniqueId().toString().toLowerCase() + "." + varname, 0);
        }
    }
     
    My playerhealthclass:

    Code (Java):
    package com.gmail.shadow501gamespl.skyblockimproved;

    import com.gmail.shadow501gamespl.skyblockimproved.utilityclasses.Utility;
    import com.gmail.shadow501gamespl.skyblockimproved.utilityclasses.Variables;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.Plugin;

    import java.util.Map;

    @SuppressWarnings("deprecation")
    public class PlayerHealth implements Listener {

        @EventHandler
        public void Playerjoin(PlayerJoinEvent ev) {
            Plugin plugin = SkyblockImproved.getPlugin(SkyblockImproved.class);
            FileConfiguration config = plugin.getConfig();
            Player player = ev.getPlayer();
            if (Variables.getInt("health", player) == 0) {
                Variables.setvar("health", player, 100);
            }
            if (Variables.getInt("maxhealth", player) == 0) {
                Variables.setvar("maxhealth", player, 100);
            }
        }
        public static void updatehealth(Player player, Integer health, Integer maxhealth) {
            if (Variables.getInt("health", player) < Variables.getInt("maxhealth", player)) {
                Variables.setvar("health", player, Variables.getInt("maxhealth", player) / 200);
                if (Variables.getInt("health", player) > Variables.getInt("maxhealth", player)) {
                    Variables.setvar("health", player, Variables.getInt("maxhealth", player));
                }
            }
            if (health < 1) {
                player.sendMessage(Utility.format("&cYou died!"));
            }
            player.sendMessage("health: " + Variables.getInt("health", player));
            player.sendMessage("varhealth: " + health);
            Variables.setvar("health", player, Variables.getInt("maxhealth", player));

            double finalhp = health / maxhealth;
            finalhp = finalhp * 20;
            player.setHealth(finalhp);
        }
    };
     
    ^ was an attempt at making it work but it didnt
    And my first prototype (the one that should work):

    Code (Java):
    package com.gmail.shadow501gamespl.skyblockimproved;

    import com.gmail.shadow501gamespl.skyblockimproved.utilityclasses.Variables;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.Plugin;

    import java.util.Map;

    @SuppressWarnings("deprecation")
    public class PlayerHealth implements Listener {

        @EventHandler
        public void Playerjoin(PlayerJoinEvent ev) {
            Plugin plugin = SkyblockImproved.getPlugin(SkyblockImproved.class);
            FileConfiguration config = plugin.getConfig();
            Player player = ev.getPlayer();
            if (Variables.getInt("health", player) == 0) {
                Variables.setvar("health", player, 100);
            }
            if (Variables.getInt("maxhealth", player) == 0) {
                Variables.setvar("maxhealth", player, 100);
            }
        }
        public static void updatehealth(Player player) {
            if (Variables.getInt("health", player) < Variables.getInt("maxhealth", player)) {
                Variables.setvar("health", player, Variables.getInt("maxhealth", player) / 200);
                if (Variables.getInt("health", player) > Variables.getInt("maxhealth", player)) {
                    Variables.setvar("health", player, Variables.getInt("maxhealth", player));
                }
            }
            if (Variables.getInt("health", player) < 1) {
                player.sendMessage("health: " + Variables.getInt("health", player));
                Variables.setvar("health", player, Variables.getInt("maxhealth", player));
                player.sendMessage("&cYou died!");
            }

            double finalhp = Variables.getInt("health", player) / Variables.getInt("maxhealth", player);
            finalhp = finalhp * 20;
            player.setHealth(finalhp);
        }
    };
     
     
  2. Where is problem? Why use static?
     
  3. It would be good to read this (static abuse), it may not be of your interest but for a better code...

    Why create a constructor to get the config?
    Code (Java):
    plugin.getConfig //IS ENOUGH
    And I honestly did not understand the problem very well. What does the file look like where the lives of the players are stored?
     
  4. So the problem is:
    Im trying to get the health value from player but instead of getting that inside of a static method i only get 0 (the default value) which doesnt happen in the loop activating it which is weird

    How the config file looks:

    Code (Text):
    b217f482-6469-4ac9-8eaa-f3c4c623fe77:
      health: 100
      maxhealth: 100
     
  5. Update i think i know what breaks it!

    So the problem is when i try to heal the player (natural regen) i divide a double by 200 but it for some reason breaks my code and sets it to just 0

    Here is the healing code:

    Code (Java):

                        if (health < maxhealth) {
                            Variables.setvar("health", player, maxhealth / 200.0);
                            health = Variables.getdouble("health", player);
                            if (health > maxhealth) {
                               Variables.setvar("health", player, Variables.getdouble("maxhealth", player));
                               health = Variables.getdouble("health", player);
                            }
                        }
     
  6. Omg sorry everyone i didnt realize i was setting the player health to the maxhealth number / 200 instead of adding it to the health