stoneharry

Untitled

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