stoneharry

Untitled

Aug 18th, 2012
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.99 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package main;
  6.  
  7. import java.util.HashMap;
  8. import java.util.Random;
  9. import java.util.Stack;
  10. import java.util.logging.Logger;
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.ChatColor;
  13. import org.bukkit.Location;
  14. import org.bukkit.Material;
  15. import org.bukkit.block.Block;
  16. import org.bukkit.entity.Entity;
  17. import org.bukkit.entity.Player;
  18. import org.bukkit.event.EventHandler;
  19. import org.bukkit.event.Listener;
  20. import org.bukkit.event.block.BlockBreakEvent;
  21. import org.bukkit.event.block.BlockIgniteEvent;
  22. import org.bukkit.event.block.BlockPlaceEvent;
  23. import org.bukkit.event.entity.EntityDamageEvent;
  24. import org.bukkit.event.entity.FoodLevelChangeEvent;
  25. import org.bukkit.event.player.PlayerJoinEvent;
  26. import org.bukkit.event.player.PlayerQuitEvent;
  27. import org.bukkit.plugin.java.JavaPlugin;
  28. import org.bukkit.event.player.PlayerMoveEvent;
  29. import org.bukkit.event.player.PlayerTeleportEvent;
  30. import org.bukkit.event.player.PlayerToggleSneakEvent;
  31. import org.bukkit.event.weather.WeatherChangeEvent;
  32. import org.bukkit.inventory.ItemStack;
  33.  
  34. /**
  35.  *
  36.  * @author Harry
  37.  */
  38.  
  39. public class zombieMain extends JavaPlugin implements Listener
  40. {
  41.     // Console references the Minecraft console
  42.     public static Logger console = null;
  43.     // The hashmap contains all existing human players
  44.     private static HashMap<String, String> humans;
  45.     // This is only true when the game is in progress
  46.     public static Boolean gameRunning = false;
  47.     // This is true when the map is resetting, it is on before a game has begun
  48.     public static Boolean resetting = true;
  49.     // Debug enables debug messages
  50.     private static final Boolean debug = false;
  51.     // The stack stores changed blocks for rollbacks
  52.     private static Stack blocks_changed = new Stack();
  53.     // The time limit is how long between each round
  54.     public static final long timeLimit = 300000;
  55.     // Last time stores the time when the round begins
  56.     public static long lastTime = 0;
  57.     // Mark zombies is used to decide whether to mark humans with lightning or not
  58.     public static Boolean mark_zombies = false;
  59.     // This handles which round should be started
  60.     public static short round = 1;
  61.    
  62.     public void onDisable()
  63.     {
  64.         // Dispose of possible large data and try to rollback
  65.         try
  66.         {
  67.             resetGame();
  68.             blocks_changed.clear();
  69.             humans.clear();
  70.         }
  71.         catch (Exception ex)
  72.         {
  73.             ex.printStackTrace();
  74.         }
  75.         console.info("[StonedZombie] Stoneharry - Disabled!");
  76.     }
  77.  
  78.     public void onEnable()
  79.     {
  80.         console = Logger.getLogger("Minecraft");
  81.         humans = new HashMap<String, String>();
  82.         // Register events
  83.         getServer().getPluginManager().registerEvents(this, this);
  84.         // This runs every ~32 seconds
  85.         getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
  86.            public void run()
  87.            {
  88.                if (gameRunning && !resetting)
  89.                {
  90.                    int result = CheckForTimeUp();
  91.                    if (result != 0)
  92.                    {
  93.                        // Get time remaining
  94.                        int seconds = (int)(timeLimit / 1000) - ( result / 1000 );
  95.                        int minutes = 0;
  96.                        while (seconds > 59)
  97.                        {
  98.                            seconds = seconds - 60;
  99.                            minutes = minutes + 1;
  100.                        }
  101.                        if (minutes == 0)
  102.                        {
  103.                             Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "There is " + String.valueOf(seconds) + " seconds remaining!");
  104.                        }
  105.                        else
  106.                        {
  107.                             Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "There is " + String.valueOf(minutes) + " minutes and " + String.valueOf(seconds) + " seconds remaining!");
  108.                        }
  109.                        // If mark zombies put lightning on humans
  110.                        if (mark_zombies)
  111.                        {
  112.                            Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "The lightning shows where the remaining humans are!");
  113.                            for (String name : humans.values())
  114.                            {
  115.                                Player player = Bukkit.getPlayer(name);
  116.                                if (player != null)
  117.                                {
  118.                                    if (player.isOnline())
  119.                                    {
  120.                                        Bukkit.getWorld("world").strikeLightningEffect(player.getLocation());
  121.                                    }
  122.                                }
  123.                            }
  124.                            mark_zombies = false;
  125.                        }
  126.                        else
  127.                        {
  128.                            mark_zombies = true;
  129.                        }
  130.                    }
  131.                     // Check for offline humans and remove them if found
  132.                     for (String name : humans.values())
  133.                     {
  134.                         if (Bukkit.getPlayer(name) == null)
  135.                         {
  136.                             humans.remove(name);
  137.                         }
  138.                         else
  139.                         {
  140.                             if (!Bukkit.getPlayer(name).isOnline())
  141.                             {
  142.                                 humans.remove(name);                      
  143.                             }
  144.                         }
  145.                     }
  146.                }
  147.            }
  148.         }, 700L, 700L);
  149.         // This thread runs every ~3 seconds and is used to get a more accurate reading of game end
  150.         getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
  151.            public void run()
  152.            {
  153.                if (gameRunning && !resetting)
  154.                {
  155.                    CheckForTimeUp();
  156.                }
  157.            }
  158.         }, 60L, 60L);
  159.         console.info("[StonedZombie] Stoneharry - Enabled!");
  160.     }
  161.    
  162.     public int CheckForTimeUp()
  163.     {
  164.        // Get time elapsed
  165.        int result = (int)(System.currentTimeMillis()-lastTime);
  166.        // See if the time elapsed is more than the time limit
  167.        if (result > timeLimit)
  168.        {
  169.            Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "The time limit is up, humans have won!");
  170.            prepareReset();
  171.            return 0;
  172.        }
  173.        return result;
  174.     }
  175.    
  176.     @EventHandler
  177.     public void onPlayerMove(PlayerMoveEvent event)
  178.     {
  179.         try
  180.         {
  181.             if (gameRunning)
  182.             {
  183.                 Player plr = event.getPlayer();
  184.                 // Get nearby players
  185.                 for (Entity entity : plr.getNearbyEntities(0.7, 0.7, 0.7))
  186.                 {
  187.                     if (entity instanceof Player)
  188.                     {          
  189.                         Player target = (Player)entity;
  190.                         // Check to see if the moving person is a zombie and the other a human
  191.                         if (humans.containsKey(target.getName()) && !humans.containsKey(plr.getName()))
  192.                         {
  193.                             // The person will no longer be a human
  194.                             humans.remove(target.getName());
  195.                             // Set the messages and change his name
  196.                             Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + plr.getName() + " has infected " + target.getName() + "!");
  197.                             target.setDisplayName("[" + ChatColor.RED + "Zombie" + ChatColor.WHITE + "] " + target.getName());
  198.                             //target.setPlayerListName("[Zombie] " + target.getName());
  199.                             // Check to see if all humans have been found
  200.                             if (humans.isEmpty() && gameRunning && !resetting)
  201.                             {
  202.                                 Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "All humans have been caught! The game will end in 10 seconds...");
  203.                                 prepareReset();
  204.                             }
  205.                             else
  206.                             {
  207.                                 Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "There are " + String.valueOf(humans.size()) + " humans remaining!");
  208.                             }
  209.                         }
  210.                     }
  211.                 }
  212.             }
  213.             else
  214.             {
  215.                 // Check to start the game
  216.                 Player players[] = Bukkit.getOnlinePlayers();
  217.                 if (players.length > 1)
  218.                 {
  219.                     // Set the time the round started
  220.                     lastTime = System.currentTimeMillis();
  221.  
  222.                     // A not very random, random number generator
  223.                      Random ran = new Random();
  224.                      int x = ran.nextInt(players.length - 1);
  225.                      for (int i = 0; i < players.length; i++)
  226.                      {
  227.                          // Reset inventory
  228.                          players[i].getInventory().clear();
  229.                          players[i].getInventory().setArmorContents(null);
  230.                          // Add items
  231.                          ItemStack reward = new ItemStack(3, 20);
  232.                          players[i].getInventory().addItem(reward);
  233.                          // Correct method unknown (?)
  234.                          players[i].updateInventory();
  235.                          // If random number = player in online players, set to zombie
  236.                          if (i == x)
  237.                          {
  238.                              Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "The game has begun and " + players[i].getName() + " is the first zombie!");
  239.                              players[i].setDisplayName("[" + ChatColor.RED + "Zombie" + ChatColor.WHITE + "] " + players[i].getName());
  240.                              //players[i].setPlayerListName("[Zombie] " + players[i].getName());
  241.                              handleTeleport(players[i], false);
  242.                          }
  243.                          // Set to human
  244.                          else
  245.                          {
  246.                              players[i].setDisplayName("[" + ChatColor.GREEN + "Human" + ChatColor.WHITE + "] " + players[i].getName());
  247.                              //players[i].setPlayerListName("[Human] " + players[i].getName());
  248.                              humans.put(players[i].getName(), players[i].getName());
  249.                              handleTeleport(players[i], true);
  250.                          }
  251.                      }
  252.                      // Set global variables
  253.                      gameRunning = true;
  254.                      resetting = false;
  255.                 }
  256.             }
  257.         }
  258.         catch (Exception ex)
  259.         {
  260.             ex.printStackTrace();
  261.         }
  262.     }
  263.    
  264.     // This function just sets the variable to show the round has ended but it is waiting 10 seconds
  265.     public void prepareReset()
  266.     {
  267.         resetting = true;
  268.         getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable()
  269.         {
  270.            public void run()
  271.            {
  272.                resetGame();
  273.            }
  274.         }, 200L);
  275.     }
  276.    
  277.     @EventHandler
  278.     public void onPlayerJoin(PlayerJoinEvent event)
  279.     {
  280.         // When a player joins, set them to zombie and update them if game is running, teleport.
  281.         if (gameRunning)
  282.         {
  283.             Player p = event.getPlayer();
  284.             p.sendMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "The game is in progress already. You have joined as a Zombie. Catch the humans!");
  285.             //Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + p.getName() + " has joined the Zombies!");
  286.             p.setDisplayName("[" + ChatColor.RED + "Zombie" + ChatColor.WHITE + "] " + p.getName());
  287.             //p.setPlayerListName("[Zombie] " + p.getName());
  288.             ItemStack reward = new ItemStack(3, 20);
  289.             p.getInventory().addItem(reward);
  290.             p.updateInventory();
  291.         }
  292.         handleTeleport(event.getPlayer(), false);
  293.         event.setJoinMessage(null);
  294.     }
  295.    
  296.     // Prevents leave server message being spammed
  297.     @EventHandler
  298.     public void onPlayerLeave(PlayerQuitEvent event)
  299.     {
  300.         event.setQuitMessage(null);
  301.     }
  302.    
  303.     // Used to send debug messages if variable is set
  304.     private void DebugMessage(String message, Player plr)
  305.     {
  306.         if (debug)
  307.         {
  308.             console.info(message);
  309.             plr.sendMessage(message);
  310.         }
  311.     }
  312.  
  313.     // Make players immune to damage
  314.     @EventHandler
  315.     public void onEntityDamage(EntityDamageEvent event)
  316.     {
  317.         event.setCancelled(true);
  318.     }
  319.    
  320.     // Prevent players getting hungry
  321.     @EventHandler
  322.     public void onFoodLevelChange(FoodLevelChangeEvent event)
  323.     {
  324.         event.setCancelled(true);
  325.     }
  326.    
  327.     @EventHandler
  328.     public void onBlockPlace(BlockPlaceEvent event)
  329.     {
  330.         // If game is in progress
  331.         if (!resetting)
  332.         {
  333.             // A block has been placed - push that the location should be air (it was air before)
  334.             blocks_changed.push(event.getBlockPlaced().getLocation());
  335.             blocks_changed.push(0);
  336.             blocks_changed.push((byte)0);
  337.         }
  338.         // When no game, no building
  339.         else
  340.         {
  341.             event.setCancelled(true);
  342.         }
  343.     }
  344.    
  345.     @EventHandler
  346.     public void onBlockBreak(BlockBreakEvent event)
  347.     {
  348.         // If game is not resetting
  349.         if (!resetting)
  350.         {
  351.             // Push the block being deleted for restorating later
  352.             Block b = event.getBlock();
  353.             blocks_changed.push(b.getLocation());
  354.             blocks_changed.push(b.getTypeId());
  355.             blocks_changed.push(b.getData());
  356.             // Does not appear to work
  357.             //b.getDrops().clear();
  358.             // If not dirt
  359.             if (b.getType() != Material.DIRT)
  360.             {
  361.                 // Cancel and change to air
  362.                 event.setCancelled(true);
  363.                 b.setType(Material.AIR);
  364.             }
  365.         }
  366.         // when no game, no building
  367.         else
  368.         {
  369.             event.setCancelled(true);
  370.         }
  371.     }
  372.    
  373.     public void resetGame()
  374.     {
  375.         try
  376.         {
  377.             // Reset the map by rolling back changes
  378.             Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "Restoring arena... This may cause some lag.");
  379.  
  380.             while (!blocks_changed.isEmpty())
  381.             {
  382.                 Byte b = (Byte)blocks_changed.pop();
  383.                 Integer mat = (Integer)blocks_changed.pop();
  384.                 Location loc = (Location)blocks_changed.pop();
  385.                 loc.getBlock().setTypeIdAndData(mat, b, true);
  386.             }
  387.  
  388.             blocks_changed.clear();
  389.  
  390.             Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "Done!");
  391.         }
  392.         catch (Exception ex)
  393.         {
  394.             ex.printStackTrace();
  395.         }
  396.         finally
  397.         {
  398.             // Make sure the needed variables change
  399.             gameRunning = false;
  400.             resetting = false;
  401.             mark_zombies = false;
  402.             incrementRound();            
  403.         }
  404.     }
  405.    
  406.     @EventHandler
  407.     public void ignitefire(BlockIgniteEvent event)
  408.     {
  409.         // Try to stop fire
  410.         event.setCancelled(true);
  411.     }
  412.    
  413.     public void handleTeleport(Player plr, Boolean human)
  414.     {
  415.         // This function teleports humans and zombies depending on the round
  416.         // Sand Arena
  417.         if (round == 1)
  418.         {
  419.             if (human)
  420.             {
  421.                 plr.teleport(new Location(Bukkit.getServer().getWorld("world"), 437, 71, 230));
  422.             }
  423.             else
  424.             {
  425.                 plr.teleport(new Location(Bukkit.getServer().getWorld("world"), 458, 84, 231));
  426.             }
  427.         }
  428.         // Diamond Arena
  429.         else if (round == 2)
  430.         {
  431.             if (human)
  432.             {
  433.                 plr.teleport(new Location(Bukkit.getServer().getWorld("world"), 438, 158, 220));
  434.             }
  435.             else
  436.             {
  437.                 plr.teleport(new Location(Bukkit.getServer().getWorld("world"), 438, 163, 220));
  438.             }
  439.         }
  440.         // The Tiny Arena
  441.         else if (round == 3)
  442.         {
  443.             if (human)
  444.             {
  445.                 plr.teleport(new Location(Bukkit.getServer().getWorld("world"), 554, 95, 206));
  446.             }
  447.             else
  448.             {
  449.                 plr.teleport(new Location(Bukkit.getServer().getWorld("world"), 554, 100, 206));
  450.             }
  451.         }
  452.         // Water/Lava Arena
  453.         else if (round == 4)
  454.         {
  455.             if (human)
  456.             {
  457.                 plr.teleport(new Location(Bukkit.getServer().getWorld("world"), 554, 107, 210));
  458.             }
  459.             else
  460.             {
  461.                 plr.teleport(new Location(Bukkit.getServer().getWorld("world"), 553, 108, 229));
  462.             }            
  463.         }
  464.         // Scholomance
  465.         else if (round == 5)
  466.         {
  467.             if (human)
  468.             {
  469.                 plr.teleport(new Location(Bukkit.getServer().getWorld("world"), 300, 146, 624));
  470.             }
  471.             else
  472.             {
  473.                 plr.teleport(new Location(Bukkit.getServer().getWorld("world"), 344, 130, 614));
  474.             }        
  475.         }
  476.     }
  477.    
  478.     public void incrementRound()
  479.     {
  480.         // This function sets the round
  481.         if (round == 1)
  482.         {
  483.             round = 2;
  484.         }
  485.         else if (round == 2)
  486.         {
  487.             round = 3;
  488.         }
  489.         else if (round == 3)
  490.         {
  491.             round = 4;
  492.         }
  493.         else if (round == 4)
  494.         {
  495.             round = 5;
  496.         }
  497.         else if (round == 5)
  498.         {
  499.             round = 1;
  500.         }
  501.     }
  502.    
  503.     @EventHandler
  504.     public void onPlayerCrouch(PlayerToggleSneakEvent event)
  505.     {
  506.         // Stop crouching as it prevents name tag showing
  507.         event.setCancelled(true);
  508.     }
  509.    
  510.     @EventHandler
  511.     public void onWeatherChange(WeatherChangeEvent event)
  512.     {
  513.         // Try to stop weather
  514.         getServer().getWorld("world").setWeatherDuration(1);
  515.         event.setCancelled(true);
  516.     }
  517.    
  518.     @EventHandler
  519.     public void onTeleport(PlayerTeleportEvent event)
  520.     {
  521.       Player player = event.getPlayer();
  522.       for (Player p : Bukkit.getOnlinePlayers())
  523.       {
  524.         p.showPlayer(player);
  525.       }
  526.     }
  527. }
Advertisement
Add Comment
Please, Sign In to add comment