Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.pgcraft.spectatorplus;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Set;
- import java.util.UUID;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.GameMode;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.OfflinePlayer;
- import org.bukkit.block.Block;
- import org.bukkit.command.CommandSender;
- import org.bukkit.command.ConsoleCommandSender;
- import org.bukkit.configuration.serialization.ConfigurationSerialization;
- import org.bukkit.entity.Damageable;
- import org.bukkit.entity.Player;
- import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.PlayerInventory;
- import org.bukkit.inventory.meta.ItemMeta;
- import org.bukkit.inventory.meta.PotionMeta;
- import org.bukkit.inventory.meta.SkullMeta;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.potion.Potion;
- import org.bukkit.potion.PotionEffect;
- import org.bukkit.potion.PotionEffectType;
- import org.bukkit.potion.PotionType;
- import org.bukkit.scoreboard.DisplaySlot;
- import org.bukkit.scoreboard.Scoreboard;
- import org.bukkit.scoreboard.ScoreboardManager;
- import org.bukkit.scoreboard.Team;
- import org.bukkit.util.Vector;
- @SuppressWarnings("deprecation")
- public class SpectatorPlus extends JavaPlugin {
- protected HashMap <String, PlayerObject> user = new HashMap<String, PlayerObject>();
- protected final static String basePrefix = ChatColor.BLUE + "Mineblockz " + ChatColor.WHITE + "KitPvP";
- protected final static String prefix = ChatColor.DARK_GRAY + "[" + basePrefix + ChatColor.DARK_GRAY + "] " + ChatColor.DARK_GRAY;
- protected double version = 2.0; // Plugin version
- protected ConsoleCommandSender console;
- protected ConfigAccessor setup = null;
- protected ConfigAccessor toggles = null;
- protected ConfigAccessor specs = null;
- protected SpectateCommand commands = null;
- protected ArenasManager arenasManager = null;
- private SpectateAPI api = null;
- // Manage toggles
- protected boolean compass;
- protected Material compassItem;
- protected boolean clock;
- protected Material clockItem;
- protected boolean spectatorsTools;
- protected Material spectatorsToolsItem;
- protected boolean inspector;
- protected Material inspectorItem;
- protected boolean tpToDeathTool, tpToDeathToolShowCause, inspectFromTPMenu, playersHealthInTeleportationMenu, playersLocationInTeleportationMenu, specChat, scoreboard, output, death, seeSpecs, blockCmds, adminBypass, newbieMode, teleportToSpawnOnSpecChangeWithoutLobby, useSpawnCommandToTeleport, enforceArenaBoundary;
- protected SpectatorMode mode = SpectatorMode.ANY;
- protected ScoreboardManager manager = null;
- protected Scoreboard board = null;
- protected Team team = null;
- // Constants for inventory title names
- protected final static String TELEPORTER_ANY_TITLE = ChatColor.BLACK + "Teleporter";
- protected final static String TELEPORTER_ARENA_TITLE = ChatColor.BLACK + "Arena "; // (Prefix only)
- protected final static String ARENA_SELECTOR_TITLE = basePrefix;
- protected final static String PLAYER_STATE_TITLE = ChatColor.RESET + "'s state"; // (Suffix only)
- protected final static String SPEC_TOOLS_TITLE = ChatColor.BLACK + "Spectators' tools";
- // Constants used for identification of the spectators' tools in the listener
- protected final static String TOOL_NORMAL_SPEED_NAME = ChatColor.DARK_AQUA + "Normal speed";
- protected final static String TOOL_SPEED_I_NAME = ChatColor.AQUA + "Speed I";
- protected final static String TOOL_SPEED_II_NAME = ChatColor.AQUA + "Speed II";
- protected final static String TOOL_SPEED_III_NAME = ChatColor.AQUA + "Speed III";
- protected final static String TOOL_SPEED_IV_NAME = ChatColor.AQUA + "Speed IV";
- protected final static String TOOL_NIGHT_VISION_INACTIVE_NAME = ChatColor.GRAY + "Enable night vision";
- protected final static String TOOL_NIGHT_VISION_ACTIVE_NAME = ChatColor.DARK_PURPLE + "Disable night vision";
- protected final static String TOOL_TP_TO_DEATH_POINT_NAME = ChatColor.YELLOW + "Go to your death point";
- /**
- * This method is not meant for public use.
- */
- @Override
- public void onLoad() {
- // Registers the Arena class as a serializable one.
- ConfigurationSerialization.registerClass(Arena.class);
- }
- /**
- * This method is not meant for public use.
- */
- @Override
- public void onEnable() {
- setup = new ConfigAccessor(this, "setup");
- toggles = new ConfigAccessor(this, "toggles");
- specs = new ConfigAccessor(this, "spectators");
- console = getServer().getConsoleSender();
- arenasManager = new ArenasManager(this);
- api = new SpectateAPI(this);
- try {
- mode = SpectatorMode.fromString(setup.getConfig().getString("mode"));
- } catch(IllegalArgumentException e) {
- getLogger().warning("The spectator mode set in the config (" + setup.getConfig().getString("mode") + ") is invalid; using the ANY mode instead!");
- setSpectatorMode(SpectatorMode.ANY);
- }
- // Add players already online to this plugin's database
- for (Player player : getServer().getOnlinePlayers()) {
- user.put(player.getName(), new PlayerObject());
- // Re-enable spectate mode if necessary
- if (specs.getConfig().contains(player.getName())) {
- enableSpectate(player, (CommandSender) player, true);
- }
- }
- reloadConfig(true); // Load config values.
- // Register event listeners
- getServer().getPluginManager().registerEvents(new SpectateListener(this), this);
- new SpectatorManagerTask(this).runTaskTimer(this, 20, 20);
- if(output) {
- console.sendMessage(prefix + "Version " + ChatColor.AQUA + version + ChatColor.GRAY + " is enabled!");
- }
- this.commands = new SpectateCommand(this);
- this.getCommand("spectate").setExecutor(commands);
- this.getCommand("spec").setExecutor(commands);
- SpectateCompleter completer = new SpectateCompleter(this);
- this.getCommand("spectate").setTabCompleter(completer);
- this.getCommand("spec").setTabCompleter(completer);
- }
- /**
- * This method is not meant for public use.
- */
- @Override
- public void onDisable() {
- if(output) {
- console.sendMessage(prefix + "Disabling...");
- }
- for (Player player : getServer().getOnlinePlayers()) {
- for (Player target : getServer().getOnlinePlayers()) {
- target.showPlayer(player);
- }
- // Disable spectate mode temporarily.
- if (getPlayerData(player).spectating) {
- disableSpectate(player, console, true, true);
- }
- }
- // Just in case
- arenasManager.save();
- }
- // ---------------
- // METHODS
- // ---------------
- /**
- * Teleports the player to the global lobby location.
- *
- * @param player
- * @return true if the player was teleported, false else.
- */
- protected boolean spawnPlayer(Player player) {
- player.setFireTicks(0);
- if (setup.getConfig().getBoolean("active")) {
- Location where = new Location(getServer().getWorld(setup.getConfig().getString("world")), setup.getConfig().getDouble("xPos"), setup.getConfig().getDouble("yPos"), setup.getConfig().getDouble("zPos"));
- Location aboveWhere = new Location(getServer().getWorld(setup.getConfig().getString("world")), setup.getConfig().getDouble("xPos"), setup.getConfig().getDouble("yPos") + 1, setup.getConfig().getDouble("zPos"));
- Location belowWhere = new Location(getServer().getWorld(setup.getConfig().getString("world")), setup.getConfig().getDouble("xPos"), setup.getConfig().getDouble("yPos") - 1, setup.getConfig().getDouble("zPos"));
- if (where.getBlock().getType() != Material.AIR || aboveWhere.getBlock().getType() != Material.AIR || (belowWhere.getBlock().getType() == Material.AIR || belowWhere.getBlock().getType() == Material.LAVA || belowWhere.getBlock().getType() == Material.WATER)) {
- while (where.getBlock().getType() != Material.AIR || aboveWhere.getBlock().getType() != Material.AIR || (belowWhere.getBlock().getType() == Material.AIR || belowWhere.getBlock().getType() == Material.LAVA || belowWhere.getBlock().getType() == Material.WATER)) {
- where.setY(where.getY()+1);
- aboveWhere.setY(aboveWhere.getY()+1);
- belowWhere.setY(belowWhere.getY()+1);
- if (where.getY() > getServer().getWorld(setup.getConfig().getString("world")).getHighestBlockYAt(where)) {
- where.setY(where.getY()-2);
- aboveWhere.setY(aboveWhere.getY()-2);
- belowWhere.setY(belowWhere.getY()-2);
- }
- }
- }
- getPlayerData(player).teleporting = true;
- player.teleport(where);
- getPlayerData(player).teleporting = false;
- return true;
- } else {
- if(teleportToSpawnOnSpecChangeWithoutLobby) {
- if(useSpawnCommandToTeleport) {
- if(getServer().getPluginCommand("spawn") != null) {
- return player.performCommand("spawn");
- }
- return false;
- }
- else {
- return player.teleport(player.getWorld().getSpawnLocation(), TeleportCause.PLUGIN);
- }
- }
- return false;
- }
- }
- /**
- * Generates a player head ItemStack to be displayed in the teleportation GUI.
- *
- * @param player The player
- * @param hidden True if the player is a displayed hidden player.
- * @return The head.
- */
- protected ItemStack generatePlayerHeadItem(Player player, Player inventoryViewer, boolean hidden) {
- ItemStack playerhead = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
- SkullMeta meta = (SkullMeta)playerhead.getItemMeta();
- meta.setOwner(player.getName());
- if(hidden) meta.setDisplayName(ChatColor.DARK_GRAY + "[HIDDEN] " + ChatColor.RESET + player.getDisplayName());
- else meta.setDisplayName(ChatColor.RESET + player.getDisplayName());
- List<String> lore = new ArrayList<String>();
- if(playersHealthInTeleportationMenu || playersLocationInTeleportationMenu) {
- if(playersHealthInTeleportationMenu) {
- lore.add(ChatColor.GRAY + "" + ((int) ((Damageable) player).getHealth()) + " " + ChatColor.WHITE + "hearts" + ChatColor.GRAY + " out of 20");
- }
- if(playersLocationInTeleportationMenu) {
- if(!player.getWorld().equals(inventoryViewer.getWorld())) {
- lore.add(ChatColor.GRAY + "You and " + player.getName() + " are not in the same world.");
- }
- else {
- int distance = (int) player.getLocation().distance(inventoryViewer.getLocation());
- String direction = null;
- // The angle between a vector pointing to the North and a vector pointing
- // from the spectator to the player, converted in degrees, -180 to have 0° for North.
- double angle = (new Vector(0, 0, -1).angle(player.getLocation().toVector().setY(0).subtract(inventoryViewer.getLocation().toVector().setY(0)).multiply(-1)) * 180/Math.PI - 180) % 360;
- if(angle < 0) angle += 360.0;
- // The calculated angle is the same for two positions symmetric of each other
- // relative to the N-S axis.
- // This lead to "west" displayed for both east and west.
- if(inventoryViewer.getLocation().getX() < player.getLocation().getX()
- && 202.5 <= angle && angle < 337.5) {
- angle -= 180.0;
- }
- if (0 <= angle && angle < 22.5) {
- direction = "North";
- } else if (22.5 <= angle && angle < 67.5) {
- direction = "North-east";
- } else if (67.5 <= angle && angle < 112.5) {
- direction = "East";
- } else if (112.5 <= angle && angle < 157.5) {
- direction = "South-east";
- } else if (157.5 <= angle && angle < 202.5) {
- direction = "South";
- } else if (202.5 <= angle && angle < 247.5) {
- direction = "South-west";
- } else if (247.5 <= angle && angle < 292.5) {
- direction = "West";
- } else if (292.5 <= angle && angle < 337.5) {
- direction = "North-west";
- } else if (337.5 <= angle && angle <= 360.0) {
- direction = "North";
- }
- if(direction != null) {
- lore.add(ChatColor.WHITE + direction + ", " + distance + " meters");
- }
- else {
- lore.add(ChatColor.WHITE + "" + distance + " meters");
- }
- }
- }
- lore.add(" "); // separator
- }
- lore.add(ChatColor.GRAY+""+ChatColor.ITALIC+"Left click"+ ChatColor.DARK_GRAY+ChatColor.ITALIC +" to be teleported");
- if(this.inspectFromTPMenu) {
- lore.add(ChatColor.GRAY+""+ChatColor.ITALIC+"Right click"+ ChatColor.DARK_GRAY+ChatColor.ITALIC +" to see inventory");
- }
- meta.setLore(lore);
- playerhead.setItemMeta(meta);
- return playerhead;
- }
- /**
- * Opens the player head GUI, to allow spectators to choose a player to teleport to.
- *
- * @param spectator The GUI will be open for this spectator.
- * @param region The UUID of the arena to use to choose the players to display on the GUI. Null if there isn't any arena set for this player.
- */
- protected void showGUI(Player spectator, UUID region) {
- if (mode == SpectatorMode.ARENA && region == null) {
- if(output) {
- spectator.sendMessage(prefix + "Pick an arena first using the arena selector!");
- }
- return;
- }
- Inventory gui = null;
- LinkedList<Player> displayedSpectators = new LinkedList<Player>();
- LinkedList<Player> displayedSpectatorsHidden = new LinkedList<Player>();
- for (Player player : getServer().getOnlinePlayers()) {
- if (mode == SpectatorMode.ARENA) {
- if (region == null) {
- if(output) {spectator.sendMessage(prefix + "Pick an arena first using the arena selector!");}
- return;
- } else {
- Location where = player.getLocation();
- Arena currentArena = arenasManager.getArena(region);
- int pos1y = currentArena.getCorner1().getBlockY();
- int pos2y = currentArena.getCorner2().getBlockY();
- int pos1x = currentArena.getCorner1().getBlockX();
- int pos2x = currentArena.getCorner2().getBlockX();
- int pos1z = currentArena.getCorner1().getBlockZ();
- int pos2z = currentArena.getCorner2().getBlockZ();
- // pos1 should have the highest co-ords of the arena, pos2 the lowest
- if (!getPlayerData(player).spectating) {
- if (Math.floor(where.getY()) < Math.floor(pos1y) && Math.floor(where.getY()) > Math.floor(pos2y)) {
- if (Math.floor(where.getX()) < pos1x && Math.floor(where.getX()) > pos2x) {
- if (Math.floor(where.getZ()) < pos1z && Math.floor(where.getZ()) > pos2z) {
- if(getPlayerData(player).hideFromTp) {
- displayedSpectatorsHidden.add(player);
- } else {
- displayedSpectators.add(player);
- }
- }
- }
- }
- }
- }
- }
- else if(mode == SpectatorMode.ANY
- || (mode == SpectatorMode.WORLD && player.getWorld().equals(spectator.getWorld()))) {
- if (!getPlayerData(player).hideFromTp && !getPlayerData(player).spectating) {
- displayedSpectators.add(player);
- }
- // Admins will still be able to see players who have used '/spec hide':
- else if (spectator.hasPermission("spectate.admin.hide.see") && !getPlayerData(player).spectating) {
- displayedSpectatorsHidden.add(player);
- }
- }
- }
- Integer inventorySize = (int) Math.ceil(Double.valueOf(displayedSpectators.size()+displayedSpectatorsHidden.size())/9) * 9;
- if(inventorySize == 0) {
- inventorySize = 9; // Avoids an empty inventory.
- }
- if(mode == SpectatorMode.ARENA) {
- gui = Bukkit.getServer().createInventory(spectator, inventorySize, TELEPORTER_ARENA_TITLE + ChatColor.ITALIC + arenasManager.getArena(region).getName());
- }
- else { // WORLD and ANY
- gui = Bukkit.getServer().createInventory(spectator, inventorySize, TELEPORTER_ANY_TITLE);
- }
- // Display hidden players first (people who have used '/spec hide')
- for(Player displayedSpectatorHidden : displayedSpectatorsHidden) {
- gui.addItem(generatePlayerHeadItem(displayedSpectatorHidden, spectator, true));
- }
- // Display normal players
- for(Player displayedSpectator : displayedSpectators) {
- gui.addItem(generatePlayerHeadItem(displayedSpectator, spectator, false));
- }
- spectator.openInventory(gui);
- }
- /**
- * Shows the arena selection GUI, full of books with the name of valid arenas.
- *
- * @param spectator The GUI will be open for this spectator.
- */
- protected void showArenaGUI(Player spectator) {
- Inventory gui = Bukkit.getServer().createInventory(spectator, 27, ARENA_SELECTOR_TITLE);
- for (Arena arena : arenasManager.getArenas()) {
- ItemStack arenaBook = new ItemStack(Material.BOOK, 1);
- ItemMeta meta = (ItemMeta)arenaBook.getItemMeta();
- meta.setDisplayName(arena.getName());
- arenaBook.setItemMeta(meta);
- gui.addItem(arenaBook);
- }
- spectator.openInventory(gui);
- }
- /**
- * Shows a representation of the inventory, the armour, the health, the XP, the potion effects
- * and the feed state of the player.
- *
- * @param spectator The GUI will be open for this spectator.
- * @param inventoryOwner The analyzed player is this player.
- */
- protected void showPlayerInventoryGUI(Player spectator, Player inventoryOwner) {
- PlayerInventory inventory = inventoryOwner.getInventory();
- // Remove item name from the inventory separator.
- ItemStack separator = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 7);
- ItemMeta separatorMeta = separator.getItemMeta();
- separatorMeta.setDisplayName(ChatColor.RESET+"");
- separator.setItemMeta(separatorMeta);
- // + 18: a separator row, and a row with armor, XP, potion effects, health and feed level.
- Inventory gui = Bukkit.getServer().createInventory(spectator, inventory.getSize() + 18, (inventoryOwner.getDisplayName().length() > 22) ? inventoryOwner.getName() : inventoryOwner.getDisplayName() + PLAYER_STATE_TITLE);
- ItemStack[] GUIContent = gui.getContents();
- // Player's inventory
- // The hotbar is 0-8
- // The inventory is 9-35
- for(int i = 9; i < inventory.getSize(); i++) {
- GUIContent[i - 9] = inventory.getItem(i);
- }
- for(int i = 0; i < 9; i++) {
- GUIContent[i + 27] = inventory.getItem(i);
- }
- // Separator
- for(int i = inventory.getSize(); i < inventory.getSize() + 9; i++) {
- GUIContent[i] = separator;
- }
- // Armor
- GUIContent[inventory.getSize() + 9] = inventory.getHelmet();
- GUIContent[inventory.getSize() + 10] = inventory.getChestplate();
- GUIContent[inventory.getSize() + 11] = inventory.getLeggings();
- GUIContent[inventory.getSize() + 12] = inventory.getBoots();
- // Separator
- GUIContent[inventory.getSize() + 13] = separator;
- // XP
- if(inventoryOwner.getLevel() > 0) {
- GUIContent[inventory.getSize() + 14] = new ItemStack(Material.EXP_BOTTLE, inventoryOwner.getLevel());
- }
- else {
- GUIContent[inventory.getSize() + 14] = new ItemStack(Material.EXP_BOTTLE, 1);
- }
- ItemMeta xpMeta = GUIContent[inventory.getSize() + 14].getItemMeta();
- xpMeta.setDisplayName(ChatColor.GREEN +""+ ChatColor.BOLD + "Experience");
- List<String> lore = new ArrayList<String>();
- lore.add(ChatColor.DARK_GRAY +""+ ChatColor.ITALIC + "Level " + ChatColor.GRAY + ChatColor.ITALIC + inventoryOwner.getLevel() + ChatColor.DARK_GRAY + ChatColor.ITALIC + " (" + ChatColor.GRAY + ChatColor.ITALIC + ((int) Math.floor(inventoryOwner.getExp()*100)) + ChatColor.DARK_GRAY + ChatColor.ITALIC + "% towards level " + ChatColor.GRAY + ChatColor.ITALIC + (inventoryOwner.getLevel()+1) + ChatColor.DARK_GRAY + ChatColor.ITALIC + ")");
- xpMeta.setLore(lore);
- GUIContent[inventory.getSize() + 14].setItemMeta(xpMeta);
- // Potion effects
- if(inventoryOwner.getActivePotionEffects().size() == 0) {
- GUIContent[inventory.getSize() + 15] = new ItemStack(Material.GLASS_BOTTLE, 1);
- ItemMeta meta = GUIContent[inventory.getSize() + 15].getItemMeta();
- meta.setLore(Arrays.asList(ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + "No active effects"));
- GUIContent[inventory.getSize() + 15].setItemMeta(meta);
- }
- else {
- GUIContent[inventory.getSize() + 15] = new Potion(PotionType.FIRE_RESISTANCE).toItemStack(1);
- PotionMeta effectsMeta = (PotionMeta) GUIContent[inventory.getSize() + 15].getItemMeta();
- effectsMeta.clearCustomEffects();
- lore = new ArrayList<String>();
- lore.add(ChatColor.GRAY +""+ ChatColor.ITALIC + inventoryOwner.getActivePotionEffects().size() + ChatColor.DARK_GRAY + ChatColor.ITALIC + " active effects");
- xpMeta.setLore(lore);
- for(PotionEffect effect : inventoryOwner.getActivePotionEffects()) {
- effectsMeta.addCustomEffect(effect, true);
- }
- GUIContent[inventory.getSize() + 15].setItemMeta(effectsMeta);
- }
- ItemMeta effectsMeta = GUIContent[inventory.getSize() + 15].getItemMeta();
- effectsMeta.setDisplayName(ChatColor.DARK_PURPLE + "" + ChatColor.BOLD + "Potion effects ");
- GUIContent[inventory.getSize() + 15].setItemMeta(effectsMeta);
- // Health
- if(((Damageable) inventoryOwner).getHealth() > 0) {
- GUIContent[inventory.getSize() + 16] = new ItemStack(Material.GOLDEN_APPLE, (int) Math.ceil(((Damageable) inventoryOwner).getHealth()));
- ItemMeta healthMeta = GUIContent[inventory.getSize() + 16].getItemMeta();
- healthMeta.setDisplayName(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Health ");
- lore = new ArrayList<String>();
- lore.add(ChatColor.GRAY +"" + ChatColor.ITALIC + (int) Math.ceil(((Damageable) inventoryOwner).getHealth()) + ChatColor.DARK_GRAY + ChatColor.ITALIC + "/20");
- healthMeta.setLore(lore);
- GUIContent[inventory.getSize() + 16].setItemMeta(healthMeta);
- }
- // Food level
- if(inventoryOwner.getFoodLevel() > 0) {
- GUIContent[inventory.getSize() + 17] = new ItemStack(Material.COOKIE, inventoryOwner.getFoodLevel());
- ItemMeta foodMeta = GUIContent[inventory.getSize() + 17].getItemMeta();
- foodMeta.setDisplayName(ChatColor.GRAY + "" + ChatColor.BOLD + "Food");
- lore = new ArrayList<String>();
- lore.add(ChatColor.DARK_GRAY +""+ ChatColor.ITALIC + "Food level: " + ChatColor.GRAY + ChatColor.ITALIC + inventoryOwner.getFoodLevel() + ChatColor.DARK_GRAY + ChatColor.ITALIC + "/20");
- lore.add(ChatColor.DARK_GRAY +""+ ChatColor.ITALIC + "Saturation: " + ChatColor.GRAY + ChatColor.ITALIC + inventoryOwner.getSaturation());
- foodMeta.setLore(lore);
- GUIContent[inventory.getSize() + 17].setItemMeta(foodMeta);
- }
- gui.setContents(GUIContent);
- spectator.openInventory(gui);
- }
- protected void showSpectatorsOptionsGUI(Player spectator) {
- Inventory gui = Bukkit.getServer().createInventory(spectator, 9, SPEC_TOOLS_TITLE);
- ItemStack[] GUIContent = gui.getContents();
- // Retrieves the current speed level, and the other enabled effects
- // 0 = no speed; 1 = speed I, etc.
- Integer speedLevel = 0;
- Boolean nightVisionActive = false;
- for(PotionEffect effect : spectator.getActivePotionEffects()) {
- if(effect.getType().equals(PotionEffectType.SPEED)) {
- speedLevel = effect.getAmplifier() + 1; // +1 because Speed I = amplifier 0.
- }
- else if(effect.getType().equals(PotionEffectType.NIGHT_VISION)) {
- nightVisionActive = true;
- }
- }
- List<String> activeLore = new ArrayList<String>();
- activeLore.add("" + ChatColor.GRAY + ChatColor.ITALIC + "Active");
- // If a death location is registered for this player, the position of the "night vision" tool
- // is not the same (6 with death point registered; 8 without).
- // That's why this is defined here, not below.
- // If the "tp to death" tool is disabled, the death location is not set. So it's useless to
- // check this here.
- Location deathPoint = getPlayerData(spectator).deathLocation;
- // Normal speed
- ItemStack normalSpeed = new ItemStack(Material.STRING);
- ItemMeta meta = normalSpeed.getItemMeta();
- meta.setDisplayName(TOOL_NORMAL_SPEED_NAME);
- if(speedLevel == 0) {
- meta.setLore(activeLore);
- }
- normalSpeed.setItemMeta(meta);
- GUIContent[0] = normalSpeed;
- // Speed I
- ItemStack speedI = new ItemStack(Material.POTION);
- meta = speedI.getItemMeta();
- meta.setDisplayName(TOOL_SPEED_I_NAME);
- if(speedLevel == 1) {
- meta.setLore(activeLore);
- }
- speedI.setItemMeta(meta);
- GUIContent[1] = speedI;
- // Speed II
- ItemStack speedII = new ItemStack(Material.POTION, 2);
- meta = speedII.getItemMeta();
- meta.setDisplayName(TOOL_SPEED_II_NAME);
- if(speedLevel == 2) {
- meta.setLore(activeLore);
- }
- speedII.setItemMeta(meta);
- GUIContent[2] = speedII;
- // Speed III
- ItemStack speedIII = new ItemStack(Material.POTION, 3);
- meta = speedIII.getItemMeta();
- meta.setDisplayName(TOOL_SPEED_III_NAME);
- if(speedLevel == 3) {
- meta.setLore(activeLore);
- }
- speedIII.setItemMeta(meta);
- GUIContent[3] = speedIII;
- // Speed IV
- ItemStack speedIV = new ItemStack(Material.POTION, 4);
- meta = speedIV.getItemMeta();
- meta.setDisplayName(TOOL_SPEED_IV_NAME);
- if(speedLevel == 4) {
- meta.setLore(activeLore);
- }
- speedIV.setItemMeta(meta);
- GUIContent[4] = speedIV;
- // Night vision
- ItemStack nightVision = new ItemStack(Material.EYE_OF_ENDER);
- meta = nightVision.getItemMeta();
- if(nightVisionActive) {
- nightVision.setType(Material.ENDER_PEARL);
- meta.setDisplayName(TOOL_NIGHT_VISION_ACTIVE_NAME);
- }
- else {
- meta.setDisplayName(TOOL_NIGHT_VISION_INACTIVE_NAME);
- }
- nightVision.setItemMeta(meta);
- if(deathPoint == null) { // No "TP to death point" tool: position #8.
- GUIContent[8] = nightVision;
- }
- else {
- GUIContent[6] = nightVision;
- }
- // Teleportation to the death point
- if(deathPoint != null) {
- ItemStack tpToDeathPoint = new ItemStack(Material.NETHER_STAR);
- meta = tpToDeathPoint.getItemMeta();
- meta.setDisplayName(TOOL_TP_TO_DEATH_POINT_NAME);
- // The death message is never set if it is disabled: check useless (same as above).
- if(getPlayerData(spectator).lastDeathMessage != null) {
- List<String> lore = new ArrayList<String>();
- lore.add("" + ChatColor.GRAY + getPlayerData(spectator).lastDeathMessage);
- meta.setLore(lore);
- }
- tpToDeathPoint.setItemMeta(meta);
- GUIContent[8] = tpToDeathPoint;
- }
- gui.setContents(GUIContent);
- spectator.openInventory(gui);
- }
- /**
- * Teleports the spectator to the player they have chosen using "/spec p <target>"
- *
- * @param spectator The spectator to teleport.
- * @param target The spectator will be teleported at the current location of this player.
- */
- protected void choosePlayer(Player spectator, Player target) {
- spectator.teleport(target);
- if(output) {
- spectator.sendMessage(prefix + "Teleported you to " + ChatColor.AQUA + target.getName());
- }
- }
- /**
- * Checks for problems and enables spectator mode for spectator, on behalf of sender.
- *
- * @param spectator The player that will be a spectator.
- * @param sender The sender of the /spec on [player] command.
- */
- protected void enableSpectate(Player spectator, CommandSender sender) {
- enableSpectate(spectator, sender, false);
- }
- /**
- * Checks for problems and enables spectator mode for spectator, on behalf of sender.
- *
- * @param spectator The player that will be a spectator.
- * @param sender The sender of the /spec on [player] command.
- * @param silent Will not output any messages - useful when using the API or command blocks.
- *
- * @since 2.0
- */
- protected void enableSpectate(Player spectator, CommandSender sender, boolean silent) {
- enableSpectate(spectator, sender, silent, false);
- }
- /**
- * Checks for problems and enables spectator mode for spectator, on behalf of sender.
- *
- * @param spectator The player that will be a spectator.
- * @param sender The sender of the /spec on [player] command.
- * @param silent Will not output any messages - useful when using the API or command blocks.
- * @param worldChange Was the enable spectate caused by a world change?
- *
- * @since 2.0
- */
- protected void enableSpectate(Player spectator, CommandSender sender, boolean silent, boolean worldChange) {
- if (user.get(spectator.getName()).spectating) {
- if (!silent) {
- // Spectator mode was already on
- if (sender instanceof Player && spectator.getName().equals(sender.getName())) {
- spectator.sendMessage(prefix + ChatColor.GRAY + "You are already spectating!");
- }
- else {
- sender.sendMessage(prefix + ChatColor.AQUA + spectator.getDisplayName() + ChatColor.GRAY + " is already spectating!");
- }
- }
- }
- else {
- // Hide them from everyone
- for (Player target : getServer().getOnlinePlayers()) {
- if(seeSpecs && getPlayerData(target).spectating) {
- spectator.showPlayer(target);
- }
- else {
- target.hidePlayer(spectator); // Hide the spectator from non-specs: if seeSpecs mode is off and the target isn't spectating
- }
- }
- // Gamemode, 'ghost' and inventory
- getPlayerData(spectator).oldGameMode = spectator.getGameMode();
- spectator.setGameMode(GameMode.ADVENTURE);
- savePlayerInv(spectator);
- getPlayerData(spectator).effects = spectator.getActivePotionEffects();
- for (PotionEffect pe : spectator.getActivePotionEffects()) {
- spectator.removePotionEffect(pe.getType());
- }
- spectator.setAllowFlight(true);
- spectator.setFoodLevel(20);
- spectator.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 15), true);
- // Disable interaction
- getPlayerData(spectator).spectating = true;
- updateSpectatorInventory(spectator);
- // Set the prefix in the tab list if the toggle is on
- if (scoreboard) {
- if (spectator.getScoreboard() != null) getPlayerData(spectator).oldScoreboard = spectator.getScoreboard(); else user.get(spectator.getName()).oldScoreboard = getServer().getScoreboardManager().getMainScoreboard();
- spectator.setScoreboard(board);
- team.addPlayer(spectator);
- }
- // Teleport them to the global lobby (not if world change)
- if (!worldChange) spawnPlayer(spectator);
- // Manage messages if spectator mode was enabled
- if (!silent) {
- if (sender instanceof Player && spectator.getName().equals(sender.getName())) {
- if(output) {
- spectator.sendMessage(prefix + ChatColor.GRAY + "Spectator mode " + ChatColor.AQUA + "enabled");
- }
- }
- else if (sender instanceof Player && !spectator.getName().equals(sender.getName())) {
- if(output) {
- spectator.sendMessage(prefix + ChatColor.GRAY + "Spectator mode " + ChatColor.AQUA + "enabled" + ChatColor.GRAY + " by " + ChatColor.AQUA + ((Player) sender).getDisplayName());
- }
- sender.sendMessage(prefix + ChatColor.GRAY + "Spectator mode " + ChatColor.AQUA + "enabled" + ChatColor.GRAY + " for " + ChatColor.AQUA + spectator.getDisplayName());
- }
- else {
- if(output) {
- spectator.sendMessage(prefix + "Spectator mode " + ChatColor.AQUA + "enabled" + ChatColor.GRAY + " by " + ChatColor.DARK_RED + "Console");
- }
- sender.sendMessage(prefix + "Spectator mode " + ChatColor.AQUA + "enabled" + ChatColor.GRAY + " for " + ChatColor.AQUA + spectator.getDisplayName());
- }
- }
- specs.getConfig().set(spectator.getName(), true);
- specs.saveConfig();
- }
- }
- /**
- * Checks for problems and disables spectator mode for spectator, on behalf of sender.
- * Convenience method for {@link #disableSpectate(Player spectator, CommandSender sender, boolean silent)}
- *
- * @param spectator The spectator that will be a normal player.
- * @param sender The sender of the /spec off [player] command.
- */
- protected void disableSpectate(Player spectator, CommandSender sender) {
- disableSpectate(spectator, sender, false);
- }
- /**
- * Checks for problems and disables spectator mode for spectator, on behalf of sender.
- * Convenience method for {@link #disableSpectate(Player spectator, CommandSender sender, boolean silent, boolean temp)}
- *
- * @param spectator The spectator that will be a normal player.
- * @param sender The sender of the /spec off [player] command.
- * @param silent Will not output any messages - useful when using the API or command blocks.
- *
- * @since 2.0
- */
- protected void disableSpectate(Player spectator, CommandSender sender, boolean silent) {
- disableSpectate(spectator, sender, silent, false);
- }
- /**
- * Checks for problems and disables spectator mode for spectator, on behalf of sender.
- *
- * @param spectator The spectator that will be a normal player.
- * @param sender The sender of the /spec off [player] command.
- * @param silent Will not output any messages - useful when using the API or command blocks.
- * @param temp If true, the next time the player re-logs, spectator mode will be re-enabled.
- *
- * @since 2.0
- */
- protected void disableSpectate(Player spectator, CommandSender sender, boolean silent, boolean temp) {
- disableSpectate(spectator, sender, silent, temp, false);
- }
- /**
- * Checks for problems and disables spectator mode for spectator, on behalf of sender.
- *
- * @param spectator The spectator that will be a normal player.
- * @param sender The sender of the /spec off [player] command.
- * @param silent Will not output any messages - useful when using the API or command blocks.
- * @param temp If true, the next time the player re-logs, spectator mode will be re-enabled.
- * @param worldChange Was the enable spectate caused by a world change?
- *
- * @since 2.0
- */
- protected void disableSpectate(Player spectator, CommandSender sender, boolean silent, boolean temp, boolean worldChange) {
- if (getPlayerData(spectator).spectating) {
- // Show them to everyone
- for (Player target : getServer().getOnlinePlayers()) {
- if (seeSpecs && getPlayerData(target).spectating) {
- spectator.hidePlayer(target);
- }
- target.showPlayer(spectator);
- }
- // Allow interaction
- getPlayerData(spectator).spectating = false;
- spectator.setAllowFlight(false);
- spectator.setGameMode(getPlayerData(spectator).oldGameMode);
- loadPlayerInv(spectator);
- // Restore effects
- spectator.removePotionEffect(PotionEffectType.INVISIBILITY);
- spectator.removePotionEffect(PotionEffectType.SPEED);
- spectator.removePotionEffect(PotionEffectType.WATER_BREATHING);
- spectator.removePotionEffect(PotionEffectType.NIGHT_VISION);
- spectator.addPotionEffects(getPlayerData(spectator).effects);
- spectator.setFlySpeed(0.1f);
- // Remove from spec team
- if (scoreboard) {
- if(getPlayerData(spectator).oldScoreboard != null) spectator.setScoreboard(getPlayerData(spectator).oldScoreboard);
- team.removePlayer(spectator);
- }
- if (!worldChange) {
- removePlayerFromArena(spectator, true); // Clear the arena they were spectating in
- spawnPlayer(spectator); // Teleport to spawn
- }
- if (!silent) {
- if (sender instanceof Player && spectator.getName().equals(sender.getName())) {
- if(output) {
- spectator.sendMessage(prefix + ChatColor.GRAY + "Spectator mode " + ChatColor.AQUA + "disabled");
- }
- }
- else if (sender instanceof Player && !spectator.getName().equals(sender.getName())) {
- if(output) {
- spectator.sendMessage(prefix + ChatColor.GRAY + "Spectator mode " + ChatColor.AQUA + "disabled" + ChatColor.GRAY + " by " + ChatColor.AQUA + ((Player) sender).getDisplayName());
- }
- sender.sendMessage(prefix + ChatColor.GRAY + "Spectator mode " + ChatColor.AQUA + "disabled" + ChatColor.GRAY + " for " + ChatColor.AQUA + spectator.getDisplayName());
- }
- else {
- if(output) {
- spectator.sendMessage(prefix + "Spectator mode " + ChatColor.AQUA + "disabled" + ChatColor.GRAY + " by " + ChatColor.DARK_RED + "Console");
- }
- sender.sendMessage(prefix + "Spectator mode " + ChatColor.AQUA + "disabled" + ChatColor.GRAY + " for " + ChatColor.AQUA + spectator.getDisplayName());
- }
- }
- if (!temp) {
- specs.getConfig().set(spectator.getName(), null);
- specs.saveConfig();
- }
- }
- else {
- // Spectate mode wasn't on
- if (!silent) {
- if (sender instanceof Player && spectator.getName().equals(sender.getName())) {
- spectator.sendMessage(prefix + "You aren't spectating!");
- }
- else {
- sender.sendMessage(prefix + ChatColor.AQUA + spectator.getDisplayName() + ChatColor.GRAY + " isn't spectating!");
- }
- }
- }
- }
- protected void reloadConfig(boolean hardReload) {
- // 'hardReload': true/false; a hard reload will reload the config values from file.
- if (hardReload) {
- setup.saveDefaultConfig();
- toggles.saveDefaultConfig();
- specs.saveDefaultConfig();
- setup.reloadConfig();
- toggles.reloadConfig();
- specs.reloadConfig();
- Set<String> togglesND = toggles.getConfig().getKeys(true); // ND = no defaults
- // Update config & add default values in
- if (togglesND.contains("version") && toggles.getConfig().getDouble("version")<version) {
- console.sendMessage(prefix+"Updating to version "+ChatColor.AQUA+version+ChatColor.GRAY+"...");
- } else if (togglesND.contains("version") && toggles.getConfig().getDouble("version")>version) { // Placeholder for version checking in future...
- console.sendMessage(ChatColor.GRAY+"Version "+ChatColor.AQUA+toggles.getConfig().getDouble("version")+ChatColor.GRAY+" available!");
- }
- // Compass: true/false
- if (!togglesND.contains("compass")) {
- toggles.getConfig().set("compass", true);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"compass: true"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // -> Compass item: <item name>
- if (!togglesND.contains("compassItem")) {
- toggles.getConfig().set("compassItem", "compass");
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"compassItem: compass"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // Arena Selector: true/false
- if (!togglesND.contains("arenaclock")) {
- toggles.getConfig().set("arenaclock", true);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"arenaclock: true"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // -> Arena selector item: <item name>
- if (!togglesND.contains("clockItem")) {
- toggles.getConfig().set("clockItem", "watch");
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"clockItem: watch"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // Spectators' tools: true/false
- if (!togglesND.contains("spectatorsTools")) {
- toggles.getConfig().set("spectatorsTools", true);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"spectatorsTools: true"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // -> Spectators' tools item: <item name>
- if (!togglesND.contains("spectatorsToolsItem")) {
- toggles.getConfig().set("spectatorsToolsItem", "magma_cream");
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"spectatorsToolsItem: book"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // Spectators' tool: TP to death: true/false
- if (!togglesND.contains("tpToDeathTool")) {
- toggles.getConfig().set("tpToDeathTool", true);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"tpToDeathTool: true"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // Spectators' tool: TP to death: show death cause true/false
- if (!togglesND.contains("tpToDeathToolShowCause")) {
- toggles.getConfig().set("tpToDeathToolShowCause", true);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"tpToDeathToolShowCause: true"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // Inspector: true/false
- if (!togglesND.contains("inspector")) {
- toggles.getConfig().set("inspector", true);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"inspector: true"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // -> Inspector item: <item name>
- if (!togglesND.contains("inspectorItem")) {
- toggles.getConfig().set("inspectorItem", "book");
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"inspectorItem: book"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // -> Inspect from teleportation menu
- if (!togglesND.contains("inspectPlayerFromTeleportationMenu")) {
- toggles.getConfig().set("inspectPlayerFromTeleportationMenu", true);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"inspectPlayerFromTeleportationMenu: true"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- if (!togglesND.contains("playersHealthInTeleportationMenu")) {
- toggles.getConfig().set("playersHealthInTeleportationMenu", true);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"playersHealthInTeleportationMenu: true"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- if (!togglesND.contains("playersLocationInTeleportationMenu")) {
- toggles.getConfig().set("playersLocationInTeleportationMenu", true);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"playersLocationInTeleportationMenu: true"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // Spectator chat: true/false
- if (!togglesND.contains("specchat")) {
- toggles.getConfig().set("specchat", true);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"specchat: true"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // Output messages from plugin: true/false
- if (!togglesND.contains("outputmessages")) {
- toggles.getConfig().set("outputmessages", true);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"outputmessages: true"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // Spectate mode enable on death: true/false
- if (!togglesND.contains("deathspec")) {
- toggles.getConfig().set("deathspec", false);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"deathspec: false"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // Prefix spectator names in tab list: true/false
- if (!togglesND.contains("colouredtablist")) {
- toggles.getConfig().set("colouredtablist", true);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"colouredtablist: true"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // Can spectators see other spectators? true/false
- if (!togglesND.contains("seespecs")) {
- toggles.getConfig().set("seespecs", false);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"seespecs: false"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // Block spectators from executing non-SpectatorPlus commands? true/false
- if (!togglesND.contains("blockcmds")) {
- toggles.getConfig().set("blockcmds", true);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"blockcmds: true"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // Can admins bypass command blocking? true/false
- if (!togglesND.contains("adminbypass")) {
- toggles.getConfig().set("adminbypass", true);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"adminbypass: true"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // Display "(Right-click)" on the names of the misc tools (teleporter, etc.)? true/false
- if(!togglesND.contains("newbieMode")) {
- toggles.getConfig().set("newbieMode", true);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"newbieMode: true"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // Teleport the players to the spawn, if there isn't any main lobby set, when the spectator
- // mode is enabled/disabled? true/false
- if(!togglesND.contains("teleportToSpawnOnSpecChangeWithoutLobby")) {
- toggles.getConfig().set("teleportToSpawnOnSpecChangeWithoutLobby", true);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"teleportToSpawnOnSpecChangeWithoutLobby: true"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // When teleporting the players to the spawn (without main lobby), use the /spawn command, or
- // the spawn point of the current world?
- if(!togglesND.contains("useSpawnCommandToTeleport")) {
- toggles.getConfig().set("useSpawnCommandToTeleport", true);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"useSpawnCommandToTeleport: true"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // When teleporting the players to the spawn (without main lobby), use the /spawn command, or
- // the spawn point of the current world?
- if(!togglesND.contains("enforceArenaBoundary")) {
- toggles.getConfig().set("enforceArenaBoundary", true);
- console.sendMessage(ChatColor.GRAY+"Added "+ChatColor.WHITE+"enforceArenaBoundary: true"+ChatColor.GRAY+" to "+ChatColor.WHITE+"toggles.yml"+ChatColor.GRAY+"...");
- }
- // Config was updated, fix version number.
- toggles.getConfig().set("version",version);
- toggles.saveConfig();
- compass = toggles.getConfig().getBoolean("compass", true);
- clock = toggles.getConfig().getBoolean("arenaclock", true);
- spectatorsTools = toggles.getConfig().getBoolean("spectatorsTools", true);
- tpToDeathTool = toggles.getConfig().getBoolean("tpToDeathTool", true);
- tpToDeathToolShowCause = toggles.getConfig().getBoolean("tpToDeathToolShowCause", true);
- inspector = toggles.getConfig().getBoolean("inspector", true);
- inspectFromTPMenu = toggles.getConfig().getBoolean("inspectPlayerFromTeleportationMenu", true);
- playersHealthInTeleportationMenu = toggles.getConfig().getBoolean("playersHealthInTeleportationMenu", true);
- playersLocationInTeleportationMenu = toggles.getConfig().getBoolean("playersLocationInTeleportationMenu", true);
- specChat = toggles.getConfig().getBoolean("specchat", true);
- output = toggles.getConfig().getBoolean("outputmessages", true);
- death = toggles.getConfig().getBoolean("deathspec", false);
- scoreboard = toggles.getConfig().getBoolean("colouredtablist", true);
- seeSpecs = toggles.getConfig().getBoolean("seespecs", false);
- blockCmds = toggles.getConfig().getBoolean("blockcmds", true);
- adminBypass = toggles.getConfig().getBoolean("adminbypass", true);
- newbieMode = toggles.getConfig().getBoolean("newbieMode", true);
- teleportToSpawnOnSpecChangeWithoutLobby = toggles.getConfig().getBoolean("teleportToSpawnOnSpecChangeWithoutLobby", true);
- useSpawnCommandToTeleport = toggles.getConfig().getBoolean("useSpawnCommandToTeleport", true);
- enforceArenaBoundary = toggles.getConfig().getBoolean("enforceArenaBoundary", true);
- compassItem = Material.matchMaterial(toggles.getConfig().getString("compassItem", "COMPASS"));
- clockItem = Material.matchMaterial(toggles.getConfig().getString("clockItem", "WATCH"));
- spectatorsToolsItem = Material.matchMaterial(toggles.getConfig().getString("spectatorsToolsItem", "MAGMA_CREAM"));
- inspectorItem = Material.matchMaterial(toggles.getConfig().getString("inspectorItem", "BOOK"));
- if(compassItem == null) compassItem = Material.COMPASS;
- if(clockItem == null) clockItem = Material.WATCH;
- if(spectatorsToolsItem == null) spectatorsToolsItem = Material.MAGMA_CREAM;
- if(inspectorItem == null) inspectorItem = Material.BOOK;
- try {
- setSpectatorMode(SpectatorMode.fromString(setup.getConfig().getString("mode")));
- } catch(IllegalArgumentException e) {
- getLogger().warning("The SpectatorPlus' mode set in the config (" + setup.getConfig().getString("mode") + ") is invalid; using the ANY mode.");
- setSpectatorMode(SpectatorMode.ANY);
- }
- } // ...end hardReload
- if (scoreboard) {
- if (manager==null) { // After a reload, if 'scoreboard' is kept on, the same scoreboard will be used.
- manager = getServer().getScoreboardManager();
- board = manager.getNewScoreboard();
- board.registerNewObjective("health", "health").setDisplaySlot(DisplaySlot.PLAYER_LIST);
- team = board.registerNewTeam("spec");
- team.setPrefix(ChatColor.DARK_GRAY + "[" + ChatColor.GRAY + "Spec" + ChatColor.DARK_GRAY + "] " + ChatColor.GRAY);
- for (Player target : getServer().getOnlinePlayers()) {
- if (user.containsKey(target.getName()) && user.get(target.getName()).spectating) {
- }
- }
- }
- // Make sure the team is empty
- for (OfflinePlayer target : team.getPlayers()) {
- team.removePlayer(target);
- }
- // Add players who are spectating & set their scoreboard
- for (Player target : getServer().getOnlinePlayers()) {
- if (getPlayerData(target) != null && getPlayerData(target).spectating) {
- target.setScoreboard(board);
- team.addPlayer(target);
- }
- }
- // Incase seeSpecs was previously disabled...
- seeSpecs = toggles.getConfig().getBoolean("seespecs", false);
- } else {
- // seeSpecs relies on using scoreboard teams. Force-disable seeSpecs if scoreboard is disabled.
- seeSpecs = false;
- // Do we need to worry about the scoreboard being previously enabled?
- if (manager != null) {
- // Remove all players from spectator team
- for (OfflinePlayer target : team.getPlayers()) {
- team.removePlayer(target);
- }
- // Reset each spectator's scoreboard to default/previous
- for (Player target : getServer().getOnlinePlayers()) {
- if (getPlayerData(target) != null && getPlayerData(target).spectating) {
- if (getPlayerData(target).oldScoreboard != null) {
- target.setScoreboard(getPlayerData(target).oldScoreboard);
- } else {
- target.setScoreboard(getServer().getScoreboardManager().getMainScoreboard());
- }
- }
- }
- }
- }
- if(team != null) team.setCanSeeFriendlyInvisibles(seeSpecs);
- // Update all spectators' inventories
- updateSpectatorInventories();
- }
- /**
- * Sets the current SpectatorPlus' mode.
- * <p>
- * <ul>
- * <li>{@code ANY}: the spectators can teleports themselves to any player in the server.</li>
- * <li>{@code ARENA}: the spectators will have to choose an arena; then they will be able
- * to teleport themselves only to the players in this arena. An option is available to prevent
- * the spectators from leaving the arena.</li>
- * <li>{@code WORLD}: the spectators will be able to teleport themselves to the players in the same world.</li>
- * </ul>
- *
- * @param mode The mode.
- * @see SpectatorPlusMode
- *
- * @since 2.0
- */
- protected void setSpectatorMode(SpectatorMode mode) {
- this.mode = mode;
- setup.getConfig().set("mode", mode.toString());
- setup.saveConfig();
- // Needed if the mode is changed from/to the arena mode.
- updateSpectatorInventories();
- }
- /**
- * Lets a player select two points and set up an arena.
- *
- * @param player The player involved in the setup process.
- * @param block The block punched by the player.
- *
- * @return True if the player was setting up an arena; false else.
- */
- protected boolean arenaSetup(Player player, Block block) {
- if (getPlayerData(player).setup == 2) {
- getPlayerData(player).pos2 = block.getLocation();
- getPlayerData(player).setup = 0;
- Location lowPos, hiPos;
- lowPos = new Location(getPlayerData(player).pos1.getWorld(), 0, 0, 0);
- hiPos = new Location(getPlayerData(player).pos1.getWorld(), 0, 0, 0);
- // yPos
- if (Math.floor(getPlayerData(player).pos1.getY()) > Math.floor(getPlayerData(player).pos2.getY())) {
- hiPos.setY(Math.floor(getPlayerData(player).pos1.getY()));
- lowPos.setY(Math.floor(getPlayerData(player).pos2.getY()));
- } else {
- lowPos.setY(Math.floor(getPlayerData(player).pos1.getY()));
- hiPos.setY(Math.floor(getPlayerData(player).pos2.getY()));
- }
- // xPos
- if (Math.floor(getPlayerData(player).pos1.getX()) > Math.floor(getPlayerData(player).pos2.getX())) {
- hiPos.setX(Math.floor(getPlayerData(player).pos1.getX()));
- lowPos.setX(Math.floor(getPlayerData(player).pos2.getX()));
- } else {
- lowPos.setX(Math.floor(getPlayerData(player).pos1.getX()));
- hiPos.setX(Math.floor(getPlayerData(player).pos2.getX()));
- }
- // zPos
- if (Math.floor(getPlayerData(player).pos1.getZ()) > Math.floor(getPlayerData(player).pos2.getZ())) {
- hiPos.setZ(Math.floor(getPlayerData(player).pos1.getZ()));
- lowPos.setZ(Math.floor(getPlayerData(player).pos2.getZ()));
- } else {
- lowPos.setZ(Math.floor(getPlayerData(player).pos1.getZ()));
- hiPos.setZ(Math.floor(getPlayerData(player).pos2.getZ()));
- }
- arenasManager.registerArena(new Arena(getPlayerData(player).arenaName, hiPos, lowPos));
- player.sendMessage(prefix + "Arena " + ChatColor.AQUA + getPlayerData(player).arenaName + ChatColor.GRAY + " successfully set up!");
- // returns true: Cancels breaking of the block that was punched
- return true;
- }
- else {
- if (getPlayerData(player).setup == 1) {
- getPlayerData(player).pos1 = block.getLocation();
- player.sendMessage(prefix + "Punch point " + ChatColor.AQUA + "#2" + ChatColor.GRAY + " - the opposite corner of the arena");
- getPlayerData(player).setup = 2;
- // returns true: Cancels breaking of the block that was punched
- return true;
- }
- else {
- // returns false: The player was not setting up an arena.
- return false;
- }
- }
- }
- /**
- * Removes an arena.
- *
- * @param arenaName
- * @return True if the arena was removed; false else (non-existant arena).
- */
- protected boolean removeArena(String arenaName) {
- Arena arenaToBeRemoved = arenasManager.getArena(arenaName);
- if(arenaToBeRemoved == null) {
- return false;
- }
- arenasManager.unregisterArena(arenaToBeRemoved);
- // The players in the deleted arena are removed to the arena
- for(Player player : this.getServer().getOnlinePlayers()) {
- if(getPlayerData(player).spectating) {
- if(getPlayerData(player).arena != null && getPlayerData(player).arena.equals(arenaToBeRemoved.getUUID())) {
- removePlayerFromArena(player);
- }
- }
- }
- return true;
- }
- /**
- * Sets the arena for the given player.
- * Teleports the player to the lobby of that arena, if a lobby is available.
- *
- * @param player The player.
- * @param arenaName The name of the arena.
- * @param teleportToLobby If true the player will be teleported to the lobby (if a lobby is set).
- * @return True if the change was effective (i.e. the arena exists).
- */
- protected boolean setArenaForPlayer(Player player, String arenaName, boolean teleportToLobby) {
- Arena arena = arenasManager.getArena(arenaName);
- getPlayerData(player).arena = arena.getUUID();
- if(teleportToLobby) {
- Location lobbyLocation = arena.getLobby();
- if(lobbyLocation == null) { // No lobby set
- player.sendMessage(prefix + "No lobby location set for " + ChatColor.AQUA + arenaName);
- return true;
- }
- if(output) {
- player.sendMessage(prefix + "Teleported you to " + ChatColor.AQUA + arenaName);
- }
- player.teleport(lobbyLocation);
- }
- return true;
- }
- /**
- * Sets the arena for the given player.
- * Teleports the player to the lobby of that arena, if a lobby is available.
- *
- * @param player The player.
- * @param arenaName The name of the arena.
- * @return True if the change was effective (i.e. the arena exists).
- */
- protected boolean setArenaForPlayer(Player player, String arenaName) {
- return setArenaForPlayer(player, arenaName, true);
- }
- /**
- * Removes a player from his arena.
- * The player is teleported to the main lobby, if such a lobby is set.
- *
- * @param player
- */
- protected void removePlayerFromArena(Player player) {
- removePlayerFromArena(player, false);
- }
- /**
- * Removes a player from his arena.
- * The player is teleported to the main lobby, if such a lobby is set.
- *
- * @param player
- * @param silent
- */
- protected void removePlayerFromArena(Player player, boolean silent) {
- getPlayerData(player).arena = null;
- boolean teleported = spawnPlayer(player);
- if(output && !silent) {
- if(teleported) {
- player.sendMessage(prefix + "You were removed from your current arena and teleported to the main lobby.");
- }
- else {
- player.sendMessage(prefix + "You were removed from your current arena.");
- }
- }
- }
- /**
- * Saves the player's inventory and clears it before enabling spectator mode.
- *
- * @param player The concerned player.
- */
- protected void savePlayerInv(Player player) {
- getPlayerData(player).inventory = player.getInventory().getContents();
- getPlayerData(player).armour = player.getInventory().getArmorContents();
- player.getInventory().clear();
- player.getInventory().setArmorContents(null);
- }
- /**
- * Loads the player's inventory after disabling the spectate mode.
- *
- * @param player The concerned player.
- */
- protected void loadPlayerInv(Player player) {
- player.getInventory().clear();
- player.getInventory().setContents(getPlayerData(player).inventory);
- player.getInventory().setArmorContents(getPlayerData(player).armour);
- getPlayerData(player).inventory = null;
- getPlayerData(player).armour = null;
- player.updateInventory(); // yes, it's deprecated. But it still works!
- }
- /**
- * Broadcasts a message to all players with spectator mode enabled, and the sender.
- *
- * @param sender The sender of the message to be broadcasted.
- * @param message The message to broadcast.
- */
- protected void broadcastToSpectators(CommandSender sender, String message) {
- String senderName = null;
- if(sender instanceof Player) {
- senderName = ChatColor.WHITE + ((Player) sender).getDisplayName();
- }
- else {
- senderName = ChatColor.DARK_RED + "CONSOLE";
- }
- String formattedMessage = ChatColor.GRAY + "[" + senderName + ChatColor.GRAY + " -> spectators] " + ChatColor.RESET + message;
- for (Player player : getServer().getOnlinePlayers()) {
- if(getPlayerData(player).spectating || player.getName().equals(sender.getName())) {
- player.sendMessage(formattedMessage);
- }
- }
- console.sendMessage(formattedMessage);
- }
- /**
- * Sends a spectator chat message, from one spectator to all other spectators.
- * Includes "/me" actions
- *
- * @param sender The sender of the message.
- * @param message The text of the message.
- * @param isAction If true, the message will be displayed as an action message (like /me <message>).
- */
- protected void sendSpectatorMessage(CommandSender sender, String message, Boolean isAction) {
- String playerName = null;
- if(sender instanceof Player) {
- playerName = ChatColor.WHITE + ((Player) sender).getDisplayName();
- } else {
- playerName = ChatColor.DARK_RED + "CONSOLE";
- }
- String invite = null;
- if(isAction) {
- invite = "* " + playerName + " " + ChatColor.GRAY;
- } else {
- invite = playerName + ChatColor.GRAY + ": ";
- }
- for (Player player : getServer().getOnlinePlayers()) {
- if(getPlayerData(player).spectating) {
- player.sendMessage(ChatColor.GRAY + "[SPEC] " + invite + message);
- }
- }
- console.sendMessage(ChatColor.GRAY + "[SPEC] " + invite + message);
- }
- /**
- * Updates the spectator inventory for a certain player.
- *
- * @param spectator The player whose inventory will be updated
- *
- * @since 2.0
- */
- protected void updateSpectatorInventory(Player spectator) {
- // Empty the inventory first...
- spectator.getInventory().clear();
- String rightClick = "", rightClickPlayer = "";
- if(newbieMode) {
- rightClick = ChatColor.GRAY + " (Right-click)";
- rightClickPlayer = ChatColor.GRAY + " (Right-click a player)";
- }
- // Give them compass if the toggle is on
- if (compass) {
- ItemStack compass = new ItemStack(compassItem, 1);
- ItemMeta compassMeta = (ItemMeta)compass.getItemMeta();
- compassMeta.setDisplayName(ChatColor.BLUE +""+ ChatColor.BOLD + "Teleporter" + rightClick);
- List<String> lore = new ArrayList<String>();
- lore.add(ChatColor.AQUA +""+ ChatColor.ITALIC + "Right click" + ChatColor.GRAY + ChatColor.ITALIC + " to choose a player");
- lore.add(ChatColor.GRAY +""+ ChatColor.ITALIC + "to teleport to");
- compassMeta.setLore(lore);
- compass.setItemMeta(compassMeta);
- spectator.getInventory().setItem(0, compass);
- }
- // Give them clock (only for arena mode and if the toggle is on)
- if (clock && mode == SpectatorMode.ARENA) {
- ItemStack watch = new ItemStack(clockItem, 1);
- ItemMeta watchMeta = (ItemMeta)watch.getItemMeta();
- watchMeta.setDisplayName(ChatColor.DARK_RED +""+ ChatColor.BOLD + "Arena selector" + rightClick);
- List<String> lore = new ArrayList<String>();
- lore.add(ChatColor.AQUA +""+ ChatColor.ITALIC + "Right click" + ChatColor.GRAY + ChatColor.ITALIC + " to choose an arena");
- lore.add(ChatColor.GRAY +""+ ChatColor.ITALIC + "to spectate in");
- watchMeta.setLore(lore);
- watch.setItemMeta(watchMeta);
- spectator.getInventory().setItem(1, watch);
- }
- // Give them magma cream (spectators tools) if the toggle is on
- if(spectatorsTools) {
- ItemStack tools = new ItemStack(spectatorsToolsItem, 1);
- ItemMeta toolsMeta = tools.getItemMeta();
- toolsMeta.setDisplayName(ChatColor.WHITE +""+ ChatColor.BOLD + "Spectators' tools" + rightClick);
- List<String> lore = new ArrayList<String>();
- lore.add(ChatColor.AQUA +""+ ChatColor.ITALIC + "Right click" + ChatColor.GRAY + ChatColor.ITALIC + " to open the spectator");
- lore.add(ChatColor.GRAY +""+ ChatColor.ITALIC + "tools menu");
- toolsMeta.setLore(lore);
- tools.setItemMeta(toolsMeta);
- spectator.getInventory().setItem(4, tools);
- }
- // Give them book if the toggle is on
- if(inspector) {
- ItemStack book = new ItemStack(inspectorItem, 1);
- ItemMeta bookMeta = (ItemMeta)book.getItemMeta();
- bookMeta.setDisplayName(ChatColor.BLUE +""+ ChatColor.BOLD + "Inspector" + rightClickPlayer);
- List<String> lore = new ArrayList<String>();
- lore.add(ChatColor.AQUA +""+ ChatColor.ITALIC + "Right click" + ChatColor.GRAY + ChatColor.ITALIC + " a player to see their");
- lore.add(ChatColor.GRAY +""+ ChatColor.ITALIC + "inventory, armour, health & more!");
- bookMeta.setLore(lore);
- book.setItemMeta(bookMeta);
- spectator.getInventory().setItem(8, book);
- }
- spectator.updateInventory();
- }
- /**
- * Updates the spectator inventories for all currently spectating players.
- *
- * @since 2.0
- */
- protected void updateSpectatorInventories() {
- for (Player target : getServer().getOnlinePlayers()) {
- if (getPlayerData(target).spectating) {
- updateSpectatorInventory(target);
- }
- }
- }
- /**
- * Get the PlayerObject (data store) for the player.
- *
- * @param target The player to get the PlayerObject of.
- *
- * @since 2.0
- */
- protected PlayerObject getPlayerData(Player target) {
- return user.get(target.getName());
- }
- /**
- * Returns the API.
- *
- * @see SpectateAPI
- *
- * @return The API.
- */
- public final SpectateAPI getAPI() {
- return api;
- }
- protected Boolean parseBoolean(String input) {
- if (input.equalsIgnoreCase("on") || input.equalsIgnoreCase("yes") || input.equalsIgnoreCase("y") || input.equalsIgnoreCase("true")) {
- return true;
- } else if (input.equalsIgnoreCase("off") || input.equalsIgnoreCase("no") || input.equalsIgnoreCase("n") || input.equalsIgnoreCase("false")) {
- return false;
- } else {
- return null;
- }
- }
- }
Add Comment
Please, Sign In to add comment