From 49fd9e81d19cec7b95a85016fbec0c7b5de0bfd9 Mon Sep 17 00:00:00 2001 From: Eric Stokes Date: Sun, 29 Jan 2012 16:57:16 -0700 Subject: [PATCH] Inital allow flight code, if we use it --- .../onarandombox/MultiverseCore/MVWorld.java | 34 ++++++++++++++++--- .../MultiverseCore/api/MultiverseWorld.java | 21 ++++++++++++ .../listeners/MVPlayerListener.java | 33 +++++++++++++++--- .../MultiverseCore/utils/MVPermissions.java | 15 ++++++++ .../MultiverseCore/utils/PermissionTools.java | 17 ++++++++++ 5 files changed, 112 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java index ab81d4e0..d72c2b9b 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java @@ -171,6 +171,9 @@ public class MVWorld implements MultiverseWorld { "There is no help available for this variable. Go bug Rigby90 about it.", "setActualKeepSpawnInMemory")); this.propertyList.put("autoload", fac.getNewProperty("autoload", true, "Set this to false ONLY if you don't want this world to load itself on server restart.")); + this.propertyList.put("allowflight", fac.getNewProperty("allowflight", false, + "Set this to false ONLY if you don't want this world to load itself on server restart.", + "setActualAllowFlight")); this.propertyList.put("bedrespawn", fac.getNewProperty("bedrespawn", true, "If a player dies in this world, shoudld they go to their bed?")); this.propertyList.put("time", fac.getNewProperty("time", "", "Set the time to whatever you want! (Will NOT freeze time)", "setActualTime", true)); this.getKnownProperty("spawn", Location.class).setValue(this.readSpawnFromConfig(this.getCBWorld())); @@ -218,9 +221,20 @@ public class MVWorld implements MultiverseWorld { this.setActualDifficulty(); this.setActualGameMode(); this.setActualSpawn(); + this.setActualAllowFlight(); this.syncMobs(); } + private boolean setActualAllowFlight() { + for (Player p : this.plugin.getServer().getWorld(this.getName()).getPlayers()) { + this.plugin.log(Level.FINER, String.format("Changing %s's permission to fly in %s to %s", + p.getName(), this.getKnownProperty("allowflight", GameMode.class).getValue().toString(), + this.getAlias())); + this.plugin.getPlayerListener().handleAllowFlight(p, this); + } + return true; + } + /** * Used by the active PVP-property to set the "actual" PVP-property. * @return True if the property was successfully set. @@ -896,10 +910,6 @@ public class MVWorld implements MultiverseWorld { /** * Sets the actual gamemode by iterating through players. * - * gameMode is not used, but it's in the reflection template. - * - * Needs a bit o' refactoring. - * * @return True if the gamemodes of players were set successfully. (always) */ public boolean setActualGameMode() { @@ -1182,6 +1192,22 @@ public class MVWorld implements MultiverseWorld { return this.getKnownProperty("portalform", AllowedPortalType.class).getValue(); } + /** + * {@inheritDoc} + */ + @Override + public boolean isFlightAllowed() { + return this.getKnownProperty("flightallowed", Boolean.class).getValue(); + } + + /** + * {@inheritDoc} + */ + @Override + public void setFlightAllowed(boolean flightAllowed) { + this.setKnownProperty("flightallowed", Boolean.toString(flightAllowed), null); + } + /** * Used by the active time-property to set the "actual" property. * @return True if the property was successfully set. diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseWorld.java b/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseWorld.java index 5bd1eb11..72f31b74 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseWorld.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseWorld.java @@ -588,4 +588,25 @@ public interface MultiverseWorld { * @return The type of portals that are allowed. */ AllowedPortalType getAllowedPortals(); + + /** + * Gets whether or not flight is allowed in this world. + *

+ * It should be noted that this refers to clientside flying. Creative + * mode will always allow server based flying. + * + * @return True if flight is allowed. + */ + boolean isFlightAllowed(); + + /** + * Sets whether or not flight is allowed in this world. + *

+ * It should be noted that this refers to clientside flying. Creative + * mode will always allow server based flying. + * + * @param flightAllowed Wheter or not flight is allowed in this world. + */ + void setFlightAllowed(boolean flightAllowed); + } diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java index 76240e42..7bb32618 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java @@ -300,7 +300,7 @@ public class MVPlayerListener implements Listener { */ public void handleGameMode(final Player player, final MultiverseWorld world) { // We perform this task one tick later to MAKE SURE that the player actually reaches the - // destination world, otherwise we'd be changing the player mode if they havent moved anywhere. + // destination world, otherwise we'd be changing the player mode if they haven't moved anywhere. if (!this.pt.playerCanIgnoreGameModeRestriction(world, player)) { this.plugin.log(Level.FINE, "Handeling gamemode for player: " + player.getName()); this.plugin.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin, @@ -308,9 +308,6 @@ public class MVPlayerListener implements Listener { public void run() { // Check that the player is in the new world and they haven't been teleported elsewhere or the event cancelled. if (player.getWorld() == world.getCBWorld()) { - MultiverseCore.staticLog(Level.FINE, "Handling gamemode for player: " + player.getName() + ", " + world.getGameMode().toString()); - MultiverseCore.staticLog(Level.FINE, "PWorld: " + player.getWorld()); - MultiverseCore.staticLog(Level.FINE, "AWorld: " + world); player.setGameMode(world.getGameMode()); } else { MultiverseCore.staticLog(Level.FINE, @@ -323,4 +320,32 @@ public class MVPlayerListener implements Listener { this.plugin.log(Level.FINE, "Player: " + player.getName() + " is IMMUNE to gamemode changes!"); } } + + /** + * Handles the allowflight attribute for the specified {@link Player}. + * @param player The {@link Player}. + * @param world The world the player is in. + */ + public void handleAllowFlight(final Player player, final MultiverseWorld world) { + // We perform this task one tick later to MAKE SURE that the player actually reaches the + // destination world, otherwise we'd be changing the player mode if they haven't moved anywhere. + if (!this.pt.playerCanIgnoreAllowFlightRestriction(world, player)) { + this.plugin.log(Level.FINE, "Handeling flight for player: " + player.getName()); + this.plugin.getServer().getScheduler().scheduleSyncDelayedTask(this.plugin, + new Runnable() { + public void run() { + // Check that the player is in the new world and they haven't been teleported elsewhere or the event cancelled. + if (player.getWorld() == world.getCBWorld()) { + player.setAllowFlight(world.isFlightAllowed()); + } else { + MultiverseCore.staticLog(Level.FINE, + String.format("The gamemode was NOT changed for player '%s' because he is now in world '%s' instead of world '%s'", + player.getName(), player.getWorld().getName(), world.getName())); + } + } + }, 1L); + } else { + this.plugin.log(Level.FINE, "Player: " + player.getName() + " is IMMUNE to flight changes!"); + } + } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/MVPermissions.java b/src/main/java/com/onarandombox/MultiverseCore/utils/MVPermissions.java index a66c0d9b..1588e347 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/MVPermissions.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/MVPermissions.java @@ -50,6 +50,21 @@ public class MVPermissions implements PermissionsInterface { return p.hasPermission("mv.bypass.gamemode." + w.getName()); } + /** + * Check if a Player can ignore GameMode restrictions for world they travel to. + * + * @param p The {@link Player} to check. + * @param w The {@link MultiverseWorld} the player wants to teleport to. + * @return True if they should bypass restrictions. + */ + public boolean canIgnoreAllowFlightRestriction(Player p, MultiverseWorld w) { + if (p.hasPermission("mv.bypass.allowflight.*")) { + this.plugin.log(Level.FINER, "Player has mv.bypass.allowflight.* they can fly!"); + return true; + } + return p.hasPermission("mv.bypass.allowflight." + w.getName()); + } + /** * Check if a Player can teleport to the Destination world from there current world. * diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java b/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java index 2be0c287..41f085c5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java @@ -246,4 +246,21 @@ public class PermissionTools { return true; } } + + /** + * Checks to see if a player should bypass allow-flight restrictions. + * + * @param toWorld world travelling to. + * @param teleportee player travelling. + * @return True if they should bypass restrictions + */ + public boolean playerCanIgnoreAllowFlightRestriction(MultiverseWorld toWorld, Player teleportee) { + if (toWorld != null) { + return this.plugin.getMVPerms().canIgnoreAllowFlightRestriction(teleportee, toWorld); + } else { + // TODO: Determine if this value is false because a world didn't exist + // or if it was because a world wasn't imported. + return true; + } + } }