504 lines
18 KiB
Java
Raw Normal View History

2012-07-17 15:50:34 -06:00
package me.Travja.HungerArena;
2012-07-18 07:14:50 +10:00
2012-08-29 17:58:39 -06:00
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
2012-07-18 07:14:50 +10:00
import java.util.ArrayList;
2013-02-16 18:03:13 -07:00
import java.util.HashMap;
2012-08-25 11:58:12 -06:00
import java.util.List;
2013-02-16 18:03:13 -07:00
import java.util.Map;
import java.util.Map.Entry;
2012-08-29 17:58:39 -06:00
import java.util.logging.Level;
2012-07-18 07:14:50 +10:00
import java.util.logging.Logger;
2012-10-08 17:55:42 -06:00
2012-07-18 07:14:50 +10:00
import net.milkbowl.vault.economy.Economy;
2012-10-08 17:55:42 -06:00
2012-10-22 21:05:25 -06:00
import org.bukkit.Bukkit;
2012-07-18 07:14:50 +10:00
import org.bukkit.ChatColor;
2012-10-08 17:55:42 -06:00
import org.bukkit.Location;
import org.bukkit.World;
2012-07-18 07:14:50 +10:00
import org.bukkit.command.CommandExecutor;
import org.bukkit.configuration.file.FileConfiguration;
2012-08-29 17:58:39 -06:00
import org.bukkit.configuration.file.YamlConfiguration;
2012-10-08 17:55:42 -06:00
import org.bukkit.entity.Player;
2012-07-18 07:14:50 +10:00
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
2012-10-22 21:05:25 -06:00
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
2012-07-18 07:14:50 +10:00
public class Main extends JavaPlugin{
2013-02-16 18:03:13 -07:00
static Logger log;
public HashMap<Integer, List<String>> Playing = new HashMap<Integer, List<String>>();
public HashMap<Integer, List<String>> Ready = new HashMap<Integer, List<String>>();
public HashMap<Integer, List<String>> Dead = new HashMap<Integer, List<String>>();
public HashMap<Integer, List<String>> Quit = new HashMap<Integer, List<String>>();
public HashMap<Integer, List<String>> Out = new HashMap<Integer, List<String>>();
public HashMap<Integer, List<String>> Watching = new HashMap<Integer, List<String>>();
public HashMap<Integer, List<String>> NeedConfirm = new HashMap<Integer, List<String>>();
public HashMap<Integer, HashMap<Integer, Location>> location = new HashMap<Integer, HashMap<Integer, Location>>();
2012-10-08 17:55:42 -06:00
public ArrayList<Player> Tele = new ArrayList<Player>();
2013-02-16 18:03:13 -07:00
public HashMap<Integer, List<String>> inArena = new HashMap<Integer, List<String>>();
public HashMap<Integer, List<String>> Frozen = new HashMap<Integer, List<String>>();
public HashMap<Integer, List<String>> arena = new HashMap<Integer, List<String>>();
public HashMap<Integer, Boolean> canjoin = new HashMap<Integer, Boolean>();
public HashMap<Integer, Integer> maxPlayers = new HashMap<Integer, Integer>();
public HashMap<Integer, Boolean> open = new HashMap<Integer, Boolean>();
2012-08-25 11:58:12 -06:00
public List<String> worlds;
2012-07-18 07:14:50 +10:00
public Listener DeathListener = new DeathListener(this);
public Listener SpectatorListener = new SpectatorListener(this);
public Listener FreezeListener = new FreezeListener(this);
public Listener JoinAndQuitListener = new JoinAndQuitListener(this);
public Listener ChatListener = new ChatListener(this);
public Listener Chests = new Chests(this);
public Listener PvP = new PvP(this);
public Listener CommandBlock = new CommandBlock(this);
2012-07-17 15:50:34 -06:00
public Listener Damage = new DmgListener(this);
public Listener Teleport = new TeleportListener(this);
2012-07-18 07:14:50 +10:00
public Listener Signs = new Signs(this);
public Listener BlockStorage = new BlockStorage(this);
2012-07-17 15:50:34 -06:00
public Listener WinGames = new WinGamesListener(this);
2012-07-18 07:14:50 +10:00
public CommandExecutor HaCommands = new HaCommands(this);
public CommandExecutor SponsorCommands = new SponsorCommands(this);
public CommandExecutor SpawnsCommand = new SpawnsCommand(this);
public boolean exists;
2012-08-25 10:09:49 -06:00
public boolean restricted;
2012-07-17 17:06:57 -06:00
public FileConfiguration config;
2012-08-29 17:58:39 -06:00
public FileConfiguration spawns = null;
public File spawnsFile = null;
2012-10-08 17:55:42 -06:00
public FileConfiguration data = null;
public File dataFile = null;
public File managementFile = null;
public FileConfiguration management = null;
2012-10-22 21:05:25 -06:00
public ArrayList<ItemStack> Reward = new ArrayList<ItemStack>();
public ArrayList<ItemStack> Cost = new ArrayList<ItemStack>();
public ArrayList<ItemStack> Fee = new ArrayList<ItemStack>();
2012-07-17 15:50:34 -06:00
public boolean vault = false;
2012-10-22 21:05:25 -06:00
public boolean eco = false;
2012-07-17 15:50:34 -06:00
public Economy econ = null;
2012-10-22 21:05:25 -06:00
int i = 0;
2013-02-16 18:03:13 -07:00
int v = 0;
int start = 0;
int deathtime = 0;
int timetodeath = 0;
int a = 0;
2012-07-18 07:14:50 +10:00
public void onEnable(){
2013-02-16 18:03:13 -07:00
log = this.getLogger();
2012-07-17 17:06:57 -06:00
config = this.getConfig();
2012-07-18 07:14:50 +10:00
config.options().copyDefaults(true);
this.saveDefaultConfig();
2012-10-08 17:55:42 -06:00
spawns = this.getSpawns();
2012-08-29 17:58:39 -06:00
spawns.options().copyDefaults(true);
this.saveSpawns();
2012-10-08 17:55:42 -06:00
data = this.getData();
data.options().copyDefaults(true);
this.saveData();
management = this.getManagement();
management.options().copyDefaults(true);
this.saveManagement();
2012-07-18 07:14:50 +10:00
getServer().getPluginManager().registerEvents(DeathListener, this);
getServer().getPluginManager().registerEvents(SpectatorListener, this);
getServer().getPluginManager().registerEvents(FreezeListener, this);
getServer().getPluginManager().registerEvents(JoinAndQuitListener, this);
getServer().getPluginManager().registerEvents(ChatListener, this);
getServer().getPluginManager().registerEvents(Chests, this);
getServer().getPluginManager().registerEvents(PvP, this);
getServer().getPluginManager().registerEvents(CommandBlock, this);
getServer().getPluginManager().registerEvents(Signs, this);
getServer().getPluginManager().registerEvents(BlockStorage, this);
2012-07-17 15:50:34 -06:00
getServer().getPluginManager().registerEvents(WinGames, this);
getServer().getPluginManager().registerEvents(Damage, this);
2012-07-18 07:14:50 +10:00
getCommand("Ha").setExecutor(HaCommands);
getCommand("Sponsor").setExecutor(SponsorCommands);
getCommand("Startpoint").setExecutor(SpawnsCommand);
2013-02-16 18:03:13 -07:00
i = 1;
//TODO THIS CRAP
if(spawns.getConfigurationSection("Spawns")!= null){
Map<String, Object> temp = spawns.getConfigurationSection("Spawns").getValues(false);
for(Entry<String, Object> entry: temp.entrySet()){
if(spawns.getConfigurationSection("Spawns." + entry.getKey())!= null){
Map<String, Object> temp2 = spawns.getConfigurationSection("Spawns." + entry.getKey()).getValues(false);
for(Map.Entry<String, Object> e: temp2.entrySet()){
if(spawns.get("Spawns." + entry.getKey() + "." + e.getKey())!= null){
String[] coords = ((String) spawns.get("Spawns." + entry.getKey() + "." + e.getKey())).split(",");
Integer a = Integer.parseInt(entry.getKey());
Integer s = Integer.parseInt(e.getKey());
if(location.get(a)== null)
location.put(a, new HashMap<Integer, Location>());
log.info("Added spawn number " + s + " in arena " + a + "!");
location.get(a).put(s, new Location(getServer().getWorld(coords[0]), Double.parseDouble(coords[1]), Double.parseDouble(coords[2]), Double.parseDouble(coords[3])));
}
}
}
}
}
for(i = 1; i <= location.size(); i++){
log.info("Loaded " + location.get(i).size() + " tribute spawns for arena " + i + "!");
Playing.put(i, new ArrayList<String>());
Ready.put(i, new ArrayList<String>());
Dead.put(i, new ArrayList<String>());
Quit.put(i, new ArrayList<String>());
Out.put(i, new ArrayList<String>());
Watching.put(i, new ArrayList<String>());
NeedConfirm.put(i, new ArrayList<String>());
inArena.put(i, new ArrayList<String>());
Frozen.put(i, new ArrayList<String>());
arena.put(i, new ArrayList<String>());
canjoin.put(i, false);
if(location.get(i).size()== config.getInt("maxPlayers")){
maxPlayers.put(i, location.get(i).size());
}else if(location.size()< config.getInt("maxPlayers")){
maxPlayers.put(i, location.get(i).size());
}else if(location.size()> config.getInt("maxPlayers")){
maxPlayers.put(i, config.getInt("maxPlayers"));
2012-10-08 17:55:42 -06:00
}
2013-02-16 18:03:13 -07:00
log.info("Max players is for arena " + i + " is " + maxPlayers.get(i));
open.put(i, true);
2012-10-08 17:55:42 -06:00
}
2012-07-17 15:50:34 -06:00
if (setupEconomy()) {
2013-02-16 18:03:13 -07:00
log.info("Found Vault! Hooking in for economy!");
2012-07-17 15:50:34 -06:00
}
if (config.getDouble("config.version") != 1.3) {
config.set("config.version", 1.3);
2012-10-22 21:05:25 -06:00
config.set("rewardEco.enabled", false);
config.set("rewardEco.reward", 100);
2012-07-17 15:50:34 -06:00
}
2012-10-22 21:05:25 -06:00
if (config.getBoolean("rewardEco.enabled", true) || config.getBoolean("sponsorEco.enabled", true) || config.getBoolean("EntryFee.eco", true)) {
2012-07-17 15:50:34 -06:00
if (vault == true) {
2012-10-22 21:05:25 -06:00
log.info("Economy hook deployed.");
eco = true;
}else{
log.info("You want economy support... yet you either don't have Vault or don't have an economy plugin. Sorry, can't give you it.");
2012-07-17 15:50:34 -06:00
}
}
2012-10-22 21:05:25 -06:00
if (!eco) {
2012-07-17 15:50:34 -06:00
if (vault == true) {
2013-02-16 18:03:13 -07:00
log.info("We see that you have Vault on your server. To set economy support to true, enable it in the config.");
2012-07-17 15:50:34 -06:00
}
}
2012-10-22 21:05:25 -06:00
try{
for(String rewards: config.getStringList("Reward")){
String[] rinfo = rewards.split(",");
Reward.add(new ItemStack(Integer.parseInt(rinfo[0]), Integer.parseInt(rinfo[1])));
}
for(String scost: config.getStringList("Sponsor_Cost")){
String[] sinfo = scost.split(",");
Cost.add(new ItemStack(Integer.parseInt(sinfo[0]), Integer.parseInt(sinfo[1])));
}
if(config.getBoolean("EntryFee.enabled")){
for(String fee: config.getStringList("EntryFee.fee")){
String[] finfo = fee.split(",");
Fee.add(new ItemStack(Integer.parseInt(finfo[0]), Integer.parseInt(finfo[1])));
}
}
}catch(Exception e){
log.warning("Could not add a reward/sponsor/entry cost! One of the rewards/costs is not a number!");
}
2012-08-25 11:58:12 -06:00
worlds = config.getStringList("worlds");
if(worlds.isEmpty()){
2012-08-25 10:09:49 -06:00
restricted = false;
2012-08-25 11:58:12 -06:00
}else if(!worlds.isEmpty()){
2012-08-25 10:09:49 -06:00
restricted = true;
}
2013-02-16 18:03:13 -07:00
log.info("Enabled v" + getDescription().getVersion());
2012-07-18 07:14:50 +10:00
}
2012-07-17 15:50:34 -06:00
public void onDisable(){
2013-02-16 18:03:13 -07:00
log.info("Disabled v" + getDescription().getVersion());
2012-07-17 15:50:34 -06:00
}
public boolean setupEconomy() {
if (getServer().getPluginManager().getPlugin("Vault") == null) {
return false;
}
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
if (rsp == null) {
return false;
}
econ = rsp.getProvider();
vault = true;
return econ != null;
}
2012-10-08 17:55:42 -06:00
public void reloadSpawns() {
if (spawnsFile == null) {
spawnsFile = new File(getDataFolder(), "spawns.yml");
}
spawns = YamlConfiguration.loadConfiguration(spawnsFile);
// Look for defaults in the jar
InputStream defConfigStream = this.getResource("spawns.yml");
if (defConfigStream != null) {
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
spawns.setDefaults(defConfig);
}
}
public FileConfiguration getSpawns() {
if (spawns == null) {
this.reloadSpawns();
}
return spawns;
}
public void saveSpawns() {
if (spawns == null || spawnsFile == null) {
return;
}
try {
getSpawns().save(spawnsFile);
} catch (IOException ex) {
this.getLogger().log(Level.SEVERE, "Could not save config to " + spawnsFile, ex);
}
}
public void reloadData() {
if (dataFile == null) {
dataFile = new File(getDataFolder(), "Data.yml");
}
data = YamlConfiguration.loadConfiguration(dataFile);
// Look for defaults in the jar
InputStream defConfigStream = this.getResource("Data.yml");
if (defConfigStream != null) {
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
data.setDefaults(defConfig);
}
}
public FileConfiguration getData() {
if (data == null) {
this.reloadData();
}
return data;
}
public void saveData() {
if (data == null || dataFile == null) {
return;
}
try {
getData().save(dataFile);
} catch (IOException ex) {
this.getLogger().log(Level.SEVERE, "Could not save config to " + dataFile, ex);
}
}
public void reloadManagement() {
if (managementFile == null) {
managementFile = new File(getDataFolder(), "commandAndBlockManagement.yml");
}
management = YamlConfiguration.loadConfiguration(managementFile);
// Look for defaults in the jar
InputStream defConfigStream = this.getResource("commandAndBlockManagement.yml");
if (defConfigStream != null) {
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
management.setDefaults(defConfig);
}
}
public FileConfiguration getManagement() {
if (management == null) {
this.reloadManagement();
}
return management;
}
public void saveManagement() {
if (management == null || managementFile == null) {
return;
}
try {
getManagement().save(managementFile);
} catch (IOException ex) {
this.getLogger().log(Level.SEVERE, "Could not save config to " + managementFile, ex);
}
}
2013-02-16 18:03:13 -07:00
public void winner(Integer a){
2012-10-22 21:05:25 -06:00
String[] Spawncoords = spawns.getString("Spawn_coords").split(",");
World spawnw = getServer().getWorld(Spawncoords[3]);
double spawnx = Double.parseDouble(Spawncoords[0]);
double spawny = Double.parseDouble(Spawncoords[1]);
double spawnz = Double.parseDouble(Spawncoords[2]);
Location Spawn = new Location(spawnw, spawnx, spawny, spawnz);
2013-02-16 18:03:13 -07:00
if(Playing.size()== 1 && canjoin.get(a)== true){
2012-10-22 21:05:25 -06:00
//Announce winner
2013-02-16 18:03:13 -07:00
for(i = 1; i < Playing.get(a).size(); i++){
String winnername = Playing.get(a).get(i++);
2012-10-22 21:05:25 -06:00
Player winner = getServer().getPlayerExact(winnername);
String winnername2 = winner.getName();
getServer().broadcastMessage(ChatColor.GREEN + winnername2 + " is the victor of this Hunger Games!");
winner.getInventory().clear();
winner.getInventory().setBoots(null);
winner.getInventory().setChestplate(null);
winner.getInventory().setHelmet(null);
winner.getInventory().setLeggings(null);
2013-02-16 18:03:13 -07:00
winner.setLevel(0);
2012-10-22 21:05:25 -06:00
for(PotionEffect pe: winner.getActivePotionEffects()){
PotionEffectType potion = pe.getType();
winner.removePotionEffect(potion);
}
Tele.add(winner);
winner.teleport(Spawn);
if(!config.getBoolean("rewardEco.enabled")){
for(ItemStack Rewards: Reward){
winner.getInventory().addItem(Rewards);
}
}else{
2013-02-16 18:03:13 -07:00
for(ItemStack Rewards: Reward){
winner.getInventory().addItem(Rewards);
}
econ.depositPlayer(winner.getName(), config.getDouble("rewardEco.reward"));
2012-10-22 21:05:25 -06:00
}
Playing.clear();
2013-02-16 18:03:13 -07:00
getServer().getScheduler().cancelTask(deathtime);
2012-10-22 21:05:25 -06:00
}
//Show spectators
2013-02-16 18:03:13 -07:00
for(String s1: Watching.get(a)){
2012-10-22 21:05:25 -06:00
Player spectator = getServer().getPlayerExact(s1);
spectator.setAllowFlight(false);
spectator.teleport(Spawn);
for(Player online:getServer().getOnlinePlayers()){
online.showPlayer(spectator);
}
}
if(config.getString("Auto_Restart").equalsIgnoreCase("True")){
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
public void run(){
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "ha restart");
}
}, 220L);
}
}
}
2013-02-16 18:03:13 -07:00
public void startGames(final Integer a){
String begin = config.getString("Start_Message");
begin = begin.replaceAll("(&([a-f0-9]))", "\u00A7$2");
final String msg = begin;
i = 10;
if(config.getString("Countdown").equalsIgnoreCase("true")){
start = getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
public void run(){
if(i > 0){
if(worlds.isEmpty()){
if(config.getBoolean("broadcastAll")){
getServer().broadcastMessage(String.valueOf(i));
}else{
for(String gn: Playing.get(a)){
Player g = getServer().getPlayer(gn);
g.sendMessage(String.valueOf(i));
}
}
}else{
for(String world: worlds){
World w = getServer().getWorld(world);
if(config.getBoolean("broadcastAll")){
for(Player wp: w.getPlayers()){
wp.sendMessage(String.valueOf(i));
}
}else{
for(String gn: Playing.get(a)){
Player g = getServer().getPlayer(gn);
g.sendMessage(String.valueOf(i));
}
}
}
}
}
i = i-1;
canjoin.put(a, true);
if(i== -1){
Frozen.get(a).clear();
if(config.getBoolean("broadcastAll")){
getServer().broadcastMessage(msg);
}else{
for(String gn: Playing.get(a)){
Player g = getServer().getPlayer(gn);
g.sendMessage(msg);
}
}
getServer().dispatchCommand(Bukkit.getConsoleSender(), "ha Refill " + a);
getServer().getScheduler().cancelTask(start);
if(config.getInt("DeathMatch")!= 0){
int death = config.getInt("DeathMatch");
timetodeath = death;
deathtime = getServer().getScheduler().scheduleSyncRepeatingTask(Bukkit.getPluginManager().getPlugin("HungerArena"), new Runnable(){
public void run(){
timetodeath = timetodeath-1;
if(config.getBoolean("broadcastAll")){
for(Player wp: location.get(a).get(0).getWorld().getPlayers()){
if(timetodeath!= 0){
wp.sendMessage(ChatColor.RED + String.valueOf(timetodeath) + " mins till the death match!");
}
}
}else{
for(String gn: Playing.get(a)){
Player g = getServer().getPlayer(gn);
g.sendMessage(ChatColor.RED + String.valueOf(timetodeath) + " mins till the death match!");
}
}
if(timetodeath== 0){
for(String playing: Playing.get(a)){
Player tribute = getServer().getPlayerExact(playing);
tribute.teleport(location.get(a).get(i));
i = i+1;
for(PotionEffect pe: tribute.getActivePotionEffects()){
PotionEffectType potion = pe.getType();
tribute.removePotionEffect(potion);
}
if(tribute.getAllowFlight()){
tribute.setAllowFlight(false);
}
}
if(config.getBoolean("broadcastAll")){
for(Player wp: location.get(a).get(0).getWorld().getPlayers()){
wp.sendMessage(ChatColor.RED + "The final battle has begun! " + Playing.size() + " tributes will be facing off!");
}
}else{
for(String gn: Playing.get(a)){
Player g = getServer().getPlayer(gn);
g.sendMessage(ChatColor.RED + "The final battle has begun! " + Playing.size() + " tributes will be facing off!");
}
}
getServer().getScheduler().cancelTask(deathtime);
}
}
}, 1200L, 1200L);
}
}
}
}, 20L, 20L);
}else{
Frozen.get(a).clear();
if(config.getBoolean("broadcastAll")){
getServer().broadcastMessage(msg);
}else{
for(String gn: Playing.get(a)){
Player g = getServer().getPlayer(gn);
g.sendMessage(msg);
}
}
canjoin.put(a, true);
getServer().dispatchCommand(Bukkit.getConsoleSender(), "ha Refill " + a);
}
}
public Integer getArena(Player p){
int x = 0;
for(x = 1; x < Playing.size(); x++){
if(Playing.get(x)!= null){
if(Playing.get(x).contains(p.getName())){
return x;
}
}
}
return null;
}
public boolean isSpectating(Player p){
int x = 0;
if(!Watching.isEmpty()){
for(x= 1; x <= Watching.size(); x++){
if(Watching.get(x).contains(p.getName())){
x = Watching.size()+1;
return true;
}else if(Watching.size()== x)
return false;
}
}
return false;
}
2012-07-18 07:14:50 +10:00
}