auto30

Code

Aug 2nd, 2012
98
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 = false; //boolean to see if the arena is taken
  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) { //if the queue is ready and the arena isn't taken
  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.getEntity().getName())){ //error here?
  146.            
  147.              if(event.getEntity().getName().equals(player1)){
  148.                  
  149.              }
  150.             Bukkit.broadcastMessage("Someone in the arena died..");  
  151.             taken = false;
  152.         }
  153.        
  154.     }
  155.  
  156.     final public Set<String> frozen = new HashSet<String>();
  157.  
  158.     public void freeze(final Player p) { //add the player to frozen
  159.         frozen.add(p.getName());
  160.     }
  161.  
  162.     public boolean isFrozen(final Player p) { //method to check if a user is frozen
  163.         return frozen.contains(p.getName());
  164.     }
  165.  
  166.     public void unfreeze(final Player p) { //remove the player from the HashSet frozen
  167.         frozen.remove(p.getName());
  168.     }
  169.  
  170.     /**
  171.      * @return {@code true} if an array is empty (i.e. filled with null)
  172.      */
  173.     public static <T> boolean arrayIsEmpty(T[] arr) {
  174.         for (T o : arr)
  175.             if (o != null)
  176.                 return false;
  177.         return true;
  178.     }
  179.  
  180.     /**
  181.      * @return {@code true} if an array of {@link org.bukkit.inventory.ItemStack} is empty or filled with air
  182.      */
  183.     public static <T extends ItemStack> boolean arrayIsEmptyOrAir(T[] arr) {
  184.         for (T o : arr)
  185.             if (o != null && o.getType() != Material.AIR)
  186.                 return false;
  187.         return true;
  188.     }
  189.  
  190.     @EventHandler(ignoreCancelled = true)
  191.     public void onMove(PlayerMoveEvent e) { //check to see if the player is frozen then freeze them
  192.         Player p = e.getPlayer();
  193.         if (isFrozen(p)) {
  194.             // Get old and new position
  195.             Location location = e.getFrom().clone();
  196.             Location to = e.getTo();
  197.  
  198.             // Allow head rotations
  199.             location.setPitch(to.getPitch());
  200.             location.setYaw(to.getYaw());
  201.  
  202.             // Reset position
  203.             e.setTo(location);
  204.         }
  205.     }
  206.  
  207.     private class ArenaFightStart implements Runnable {
  208.         private final Player player1;
  209.         private final Player player2;
  210.         private final FileConfiguration config;
  211.         private final Server server;
  212.  
  213.         public ArenaFightStart(Player player1, Player player2, FileConfiguration config, Server server) {
  214.             this.player1 = player1;
  215.             this.player2 = player2;
  216.             this.config = config;
  217.             this.server = server;
  218.         }
  219.  
  220.         @Override
  221.         public void run() {
  222.             taken = true;
  223.             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
  224.             player2.teleport(player1.getWorld().getBlockAt(config.getInt("Arena.1.2.x"), config.getInt("Arena.1.2.y"), config.getInt("Arena.1.2.z")).getLocation());
  225.  
  226.             // countdown to fight! :D
  227.  
  228.             class Countdown implements Runnable {
  229.                 private int count;
  230.                 private int tid = -1;
  231.  
  232.                 Countdown(int count) {
  233.                     this.count = count;
  234.                 }
  235.  
  236.                 @Override
  237.                 public void run() {
  238.                     freeze(player1);
  239.                     freeze(player2);
  240.  
  241.                     final String fightMsg = String.format(FIGHT_STARTING_IN, count);
  242.                     player1.sendMessage(fightMsg);
  243.                     player2.sendMessage(fightMsg);
  244.  
  245.                     if (count == 0) {
  246.                         server.getScheduler().cancelTask(tid);
  247.                         unfreeze(player1);
  248.                         unfreeze(player2);
  249.                         player1.sendMessage(FIGHT_STARTED);
  250.                         player2.sendMessage(FIGHT_STARTED);
  251.                        
  252.                        
  253.                     } else count--;
  254.                    
  255.                     playing.add(player1.getName());
  256.                     playing.add(player2.getName());
  257.                 }
  258.  
  259.                 void start() {
  260.                     tid = server.getScheduler().scheduleSyncRepeatingTask(Arena.this, this, 0, 20);
  261.                 }
  262.             }
  263.  
  264.            
  265.             new Countdown(config.getInt("Wait.time")).start();
  266.         }
  267.     }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment