Skip to content

Instantly share code, notes, and snippets.

@Cryptite
Last active December 20, 2015 22:38
Show Gist options
  • Save Cryptite/6206128 to your computer and use it in GitHub Desktop.
Save Cryptite/6206128 to your computer and use it in GitHub Desktop.
ArenaPlayer class for Loka's custom Elo Rated Arena system
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import com.n9works.bukkit.ConfigFile;
import com.n9works.bukkit.SerializeInventory;
import com.n9works.bukkit.TheArtifact;
public class ArenaPlayer {
Logger log = Logger.getLogger("Artifact-ArenaPlayer");
protected TheArtifact plugin;
private Inventory inventory;
public String serializedInventory;
public String serializedArmorInventory;
public ItemStack[] invStack;
public ItemStack[] armorStack;
public String partner;
public ArenaParty party;
private int level = 0;
private int hunger = 5;
private float xp = 0;
public Location location;
public World world;
public Integer arena;
public String opponent;
public Integer ratingChange = 0;
public double damage = 0;
public String name;
public long timeInQueue;
public long timeOut;
public int queueType = 1;
public Boolean ready = false;
public Boolean isAlive = true;
public Boolean spectator = false;
public Color playerColor;
public ChatColor playerChatColor;
private String historyFile;
public ConfigFile config;
public Map<String, Integer> recentOpponents = new HashMap<String, Integer>();
public List<String> prematchedOpponents = new ArrayList<String>();
//Stats
public int twopr = 1500;
public int highesttwopr = 1500;
public int arenarating = 1500;
public int arenarating2v2 = 1500;
public int highestrating = 1500;
public int highest2v2rating = 1500;
public int arenawins = 0;
public int arenawins2v2 = 0;
public int arenalosses = 0;
public int arenalosses2v2 = 0;
public int streak = 0;
public int streak2v2 = 0;
public long deserterTime = 0;
public ArenaPlayer(TheArtifact plugin, String name) {
this.name = name;
historyFile = (plugin.getDataFolder().getPath() + "/arenaplayers/" + name + ".history");
this.config = new ConfigFile(plugin, "arenaplayers/" + name + ".yml");
this.plugin = plugin;
}
public synchronized boolean save(ArenaMatch match) {
config.set("name", name);
config.set("arenarating", arenarating);
config.set("twopr", twopr);
config.set("highesttwopr", highesttwopr);
config.set("arenawins", arenawins);
config.set("arenalosses", arenalosses);
config.set("valleyKills", valleyKills);
config.set("arenawins2v2", arenawins2v2);
config.set("arenalosses2v2", arenalosses2v2);
config.set("valleyDeaths", valleyDeaths);
config.set("valleyCaps", valleyCaps);
config.set("valleyWins", valleyWins);
config.set("valleyLosses", valleyLosses);
config.set("streak", streak);
config.set("streak2v2", streak2v2);
config.set("deserterTime", deserterTime);
config.set("highestrating", highestrating);
config.set("highest2v2rating", highest2v2rating);
config.set("level", level);
config.set("hunger", hunger);
config.set("xp", xp);
config.set("info_firstTimeArena", info_firstTimeArena);
config.set("info_firstTimeBG", info_firstTimeBG);
if ((location != null) && (!plugin.getServer().getPlayerExact(name).isDead())) {
config.set("location", coordsToString(location));
}
if (serializedInventory != null) {
config.set("inventory", serializedInventory);
}
if (serializedArmorInventory != null) {
config.set("armor", serializedArmorInventory);
}
config.save();
return false;
}
public void load() {
arenarating = Integer.parseInt(config.get("arenarating", arenarating));
arenarating2v2 = Integer.parseInt(config.get("arenarating2v2", arenarating2v2));
twopr = Integer.parseInt(config.get("twopr", twopr));
highesttwopr = Integer.parseInt(config.get("highesttwopr", highesttwopr));
arenawins = Integer.parseInt(config.get("arenawins", arenawins));
arenawins2v2 = Integer.parseInt(config.get("arenawins2v2", arenawins2v2));
arenalosses = Integer.parseInt(config.get("arenalosses", arenalosses));
arenalosses2v2 = Integer.parseInt(config.get("arenalosses2v2", arenalosses2v2));
streak = Integer.parseInt(config.get("streak", streak));
streak2v2 = Integer.parseInt(config.get("streak2v2", streak2v2));
highestrating = Integer.parseInt(config.get("highestrating", highestrating));
highest2v2rating = Integer.parseInt(config.get("highestrating", highestrating));
valleyKills = Integer.parseInt(config.get("valleyKills", valleyKills));
valleyDeaths = Integer.parseInt(config.get("valleyDeaths", valleyDeaths));
valleyCaps = Integer.parseInt(config.get("valleyCaps", valleyCaps));
valleyWins = Integer.parseInt(config.get("valleyWins", valleyWins));
valleyLosses = Integer.parseInt(config.get("valleyLosses", valleyLosses));
deserterTime = Long.parseLong(config.get("deserterTime", deserterTime));
info_firstTimeArena = Boolean.parseBoolean(config.get("info_firstTimeArena", info_firstTimeArena));
info_firstTimeBG = Boolean.parseBoolean(config.get("info_firstTimeBG", info_firstTimeBG));
level = Integer.parseInt(config.get("level", level));
hunger = Integer.parseInt(config.get("hunger", hunger));
xp = Float.parseFloat(config.get("xp", xp));
serializedInventory = config.get("inventory", serializedInventory);
serializedArmorInventory = config.get("armor", serializedArmorInventory);
location = parseCoord(config.get("location", location));
}
public void sendMessage(String message) {
Player p = plugin.getServer().getPlayerExact(name);
if (p != null && p.isOnline()) {
p.sendMessage(message);
}
}
public Arena getArena() {
if (arena != null) {
return plugin.arenas.get(arena);
}
return null;
}
private Location parseCoord(String string) {
if (string != null) {
String[] elems = string.split(",");
World world = plugin.getServer().getWorld(elems[0]);
return new Location(world, Double.parseDouble(elems[1]),
Double.parseDouble(elems[2]), Double.parseDouble(elems[3]),
elems.length > 4 ? Float.parseFloat(elems[4]) : 0f,
elems.length > 5 ? Float.parseFloat(elems[5]) : 0f);
} else {
return null;
}
}
private String coordsToString(Location point) {
return point.getWorld().getName() + "," +
(int) point.getX() + "," +
(int) point.getY() + "," +
(int) point.getZ() + "," +
(int) point.getYaw() + "," +
(int) point.getPitch();
}
public void savePlayer() {
Player player = plugin.getServer().getPlayerExact(name);
inventory = player.getInventory();
invStack = inventory.getContents();
location = player.getLocation();
armorStack = player.getInventory().getArmorContents();
level = player.getLevel();
hunger = player.getFoodLevel();
xp = player.getExp();
Inventory inv = SerializeInventory.getContentInventory(player.getInventory());
Inventory armor = SerializeInventory.getArmorInventory(player.getInventory());
serializedInventory = SerializeInventory.toBase64(inv);
serializedArmorInventory = SerializeInventory.toBase64(armor);
save(null);
}
public void restorePlayer(Boolean tp) {
log.info("[ARENA] Restoring " + name);
Player p = plugin.getServer().getPlayer(name);
bg = null;
arena = null;
spectator = false;
ready = false;
isAlive = true;
damage = 0;
//load();
if ((tp) && (location != null)) {
p.teleport(location);
p.setFallDistance(0);
plugin.effectSmoke(location);
}
if (spectator) {
plugin.giveFlight(p, false);
p.setFallDistance(0);
}
if (invStack != null) {
p.getInventory().setContents(invStack);
} else {
log.info("Restoring " + name + " inventory from file.");
Inventory inv = SerializeInventory.fromBase64(serializedInventory);
invStack = inv.getContents();
p.getInventory().setContents(invStack);
}
if (armorStack != null) {
p.getInventory().setArmorContents(armorStack);
} else {
log.info("Restoring " + name + " armor from file.");
Inventory armor = SerializeInventory.fromBase64(serializedArmorInventory);
armorStack = armor.getContents();
p.getInventory().setArmorContents(armorStack);
}
//Reset health to 20.
p.setHealth(20);
p.setLevel(level);
p.setFoodLevel(hunger);
p.setExp(xp);
if (party == null) {
p.setScoreboard(plugin.board);
} else {
p.setScoreboard(party.board);
}
plugin.arenaLosers.remove(name);
plugin.saveArenaLosers();
plugin.arenaPlayers.remove(name);
prematchedOpponents.clear();
}
public void checkNewColorAchievement(int newRating) {
Boolean achieved = true;
String colorStr = "white";
ChatColor newColor = ChatColor.WHITE;
if ((highestrating < 1550) && (newRating >= 1550)) {
newColor = ChatColor.YELLOW;
colorStr = "yellow";
} else if ((highestrating >= 1550) && (highestrating < 1600) && (newRating >= 1600)) {
newColor = ChatColor.GOLD;
colorStr = "gold";
} else if ((highestrating >= 1600) && (highestrating < 1650) && (newRating >= 1650)) {
newColor = ChatColor.GREEN;
colorStr = "light green";
} else if ((highestrating >= 1650) && (highestrating < 1700) && (newRating >= 1700)) {
newColor = ChatColor.DARK_GREEN;
colorStr = "green";
} else if ((highestrating >= 1700) && (highestrating < 1750) && (newRating >= 1750)) {
newColor = ChatColor.AQUA;
colorStr = "light blue";
} else if ((highestrating >= 1750) && (highestrating < 1800) && (newRating >= 1800)) {
newColor = ChatColor.BLUE;
colorStr = "blue";
} else if ((highestrating >= 1800) && (highestrating < 1850) && (newRating >= 1850)) {
newColor = ChatColor.LIGHT_PURPLE;
colorStr = "fuschia";
} else if ((highestrating >= 1850) && (highestrating < 1900) && (newRating >= 1900)) {
newColor = ChatColor.RED;
colorStr = "red";
} else if ((highestrating >= 1900) && (highestrating < 1950) && (newRating >= 1950)) {
newColor = ChatColor.GRAY;
colorStr = "silver";
} else if ((highestrating >= 1950) && (highestrating < 2000) && (newRating >= 2000)) {
newColor = ChatColor.DARK_GRAY;
colorStr = "gray";
} else if ((highestrating >= 2000) && (highestrating < 2050) && (newRating >= 2050)) {
newColor = ChatColor.BLACK;
colorStr = "black";
} else if (highestrating >= 2050) {
newColor = ChatColor.BLACK;
colorStr = "black";
} else {
achieved = false;
}
}
public Color getRatingColor() {
Arena arena = getArena();
if (arena != null) {
int ratingToCompare = highestrating;
if (arena.matchType == 2) {
ratingToCompare = highesttwopr;
}
if (ratingToCompare < 1550) {
playerColor = Color.WHITE;
playerChatColor = ChatColor.WHITE;
} else if ((ratingToCompare >= 1550) && (ratingToCompare < 1600)) {
playerColor = Color.YELLOW;
playerChatColor = ChatColor.YELLOW;
} else if ((ratingToCompare >= 1600) && (ratingToCompare < 1650)) {
playerColor = Color.ORANGE;
playerChatColor = ChatColor.GOLD;
} else if ((ratingToCompare >= 1650) && (ratingToCompare < 1700)) {
playerColor = Color.LIME;
playerChatColor = ChatColor.GREEN;
} else if ((ratingToCompare >= 1700) && (ratingToCompare < 1750)) {
playerColor = Color.GREEN;
playerChatColor = ChatColor.DARK_GREEN;
} else if ((ratingToCompare >= 1750) && (ratingToCompare < 1800)) {
playerColor = Color.AQUA;
playerChatColor = ChatColor.AQUA;
} else if ((ratingToCompare >= 1800) && (ratingToCompare < 1850)) {
playerColor = Color.BLUE;
playerChatColor = ChatColor.BLUE;
} else if ((ratingToCompare >= 1850) && (ratingToCompare < 1900)) {
playerColor = Color.PURPLE;
playerChatColor = ChatColor.LIGHT_PURPLE;
} else if ((ratingToCompare >= 1900) && (ratingToCompare < 1950)) {
playerColor = Color.RED;
playerChatColor = ChatColor.RED;
} else if ((ratingToCompare >= 1950) && (ratingToCompare < 2000)) {
playerColor = Color.SILVER;
playerChatColor = ChatColor.GRAY;
} else if ((ratingToCompare >= 2000) && (ratingToCompare < 2050)) {
playerColor = Color.GRAY;
playerChatColor = ChatColor.DARK_GRAY;
} else if (ratingToCompare >= 2050) {
playerColor = Color.BLACK;
playerChatColor = ChatColor.BLACK;
}
}
return playerColor;
}
public void setArmorMetas(ItemStack armorPiece) {
LeatherArmorMeta lam = (LeatherArmorMeta)armorPiece.getItemMeta();
getRatingColor();
lam.setColor(playerColor);
armorPiece.setItemMeta(lam);
armorPiece.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 4);
}
public void setPlayerGear(Boolean battleground, Player p) {
if (p == null) {
//Presumably this should never fire but it's a backup for paranoia reasons. Don't ask.
p = plugin.getServer().getPlayerExact(name);
}
if (p != null) {
p.getInventory().clear();
//Weapons
ItemStack sword = new ItemStack(Material.IRON_SWORD);
ItemStack bow = new ItemStack(Material.BOW);
bow.addEnchantment(Enchantment.ARROW_DAMAGE, 1);
ItemStack arrows = new ItemStack(Material.ARROW);
arrows.setAmount(24);
//Armor
ItemStack helm = new ItemStack(Material.LEATHER_HELMET);
ItemStack boots = new ItemStack(Material.LEATHER_BOOTS);
ItemStack chest = new ItemStack(Material.LEATHER_CHESTPLATE);
ItemStack leggings = new ItemStack(Material.LEATHER_LEGGINGS);
//Set armor enchants + colors
setArmorMetas(helm);
setArmorMetas(boots);
setArmorMetas(chest);
setArmorMetas(leggings);
p.getInventory().addItem(sword);
p.getInventory().addItem(bow);
if (battleground) {
p.getInventory().addItem(new ItemStack(Material.POTION, 2, (short) 16389));
} else {
p.getInventory().addItem(new ItemStack(Material.POTION, 1, (short) 16389));
}
p.getInventory().addItem(new ItemStack(Material.POTION, 2, (short) 16428));
p.getInventory().addItem(arrows);
p.getInventory().setHelmet(helm);
p.getInventory().setBoots(boots);
p.getInventory().setChestplate(chest);
p.getInventory().setLeggings(leggings);
}
}
public void updateRecentOpponents(String player, Integer offset) {
if (recentOpponents.containsKey(player)) {
Integer timesPlayed = recentOpponents.get(player);
if (timesPlayed > 9) {
timesPlayed = 8;
}
if (offset != null) {
recentOpponents.put(player, offset);
} else {
recentOpponents.put(player, timesPlayed+2);
}
} else {
if (offset != null) {
recentOpponents.put(player, offset);
} else {
recentOpponents.put(player, 2);
}
}
prematchedOpponents.add(player);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment