auto30

Problem?

Aug 7th, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package arena.newliberty.com;
  2.  
  3.  
  4. import net.milkbowl.vault.economy.Economy;
  5.  
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.ChatColor;
  8. import org.bukkit.Location;
  9. import org.bukkit.Server;
  10. import org.bukkit.command.Command;
  11. import org.bukkit.command.CommandSender;
  12. import org.bukkit.configuration.file.FileConfiguration;
  13. import org.bukkit.entity.Player;
  14. import org.bukkit.event.EventHandler;
  15. import org.bukkit.event.Listener;
  16. import org.bukkit.event.entity.PlayerDeathEvent;
  17. import org.bukkit.event.player.PlayerMoveEvent;
  18. import org.bukkit.plugin.RegisteredServiceProvider;
  19. import org.bukkit.plugin.java.JavaPlugin;
  20. import java.util.HashSet;
  21. import java.util.LinkedList;
  22. import java.util.Set;
  23.  
  24. public class Arena extends JavaPlugin implements Listener {
  25.  
  26.     //TODO put these in the config
  27.     public static final String ENTERING_OLYMPIAD = ChatColor.RED + "Entering Olympiad in 3 seconds...";
  28.     public static final String FIGHT_STARTING_IN = ChatColor.RED + "Fight starting in %d seconds...";
  29.     public static final String FIGHT_STARTED = ChatColor.BLUE + "Fight!";
  30.     LinkedList<Player> queue = new LinkedList<Player>();
  31.     public boolean taken = false; //boolean to see if the arena is taken
  32.     private Player player1;
  33.     private Player player2;
  34.     public static Economy econ = null;
  35.  
  36.     public void onEnable() {
  37.         // Create all the default configs under a tree
  38.         final FileConfiguration config = getConfig();
  39.         config.addDefault("Command.join.price", 100);
  40.         config.addDefault("Command.join.message.success", "You were successfully added to the queue.");
  41.         config.addDefault("Command.join.message.error.queue", "You are already in the queue!");
  42.         config.addDefault("Command.join.message.error.money", "You do not have enough money!");
  43.         config.addDefault("Command.leave.message.success", "You were successfully removed from the queue.");
  44.         config.addDefault("Command.leave.message.error.queue", "You are not in the queue!");
  45.         config.addDefault("Command.check.message.success", "Your position in the queue is: %position%");
  46.         config.addDefault("Command.check.message.error.queue", "You are not in the queue!");
  47.         config.addDefault("Arena.open", "The Olympiad arena is now open!");
  48.         config.addDefault("Winner.message", "WINNER!\nPlease collect your items...");
  49.         config.addDefault("Wait.time", 10);
  50.         config.addDefault("Cleanup.time", 30);
  51.         config.addDefault("Command.help.message", "/olympiad j to join the queue.\n/olympiad l to leave the queue.\n/olympiad c to check your position in the queue.");
  52.         config.addDefault("Timers.check.queue", 100L);
  53.         getConfig().addDefault("Command.join.price", 100);
  54.  
  55.         config.addDefault("Arena.1.1.x", 0);
  56.         config.addDefault("Arena.1.1.y", 0);
  57.         config.addDefault("Arena.1.1.z", 0);
  58.         config.addDefault("Arena.1.2.x", 0);
  59.         config.addDefault("Arena.1.2.y", 0);
  60.         config.addDefault("Arena.1.2.z", 0);
  61.  
  62.         config.addDefault("Hold.1.1.x", 0);
  63.         config.addDefault("Hold.1.1.y", 0);
  64.         config.addDefault("Hold.1.1.z", 0);
  65.  
  66.         config.options().copyDefaults(true);
  67.  
  68.         saveConfig(); //save the file
  69.        
  70.         setupEconomy(); //set up vault
  71.  
  72.        
  73.         final Server server = getServer();
  74.         server.getPluginManager().registerEvents(this, this);
  75.  
  76.  
  77.         /*
  78.         * When the queue size greater than 1 start the arena and remove those 2 players from the queue
  79.         */
  80.         server.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
  81.             public void run() {
  82.                 if (queue.size() >= 2 && taken == false) { //if the queue is ready and the arena isn't taken
  83.                     Player first = queue.remove(0);
  84.                     Player second = queue.remove(0);
  85.                     start(first, second);
  86.                 }
  87.             }
  88.  
  89.  
  90.             /**
  91.              * Teleport the players to the arena
  92.              */
  93.             private void start(final Player player1, final Player player2) {
  94.                 player1.sendMessage(ENTERING_OLYMPIAD); //starting ..
  95.                 player2.sendMessage(ENTERING_OLYMPIAD);
  96.  
  97.                 server.getScheduler().scheduleSyncDelayedTask(Arena.this, new ArenaFightStart(player1, player2, config, server), 60L); // 3 second wait = 60 tick
  98.             }
  99.  
  100.  
  101.         }, 0L, config.getLong("Timers.check.queue"));
  102.     }
  103.  
  104.     /*
  105.       * onCommand method gets the command the user did
  106.       * if the user did join or j then it checks to see if inventory is empty and adds them to the queue
  107.       * if they did leave it checks to see if they are in the queue then removes them
  108.       * if the user this check it gets their spot in the queue
  109.       * if the user did ? it displays a message
  110.       */
  111.     public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
  112.         Player player = (Player) sender;
  113.         if (cmd.getName().equalsIgnoreCase("olympiad") && args.length == 1) {
  114.             if (args[0].equalsIgnoreCase("join") || args[0].equalsIgnoreCase("j")) {
  115.                     if (!queue.contains(player) && econ.has(player.getPlayerListName(), getConfig().getDouble("Command.join.price"))) {
  116.                         queue.addLast(player);
  117.                         econ.withdrawPlayer(player.getPlayerListName(), getConfig().getDouble("Command.join.price"));
  118.                         player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.join.message.success")));
  119.                         player.teleport(player.getWorld().getBlockAt(getConfig().getInt("Hold.1.1.x"), getConfig().getInt("Hold.1.1.y"), getConfig().getInt("H.1.1.z")).getLocation());
  120.                     } else
  121.                         if (queue.contains(player))  { // At this point the player will always be in the queue
  122.                         player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.join.message.error.queue")));
  123.                    
  124.                         }
  125.                     else {
  126.                         player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.join.message.error.money")));
  127.                     }
  128.                    
  129.                
  130.             } else if (args[0].equalsIgnoreCase("leave") || args[0].equalsIgnoreCase("l")) {
  131.                 if (queue.remove(player)) {
  132.                     econ.depositPlayer(player.getPlayerListName(), getConfig().getDouble("Command.join.price"));
  133.                     player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.leave.message.success")));
  134.                 } else {
  135.                     player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.leave.message.error.queue")));
  136.                 }
  137.             } else if (args[0].equalsIgnoreCase("check") || args[0].equalsIgnoreCase("c")) {
  138.                 int queuePos = queue.indexOf(player);
  139.                 if (queuePos != -1) {
  140.                     econ.withdrawPlayer(player.getPlayerListName(), getConfig().getDouble("Command.join.price"));
  141.                     player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.check.message.success").replaceAll("%position%", Integer.toString(queuePos))));
  142.                 } else {
  143.                     player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.check.message.error.queue")));
  144.                 }
  145.             } else if (args[0].equalsIgnoreCase("help") || args[0].equals("?")) {
  146.                 player.sendMessage(ChatColor.BLUE + (getConfig().getString("Command.help.message")));
  147.             }
  148.             return true;
  149.         }
  150.         else if (cmd.getName().equalsIgnoreCase("olympiad") && args.length == 0){
  151.             player.sendMessage(ChatColor.BLUE + (getConfig().getString("Command.help.message")));
  152.         }
  153.         return false;
  154.     }
  155.  
  156.     final public HashSet<String> playing = new HashSet<String>();
  157.    
  158.     @EventHandler
  159.     public void onDeath(PlayerDeathEvent event){
  160.        
  161.          if(playing.contains(event.getEntity().getName())){ //error here?
  162.              
  163.             if(event.getEntity().getName().equals(player1.getName())){ //
  164.              
  165.                 player2.sendMessage(getConfig().getString("Winner.message"));
  166.                 playing.remove(player2);
  167.                 playing.remove(player1);
  168.                 class Countdown implements Runnable {
  169.                     private int count;
  170.                     private int tid = -1;
  171.  
  172.                     Countdown(int count) {
  173.                         this.count = count;
  174.                     }
  175.  
  176.                     @Override
  177.                     public void run() {
  178.  
  179.                         final String fightMsg = (count + " seconds left in cleanup...");
  180.                         if(count == 30 || count == 15 || count == 10 || count <= 5){
  181.                         player2.sendMessage(fightMsg);
  182.                         }
  183.                         if (count == 0) {
  184.                             getServer().getScheduler().cancelTask(tid);
  185.                             player2.sendMessage(ChatColor.BLUE + "Teleporting to spawn...");
  186.                             player2.teleport(player2.getWorld().getSpawnLocation());
  187.                            
  188.                            
  189.                         } else count--;
  190.  
  191.                     }
  192.  
  193.                     void start() {
  194.                         tid = getServer().getScheduler().scheduleSyncRepeatingTask(Arena.this, this, 0, 20);
  195.                     }
  196.                 }
  197.  
  198.                
  199.                 new Countdown(getConfig().getInt("Cleanup.time")).start();
  200.  
  201.                 long x = getConfig().getInt("Cleanup.time") * 20;
  202.                 getServer().getScheduler().scheduleSyncDelayedTask(Arena.this, new Runnable() {
  203.  
  204.                        public void run() {
  205.                            Bukkit.broadcastMessage(ChatColor.GREEN + "The Olympiad arena is now open!");
  206.                            taken = false;
  207.                        }
  208.                     }, x);
  209.                
  210.              }
  211.             else if(event.getEntity().getName().equals(player2.getName())){
  212.                
  213.                 player1.sendMessage(getConfig().getString("Winner.message"));
  214.                 playing.remove(player2);
  215.                 playing.remove(player1);
  216.                
  217.                 class Countdown implements Runnable {
  218.                     private int count;
  219.                     private int tid = -1;
  220.  
  221.                     Countdown(int count) {
  222.                         this.count = count;
  223.                     }
  224.  
  225.                     @Override
  226.                     public void run() {
  227.  
  228.                         final String fightMsg = (count + " seconds left in cleanup..");
  229.                         if(count == 30 || count == 15 || count == 10 || count <= 5){
  230.                         player1.sendMessage(fightMsg);
  231.                         }
  232.  
  233.                         if (count == 0) {
  234.                             getServer().getScheduler().cancelTask(tid);
  235.                             player1.sendMessage(ChatColor.BLUE + "Teleporting to spawn...");
  236.                             player1.teleport(player2.getWorld().getSpawnLocation());
  237.                            
  238.                         } else count--;
  239.  
  240.                     }
  241.  
  242.                     void start() {
  243.                         tid = getServer().getScheduler().scheduleSyncRepeatingTask(Arena.this, this, 0, 20);
  244.                     }
  245.                 }
  246.  
  247.                
  248.                 new Countdown(getConfig().getInt("Cleanup.time")).start();
  249.                
  250.                 long x = getConfig().getInt("Cleanup.time") * 20;
  251.                 getServer().getScheduler().scheduleSyncDelayedTask(Arena.this, new Runnable() {
  252.  
  253.                        public void run() {
  254.                            Bukkit.broadcastMessage(ChatColor.GREEN + "The Olympiad arena is now open! Players are: " + playing);
  255.                            taken = false;
  256.                        }
  257.                     }, x);
  258.              }
  259.            
  260.         }
  261.        
  262.     }
  263.  
  264.     final public Set<String> frozen = new HashSet<String>();
  265.  
  266.     public void freeze(final Player p) { //add the player to frozen
  267.         frozen.add(p.getName());
  268.     }
  269.  
  270.     public boolean isFrozen(final Player p) { //method to check if a user is frozen
  271.         return frozen.contains(p.getName());
  272.     }
  273.  
  274.     public void unfreeze(final Player p) { //remove the player from the HashSet frozen
  275.         frozen.remove(p.getName());
  276.     }
  277.  
  278.  
  279.     @EventHandler(ignoreCancelled = true)
  280.     public void onMove(PlayerMoveEvent e) { //check to see if the player is frozen then freeze them
  281.         Player p = e.getPlayer();
  282.         if (isFrozen(p)) {
  283.             // Get old and new position
  284.             Location location = e.getFrom().clone();
  285.             Location to = e.getTo();
  286.  
  287.             // Allow head rotations
  288.             location.setPitch(to.getPitch());
  289.             location.setYaw(to.getYaw());
  290.  
  291.             // Reset position
  292.             e.setTo(location);
  293.         }
  294.     }
  295.  
  296.     private class ArenaFightStart implements Runnable {
  297.         private final FileConfiguration config;
  298.         private final Server server;
  299.  
  300.         public ArenaFightStart(Player player1, Player player2, FileConfiguration config, Server server) {
  301.             Arena.this.player1 = player1;
  302.             Arena.this.player2 = player2;
  303.             this.config = config;
  304.             this.server = server;
  305.         }
  306.  
  307.         @Override
  308.         public void run() {
  309.             taken = true;
  310.             player1.teleport(player1.getWorld().getBlockAt(config.getInt("Arena.1.1.x"), config.getInt("Arena.1.1.y"), config.getInt("Arena.1.1.z")).getLocation()); //teleport the players to the designated spot
  311.             player2.teleport(player1.getWorld().getBlockAt(config.getInt("Arena.1.2.x"), config.getInt("Arena.1.2.y"), config.getInt("Arena.1.2.z")).getLocation());
  312.  
  313.             // countdown to fight! :D
  314.  
  315.             class Countdown implements Runnable {
  316.                 private int count;
  317.                 private int tid = -1;
  318.  
  319.                 Countdown(int count) {
  320.                     this.count = count;
  321.                 }
  322.  
  323.                 @Override
  324.                 public void run() {
  325.                     freeze(player1);
  326.                     freeze(player2);
  327.  
  328.                     final String fightMsg = String.format(FIGHT_STARTING_IN, count);
  329.                     player1.sendMessage(fightMsg);
  330.                     player2.sendMessage(fightMsg);
  331.  
  332.                     if (count == 0) {
  333.                         server.getScheduler().cancelTask(tid);
  334.                         unfreeze(player1);
  335.                         unfreeze(player2);
  336.                         player1.sendMessage(FIGHT_STARTED);
  337.                         player2.sendMessage(FIGHT_STARTED);
  338.                        
  339.                        
  340.                     } else count--;
  341.                    
  342.                     playing.add(player1.getName());
  343.                     playing.add(player2.getName());
  344.                 }
  345.  
  346.                 void start() {
  347.                     tid = server.getScheduler().scheduleSyncRepeatingTask(Arena.this, this, 0, 20);
  348.                 }
  349.             }
  350.  
  351.            
  352.             new Countdown(config.getInt("Wait.time")).start();
  353.         }
  354.     }
  355.     private boolean setupEconomy() {
  356.         if (getServer().getPluginManager().getPlugin("Vault") == null) {
  357.             return false;
  358.         }
  359.         RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
  360.         if (rsp == null) {
  361.             return false;
  362.         }
  363.         econ = rsp.getProvider();
  364.         return econ != null;
  365.     }
  366.    
  367.    
  368.    
  369. }
Advertisement
Add Comment
Please, Sign In to add comment