com.destroystokyo.debuggery.api.DebuggeryPlayer.java Source code

Java tutorial

Introduction

Here is the source code for com.destroystokyo.debuggery.api.DebuggeryPlayer.java

Source

/*
 * Copyright (c) 2014 -- Zach Brown (godzilla@destroystokyo.com)
 * This software may be redistributed under the terms of the GNU GPL v3 License
 * You should have received a copy of the license with the software. If not, one
 * can be obtained from here: https://www.gnu.org/licenses/gpl-3.0.html
 */

package com.destroystokyo.debuggery.api;

import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.UUID;

public class DebuggeryPlayer {

    /**
     * Get a player object from the string provided.
     * The provided string can be either a UUID or a player's name
     *
     * @param string UUID or nickname to check
     * @return a player object if it can be found or null if it can't
     * @throws IllegalArgumentException if string is null
     */
    public static Player getPlayerFromString(String string) throws IllegalArgumentException {
        Validate.notNull(string, "String cannot be null!");
        Player player;
        try {
            UUID playerUUID = UUID.fromString(string);
            player = Bukkit.getServer().getPlayer(playerUUID);
        } catch (IllegalArgumentException e) {
            player = Bukkit.getServer().getPlayer(string);
        }
        return player;
    }
}