stoneharry

Untitled

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