Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package main;
- import java.util.HashMap;
- import java.util.Random;
- import java.util.Stack;
- import java.util.logging.Logger;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.Location;
- import org.bukkit.block.Block;
- import org.bukkit.entity.Entity;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.block.BlockBreakEvent;
- import org.bukkit.event.block.BlockIgniteEvent;
- import org.bukkit.event.block.BlockPlaceEvent;
- import org.bukkit.event.entity.EntityDamageEvent;
- import org.bukkit.event.entity.FoodLevelChangeEvent;
- import org.bukkit.event.player.PlayerJoinEvent;
- import org.bukkit.event.player.PlayerQuitEvent;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.event.player.PlayerMoveEvent;
- import org.bukkit.event.player.PlayerToggleSneakEvent;
- import org.bukkit.event.weather.WeatherChangeEvent;
- import org.bukkit.inventory.ItemStack;
- /**
- *
- * @author Harry
- */
- public class zombieMain extends JavaPlugin implements Listener
- {
- // Console references the Minecraft console
- public static Logger console = null;
- // The hashmap contains all existing human players
- private static HashMap<String, String> humans;
- // This is only true when the game is in progress
- public static Boolean gameRunning = false;
- // This is true when the map is resetting, it is on before a game has begun
- public static Boolean resetting = true;
- // Debug enables debug messages
- private static final Boolean debug = false;
- // The stack stores changed blocks for rollbacks
- private static Stack blocks_changed = new Stack();
- // The time limit is how long between each round
- public static final long timeLimit = 300000;
- // Last time stores the time when the round begins
- public static long lastTime = 0;
- // Mark zombies is used to decide whether to mark humans with lightning or not
- public static Boolean mark_zombies = false;
- // This handles which round should be started
- public static short round = 1;
- public void onDisable()
- {
- // Dispose of possible large data
- blocks_changed.clear();
- humans.clear();
- console.info("[StonedZombie] Stoneharry - Disabled!");
- }
- public void onEnable()
- {
- console = Logger.getLogger("Minecraft");
- humans = new HashMap<String, String>();
- // Register events
- getServer().getPluginManager().registerEvents(this, this);
- // This runs every ~32 seconds
- getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
- public void run()
- {
- if (gameRunning && !resetting)
- {
- // Get time elapsed
- int result = (int)(System.currentTimeMillis()-lastTime);
- // See if the time elapsed is more than the time limit
- if (result > timeLimit)
- {
- Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "The time limit is up, humans have won!");
- prepareReset();
- }
- else
- {
- // Get time remaining
- int seconds = (int)(timeLimit / 1000) - ( result / 1000 );
- int minutes = 0;
- while (seconds > 59)
- {
- seconds = seconds - 60;
- minutes = minutes + 1;
- }
- if (minutes == 0)
- {
- Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "There is " + String.valueOf(seconds) + " seconds remaining!");
- }
- else
- {
- Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "There is " + String.valueOf(minutes) + " minutes and " + String.valueOf(seconds) + " seconds remaining!");
- }
- // If mark zombies put lightning on humans
- if (mark_zombies)
- {
- Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "The lightning shows where the remaining humans are!");
- for (String name : humans.values())
- {
- Player player = Bukkit.getPlayer(name);
- if (player != null)
- {
- if (player.isOnline())
- {
- Bukkit.getWorld("world").strikeLightningEffect(player.getLocation());
- }
- }
- }
- mark_zombies = false;
- }
- else
- {
- mark_zombies = true;
- }
- }
- // Check for offline humans and remove them if found
- for (String name : humans.values())
- {
- if (Bukkit.getPlayer(name) == null)
- {
- humans.remove(name);
- }
- else
- {
- if (!Bukkit.getPlayer(name).isOnline())
- {
- humans.remove(name);
- }
- }
- }
- }
- }
- }, 700L, 700L);
- console.info("[StonedZombie] Stoneharry - Enabled!");
- }
- @EventHandler
- public void onPlayerMove(PlayerMoveEvent event)
- {
- try
- {
- if (gameRunning)
- {
- Player plr = event.getPlayer();
- // Get nearby players
- for (Entity entity : plr.getNearbyEntities(0.7, 0.7, 0.7))
- {
- if (entity instanceof Player)
- {
- Player target = (Player)entity;
- // Check to see if the moving person is a zombie and the other a human
- if (humans.containsKey(target.getName()) && !humans.containsKey(plr.getName()))
- {
- // The person will no longer be a human
- humans.remove(target.getName());
- // Set the messages and change his name
- Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + plr.getName() + " has infected " + target.getName() + "!");
- target.setDisplayName("[" + ChatColor.RED + "Zombie" + ChatColor.WHITE + "] " + target.getName());
- //target.setPlayerListName("[Zombie] " + target.getName());
- // Check to see if all humans have been found
- if (humans.isEmpty() && gameRunning && !resetting)
- {
- Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "All humans have been caught! The game will end in 10 seconds...");
- prepareReset();
- }
- else
- {
- Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "There are " + String.valueOf(humans.size()) + " humans remaining!");
- }
- }
- }
- }
- }
- else
- {
- // Check to start the game
- Player players[] = Bukkit.getOnlinePlayers();
- if (players.length > 1)
- {
- // Set the time the round started
- lastTime = System.currentTimeMillis();
- // A not very random, random number generator
- Random ran = new Random();
- int x = ran.nextInt(players.length - 1);
- for (int i = 0; i < players.length; i++)
- {
- // Reset inventory
- players[i].getInventory().clear();
- players[i].getInventory().setArmorContents(null);
- // Add items
- ItemStack reward = new ItemStack(3, 20);
- players[i].getInventory().addItem(reward);
- // Correct method unknown (?)
- players[i].updateInventory();
- // If random number = player in online players, set to zombie
- if (i == x)
- {
- Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "The game has begun and " + players[i].getName() + " is the first zombie!");
- players[i].setDisplayName("[" + ChatColor.RED + "Zombie" + ChatColor.WHITE + "] " + players[i].getName());
- //players[i].setPlayerListName("[Zombie] " + players[i].getName());
- handleTeleport(players[i], false);
- }
- // Set to human
- else
- {
- players[i].setDisplayName("[" + ChatColor.GREEN + "Human" + ChatColor.WHITE + "] " + players[i].getName());
- //players[i].setPlayerListName("[Human] " + players[i].getName());
- humans.put(players[i].getName(), players[i].getName());
- handleTeleport(players[i], true);
- }
- }
- // Set global variables
- gameRunning = true;
- resetting = false;
- }
- }
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- // This function just sets the variable to show the round has ended but it is waiting 10 seconds
- public void prepareReset()
- {
- resetting = true;
- getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable()
- {
- public void run()
- {
- resetGame();
- }
- }, 200L);
- }
- @EventHandler
- public void onPlayerJoin(PlayerJoinEvent event)
- {
- // When a player joins, set them to zombie and update them if game is running, teleport.
- if (gameRunning)
- {
- Player p = event.getPlayer();
- p.sendMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "The game is in progress already. You have joined as a Zombie. Catch the humans!");
- //Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + p.getName() + " has joined the Zombies!");
- p.setDisplayName("[" + ChatColor.RED + "Zombie" + ChatColor.WHITE + "] " + p.getName());
- //p.setPlayerListName("[Zombie] " + p.getName());
- ItemStack reward = new ItemStack(3, 20);
- p.getInventory().addItem(reward);
- p.updateInventory();
- }
- handleTeleport(event.getPlayer(), false);
- event.setJoinMessage(null);
- }
- // Prevents leave server message being spammed
- @EventHandler
- public void onPlayerLeave(PlayerQuitEvent event)
- {
- event.setQuitMessage(null);
- }
- // Used to send debug messages if variable is set
- private void DebugMessage(String message, Player plr)
- {
- if (debug)
- {
- console.info(message);
- plr.sendMessage(message);
- }
- }
- // Make players immune to damage
- @EventHandler
- public void onEntityDamage(EntityDamageEvent event)
- {
- event.setCancelled(true);
- }
- // Prevent players getting hungry
- @EventHandler
- public void onFoodLevelChange(FoodLevelChangeEvent event)
- {
- event.setCancelled(true);
- }
- @EventHandler
- public void onBlockPlace(BlockPlaceEvent event)
- {
- // If game is in progress
- if (!resetting)
- {
- // A block has been placed - push that the location should be air (it was air before)
- blocks_changed.push(event.getBlockPlaced().getLocation());
- blocks_changed.push(0);
- blocks_changed.push((byte)0);
- }
- // When no game, no building
- else
- {
- event.setCancelled(true);
- }
- }
- @EventHandler
- public void onBlockBreak(BlockBreakEvent event)
- {
- // If game is not resetting
- if (!resetting)
- {
- // Push the block being deleted for restorating later
- Block b = event.getBlock();
- blocks_changed.push(b.getLocation());
- blocks_changed.push(b.getTypeId());
- blocks_changed.push(b.getData());
- // Does not appear to work
- b.getDrops().clear();
- }
- // when no game, no building
- else
- {
- event.setCancelled(true);
- }
- }
- public void resetGame()
- {
- try
- {
- // Reset the map by rolling back changes
- Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "Restoring arena... This may cause some lag.");
- while (!blocks_changed.isEmpty())
- {
- Byte b = (Byte)blocks_changed.pop();
- Integer mat = (Integer)blocks_changed.pop();
- Location loc = (Location)blocks_changed.pop();
- loc.getBlock().setTypeIdAndData(mat, b, true);
- }
- blocks_changed.clear();
- gameRunning = false;
- resetting = false;
- mark_zombies = false;
- incrementRound();
- Bukkit.broadcastMessage(ChatColor.RED + "[ChaoticUnited] " + ChatColor.AQUA + "Done!");
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- finally
- {
- // Make sure the needed variables change
- gameRunning = false;
- resetting = false;
- mark_zombies = false;
- incrementRound();
- }
- }
- @EventHandler
- public void ignitefire(BlockIgniteEvent event)
- {
- // Try to stop fire
- event.setCancelled(true);
- }
- public void handleTeleport(Player plr, Boolean human)
- {
- // This function teleports humans and zombies depending on the round
- // Sand Arena
- if (round == 1)
- {
- if (human)
- {
- plr.teleport(new Location(Bukkit.getServer().getWorld("world"), 437, 71, 230));
- }
- else
- {
- plr.teleport(new Location(Bukkit.getServer().getWorld("world"), 458, 84, 231));
- }
- }
- // Diamond Arena
- else if (round == 2)
- {
- if (human)
- {
- plr.teleport(new Location(Bukkit.getServer().getWorld("world"), 438, 158, 220));
- }
- else
- {
- plr.teleport(new Location(Bukkit.getServer().getWorld("world"), 438, 163, 220));
- }
- }
- // The Tiny Arena
- else if (round == 3)
- {
- if (human)
- {
- plr.teleport(new Location(Bukkit.getServer().getWorld("world"), 554, 95, 206));
- }
- else
- {
- plr.teleport(new Location(Bukkit.getServer().getWorld("world"), 554, 100, 206));
- }
- }
- // Water/Lava Arena
- else if (round == 4)
- {
- if (human)
- {
- plr.teleport(new Location(Bukkit.getServer().getWorld("world"), 554, 107, 210));
- }
- else
- {
- plr.teleport(new Location(Bukkit.getServer().getWorld("world"), 553, 108, 229));
- }
- }
- }
- public void incrementRound()
- {
- // This function sets the round
- if (round == 1)
- {
- round = 2;
- }
- else if (round == 2)
- {
- round = 3;
- }
- else if (round == 3)
- {
- round = 4;
- }
- else if (round == 4)
- {
- round = 1;
- }
- }
- @EventHandler
- public void onPlayerCrouch(PlayerToggleSneakEvent event)
- {
- // Stop crouching as it prevents name tag showing
- event.setCancelled(true);
- }
- @EventHandler
- public void onWeatherChange(WeatherChangeEvent event)
- {
- // Try to stop weather
- getServer().getWorld("world").setWeatherDuration(1);
- event.setCancelled(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment