Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.List;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.block.Chest;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.entity.EntityType;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.EventPriority;
- import org.bukkit.event.Listener;
- import org.bukkit.event.block.Action;
- import org.bukkit.event.entity.EntityDamageByEntityEvent;
- import org.bukkit.event.entity.PlayerDeathEvent;
- import org.bukkit.event.inventory.InventoryClickEvent;
- import org.bukkit.event.player.PlayerInteractEvent;
- import org.bukkit.event.player.PlayerMoveEvent;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.scheduler.BukkitRunnable;
- import org.bukkit.scheduler.BukkitTask;
- import me.wazup.partygames.Cuboid;
- import me.wazup.partygames.Enums.PlayCounter;
- import me.wazup.partygames.PartyGames;
- import me.wazup.partygames.minigames.MiniGame;
- import me.wazup.partygames.minigames.MiniGameMap;
- public class SurvivalGames extends MiniGame {
- String prefix = ChatColor.DARK_GREEN + "[Surival Games] " + ChatColor.GREEN;
- String[] tutorialMessages;
- List<ItemStack> chestItems;
- int roundDuration;
- Addon plugin;
- public SurvivalGames(Addon plugin) {
- this.plugin = plugin;
- }
- @Override
- public String getName() {
- return "Survival Games";
- }
- @Override
- public Material getRepresentingMaterial() {
- return Material.CHEST;
- }
- @Override
- public String[] getTutorialMessages() {
- return tutorialMessages;
- }
- @Override
- public void handleSetupCommand(Player p, String[] args) {
- if(args.length == 0) {
- p.sendMessage(ChatColor.DARK_GREEN + " -------- " + ChatColor.GREEN + getName() + " Commands " + ChatColor.DARK_GREEN + " -------- ");
- p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + "CreateMap <Name>" + ChatColor.RED + " - Requires selection");
- p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + "DeleteMap <Name>");
- p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + "AddSpawn <Map>");
- p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + "RemoveSpawn <Map>");
- p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + "List");
- return;
- }
- String subCommand = args[0].toLowerCase();
- if(subCommand.equals("createmap")) {
- if(args.length == 1) {
- p.sendMessage(prefix + "You must specify a map name!");
- return;
- }
- String mapName = args[1];
- if(maps.containsKey(mapName.toLowerCase())) {
- p.sendMessage(prefix + "There is already a map with that name!");
- return;
- }
- Cuboid cuboid = PartyGames.api.getPlayerSelection(p);
- if(cuboid == null) {
- p.sendMessage(prefix + "You must select the 2 corners of the map first! Use '/pg wand' to receive the selection wand!");
- return;
- }
- PartyGames.api.removePlayerSelection(p);
- maps.put(mapName.toLowerCase(), new SurvivalGamesMap(mapName, cuboid, new ArrayList<Location>()));
- FileConfiguration config = getConfiguration();
- config.set("Maps." + mapName + ".Map-Cuboid", cuboid.toString());
- saveConfiguration(config);
- p.sendMessage(prefix + "Map has been created successfully!");
- return;
- }
- if(subCommand.equals("deletemap")) {
- if(args.length == 1) {
- p.sendMessage(prefix + "You must specify a map name!");
- return;
- }
- String mapName = args[1];
- if(!maps.containsKey(mapName.toLowerCase())) {
- p.sendMessage(prefix + "Could not find a map with that name!");
- return;
- }
- maps.remove(mapName.toLowerCase());
- FileConfiguration config = getConfiguration();
- config.set("Maps." + mapName, null);
- saveConfiguration(config);
- p.sendMessage(prefix + "Map has been removed successfully!");
- return;
- }
- if(subCommand.equals("addspawn")) {
- if(args.length == 1) {
- p.sendMessage(prefix + "You must specify a map name!");
- return;
- }
- String mapName = args[1];
- if(!maps.containsKey(mapName.toLowerCase())) {
- p.sendMessage(prefix + "Could not find a map with that name!");
- return;
- }
- SurvivalGamesMap map = (SurvivalGamesMap) maps.get(mapName.toLowerCase());
- map.spawnpoints.add(p.getLocation());
- int spawnId = map.spawnpoints.size();
- FileConfiguration config = getConfiguration();
- config.set("Maps." + mapName + ".Spawnpoints." + spawnId, PartyGames.plugin.getStringFromLocation(p.getLocation(), true));
- saveConfiguration(config);
- p.sendMessage(prefix + "Map spawnpoint with id #" + spawnId + " has been set successfully!");
- return;
- }
- if(subCommand.equals("removespawn")) {
- if(args.length == 1) {
- p.sendMessage(prefix + "You must specify a map name!");
- return;
- }
- String mapName = args[1];
- if(!maps.containsKey(mapName.toLowerCase())) {
- p.sendMessage(prefix + "Could not find a map with that name!");
- return;
- }
- SurvivalGamesMap map = (SurvivalGamesMap) maps.get(mapName.toLowerCase());
- if(map.spawnpoints.isEmpty()) {
- p.sendMessage(prefix + "That map does not have any spawnpoints!");
- return;
- }
- int spawnId = map.spawnpoints.size();
- Location lastSpawnpoint = map.spawnpoints.get(spawnId - 1);
- map.spawnpoints.remove(lastSpawnpoint);
- FileConfiguration config = getConfiguration();
- config.set("Maps." + mapName + ".Spawnpoints." + spawnId, null);
- saveConfiguration(config);
- p.sendMessage(prefix + "Map spawnpoint with id #" + spawnId + " has been removed successfully!");
- return;
- }
- if(subCommand.equals("list")) {
- if(maps.isEmpty()) {
- p.sendMessage(prefix + "No maps are loaded!");
- return;
- }
- p.sendMessage(ChatColor.DARK_GREEN + " -------- " + ChatColor.GREEN + getName() + " Maps " + ChatColor.DARK_GREEN + " -------- ");
- for(MiniGameMap map: maps.values()) {
- p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + map.name + ChatColor.DARK_GREEN + " - Is Setup: " + ChatColor.GREEN + map.isSetup());
- }
- return;
- }
- p.sendMessage(prefix + "Unknown command!");
- }
- @Override
- public void load() {
- maps = new HashMap<String, MiniGameMap>();
- FileConfiguration config = getConfiguration();
- boolean save = false;
- if(!config.contains("Tutorial-Messages")) {
- config.set("Tutorial-Messages", Arrays.asList(new String[] {
- "&2 --------- &aSurvival Games &2--------- ",
- "- Get to the middle and loot the chests",
- "to eliminate your enemies!"
- }));
- save = true;
- }
- if(!config.contains("Round-Duration")) {
- config.set("Round-Duration", 120);
- save = true;
- }
- if(!config.contains("Chest-Items")) {
- config.set("Chest-Items", Arrays.asList(new String[] {
- "STONE_SWORD : 1",
- "WOOD_SWORD : 1",
- "WOOD_SWORD : 1",
- "WOOD_SWORD : 1",
- "WOOD_SWORD : 1",
- "WOOD_SWORD : 1",
- "WOOD_SWORD : 1",
- "WOOD_SWORD : 1",
- "WOOD_SWORD : 1",
- "WOOD_SWORD : 1",
- "WOOD_SWORD : 1",
- "WOOD_SWORD : 1",
- "MUSHROOM_SOUP : 1",
- "BOW : 1 : enchant:ARROW_DAMAGE:1 : name:&bHave Fun",
- "LEATHER_HELMET : 1",
- "LEATHER_CHESTPLATE : 1",
- "LEATHER_LEGGINGS : 1",
- "IRON_HELMET : 1",
- "IRON_CHESTPLATE : 1",
- "IRON_LEGGINGS : 1",
- "ARROW : 20",
- "FISHING_ROD : 1",
- "GOLD_HELMET : 1",
- "GOLD_CHESTPLATE : 1",
- "BONE : 1",
- "GOLDEN_APPLE : 1",
- "ARROW : 20",
- "APPLE : 2",
- "APPLE : 2",
- "APPLE : 2"
- }));
- save = true;
- }
- if(save) saveConfiguration(config);
- List<String> temp = config.getStringList("Tutorial-Messages");
- tutorialMessages = new String[temp.size()];
- for(int i = 0; i < tutorialMessages.length; i++) tutorialMessages[i] = ChatColor.translateAlternateColorCodes('&', temp.get(i));
- if(config.getConfigurationSection("Maps") != null && !config.getConfigurationSection("Maps").getKeys(false).isEmpty()) {
- for(String mapName: config.getConfigurationSection("Maps").getKeys(false)) {
- Cuboid cuboid = new Cuboid(config.getString("Maps." + mapName + ".Map-Cuboid"));
- List<Location> spawnpoints = new ArrayList<Location>();
- if(config.getConfigurationSection("Maps." + mapName + ".Spawnpoints") != null && !config.getConfigurationSection("Maps." + mapName + ".Spawnpoints").getKeys(false).isEmpty()) {
- for(String locationId: config.getConfigurationSection("Maps." + mapName + ".Spawnpoints").getKeys(false)) {
- spawnpoints.add(PartyGames.plugin.getLocationFromString(config.getString("Maps." + mapName + ".Spawnpoints." + locationId)));
- }
- }
- maps.put(mapName.toLowerCase(), new SurvivalGamesMap(mapName, cuboid, spawnpoints));
- }
- }
- chestItems = new ArrayList<ItemStack>();
- for(String chestItem: config.getStringList("Chest-Items")) {
- try { chestItems.add(PartyGames.api.getItemStackFromString(chestItem));
- } catch (Exception e) { Bukkit.getConsoleSender().sendMessage(prefix + ChatColor.RED + "Failed to load an item in survival games. The item has the following text: " + chestItem); }
- }
- roundDuration = config.getInt("Round-Duration");
- }
- @Override
- public int getAliveCount(String mapName) {
- return ((SurvivalGamesMap) maps.get(mapName.toLowerCase())).alive.size();
- }
- private class SurvivalGamesMap extends MiniGameMap implements Listener {
- Cuboid mainCuboid;
- List<Location> spawnpoints;
- ArrayList<String> alive;
- String[] topPlayers;
- BukkitTask task;
- List<Location> openedChests;
- boolean allowActivity;
- public SurvivalGamesMap(String name, Cuboid mainCuboid, List<Location> spawnpoints) {
- this.name = name;
- this.mainCuboid = mainCuboid;
- this.spawnpoints = spawnpoints;
- alive = new ArrayList<String>();
- topPlayers = new String[3];
- allowActivity = false;
- openedChests = new ArrayList<Location>();
- Bukkit.getPluginManager().registerEvents(this, plugin);
- }
- public boolean isSetup() {
- return !spawnpoints.isEmpty();
- }
- public void start() {
- for(int i = 0; i < topPlayers.length; i++) topPlayers[i] = null;
- int i = 0;
- for(Player p: arena.getPlayers()) {
- if(i >= spawnpoints.size()) i = 0;
- p.teleport(spawnpoints.get(i++));
- alive.add(p.getName());
- }
- for(Player p: arena.getSpectators()) p.teleport(spawnpoints.get(0));
- this.arena = arena;
- startTask();
- }
- public void cancel(boolean immediate) {
- arena = null;
- allowActivity = false;
- alive.clear();
- mainCuboid.clearEntities();
- openedChests.clear();
- if(task != null) {
- task.cancel();
- task = null;
- }
- }
- public void leave(Player p) {
- if(alive.contains(p.getName())) {
- alive.remove(p.getName());
- if(arena.scoreboard != null) arena.scoreboard.updatePlaceholder(null, arena, PartyGames.plugin, "alive_players", alive.size());
- if(alive.size() == 1) {
- finish();
- }
- }
- }
- private void startTask() {
- task = new BukkitRunnable() {
- int activityTimer = 5;
- int timer = roundDuration;
- public void run() {
- if(activityTimer > 0) {
- activityTimer--;
- if(activityTimer == 4) arena.sendPlayCounterNotification(PlayCounter.READY);
- else if(activityTimer == 2) arena.sendPlayCounterNotification(PlayCounter.SET);
- else if(activityTimer == 0) {
- arena.sendPlayCounterNotification(PlayCounter.GO);
- allowActivity = true;
- }
- } else {
- for(Player p: arena.getPlayers()) p.setLevel(timer);
- timer--;
- if(timer == 0) {
- finish();
- }
- }
- }
- }.runTaskTimer(plugin, 40L, 20L);
- }
- private void eliminatePlayer(Player p) {
- alive.remove(p.getName());
- p.sendMessage(PartyGames.api.getMessage("Player-Lose-Round"));
- p.getInventory().clear();
- p.getInventory().setArmorContents(null);
- String eliminationMessage = PartyGames.api.getMessage("Player-Lose-Round-Announce").replace("%player%", p.getName());
- for(Player x: arena.getAllPlayers()) x.sendMessage(eliminationMessage);
- if(arena.scoreboard != null) arena.scoreboard.updatePlaceholder(null, arena, PartyGames.plugin, "alive_players", alive.size());
- PartyGames.api.MakeSpectator(p, true);
- if(alive.size() < 3) topPlayers[alive.size()] = p.getName(); //If size is 2, then this player is 3rd which is number 2 in []
- if(alive.size() == 1) {
- finish();
- }
- }
- private void finish() {
- if(task != null) {
- task.cancel();
- task = null;
- }
- for(int i = 0; i < alive.size(); i++) if(i < topPlayers.length) topPlayers[i] = alive.get(i);
- PartyGames.api.sendRoundConclusion(arena, topPlayers);
- String winnerName = topPlayers[0];
- final Player winner = Bukkit.getPlayer(winnerName);
- if(winner != null) winner.setHealth(winner.getMaxHealth());
- task = new BukkitRunnable() {
- int timer = 5;
- boolean fireworks = false;
- public void run() {
- timer--;
- fireworks = !fireworks;
- if(fireworks) {
- if(winner != null) PartyGames.plugin.fireWorkEffect(winner, true);
- }
- if(timer == 0) {
- for(Player p: arena.getPlayers()) {
- PartyGames.api.MakeSpectator(p, false);
- }
- arena.finishCurrentMinigame(topPlayers);
- }
- }
- }.runTaskTimer(plugin, 0, 20L);
- }
- public void handleEntityDamageByEntityEvent(EntityDamageByEntityEvent e) {
- if(!e.getEntityType().equals(EntityType.PLAYER)) return;
- Player p = (Player) e.getEntity();
- if(alive.contains(p.getName()) && allowActivity && !e.getDamager().getType().equals(EntityType.FIREWORK)) {
- e.setCancelled(false);
- }
- }
- @EventHandler(priority = EventPriority.HIGH)
- public void onPlayerDeath(PlayerDeathEvent e) {
- Player p = e.getEntity();
- if(alive.contains(p.getName()) && allowActivity) {
- eliminatePlayer(p);
- }
- }
- public void handlePlayerInteractEvent(PlayerInteractEvent e) {
- Player p = e.getPlayer();
- if(alive.contains(p.getName()) && allowActivity) {
- e.setCancelled(false);
- if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
- if(e.getClickedBlock().getType().equals(Material.CHEST) && !openedChests.contains(e.getClickedBlock().getLocation())) {
- openedChests.add(e.getClickedBlock().getLocation());
- Inventory inv = ((Chest) e.getClickedBlock().getState()).getInventory();
- inv.clear();
- int amount = random.nextInt(3) + 4;
- for(int i = 0; i < amount; i++){
- int slot = random.nextInt(27);
- for(int x = 0; x < 2; x++){
- if(inv.getItem(slot) != null) slot = random.nextInt(27);
- else break;
- }
- inv.setItem(slot, chestItems.get(random.nextInt(chestItems.size())));
- }
- }
- }
- }
- }
- @EventHandler
- public void onPlayerMoveEvent(PlayerMoveEvent e){
- Player p = e.getPlayer();
- if(!alive.contains(p.getName()) || allowActivity) return;
- if(e.getFrom().getBlockX() != e.getTo().getBlockX() || e.getFrom().getBlockZ() != e.getTo().getBlockZ()) p.teleport(e.getFrom());
- }
- @EventHandler(priority = EventPriority.HIGH)
- public void onInventoryClickEvent(InventoryClickEvent e){
- Player p = (Player) e.getWhoClicked();
- if(alive.contains(p.getName())) e.setCancelled(false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment