1.15.2 Cast OfflinePlayer to a Player?

Discussion in 'Spigot Plugin Development' started by Foreignlos, Feb 8, 2020.

  1. Hey, I'm creating a plugin which has an Object.
    Code (Text):

    Player player;
    String groupName;
    Player target;
    OfflinePlayer offlineTarget;
     
    I have like 4 different constructors for different uses.
    My question is - can I somehow cast OfflinePlayer to a Player?
    I'm asking this because I feel like my code is very messy, for example:
    Code (Text):


    public void kickPlayer() {
        try{
            if(isOwner()) {
                if (isInGroup()) {
                    if(target == player){
                        player.sendMessage("You can't kick yourself!");
                        return;
                    }
                    PreparedStatement kickPlayer = ... //MySQL Update
                    if (offlineTarget == null) {
                       //Here I try and check if offlineTarget is null, meaning the constructor used to launch this method is not the constructor which has an input for an OfflinePlayer, which isprobably a bad approach.
                        kickPlayer.setString(1, target.getUniqueId().toString());
                        kickPlayer.executeUpdate();
                        player.sendMessage(target.getName() + " has been kicked from the group!");
                    } else {
                        kickPlayer.setString(1, offlineTarget.getUniqueId().toString());
                        kickPlayer.executeUpdate();
                        PreparedStatement getName = ... //MySQL Query
                        getName.setString(1, offlineTarget.getUniqueId().toString());
                        ResultSet result = getName.executeQuery();
                        if (result.next()) {
                            player.sendMessage(result.getString("name") + " has been kicked from the group!");
                        }

                    }

                } else {
                    if(offlineTarget == null) {
                      //Again checking if it's null...
                        player.sendMessage(...);
                    }else{
                        PreparedStatement getName = ... //QUERY
                        getName.setString(1, offlineTarget.getUniqueId().toString());
                        ResultSet result = getName.executeQuery();
                        if(result.next()) {
                            player.sendMessage(result.getString("name") + " is not in your group!");
                        }
                    }
                }
            }else{
                player.sendMessage("You do not have permissions to kick from this group!");
            }

        }catch (SQLException ex){
            ex.printStackTrace();
        }
    }
     
    I think I'm doing something wrong/Not the best approach.
     
  2. Casting:
    Code (Text):
    Player player = (Player) offlinePlayer;
    Better:
    Code (Java):

    if (offlinePlayer.isOnline()) Player player = (Player) offlinePlayer;
     
     
  3. The whole point is that it's an offline player,
    and normally casting the OfflinePlayer to a Player will just return null values.
    and as I said the whole point is that it's an offline player so I wouldn't wanna check if he's online.
    EDIT: My question wasn't clear, when asking to cast an OfflinePlayer to Player is to make the methods inside the Object be much cleaner, Normally casting an OfflinePlayer to Player will return null values.
     
  4. If the player isn‘t online, you can’t cast it to a player...
    For your code: either try to do it the other way (first checking if the player is null and then doing that other check), or split up your code in smaller tasks
     
  5. SteelPhoenix

    Moderator

    You shouldn't be referencing two player-objects and an offline player-object anyways unless you know what you're doing. It can cause memory leaks.
     
  6. I have 2 constructors for this: (player,target) being passed, or (player, offlineTarget) being passed.
    I don't see how it could cause a problem + without passing them there's no way of making this groups plugin I'm trying to make.