Solved Instance not saving

Discussion in 'Spigot Plugin Development' started by NormaalBart, May 29, 2017.

  1. Hey,

    I'm a bit new to instances etcetra, especially player instances.
    I'm making a playermanager class what has to save the player's amount of money.. invites etcetra.
    but on when I'm inviting someone and he accepts, it will add it to the instance,
    but when I'm inviting him again. it will simply tell that the invite was new, so the other invite isn't saved... How can I fix this. This is the code:
    Code (Text):
        private List<SkyBlockManager> visits = new ArrayList<>();

        public List<SkyBlockManager> getVisits() {
            return visits;
        }

        public void addVisits(SkyBlockManager sbm) {
            visits.add(sbm);
            Bukkit.broadcastMessage("AV: " + visits.size());
        }
    Adding:
    Code (Text):
                                PlayerManager ppm = PlayerManager.getPlayerManager(p.getUniqueId());
                           
                                ppm.addVisits(tpm.getIsland());
    PlayerManager won't return null!
    (it is ofcourse more but it isn't necesarry for this.

    EDIT:
    Code (Text):
    package me.BartVV.SK.Manager;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;

    import me.BartVV.SK.Enums.Rank;
    import me.BartVV.SK.Utils.SkyBlock;

    public class PlayerManager{
       
        private static HashMap<UUID, PlayerManager> playermanager = new HashMap<>();

        private SkyBlockManager sbm;
        private Rank rank = Rank.NO_SKYBLOCK;
        private Player p;
        private List<SkyBlockManager> visits = new ArrayList<>();
       
        @SuppressWarnings("deprecation")
        public PlayerManager(Player p){
            if(SkyBlock.playerdata.getString("playerdata." + p.getUniqueId().toString()) != null){
                FileConfiguration pdc = SkyBlock.playerdata;
                pdc.set("playerdata." + p.getUniqueId().toString() + ".name", p.getName());
                this.sbm = SkyBlockManager.getSkyBlock(p);
                if (sbm == null){
                    rank = Rank.NO_SKYBLOCK;
                }else{
                    rank = sbm.getRank(p);
                }
                this.p = p;
                playermanager.put(p.getUniqueId(), this);
            }else{
                this.p = p;
                sbm = SkyBlockManager.getSkyBlock(p);
                if (sbm == null){
                    rank = Rank.NO_SKYBLOCK;
                }else{
                    rank = sbm.getRank(p);
                }
                playermanager.put(p.getUniqueId(), this);
            }
        }
       
        public SkyBlockManager getIsland(){
            return sbm;
        }
       
        public Rank getRank(){
            return rank;
        }
       
        public void setRank(Rank rank){
            this.rank = rank;
        }
       
        public Player getPlayer(){
            return p;
        }
       
        public static PlayerManager getPlayerManager(UUID uuid){
            PlayerManager pm = null;
            if (pm == null){
                pm = new PlayerManager(Bukkit.getPlayer(uuid));
            }          
            return pm;
        }
       
        @Deprecated
        public static PlayerManager getPlayerManager(Player p){
            PlayerManager pm = playermanager.get(p.getUniqueId());
            if (pm == null){
                pm = new PlayerManager(Bukkit.getPlayer(p.getUniqueId()));
               
            }          
            return pm;  
        }

        @Deprecated
        public static PlayerManager getPlayerManager(OfflinePlayer p){
            PlayerManager pm = playermanager.get(p.getUniqueId());
            if (pm == null){
                pm = new PlayerManager(Bukkit.getPlayer(p.getUniqueId()));
               
            }          
            return pm;  
        }
       
        @Deprecated
        public static PlayerManager getPlayerManager(String player){
            PlayerManager pm;
            if(Bukkit.getOfflinePlayer(player) != null){
                OfflinePlayer p = Bukkit.getOfflinePlayer(player);
                pm = playermanager.get(p.getUniqueId());
                if (pm == null){
                    pm = new PlayerManager(Bukkit.getPlayer(p.getUniqueId()));
                   
                }      
            }else if(Bukkit.getPlayer(player) != null){
                Player p = Bukkit.getPlayer(player);
                pm = getPlayerManager(p);
            }else{
                pm = null;
            }
            return pm;  
        }

        public void setIsland(SkyBlockManager skyBlockManager) {
            sbm = skyBlockManager;
        }
       
        public void removePlayerManager(){
            playermanager.remove(this);
        }
       
        public static void removePlayerManager(PlayerManager pm){
            playermanager.remove(pm);
        }
       
        public static void clearPlayerManager(){
            playermanager.clear();
        }

       
        public List<SkyBlockManager> getVisits() {
            return visits;
        }

        public void addVisits(SkyBlockManager sbm) {
            visits.add(sbm);
            Bukkit.broadcastMessage("AV: " + visits.size());
        }
    }
     
     
    #1 NormaalBart, May 29, 2017
    Last edited: May 29, 2017
  2. I am not 100% certain I know all of what you are asking, but I assume you need to make visits static, so it stays the same when you get a new instance of the class.
     
  3. I don't think so since when I'm making it static it won't be player dependend. And that is what I want.
     
  4. Are you keeping the same object/instance of the class for each player? If not, you can make it into a static hashmap with UUID as the key, and change your functions as necessary.
     
  5. Don't abuse static. And don't use it if you don't know what it does.
    Are you using a player variable to save it or a UUID>?
     
  6. Yes, look in the post, I copied my Playermanager class.
     
  7. Well, it turns out I defined playermanager to null every time I wanted to get the playermanager.
    If I was a bit less stupid I was able to fix it myself.
    But thanks everyone for trying to help me out!
    Marked as SOLVED