/*
 *                 Copyright (c) 2016-2017
 *         SparklingComet @ http://shanerx.org
 *      KillerOfPie @ http://killerofpie.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *              http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * NOTICE: All modifications made by others to the source code belong
 * to the respective contributor. No contributor should be held liable for
 * any damages of any kind, whether be material or moral, which were
 * caused by their contribution(s) to the project. See the full License for more information.
 */

package org.shanerx.tradeshop.objects;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.shanerx.tradeshop.enumys.ShopRole;

import java.io.Serializable;
import java.util.UUID;

@SuppressWarnings("unused")
public class ShopUser implements Serializable {

	private transient OfflinePlayer player;
	@SerializedName("player")
	private String playerUUID;
	private ShopRole role;

	public ShopUser(OfflinePlayer player, ShopRole role) {
		this.player = player;
		playerUUID = player.getUniqueId().toString();
		this.role = role;
	}

	public OfflinePlayer getPlayer() {
		fix();
		return player;
	}

	public UUID getUUID() {
		fix();
		return player.getUniqueId();
	}

	public ShopRole getRole() {
		fix();
		return role;
	}

	public String getName() {
		fix();
		return getPlayer().getName();
	}

	private void fix() {
		if (player == null && playerUUID != null && playerUUID.equalsIgnoreCase("")) {
			player = Bukkit.getOfflinePlayer(UUID.fromString(playerUUID));
		}
	}

	public String serialize() {
		return new Gson().toJson(this);
	}

	public static ShopUser deserialize(String serialized) {
		ShopUser shopUser = new Gson().fromJson(serialized, ShopUser.class);
		shopUser.player = Bukkit.getOfflinePlayer(UUID.fromString(shopUser.playerUUID));
		return shopUser;
	}
}
@@ -1,25 +1,13 @@
 /*%0A *
-            
      Cop
@@ -26,27 +26,16 @@
 016-2017
-%0A *        
  Sparkli
@@ -67,59 +67,8 @@
 org%0A
- *      KillerOfPie @ http://killerofpie.github.io%0A
  *%0A 
@@ -939,17 +939,16 @@
 ormation
-.
 %0A */%0A%0Apa
@@ -1860,16 +1860,16 @@
 fix() %7B%0A
-
 %09%09if (pl
@@ -1898,32 +1898,33 @@
 UUID != null && 
+!
 playerUUID.equal

Fix for NPE on SU:56 -> CR:262