Solved offline player to player object conversion

Discussion in 'Spigot Plugin Development' started by dogboy60, Apr 22, 2017.

  1. How can I convert a offlineplayer to player?
     
  2. Player is null when he/she logs out hence the OfflinePlayer. Just use Player player = getPlayer();
    You use OfflinePlayer for when the player is offline.
     
  3. OfflinePlayer op = someplayer;
    Player p = op.getPlayer();
     
    • Funny Funny x 1
    • Useful Useful x 1
  4. No the issue is I need to update players that have debt every 24 hours, even if they are offline
     
  5. it gives me a null pointer exception when i try to get a name after doing what you did
     
  6. You can't cast real offline players to normal players. You have to work with the name or the UUID only. How would you teleport an offline player for example? :D
     
  7. Mas

    Mas

    Where are you storing the data about which players are in debt? Assuming you are using a yaml file you can just get the path to the player's data and update it however you want?
     
  8. Then you need to use offlinePlayer.
    In that case, check if the Player is not null/online then use Player player = getPlayer(); if it is null/offline use offlinePlayer.

    Like:

    Code (Java):

    if(Bukkit.getPlayer("Henk") == null){
    //Player is offline or does not exist.
    //So use offlinePlayer
    OfflinePlayer offlineP = getOfflinePlayer();
    }else{
    //Player is online
    //Use Player
    Player player = getPlayer();
    }
     
     
  9. So heres my thought process:
    get the players who have registered from yml file
    i only have their UUIDs btw
    and have a loop where if the player is offline or online and them to the interest lie
    these players are the ones over debt and the they are charged interest.
     
  10. Code (Text):
    for(UUID u : UUIDs){

                        if(Bukkit.getPlayer(u) != null){
                            CardInfo cardInfo = getCard(u);
                            long dayOfLoan = cardInfo.getloanTimestamp();
                            int chargeDate = plugin.getFileManager().getperdays();
                            int DOL = convertion(dayOfLoan);
                            int differance = (DOL+30) - now.getDayOfYear();

                            if( differance > (DOL+30) || differance < 0) {
                                SendConsoleMessage.debug("Player: "+Bukkit.getPlayer(u).getName());
                                overDuePlayers.add(Bukkit.getPlayer(u));
                                //SendConsoleMessage.info("Interest for Player: " + Bukkit.getPlayer(u).getName()+ " was added");
                            }
                        }
                        else if(Bukkit.getOfflinePlayer(u)!= null) {
                            SendConsoleMessage.debug("getOfflinePlayer IS NOT NULL");
                            CardInfo cardInfo = getCard(u);
                            long dayOfLoan = cardInfo.getloanTimestamp();
                            int chargeDate = plugin.getFileManager().getperdays();
                            int DOL = convertion(dayOfLoan);
                            int differance = (DOL+30) - now.getDayOfYear();
                            String p1 = cardInfo.getName();
                            if( differance > (DOL+30) || differance < 0) {
                                SendConsoleMessage.debug("Player: "+ Bukkit.getPlayer(p1));
                                SendConsoleMessage.debug("Player: "+ p1);

                                overDuePlayers.add((Player) Bukkit.getOfflinePlayer(u));
                                //SendConsoleMessage.info("Interest for Player: " + Bukkit.getPlayer(u).getName()+ " was added");
                            }
                        }

    UUIDs is all users in the yml that have a credit card
     
  11. I had 2 arrays, 1 for players and 1 for offlineplayers, i had a on join and on exit methods. So when they join or exit they switch arrays
     
  12. Why do you need to get the OfflinePlayer - Object? Is a UUID not enough? Seems like you only use it to fetch player's name.
     
  13. WAS

    WAS

    You can also just get the player. If it's null it doesn't exist, if object#isOnline(), they are online.

    OfflinePlayer has their UUID. You can fetch a player object with that UUID and test if they are valid and online.

    Final: You should not store a offline player or online player object. You store the UUID. It's what it is meant for. You than retrieve a object based on that UUID. Not only because it's sane, but easier on memory. When you store a player object, and that object changes in the game, your stored object will not reflect this.
     
  14. I think the thread is marked as solved already.