From d839d9b97ffa45b27fb22939c95a59d6b534d862 Mon Sep 17 00:00:00 2001 From: David Carpenter Date: Thu, 12 Sep 2013 17:57:32 -0700 Subject: [PATCH] Finishing touches --- config.yml | 8 + src/com/Chipmunk9998/Spectate/Spectate.java | 39 ++++- .../Spectate/SpectateCommandExecutor.java | 27 ++- .../Spectate/SpectateListener.java | 10 +- .../Spectate/api/SpectateManager.java | 161 +++++++++++++----- 5 files changed, 190 insertions(+), 55 deletions(-) create mode 100644 config.yml diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..51ab854 --- /dev/null +++ b/config.yml @@ -0,0 +1,8 @@ +######################################### Config Options ######################################### + +# Enabling this permission stops spectators from spectating people with +# the "spectate.cantspectate" permission and the '*' permission. (OPs included) +cantspectate-permission-enabled: false + +# Enabling this will stop spectators from executing any commands except '/spectate' while spectating. +disable-commands-while-spectating: false \ No newline at end of file diff --git a/src/com/Chipmunk9998/Spectate/Spectate.java b/src/com/Chipmunk9998/Spectate/Spectate.java index 2a20478..36f3991 100644 --- a/src/com/Chipmunk9998/Spectate/Spectate.java +++ b/src/com/Chipmunk9998/Spectate/Spectate.java @@ -1,6 +1,10 @@ package com.Chipmunk9998.Spectate; +import java.io.File; + import org.bukkit.ChatColor; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -8,18 +12,21 @@ public class Spectate extends JavaPlugin { - //TODO: Control command - //TODO: Fix inventory compatibility (Multiverse Inventories, Mob Arena) - //TODO: Config + //TODO: (IN THE FUTURE - DON'T HAVE TIME FOR THIS RIGHT NOW) Control command static SpectateManager Manager; + public boolean cantspectate_permission_enabled = false; + public boolean disable_commands = false; + public void onEnable() { Manager = new SpectateManager(this); + loadConfig(); + getServer().getPluginManager().registerEvents(new SpectateListener(), this); - getCommand("spectate").setExecutor(new SpectateCommandExecutor()); + getCommand("spectate").setExecutor(new SpectateCommandExecutor(this)); getAPI().startSpectateTask(); } @@ -42,5 +49,29 @@ public static SpectateManager getAPI() { return Manager; } + + public boolean multiverseInvEnabled() { + + if (getServer().getPluginManager().getPlugin("Multiverse-Inventories") != null && getServer().getPluginManager().getPlugin("Multiverse-Inventories").isEnabled()) { + + return true; + + } + + return false; + + } + + public void loadConfig() { + + saveDefaultConfig(); + + File tutorialFile = new File(getDataFolder(), "config.yml"); + FileConfiguration config = YamlConfiguration.loadConfiguration(tutorialFile); + + cantspectate_permission_enabled = config.getBoolean("cantspectate-permission-enabled"); + disable_commands = config.getBoolean("disable-commands-while-spectating"); + + } } diff --git a/src/com/Chipmunk9998/Spectate/SpectateCommandExecutor.java b/src/com/Chipmunk9998/Spectate/SpectateCommandExecutor.java index 18bd956..1404849 100644 --- a/src/com/Chipmunk9998/Spectate/SpectateCommandExecutor.java +++ b/src/com/Chipmunk9998/Spectate/SpectateCommandExecutor.java @@ -11,6 +11,14 @@ import com.Chipmunk9998.Spectate.api.SpectateMode; public class SpectateCommandExecutor implements CommandExecutor { + + Spectate plugin; + + public SpectateCommandExecutor(Spectate plugin) { + + this.plugin = plugin; + + } public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { @@ -27,7 +35,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, if (args.length == 0) { - if (!cmdsender.hasPermission("spectate.help") && !cmdsender.hasPermission("spectate.use")) { + if (!cmdsender.hasPermission("spectate.help") && !cmdsender.hasPermission("spectate.on") && !cmdsender.hasPermission("spectate.off")) { cmdsender.sendMessage(ChatColor.RED + "You do not have permission."); return true; @@ -41,7 +49,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, if (args[0].equalsIgnoreCase("off")) { - if (!cmdsender.hasPermission("spectate.use")) { + if (!cmdsender.hasPermission("spectate.off")) { cmdsender.sendMessage(ChatColor.RED + "You do not have permission."); return true; @@ -239,7 +247,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, }else if (args[0].equalsIgnoreCase("help")) { - if (!cmdsender.hasPermission("spectate.help") && !cmdsender.hasPermission("spectate.use")) { + if (!cmdsender.hasPermission("spectate.help") && !cmdsender.hasPermission("spectate.on") && !cmdsender.hasPermission("spectate.off")) { cmdsender.sendMessage(ChatColor.RED + "You do not have permission."); return true; @@ -251,7 +259,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, } - if (!cmdsender.hasPermission("spectate.use")) { + if (!cmdsender.hasPermission("spectate.on")) { cmdsender.sendMessage(ChatColor.RED + "You do not have permission."); return true; @@ -273,6 +281,17 @@ public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, return true; } + + if (plugin.cantspectate_permission_enabled) { + + if (targetPlayer.hasPermission("spectate.cantspectate")) { + + cmdsender.sendMessage(ChatColor.GRAY + "This person can not be spectated."); + return true; + + } + + } if (Spectate.getAPI().isSpectating(cmdsender)) { diff --git a/src/com/Chipmunk9998/Spectate/SpectateListener.java b/src/com/Chipmunk9998/Spectate/SpectateListener.java index 27de174..870b742 100644 --- a/src/com/Chipmunk9998/Spectate/SpectateListener.java +++ b/src/com/Chipmunk9998/Spectate/SpectateListener.java @@ -65,14 +65,14 @@ public void onPlayerQuit(PlayerQuitEvent event) { Bukkit.getServer().getPluginManager().callEvent(scrollEvent); ArrayList playerList = scrollEvent.getSpectateList(); - + playerList.remove(p); playerList.remove(event.getPlayer()); p.sendMessage(ChatColor.GRAY + "The person you were previously spectating has disconnected."); if (!Spectate.getAPI().scrollRight(p, playerList)) { - + Spectate.getAPI().stopSpectating(p, true); p.sendMessage(ChatColor.GRAY + "You were forced to stop spectating because there is nobody left to spectate."); @@ -106,14 +106,14 @@ public void onPlayerDeath(PlayerDeathEvent event) { Bukkit.getServer().getPluginManager().callEvent(scrollEvent); ArrayList playerList = scrollEvent.getSpectateList(); - + playerList.remove(p); playerList.remove(event.getEntity()); p.sendMessage(ChatColor.GRAY + "The person you were previously spectating has died."); if (!Spectate.getAPI().scrollRight(p, playerList)) { - + Spectate.getAPI().stopSpectating(p, true); p.sendMessage(ChatColor.GRAY + "You were forced to stop spectating because there is nobody left to spectate."); @@ -346,7 +346,7 @@ public void onInventoryClick(InventoryClickEvent event) { } } - + @EventHandler public void onPlayerDropItem(PlayerDropItemEvent event) { diff --git a/src/com/Chipmunk9998/Spectate/api/SpectateManager.java b/src/com/Chipmunk9998/Spectate/api/SpectateManager.java index f9cfe33..217fecd 100644 --- a/src/com/Chipmunk9998/Spectate/api/SpectateManager.java +++ b/src/com/Chipmunk9998/Spectate/api/SpectateManager.java @@ -36,6 +36,7 @@ public class SpectateManager { private HashMap scanTask = new HashMap(); private HashMap states = new HashMap(); + private HashMap multiInvStates = new HashMap(); public SpectateManager(Spectate plugin) { @@ -53,6 +54,18 @@ public void run() { if (isSpectating(p)) { + if (plugin.multiverseInvEnabled()) { + + if (!p.getWorld().getName().equals(getTarget(p).getWorld().getName())) { + + p.sendMessage(ChatColor.GRAY + "You were forced to stop spectating because the person you were spectating switched worlds."); + stopSpectating(p, true); + continue; + + } + + } + if (getSpectateAngle(p) == SpectateAngle.FIRST_PERSON) { if (roundTwoDecimals(p.getLocation().getX()) != roundTwoDecimals(getTarget(p).getLocation().getX()) || roundTwoDecimals(p.getLocation().getY()) != roundTwoDecimals(getTarget(p).getLocation().getY()) || roundTwoDecimals(p.getLocation().getZ()) != roundTwoDecimals(getTarget(p).getLocation().getZ()) || roundTwoDecimals(p.getLocation().getYaw()) != roundTwoDecimals(getTarget(p).getLocation().getYaw()) || roundTwoDecimals(p.getLocation().getPitch()) != roundTwoDecimals(getTarget(p).getLocation().getPitch())) { @@ -74,13 +87,22 @@ public void run() { p.getInventory().setContents(getTarget(p).getInventory().getContents()); p.getInventory().setArmorContents(getTarget(p).getInventory().getArmorContents()); - if (getTarget(p).getHealth() > 0) { + if (getTarget(p).getHealth() == 0) { - p.setHealth(getTarget(p).getHealth()); + p.setHealth(1); }else { - p.setHealth(1); + if (getTarget(p).getHealth() < p.getHealth()) { + + double difference = p.getHealth() - getTarget(p).getHealth(); + p.damage(difference); + + }else if (getTarget(p).getHealth() > p.getHealth()) { + + p.setHealth(getTarget(p).getHealth()); + + } } @@ -150,42 +172,42 @@ public void stopSpectateTask() { } public void startSpectating(Player p, Player target, boolean saveState) { - + if (!isSpectating(p)) { - + if (saveState) { savePlayerState(p); } - + } - - for (Player player1 : plugin.getServer().getOnlinePlayers()) { - player1.hidePlayer(p); + boolean saveMultiInvState = false; - } - - p.teleport(target); + if (plugin.multiverseInvEnabled()) { - if (isSpectating(p)) { + if (!p.getWorld().getName().equals(target.getWorld().getName())) { - setBeingSpectated(getTarget(p), false); - p.showPlayer(getTarget(p)); - removeSpectator(getTarget(p), p); + saveMultiInvState = true; + + } } - for (PotionEffect e : p.getActivePotionEffects()) { + for (Player player1 : plugin.getServer().getOnlinePlayers()) { - p.removePotionEffect(e.getType()); + player1.hidePlayer(p); } - setTarget(p, target); - addSpectator(target, p); + if (saveMultiInvState) { + + p.teleport(target.getWorld().getSpawnLocation()); + multiInvStates.put(p, new PlayerState(p)); + } + String playerListName = p.getPlayerListName(); if (getSpectateAngle(p) == SpectateAngle.FIRST_PERSON) { @@ -199,6 +221,27 @@ public void startSpectating(Player p, Player target, boolean saveState) { } p.setPlayerListName(playerListName); + + p.setHealth(target.getHealth()); + + p.teleport(target); + + if (isSpectating(p)) { + + setBeingSpectated(getTarget(p), false); + p.showPlayer(getTarget(p)); + removeSpectator(getTarget(p), p); + + } + + for (PotionEffect e : p.getActivePotionEffects()) { + + p.removePotionEffect(e.getType()); + + } + + setTarget(p, target); + addSpectator(target, p); p.setGameMode(target.getGameMode()); p.setFoodLevel(target.getFoodLevel()); @@ -248,7 +291,7 @@ public boolean scrollRight(Player p, ArrayList playerList) { SpectateScrollEvent event = new SpectateScrollEvent(p, playerList, ScrollDirection.RIGHT); plugin.getServer().getPluginManager().callEvent(event); - playerList = event.getSpectateList(); + playerList = new ArrayList(event.getSpectateList()); playerList.remove(p); @@ -258,6 +301,24 @@ public boolean scrollRight(Player p, ArrayList playerList) { } + if (plugin.multiverseInvEnabled()) { + + if (isScanning(p)) { + + for (Player players : event.getSpectateList()) { + + if (!players.getWorld().getName().equals(p.getWorld().getName())) { + + playerList.remove(players); + + } + + } + + } + + } + int scrollToIndex = 1; if (getScrollNumber(p, playerList) == playerList.size()) { @@ -281,7 +342,7 @@ public boolean scrollLeft(Player p, ArrayList playerList) { SpectateScrollEvent event = new SpectateScrollEvent(p, playerList, ScrollDirection.LEFT); plugin.getServer().getPluginManager().callEvent(event); - playerList = event.getSpectateList(); + playerList = new ArrayList(event.getSpectateList()); playerList.remove(p); @@ -291,6 +352,24 @@ public boolean scrollLeft(Player p, ArrayList playerList) { } + if (plugin.multiverseInvEnabled()) { + + if (isScanning(p)) { + + for (Player players : event.getSpectateList()) { + + if (!players.getWorld().getName().equals(p.getWorld().getName())) { + + playerList.remove(players); + + } + + } + + } + + } + int scrollToIndex = 1; if (getScrollNumber(p, playerList) == 1) { @@ -454,9 +533,7 @@ public ArrayList getSpectateablePlayers() { } - /* - - if (plugin.config.getBoolean("cantspectate Permission Enabled?")) { + if (plugin.cantspectate_permission_enabled) { if (onlinePlayers.hasPermission("spectate.cantspectate")) { @@ -466,8 +543,6 @@ public ArrayList getSpectateablePlayers() { } - */ - spectateablePlayers.add(onlinePlayers); } @@ -684,14 +759,6 @@ public PlayerState getPlayerState(Player p) { } - //vanish them - //teleport to target - //save state - - //set spectating off - //restore inventory - //teleport them back to original location - public void savePlayerState(Player p) { PlayerState playerstate = new PlayerState(p); @@ -707,7 +774,21 @@ public void loadPlayerState(Player toPlayer) { public void loadPlayerState(Player fromState, Player toPlayer) { - PlayerState state = getPlayerState(fromState); + if (plugin.multiverseInvEnabled() && multiInvStates.get(fromState) != null) { + + loadFinalState(multiInvStates.get(fromState), toPlayer); + multiInvStates.remove(fromState); + + } + + loadFinalState(getPlayerState(fromState), toPlayer); + states.remove(fromState); + + } + + private void loadFinalState(PlayerState state, Player toPlayer) { + + toPlayer.teleport(state.location); toPlayer.getInventory().setContents(state.inventory); toPlayer.getInventory().setArmorContents(state.armor); @@ -718,13 +799,11 @@ public void loadPlayerState(Player fromState, Player toPlayer) { toPlayer.getInventory().setHeldItemSlot(state.slot); toPlayer.setGameMode(state.mode); - toPlayer.teleport(state.location); - for (Player onlinePlayers : plugin.getServer().getOnlinePlayers()) { - if (!getVanishedFromList(fromState).contains(onlinePlayers)) { + if (!state.vanishedFrom.contains(onlinePlayers)) { - onlinePlayers.showPlayer(fromState); + onlinePlayers.showPlayer(toPlayer); } @@ -736,8 +815,6 @@ public void loadPlayerState(Player fromState, Player toPlayer) { } - states.remove(fromState); - } public ArrayList getVanishedFromList(Player p) {