auto30

Code

Aug 2nd, 2012
68
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.             // if(event.getEntity().getName().equals(player1){ <--How do i get the player1 variable?
  147.              //player 2 wins!
  148.              // }
  149.             Bukkit.broadcastMessage(ChatColor.GREEN + "The Olympiad arena is now open!");  
  150.             taken = false;
  151.         }
  152.        
  153.     }
  154.  
  155.     final public Set<String> frozen = new HashSet<String>();
  156.  
  157.     public void freeze(final Player p) { //add the player to frozen
  158.         frozen.add(p.getName());
  159.     }
  160.  
  161.     public boolean isFrozen(final Player p) { //method to check if a user is frozen
  162.         return frozen.contains(p.getName());
  163.     }
  164.  
  165.     public void unfreeze(final Player p) { //remove the player from the HashSet frozen
  166.         frozen.remove(p.getName());
  167.     }
  168.  
  169.  
  170.     @EventHandler(ignoreCancelled = true)
  171.     public void onMove(PlayerMoveEvent e) { //check to see if the player is frozen then freeze them
  172.         Player p = e.getPlayer();
  173.         if (isFrozen(p)) {
  174.             // Get old and new position
  175.             Location location = e.getFrom().clone();
  176.             Location to = e.getTo();
  177.  
  178.             // Allow head rotations
  179.             location.setPitch(to.getPitch());
  180.             location.setYaw(to.getYaw());
  181.  
  182.             // Reset position
  183.             e.setTo(location);
  184.         }
  185.     }
  186.  
  187.     private class ArenaFightStart implements Runnable {
  188.         private final Player player1;
  189.         private final Player player2;
  190.         private final FileConfiguration config;
  191.         private final Server server;
  192.  
  193.         public ArenaFightStart(Player player1, Player player2, FileConfiguration config, Server server) {
  194.             this.player1 = player1;
  195.             this.player2 = player2;
  196.             this.config = config;
  197.             this.server = server;
  198.         }
  199.  
  200.         @Override
  201.         public void run() {
  202.             taken = true;
  203.             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
  204.             player2.teleport(player1.getWorld().getBlockAt(config.getInt("Arena.1.2.x"), config.getInt("Arena.1.2.y"), config.getInt("Arena.1.2.z")).getLocation());
  205.  
  206.             // countdown to fight! :D
  207.  
  208.             class Countdown implements Runnable {
  209.                 private int count;
  210.                 private int tid = -1;
  211.  
  212.                 Countdown(int count) {
  213.                     this.count = count;
  214.                 }
  215.  
  216.                 @Override
  217.                 public void run() {
  218.                     freeze(player1);
  219.                     freeze(player2);
  220.  
  221.                     final String fightMsg = String.format(FIGHT_STARTING_IN, count);
  222.                     player1.sendMessage(fightMsg);
  223.                     player2.sendMessage(fightMsg);
  224.  
  225.                     if (count == 0) {
  226.                         server.getScheduler().cancelTask(tid);
  227.                         unfreeze(player1);
  228.                         unfreeze(player2);
  229.                         player1.sendMessage(FIGHT_STARTED);
  230.                         player2.sendMessage(FIGHT_STARTED);
  231.                        
  232.                        
  233.                     } else count--;
  234.                    
  235.                     playing.add(player1.getName());
  236.                     playing.add(player2.getName());
  237.                 }
  238.  
  239.                 void start() {
  240.                     tid = server.getScheduler().scheduleSyncRepeatingTask(Arena.this, this, 0, 20);
  241.                 }
  242.             }
  243.  
  244.            
  245.             new Countdown(config.getInt("Wait.time")).start();
  246.         }
  247.     }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment