stoneharry

Untitled

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