From 0b1a38700315d4d63e0a5a8335a2aefbf5cb7bc2 Mon Sep 17 00:00:00 2001 From: Ammar Askar Date: Thu, 25 Oct 2012 03:43:15 +0500 Subject: [PATCH] Add player limit per world. Addresses #727 --- .../onarandombox/MultiverseCore/MVWorld.java | 27 +++++++++++++++++++ .../MultiverseCore/api/MultiverseWorld.java | 16 +++++++++++ .../listeners/MVPlayerListener.java | 24 +++++++++++++++-- .../MultiverseCore/utils/PermissionTools.java | 19 +++++++++++++ 4 files changed, 84 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java index c51ba86e..0178b954 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java @@ -76,6 +76,7 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { PROPERTY_ALIASES.put("mode", "gameMode"); PROPERTY_ALIASES.put("diff", "difficulty"); PROPERTY_ALIASES.put("spawnlocation", "spawn"); + PROPERTY_ALIASES.put("limit", "playerLimit"); PROPERTY_ALIASES.put("animals", "spawning.animals.spawn"); PROPERTY_ALIASES.put("monsters", "spawning.monsters.spawn"); PROPERTY_ALIASES.put("animalsrate", "spawning.animals.spawnrate"); @@ -473,12 +474,15 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { private volatile long seed; @Property private volatile String generator; + @Property + private volatile int playerLimit; // End of properties // -------------------------------------------------------------- private Permission permission; private Permission exempt; private Permission ignoreperm; + private Permission limitbypassperm; public MVWorld(boolean fixSpawn) { super(); @@ -584,15 +588,21 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { this.exempt = new Permission("multiverse.exempt." + this.getName(), "A player who has this does not pay to enter this world, or use any MV portals in it " + this.getName(), PermissionDefault.OP); + + this.limitbypassperm = new Permission("mv.bypass.playerlimit." + this.getName(), + "A player who can enter this world regardless of wether its full", PermissionDefault.OP); try { this.plugin.getServer().getPluginManager().addPermission(this.permission); this.plugin.getServer().getPluginManager().addPermission(this.exempt); this.plugin.getServer().getPluginManager().addPermission(this.ignoreperm); + this.plugin.getServer().getPluginManager().addPermission(this.limitbypassperm); // Add the permission and exempt to parents. this.addToUpperLists(this.permission); // Add ignore to it's parent: this.ignoreperm.addParent("mv.bypass.gamemode.*", true); + // Add limit bypass to it's parent + this.limitbypassperm.addParent("mv.bypass.playerlimit.*", true); } catch (IllegalArgumentException e) { this.plugin.log(Level.FINER, "Permissions nodes were already added for " + this.name); } @@ -664,6 +674,7 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { this.bedRespawn = true; this.worldBlacklist = new ArrayList(); this.generator = null; + this.playerLimit = -1; } /** @@ -922,6 +933,22 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld { public void setGenerator(String generator) { this.setPropertyValueUnchecked("generator", generator); } + + /** + * {@inheritDoc} + */ + @Override + public int getPlayerLimit() { + return this.playerLimit; + } + + /** + * {@inheritDoc} + */ + @Override + public void setPlayerLimit(int limit) { + this.setPropertyValueUnchecked("playerLimit", limit); + } /** * {@inheritDoc} diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseWorld.java b/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseWorld.java index 2266594c..a1ebc760 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseWorld.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MultiverseWorld.java @@ -620,6 +620,22 @@ public interface MultiverseWorld { * @param autoLoad True if players dying in this world respawn at their bed. */ void setBedRespawn(boolean autoLoad); + + /** + * Sets the player limit for this world after which players without an override + * permission node will not be allowed in. A value of -1 or less signifies no limit + * + * @param limit The new limit + */ + void setPlayerLimit(int limit); + + /** + * Gets the player limit for this world after which players without an override + * permission node will not be allowed in. A value of -1 or less signifies no limit + * + * @return The player limit + */ + int getPlayerLimit(); /** * Same as {@link #getTime()}, but returns a string. diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java index 632b5b36..c93e00dd 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java @@ -193,6 +193,8 @@ public class MVPlayerListener implements Listener { + "' don't have the FUNDS required to enter it."); return; } + + // Check if player is allowed to enter the world if we're enforcing permissions if (plugin.getMVConfig().getEnforceAccess()) { event.setCancelled(!pt.playerCanGoFromTo(fromWorld, toWorld, teleporter, teleportee)); if (event.isCancelled() && teleporter != null) { @@ -200,13 +202,31 @@ public class MVPlayerListener implements Listener { + "' was DENIED ACCESS to '" + toWorld.getAlias() + "' because '" + teleporter.getName() + "' don't have: multiverse.access." + event.getTo().getWorld().getName()); - } else { - this.stateSuccess(teleportee.getName(), toWorld.getAlias()); + return; } } else { this.plugin.log(Level.FINE, "Player '" + teleportee.getName() + "' was allowed to go to '" + toWorld.getAlias() + "' because enforceaccess is off."); } + + // Does a limit actually exist? + if (toWorld.getPlayerLimit() > -1) { + // Are there equal or more people on the world than the limit? + if (toWorld.getCBWorld().getPlayers().size() >= toWorld.getPlayerLimit()) { + // Ouch the world is full, lets see if the player can bypass that limitation + if (!pt.playerCanBypassPlayerLimit(toWorld, teleporter, teleportee)) { + this.plugin.log(Level.FINE, "Player '" + teleportee.getName() + + "' was DENIED ACCESS to '" + toWorld.getAlias() + + "' because the world is full and '" + teleporter.getName() + + "' doesn't have: mv.bypass.playerlimit." + event.getTo().getWorld().getName()); + event.setCancelled(true); + return; + } + } + } + + // By this point anything cancelling the event has returned on the method, meaning the teleport is a success \o/ + this.stateSuccess(teleportee.getName(), toWorld.getAlias()); } private void stateSuccess(String playerName, String worldName) { diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java b/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java index 2851fc25..d8fd2822 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/PermissionTools.java @@ -257,6 +257,25 @@ public class PermissionTools { } return true; } + + public boolean playerCanBypassPlayerLimit(MultiverseWorld toWorld, CommandSender teleporter, Player teleportee) { + + if (teleporter == null) { + teleporter = teleportee; + } + + if (!(teleporter instanceof Player)) { + return true; + } + + MVPermissions perms = plugin.getMVPerms(); + if (perms.hasPermission(teleportee, "mv.bypass.playerlimit." + toWorld.getName(), false)) { + return true; + } else { + teleporter.sendMessage("The world " + toWorld.getColoredWorldString() + " is full"); + return false; + } + } /** * Checks to see if a player should bypass game mode restrictions.