Skip to content

Instantly share code, notes, and snippets.

@codermason
Last active January 1, 2016 09:59
Show Gist options
  • Save codermason/8128056 to your computer and use it in GitHub Desktop.
Save codermason/8128056 to your computer and use it in GitHub Desktop.
a simple way to store everything about a player, and then later refund them of their attributes
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
/**
* feel free to redistribute and modify :)
*
* this class is used for preparing players for minigames
* it's a whole bunch of getting and setting values to the player :L not fun
*
* @author codermason
*/
public class PlayerBank {
/*
* example use:
*
* PlayerBank pb = PlayerBank.Util.createBank(player, player.getLocation());
* pb.clearAccount(); //saves the player, then wipes them clean
*
* //later on in your code
* PlayerBank pb = PlayerBank.Util.getBank(player)
* if(pb != null)
* pb.withdraw(); //refunds the player and teleports them
*/
static class Util {
static List<PlayerBank> banks = new ArrayList<PlayerBank>();
/**
* @return list of all player banks
*/
public static List<PlayerBank> getAllBanks() {
return banks;
}
/**
* @param p = targeted player
* @return instance of the player's bank if there
*/
public static PlayerBank getBank(Player p) {
for(PlayerBank pb : banks) {
if(pb.getPlayer().getName().equals(p.getName()))
return pb;
}
return null;
}
/**
* @param p = targeted player
* @return if the player owns a bank
*/
public static boolean hasBank(Player p) {
return banks.contains(getBank(p));
}
/**
* @param p = targeted player
* @param toTeleport = where the player should be teleported (a minigame spawn)
* @return new instance of a playerbank
*/
public static PlayerBank createBank(Player p, Location toTeleport) {
if(hasBank(p))
return getBank(p);
PlayerBank pb = new PlayerBank(p, toTeleport);
banks.add(pb);
return pb;
}
/**
* @param p = targeted player
* @return void
*/
public static void removeBank(Player p) {
if(hasBank(p))
banks.remove(getBank(p));
}
}
private Player p;
private double health;
private int foodLevel, fireTicks;
private float exp, exhaustion;
private GameMode gm;
private Collection<PotionEffect> pes;
private ItemStack[] armor, inventory;
private Location toTeleport, original;
private boolean hasDeposited = false;
/**
* @param p = targeted player
* @param toTeleport = where the player should be teleported (a minigame spawn)
*/
private PlayerBank(Player p, Location toTeleport) {
this.p = p;
this.toTeleport = toTeleport;
}
public Player getPlayer() {
return this.p;
}
public void deposit() {
this.health = p.getHealth();
this.foodLevel = p.getFoodLevel();
this.exhaustion = p.getExhaustion();
this.fireTicks = p.getFireTicks();
this.exp = p.getExp();
this.gm = p.getGameMode();
this.pes = p.getActivePotionEffects();
this.armor = p.getInventory().getArmorContents();
this.inventory = p.getInventory().getContents();
this.original = p.getLocation();
p.teleport(this.toTeleport);
this.hasDeposited = true;
}
public void withdraw() {
p.setHealth(this.health);
p.setFoodLevel(this.foodLevel);
p.setExhaustion(this.exhaustion);
p.setFireTicks(this.fireTicks);
p.setExp(this.exp);
p.setGameMode(this.gm);
p.getInventory().setArmorContents(this.armor);
p.getInventory().setContents(this.inventory);
for(PotionEffect pe : this.pes)
p.addPotionEffect(pe);
p.teleport(this.original);
}
public void clearAccount() {
if(!hasDeposited)
deposit();
p.setHealth(20.00);
p.setFoodLevel(20);
p.setExhaustion(0);
p.setExp(0);
p.setFireTicks(0);
p.setGameMode(GameMode.SURVIVAL);
p.getInventory().clear();
for(PotionEffect pe : p.getActivePotionEffects())
p.removePotionEffect(pe.getType());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment