Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BT2ArenaManager {
- //save where the player teleported
- public Map<String, Location> locs = new HashMap<String, Location>();
- //make a new instance of the class
- public static BT2ArenaManager am = new BT2ArenaManager();
- //a few other fields
- Map<String, ItemStack[]> inv = new HashMap<String, ItemStack[]>();
- Map<String, ItemStack[]> armor = new HashMap<String, ItemStack[]>();
- //list of BT2Arenas
- List<BT2Arena> BT2Arenas = new ArrayList<BT2Arena>();
- int BT2ArenaSize = 0; // <--- that is how the arenas get their id's
- GameMode g = null;
- //Stuff to add to inventory for minigame
- ItemStack bow = new ItemStack(Material.BOW, 1);
- ItemStack arrow = new ItemStack(Material.ARROW, 1);
- static BowThrow2 plugin;
- public BT2ArenaManager(BowThrow2 bowThrow2) {
- plugin = bowThrow2;
- }
- public BT2ArenaManager(){
- }
- //we want to get an instance of the manager to work with it statically
- public static BT2ArenaManager getManager(){
- return am;
- }
- //get an BT2Arena object from the list
- public BT2Arena getBT2Arena(int i){
- for(BT2Arena a : BT2Arenas){
- if(a.getId() == i){
- return a;
- }
- }
- return null;
- }
- //add players to the BT2Arena, save their inventory
- public void addPlayer(Player p, int i){
- BT2Arena a = getBT2Arena(i);//get the BT2Arena you want to join
- if(a == null){//make sure it is not null
- p.sendMessage(ChatColor.RED + "Arena is null (probably doesn't exist)!");
- return;
- }
- a.getPlayers().add(p.getName());//add them to the BT2Arena list of players
- inv.put(p.getName(), p.getInventory().getContents());//save inventory
- armor.put(p.getName(), p.getInventory().getArmorContents());
- p.getInventory().setArmorContents(null);
- p.getInventory().clear();
- p.getInventory().addItem(bow);
- p.getInventory().addItem(arrow);
- g = p.getGameMode();
- p.setGameMode(GameMode.SURVIVAL);
- BT2GameListener.addPlayer(p);
- locs.put(p.getName(), p.getLocation());
- p.teleport(a.spawn);
- p.sendMessage(ChatColor.GREEN + "You joined Arena " + a.id);
- }
- //remove players
- public void removePlayer(Player p){
- BT2Arena a = null;//make an BT2Arena
- for(BT2Arena BT2Arena : BT2Arenas){
- if(BT2Arena.getPlayers().contains(p.getName())){
- a = BT2Arena;//if the BT2Arena has the player, the BT2Arena field would be the BT2Arena containing the player
- }
- //if none is found, the BT2Arena will be null
- }
- if(a == null || !a.getPlayers().contains(p.getName())){//make sure it is not null
- p.sendMessage("Invalid operation!");
- return;
- }
- a.getPlayers().remove(p.getName());//remove from BT2Arena
- p.getInventory().clear();
- p.getInventory().setArmorContents(null);
- p.getInventory().setContents(inv.get(p.getName()));//restore inventory
- p.getInventory().setArmorContents(armor.get(p.getName()));
- inv.remove(p.getName());//remove entries from hashmaps
- armor.remove(p.getName());
- p.teleport(locs.get(p.getName()));
- locs.remove(p.getName());
- p.setGameMode(g);
- p.setFireTicks(0);
- }
- //create BT2Arena
- public BT2Arena createBT2Arena(Location l){
- int num = BT2ArenaSize + 1;
- BT2ArenaSize++;
- BT2Arena a = new BT2Arena(l, num);
- BT2Arenas.add(a);
- plugin.getConfig().set("BT2Arenas." + num, serializeLoc(l));
- List<Integer> list = plugin.getConfig().getIntegerList("BT2Arenas.BT2Arenas");
- list.add(num);
- plugin.getConfig().set("BT2Arenas.BT2Arenas", list);
- plugin.saveConfig();
- return a;
- }
- public BT2Arena reloadBT2Arena(Location l) {
- int num = BT2ArenaSize + 1;
- BT2ArenaSize++;
- BT2Arena a = new BT2Arena(l, num);
- BT2Arenas.add(a);
- return a;
- }
- public void removeBT2Arena(int i) {
- BT2Arena a = getBT2Arena(i);
- if(a == null) {
- return;
- }
- BT2Arenas.remove(a);
- BT2ArenaSize--;
- if (BT2ArenaSize == -1) {
- BT2ArenaSize = 0;
- }
- plugin.getConfig().set("BT2Arenas." + i, null);
- List<Integer> list = plugin.getConfig().getIntegerList("BT2Arenas.BT2Arenas");
- list.remove(a);
- plugin.getConfig().set("BT2Arenas.BT2Arenas", list);
- plugin.saveConfig();
- }
- public boolean isInGame(Player p){
- for(BT2Arena a : BT2Arenas){
- if(a.getPlayers().contains(p.getName()))
- return true;
- }
- return false;
- }
- public void loadGames(){
- BT2ArenaSize = 0;
- if(plugin.getConfig().getIntegerList("BT2Arenas.BT2Arenas").isEmpty()){
- return;
- }
- for(int i : plugin.getConfig().getIntegerList("BT2Arenas.BT2Arenas")){
- BT2Arena a = reloadBT2Arena(deserializeLoc(plugin.getConfig().getString("BT2Arenas." + i)));
- a.id = i;
- }
- }
- public String serializeLoc(Location l){
- return l.getWorld().getName()+","+l.getBlockX()+","+l.getBlockY()+","+l.getBlockZ();
- }
- public Location deserializeLoc(String s){
- String[] st = s.split(",");
- return new Location(Bukkit.getWorld(st[0]), Integer.parseInt(st[1]), Integer.parseInt(st[2]), Integer.parseInt(st[3]));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement