auto30

Updated..

Aug 2nd, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package arena.newliberty.com;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.ChatColor;
  5. import org.bukkit.Location;
  6. import org.bukkit.Material;
  7. import org.bukkit.Server;
  8. import org.bukkit.command.Command;
  9. import org.bukkit.command.CommandSender;
  10. import org.bukkit.configuration.file.FileConfiguration;
  11. import org.bukkit.entity.Player;
  12. import org.bukkit.event.EventHandler;
  13. import org.bukkit.event.Listener;
  14. import org.bukkit.event.entity.PlayerDeathEvent;
  15. import org.bukkit.event.player.PlayerMoveEvent;
  16. import org.bukkit.inventory.ItemStack;
  17. import org.bukkit.plugin.java.JavaPlugin;
  18.  
  19. import java.util.HashSet;
  20. import java.util.LinkedList;
  21. import java.util.Set;
  22.  
  23. public class Arena extends JavaPlugin implements Listener {
  24.  
  25.     //TODO put these in the config
  26.     public static final String ENTERING_OLYMPIAD = ChatColor.RED + "Entering Olympiad in 3 seconds...";
  27.     public static final String FIGHT_STARTING_IN = ChatColor.RED + "Fight starting in %d seconds...";
  28.     public static final String FIGHT_STARTED = ChatColor.BLUE + "Fight!";
  29.     LinkedList<Player> queue = new LinkedList<Player>();
  30.     public boolean taken = true;
  31.  
  32.     public void onEnable() {
  33.         // Create all the default configs under a tree
  34.         final FileConfiguration config = getConfig();
  35.         config.addDefault("Command.join.price", 100);
  36.         config.addDefault("Command.join.message.success", "You were successfully added to the queue.");
  37.         config.addDefault("Command.join.message.error.queue", "You are already in the queue!");
  38.         config.addDefault("Command.join.message.error.money", "You do not have enough money!");
  39.         config.addDefault("Command.leave.message.success", "You were successfully removed from the queue.");
  40.         config.addDefault("Command.leave.message.error.queue", "You are not in the queue!");
  41.         config.addDefault("Command.check.message.success", "Your position in the queue is: %position%");
  42.         config.addDefault("Command.check.message.error.queue", "You are not in the queue!");
  43.  
  44.         config.addDefault("Wait.time", 10);
  45.         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.");
  46.         config.addDefault("Timers.check.queue", 100L);
  47.  
  48.         config.addDefault("Arena.1.1.x", 0);
  49.         config.addDefault("Arena.1.1.y", 0);
  50.         config.addDefault("Arena.1.1.z", 0);
  51.         config.addDefault("Arena.1.2.x", 0);
  52.         config.addDefault("Arena.1.2.y", 0);
  53.         config.addDefault("Arena.1.2.z", 0);
  54.  
  55.         config.addDefault("Hold.1.1.x", 0);
  56.         config.addDefault("Hold.1.1.y", 0);
  57.         config.addDefault("Hold.1.1.z", 0);
  58.  
  59.         config.options().copyDefaults(true);
  60.  
  61.         saveConfig(); //save the file
  62.  
  63.         final Server server = getServer();
  64.         server.getPluginManager().registerEvents(this, this);
  65.  
  66.  
  67.         /*
  68.         * When the queue size greater than 1 start the arena and remove those 2 players from the queue
  69.         */
  70.         server.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
  71.             public void run() {
  72.                 if (queue.size() >= 2 && taken == false) {
  73.                     Player first = queue.remove(0);
  74.                     Player second = queue.remove(0);
  75.                     start(first, second);
  76.                 }
  77.             }
  78.  
  79.  
  80.             /**
  81.              * Teleport the players to the arena
  82.              */
  83.             private void start(final Player player1, final Player player2) {
  84.                 player1.sendMessage(ENTERING_OLYMPIAD); //starting ..
  85.                 player2.sendMessage(ENTERING_OLYMPIAD);
  86.  
  87.                 server.getScheduler().scheduleSyncDelayedTask(Arena.this, new ArenaFightStart(player1, player2, config, server), 60L); // 3 second wait = 60 tick
  88.             }
  89.  
  90.  
  91.         }, 0L, config.getLong("Timers.check.queue"));
  92.     }
  93.  
  94.     /*
  95.       * onCommand method gets the command the user did
  96.       * if the user did join or j then it checks to see if inventory is empty and adds them to the queue
  97.       * if they did leave it checks to see if they are in the queue then removes them
  98.       * if the user this check it gets their spot in the queue
  99.       * if the user did ? it displays a message
  100.       */
  101.     public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
  102.         Player player = (Player) sender;
  103.         if (cmd.getName().equalsIgnoreCase("olympiad") && args.length == 1) {
  104.             if (args[0].equalsIgnoreCase("join") || args[0].equalsIgnoreCase("j")) {
  105.                 if (!arrayIsEmpty(player.getInventory().getContents()) || !arrayIsEmptyOrAir(player.getInventory().getArmorContents())) {
  106.                     player.sendMessage(ChatColor.RED + "Your inventory must be empty in order to join Olympiad!");
  107.                 } else {
  108.                     if (!queue.contains(player)) {
  109.                         queue.addLast(player);
  110.                         player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.join.message.success")));
  111.                         player.teleport(player.getWorld().getBlockAt(getConfig().getInt("Hold.1.1.x"), getConfig().getInt("Hold.1.1.y"), getConfig().getInt("H.1.1.z")).getLocation());
  112.                     } else /* if (queue.contains(player)) */ { // At this point the player will always be in the queue
  113.                         player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.join.message.error.queue")));
  114.                     }
  115.                     //TODO this never happens:
  116.                     /*else {
  117.                         player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.join.message.error.money")));
  118.                     }*/
  119.                 }
  120.             } else if (args[0].equalsIgnoreCase("leave") || args[0].equalsIgnoreCase("l")) {
  121.                 if (queue.remove(player)) {
  122.                     player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.leave.message.success")));
  123.                 } else {
  124.                     player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.leave.message.error.queue")));
  125.                 }
  126.             } else if (args[0].equalsIgnoreCase("check") || args[0].equalsIgnoreCase("c")) {
  127.                 int queuePos = queue.indexOf(player);
  128.                 if (queuePos != -1) {
  129.                     player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.check.message.success").replaceAll("%position%", Integer.toString(queuePos))));
  130.                 } else {
  131.                     player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.check.message.error.queue")));
  132.                 }
  133.             } else if (args[0].equalsIgnoreCase("help") || args[0].equals("?")) {
  134.                 player.sendMessage(ChatColor.BLUE + (getConfig().getString("Command.help.message")));
  135.             }
  136.             return true;
  137.         }
  138.         return false;
  139.     }
  140.  
  141.     final public HashSet<String> playing = new HashSet<String>();
  142.    
  143.     @EventHandler
  144.     public void onDeath(PlayerDeathEvent event){
  145.          if(playing.contains(event.getPlayer().getName())){ //error here?
  146.            
  147.             Bukkit.broadcastMessage("Someone in the arena died..");
  148.             taken = false;
  149.             break;
  150.         }
  151.        
  152.     }
  153.  
  154.     final public Set<String> frozen = new HashSet<String>();
  155.  
  156.     public void freeze(final Player p) { //add the player to frozen
  157.         frozen.add(p.getName());
  158.     }
  159.  
  160.     public boolean isFrozen(final Player p) { //method to check if a user is frozen
  161.         return frozen.contains(p.getName());
  162.     }
  163.  
  164.     public void unfreeze(final Player p) { //remove the player from the HashSet frozen
  165.         frozen.remove(p.getName());
  166.     }
  167.  
  168.     /**
  169.      * @return {@code true} if an array is empty (i.e. filled with null)
  170.      */
  171.     public static <T> boolean arrayIsEmpty(T[] arr) {
  172.         for (T o : arr)
  173.             if (o != null)
  174.                 return false;
  175.         return true;
  176.     }
  177.  
  178.     /**
  179.      * @return {@code true} if an array of {@link org.bukkit.inventory.ItemStack} is empty or filled with air
  180.      */
  181.     public static <T extends ItemStack> boolean arrayIsEmptyOrAir(T[] arr) {
  182.         for (T o : arr)
  183.             if (o != null && o.getType() != Material.AIR)
  184.                 return false;
  185.         return true;
  186.     }
  187.  
  188.     @EventHandler(ignoreCancelled = true)
  189.     public void onMove(PlayerMoveEvent e) { //check to see if the player is frozen then freeze them
  190.         Player p = e.getPlayer();
  191.         if (isFrozen(p)) {
  192.             // Get old and new position
  193.             Location location = e.getFrom().clone();
  194.             Location to = e.getTo();
  195.  
  196.             // Allow head rotations
  197.             location.setPitch(to.getPitch());
  198.             location.setYaw(to.getYaw());
  199.  
  200.             // Reset position
  201.             e.setTo(location);
  202.         }
  203.     }
  204.  
  205.     private class ArenaFightStart implements Runnable {
  206.         private final Player player1;
  207.         private final Player player2;
  208.         private final FileConfiguration config;
  209.         private final Server server;
  210.  
  211.         public ArenaFightStart(Player player1, Player player2, FileConfiguration config, Server server) {
  212.             this.player1 = player1;
  213.             this.player2 = player2;
  214.             this.config = config;
  215.             this.server = server;
  216.         }
  217.  
  218.         @Override
  219.         public void run() {
  220.             taken = true;
  221.             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
  222.             player2.teleport(player1.getWorld().getBlockAt(config.getInt("Arena.1.2.x"), config.getInt("Arena.1.2.y"), config.getInt("Arena.1.2.z")).getLocation());
  223.  
  224.             // countdown to fight! :D
  225.  
  226.             class Countdown implements Runnable {
  227.                 private int count;
  228.                 private int tid = -1;
  229.  
  230.                 Countdown(int count) {
  231.                     this.count = count;
  232.                 }
  233.  
  234.                 @Override
  235.                 public void run() {
  236.                     freeze(player1);
  237.                     freeze(player2);
  238.  
  239.                     final String fightMsg = String.format(FIGHT_STARTING_IN, count);
  240.                     player1.sendMessage(fightMsg);
  241.                     player2.sendMessage(fightMsg);
  242.  
  243.                     if (count == 0) {
  244.                         server.getScheduler().cancelTask(tid);
  245.                         unfreeze(player1);
  246.                         unfreeze(player2);
  247.                         player1.sendMessage(FIGHT_STARTED);
  248.                         player2.sendMessage(FIGHT_STARTED);
  249.                        
  250.                        
  251.                     } else count--;
  252.                    
  253.                     playing.add(player1.getName());
  254.                     playing.add(player2.getName());
  255.                 }
  256.  
  257.                 void start() {
  258.                     tid = server.getScheduler().scheduleSyncRepeatingTask(Arena.this, this, 0, 20);
  259.                 }
  260.             }
  261.  
  262.             new Countdown(config.getInt("Wait.time")).start();
  263.         }
  264.     }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment