Skip to content

Commit

Permalink
Fixes #52: Adds option to always use /gamemode 3 (spectator/no-clip
Browse files Browse the repository at this point in the history
gamemode) instead of adventure mode.
  • Loading branch information
pgmann committed Jun 13, 2015
1 parent cad95f8 commit f488e53
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 20 deletions.
16 changes: 10 additions & 6 deletions SpectatorPlus/src/com/pgcraft/spectatorplus/SpectateCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -792,12 +792,16 @@ private void doHide(CommandSender sender, Command command, String label, String[
*/
private void doB(CommandSender sender, Command command, String label, String[] args) {
if(sender instanceof Player && p.getPlayerData((Player) sender).spectating && ((Player) sender).getGameMode() == GameMode.SPECTATOR) {
((Player) sender).setGameMode(GameMode.ADVENTURE);

((Player) sender).setAllowFlight(true);
((Player) sender).setFlying(true);

p.updateSpectatorInventory((Player) sender);
if(!p.vanillaSpectate) {
((Player) sender).setGameMode(GameMode.ADVENTURE);

((Player) sender).setAllowFlight(true);
((Player) sender).setFlying(true);

p.updateSpectatorInventory((Player) sender);
} else {
sender.sendMessage(SpectatorPlus.prefix+ChatColor.DARK_RED+"Exiting no-clip mode is disabled.");
}
}
}

Expand Down
20 changes: 13 additions & 7 deletions SpectatorPlus/src/com/pgcraft/spectatorplus/SpectateListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -927,13 +927,19 @@ else if(e.getWhoClicked().getGameMode() == GameMode.SPECTATOR) {
}

else if(toolSelected.getItemMeta().getDisplayName().startsWith(SpectatorPlus.TOOL_NOCLIP_QUIT_NAME)) {
spectator.setGameMode(GameMode.ADVENTURE);

spectator.setAllowFlight(true);
spectator.setFlying(true);

spectator.closeInventory();
p.updateSpectatorInventory(spectator);
// Take care of the vanilla spectate mode - spectators should always be in SPECTATOR gamemode then.
// (this item is removed from the GUI in this case, so technically this should never be needed)
if(!p.vanillaSpectate) {
spectator.setGameMode(GameMode.ADVENTURE);

spectator.setAllowFlight(true);
spectator.setFlying(true);

spectator.closeInventory();
p.updateSpectatorInventory(spectator);
} else {
spectator.sendMessage(SpectatorPlus.prefix+ChatColor.DARK_RED+"Exiting no-clip mode is disabled.");
}
}
} catch(NullPointerException ex) {
// This happens if there isn't any meta, aka here if the spectator
Expand Down
28 changes: 24 additions & 4 deletions SpectatorPlus/src/com/pgcraft/spectatorplus/SpectatorPlus.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class SpectatorPlus extends JavaPlugin {
protected Material spectatorsToolsItem;
protected Boolean inspector;
protected Material inspectorItem;
protected Boolean tpToDeathTool, tpToDeathToolShowCause, divingSuitTool, nightVisionTool, noClipTool, speedTool, glowOnActiveTools, inspectFromTPMenu, playersHealthInTeleportationMenu, playersLocationInTeleportationMenu, specChat, scoreboard, output, death, seeSpecs, blockCmds, adminBypass, newbieMode, teleportToSpawnOnSpecChangeWithoutLobby, useSpawnCommandToTeleport, enforceArenaBoundary;
protected Boolean tpToDeathTool, tpToDeathToolShowCause, divingSuitTool, nightVisionTool, noClipTool, speedTool, glowOnActiveTools, inspectFromTPMenu, playersHealthInTeleportationMenu, playersLocationInTeleportationMenu, specChat, scoreboard, vanillaSpectate, output, death, seeSpecs, blockCmds, adminBypass, newbieMode, teleportToSpawnOnSpecChangeWithoutLobby, useSpawnCommandToTeleport, enforceArenaBoundary;

protected SpectatorMode mode = SpectatorMode.ANY;

Expand Down Expand Up @@ -913,7 +913,8 @@ protected void enableSpectate(Player spectator, CommandSender sender, boolean si

// Gamemode, 'ghost' and inventory
getPlayerData(spectator).oldGameMode = spectator.getGameMode();
spectator.setGameMode(GameMode.ADVENTURE);
GameMode gm = (vanillaSpectate)? GameMode.SPECTATOR : GameMode.ADVENTURE;
spectator.setGameMode(gm);

savePlayerInv(spectator);
getPlayerData(spectator).effects = spectator.getActivePotionEffects();
Expand Down Expand Up @@ -964,6 +965,8 @@ else if (sender instanceof Player && !spectator.getName().equals(sender.getName(

specs.getConfig().set(spectator.getName(), true);
specs.saveConfig();


}
}

Expand Down Expand Up @@ -1131,6 +1134,7 @@ protected void reloadConfig(boolean hardReload) {
output = toggles.getBoolean(Toggle.OUTPUT_MESSAGES);
death = toggles.getBoolean(Toggle.SPECTATOR_MODE_ON_DEATH);
scoreboard = toggles.getBoolean(Toggle.SPECTATORS_TABLIST_PREFIX);
vanillaSpectate = toggles.getBoolean(Toggle.SPECTATORS_USE_VANILLA);
seeSpecs = toggles.getBoolean(Toggle.SPECTATORS_SEE_OTHERS);
blockCmds = toggles.getBoolean(Toggle.CHAT_BLOCKCOMMANDS_ENABLED);
adminBypass = toggles.getBoolean(Toggle.CHAT_BLOCKCOMMANDS_ADMINBYPASS);
Expand Down Expand Up @@ -1206,6 +1210,18 @@ protected void reloadConfig(boolean hardReload) {

// Update all spectators' inventories
updateSpectatorInventories();

for (Player target : getServer().getOnlinePlayers()) {
if (getPlayerData(target) != null && getPlayerData(target).spectating) {
if(vanillaSpectate) {
// Set all spectators to SPECTATOR gamemode.
target.setGameMode(GameMode.SPECTATOR);
} else {
// Set all spectators to ADVENTURE gamemode.
target.setGameMode(GameMode.ADVENTURE);
}
}
}
}

/**
Expand Down Expand Up @@ -1571,9 +1587,13 @@ else if(spectator.getGameMode() == GameMode.SPECTATOR) {

ItemStack quitNoClip = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
SkullMeta sMeta = (SkullMeta) quitNoClip.getItemMeta();
sMeta.setDisplayName(TOOL_NOCLIP_QUIT_NAME + spectator.getName());
String disableExit="";
if(vanillaSpectate) {
disableExit = ChatColor.RED+""+ChatColor.BOLD+" DISABLED";
}
sMeta.setDisplayName(TOOL_NOCLIP_QUIT_NAME + spectator.getName() +disableExit);
List<String> lore = new ArrayList<String>();
lore.add(ChatColor.GRAY + "Disables the no-clip mode");
lore.add(ChatColor.GRAY + "Leave no-clip mode");
lore.add(ChatColor.DARK_GRAY + "You can also use /spec b");
sMeta.setLore(lore);
sMeta.setOwner(spectator.getName());
Expand Down
9 changes: 9 additions & 0 deletions SpectatorPlus/src/com/pgcraft/spectatorplus/Toggle.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
@SuppressWarnings("rawtypes")
public enum Toggle {

/* Adding a toggle CHECKLIST
* - Add the toggle to toggles.yml
* - Add the toggle here in Toggle.java
* - Add it to the ToggleManager's set(Toggle, Object, Boolean) method.
* - Declare related variable in SpectatorPlus.java
* - Add the declared variable to reloadConfig(Boolean), also in SpectatorPlus.java
*/

// Tools-related toggles

TOOLS_TELEPORTER_ENABLED(Boolean.class, true, "If true the teleportation menu will be available for spectators."),
Expand Down Expand Up @@ -56,6 +64,7 @@ public enum Toggle {

SPECTATORS_TABLIST_PREFIX("spectators.tabListPrefix", Boolean.class, true, "Prefix spectator names in the tab list? This will change the Scoreboard used, and restore the old one when spectator mode is disabled. If you see another plugin's sidebar/infos on players list disappearing when you enable the spectator mode, try to disable this."),
SPECTATORS_SEE_OTHERS("spectators.spectatorsSeeSpectators", Boolean.class, true, "See other spectators when you're spectating? (*requires spectators.tabListPrefix to be true*)"),
SPECTATORS_USE_VANILLA("spectators.useVanillaMode", Boolean.class, false, "Use vanilla spectate mode (gamemode 3) instead of adventure mode. Same as no-clip mode tool, but mandatory."),

// What to do when the spectator mode is changed (enabled or disabled)?

Expand Down
18 changes: 17 additions & 1 deletion SpectatorPlus/src/com/pgcraft/spectatorplus/ToggleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import java.util.Set;

import org.apache.commons.lang.Validate;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.configuration.Configuration;
import org.bukkit.entity.Player;

/**
* This class manages the SpectatorPlus' toggles.
Expand Down Expand Up @@ -210,6 +212,20 @@ public void set(Toggle toggle, Object value, boolean temp) {
p.scoreboard = (Boolean) value;
break;

case SPECTATORS_USE_VANILLA:
p.vanillaSpectate = (Boolean) value;
GameMode gm = (p.vanillaSpectate)? GameMode.SPECTATOR : GameMode.ADVENTURE;
for (Player target : p.getServer().getOnlinePlayers()) {
if (p.getPlayerData(target) != null && p.getPlayerData(target).spectating) {
// Update each player to reflect the new gamemode.
p.getPlayerData(target).gamemodeChangeAllowed=true;
target.setGameMode(gm);
p.getPlayerData(target).gamemodeChangeAllowed=false;
}
}
p.updateSpectatorInventories();
break;

case SPECTATOR_MODE_ON_DEATH:
p.death = (Boolean) value;
break;
Expand Down Expand Up @@ -330,7 +346,7 @@ public ConfigAccessor getConfigAccessor() {

/**
* Upgrade the configuration, populating the configuration file with new keys and the
* appropried default values.
* appropriate default values.
*/
protected void upgrade() {
Set<String> togglesND = getConfiguration().getKeys(true); // ND = no defaults
Expand Down
7 changes: 5 additions & 2 deletions SpectatorPlus/toggles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ tools:

glowEffectIfActive: true

# What spectators can get from the spectators' tools?
# What spectators can get from the spectators' tools?
speed: true # They can get speed
nightVision: true # They can get night-vision and water breathing
divingSuit: true # They can get some Depth-Strider-III boots
noClipMode: true # They can get a no-clip mode (i.e. the vanilla spectator mode)

tpToDeath:
enabled: true
# Display the cause of the death in the tooltip? (Example: « You starved to death ».)
# Display the cause of the death in the tooltip? (Example: « You starved to death ».)
displayCause: true

inspector:
Expand Down Expand Up @@ -75,6 +75,9 @@ spectators:

# See other spectators when you're spectating? (*requires spectators.tabListPrefix to be true*)
spectatorsSeeSpectators: true

# Use vanilla spectate mode (gamemode 3) instead of adventure mode. Same as no-clip mode tool, but mandatory.
useVanillaMode: false


# What to do when the spectator mode is changed (enabled or disabled)?
Expand Down

0 comments on commit f488e53

Please sign in to comment.