Skip to content

Instantly share code, notes, and snippets.

@Erezbiox1
Last active June 28, 2017 23:01
Show Gist options
  • Save Erezbiox1/c5f978b747afcef4180763f57d298b7d to your computer and use it in GitHub Desktop.
Save Erezbiox1/c5f978b747afcef4180763f57d298b7d to your computer and use it in GitHub Desktop.
Spigot - Saveable Gamer
import com.erezbiox1.bioarena.Main;
import com.erezbiox1.bioarena.Utils.Serializer;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.io.Serializable;
import java.util.*;
/**
* Created by Erezbiox1 on 01/05/2017.
* (C) 2016 Erez Rotem All Rights Reserved.
*/
public class Gamer implements Serializable{
private transient static java.util.Map<UUID, Gamer> gamers = new HashMap<>();
private transient Player player;
private UUID uuid;
// Fields - Trasniet for non saveable ones.
// *****
// Fields
// Loading the gamer
private Gamer(UUID uuid){
load(uuid);
}
private Gamer load(UUID uuid){
this.uuid = uuid;
gamers.put(uuid, this);
player = Bukkit.getPlayer(uuid);
return this;
}
// Methods
// ******
// Methods
public Player getPlayer(){
return player;
}
// getGamer
public static Gamer getGamer(UUID uuid){
if(gamers.containsKey(uuid))
return gamers.get(uuid);
Serializer serializer = Main.getSerializer();
String key = uuid.toString();
if(serializer.doesHave(key))
return ((Gamer) serializer.load(key)).load(uuid);
else{
Gamer gamer = new Gamer(uuid);
serializer.add(key, gamer);
return gamer;
}
}
public static Gamer getGamer(Player player){
return getGamer(player.getUniqueId());
}
public static Gamer getGamer(String username){
Player player = Bukkit.getPlayer(username);
if (player == null) return null;
return getGamer(player.getUniqueId());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment