auto30

Global?

Aug 2nd, 2012
75
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.         config.addDefault("Arena.open", "The Olympiad arena is now open!");
  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 (!queue.contains(player)) {
  106.                         queue.addLast(player);
  107.                         player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.join.message.success")));
  108.                         player.teleport(player.getWorld().getBlockAt(getConfig().getInt("Hold.1.1.x"), getConfig().getInt("Hold.1.1.y"), getConfig().getInt("H.1.1.z")).getLocation());
  109.                         player.getInventory().addItem(new ItemStack(Material.IRON_INGOT, 10));
  110.                     } else /* if (queue.contains(player)) */ { // At this point the player will always be in the queue
  111.                         player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.join.message.error.queue")));
  112.                     }
  113.                     //TODO this never happens:
  114.                     /*else {
  115.                         player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.join.message.error.money")));
  116.                     }*/
  117.                
  118.             } else if (args[0].equalsIgnoreCase("leave") || args[0].equalsIgnoreCase("l")) {
  119.                 if (queue.remove(player)) {
  120.                     player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.leave.message.success")));
  121.                 } else {
  122.                     player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.leave.message.error.queue")));
  123.                 }
  124.             } else if (args[0].equalsIgnoreCase("check") || args[0].equalsIgnoreCase("c")) {
  125.                 int queuePos = queue.indexOf(player);
  126.                 if (queuePos != -1) {
  127.                     player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.check.message.success").replaceAll("%position%", Integer.toString(queuePos))));
  128.                 } else {
  129.                     player.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("Command.check.message.error.queue")));
  130.                 }
  131.             } else if (args[0].equalsIgnoreCase("help") || args[0].equals("?")) {
  132.                 player.sendMessage(ChatColor.BLUE + (getConfig().getString("Command.help.message")));
  133.             }
  134.             return true;
  135.         }
  136.         return false;
  137.     }
  138.  
  139.     final public HashSet<String> playing = new HashSet<String>();
  140.    
  141.     @EventHandler
  142.     public void onDeath(PlayerDeathEvent event){
  143.        
  144.          if(playing.contains(event.getEntity().getName())){ //error here?
  145.              
  146.             getName();
  147.             Bukkit.broadcastMessage(ChatColor.GREEN + "The Olympiad arena is now open!");  
  148.             taken = false;
  149.         }
  150.        
  151.     }
  152.  
  153.     final public Set<String> frozen = new HashSet<String>();
  154.  
  155.     public void freeze(final Player p) { //add the player to frozen
  156.         frozen.add(p.getName());
  157.     }
  158.  
  159.     public boolean isFrozen(final Player p) { //method to check if a user is frozen
  160.         return frozen.contains(p.getName());
  161.     }
  162.  
  163.     public void unfreeze(final Player p) { //remove the player from the HashSet frozen
  164.         frozen.remove(p.getName());
  165.     }
  166.  
  167.  
  168.     @EventHandler(ignoreCancelled = true)
  169.     public void onMove(PlayerMoveEvent e) { //check to see if the player is frozen then freeze them
  170.         Player p = e.getPlayer();
  171.         if (isFrozen(p)) {
  172.             // Get old and new position
  173.             Location location = e.getFrom().clone();
  174.             Location to = e.getTo();
  175.  
  176.             // Allow head rotations
  177.             location.setPitch(to.getPitch());
  178.             location.setYaw(to.getYaw());
  179.  
  180.             // Reset position
  181.             e.setTo(location);
  182.         }
  183.     }
  184.  
  185.     private class ArenaFightStart implements Runnable {
  186.         private final Player player1;
  187.         private final Player player2;
  188.         private final FileConfiguration config;
  189.         private final Server server;
  190.  
  191.         public ArenaFightStart(Player player1, Player player2, FileConfiguration config, Server server) {
  192.             this.player1 = player1;
  193.             this.player2 = player2;
  194.             this.config = config;
  195.             this.server = server;
  196.         }
  197.  
  198.         @Override
  199.         public void run() {
  200.             taken = true;
  201.             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
  202.             player2.teleport(player1.getWorld().getBlockAt(config.getInt("Arena.1.2.x"), config.getInt("Arena.1.2.y"), config.getInt("Arena.1.2.z")).getLocation());
  203.  
  204.             // countdown to fight! :D
  205.  
  206.             class Countdown implements Runnable {
  207.                 private int count;
  208.                 private int tid = -1;
  209.  
  210.                 Countdown(int count) {
  211.                     this.count = count;
  212.                 }
  213.  
  214.                 @Override
  215.                 public void run() {
  216.                     freeze(player1);
  217.                     freeze(player2);
  218.  
  219.                     final String fightMsg = String.format(FIGHT_STARTING_IN, count);
  220.                     player1.sendMessage(fightMsg);
  221.                     player2.sendMessage(fightMsg);
  222.  
  223.                     if (count == 0) {
  224.                         server.getScheduler().cancelTask(tid);
  225.                         unfreeze(player1);
  226.                         unfreeze(player2);
  227.                         player1.sendMessage(FIGHT_STARTED);
  228.                         player2.sendMessage(FIGHT_STARTED);
  229.                        
  230.                        
  231.                     } else count--;
  232.                    
  233.                     playing.add(player1.getName());
  234.                     playing.add(player2.getName());
  235.                 }
  236.  
  237.                 void start() {
  238.                     tid = server.getScheduler().scheduleSyncRepeatingTask(Arena.this, this, 0, 20);
  239.                 }
  240.             }
  241.  
  242.            
  243.             new Countdown(config.getInt("Wait.time")).start();
  244.         }
  245.     }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment