Add player limit per world. Addresses #727

This commit is contained in:
Ammar Askar 2012-10-25 03:43:15 +05:00
parent a9579a6594
commit 0b1a387003
4 changed files with 84 additions and 2 deletions

View File

@ -76,6 +76,7 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld {
PROPERTY_ALIASES.put("mode", "gameMode"); PROPERTY_ALIASES.put("mode", "gameMode");
PROPERTY_ALIASES.put("diff", "difficulty"); PROPERTY_ALIASES.put("diff", "difficulty");
PROPERTY_ALIASES.put("spawnlocation", "spawn"); PROPERTY_ALIASES.put("spawnlocation", "spawn");
PROPERTY_ALIASES.put("limit", "playerLimit");
PROPERTY_ALIASES.put("animals", "spawning.animals.spawn"); PROPERTY_ALIASES.put("animals", "spawning.animals.spawn");
PROPERTY_ALIASES.put("monsters", "spawning.monsters.spawn"); PROPERTY_ALIASES.put("monsters", "spawning.monsters.spawn");
PROPERTY_ALIASES.put("animalsrate", "spawning.animals.spawnrate"); PROPERTY_ALIASES.put("animalsrate", "spawning.animals.spawnrate");
@ -473,12 +474,15 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld {
private volatile long seed; private volatile long seed;
@Property @Property
private volatile String generator; private volatile String generator;
@Property
private volatile int playerLimit;
// End of properties // End of properties
// -------------------------------------------------------------- // --------------------------------------------------------------
private Permission permission; private Permission permission;
private Permission exempt; private Permission exempt;
private Permission ignoreperm; private Permission ignoreperm;
private Permission limitbypassperm;
public MVWorld(boolean fixSpawn) { public MVWorld(boolean fixSpawn) {
super(); super();
@ -584,15 +588,21 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld {
this.exempt = new Permission("multiverse.exempt." + this.getName(), 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); "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 { try {
this.plugin.getServer().getPluginManager().addPermission(this.permission); this.plugin.getServer().getPluginManager().addPermission(this.permission);
this.plugin.getServer().getPluginManager().addPermission(this.exempt); this.plugin.getServer().getPluginManager().addPermission(this.exempt);
this.plugin.getServer().getPluginManager().addPermission(this.ignoreperm); this.plugin.getServer().getPluginManager().addPermission(this.ignoreperm);
this.plugin.getServer().getPluginManager().addPermission(this.limitbypassperm);
// Add the permission and exempt to parents. // Add the permission and exempt to parents.
this.addToUpperLists(this.permission); this.addToUpperLists(this.permission);
// Add ignore to it's parent: // Add ignore to it's parent:
this.ignoreperm.addParent("mv.bypass.gamemode.*", true); this.ignoreperm.addParent("mv.bypass.gamemode.*", true);
// Add limit bypass to it's parent
this.limitbypassperm.addParent("mv.bypass.playerlimit.*", true);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
this.plugin.log(Level.FINER, "Permissions nodes were already added for " + this.name); 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.bedRespawn = true;
this.worldBlacklist = new ArrayList<String>(); this.worldBlacklist = new ArrayList<String>();
this.generator = null; this.generator = null;
this.playerLimit = -1;
} }
/** /**
@ -923,6 +934,22 @@ public class MVWorld extends SerializationConfig implements MultiverseWorld {
this.setPropertyValueUnchecked("generator", 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} * {@inheritDoc}
*/ */

View File

@ -621,6 +621,22 @@ public interface MultiverseWorld {
*/ */
void setBedRespawn(boolean autoLoad); 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. * Same as {@link #getTime()}, but returns a string.
* @return The time as a short string: 12:34pm * @return The time as a short string: 12:34pm

View File

@ -193,6 +193,8 @@ public class MVPlayerListener implements Listener {
+ "' don't have the FUNDS required to enter it."); + "' don't have the FUNDS required to enter it.");
return; return;
} }
// Check if player is allowed to enter the world if we're enforcing permissions
if (plugin.getMVConfig().getEnforceAccess()) { if (plugin.getMVConfig().getEnforceAccess()) {
event.setCancelled(!pt.playerCanGoFromTo(fromWorld, toWorld, teleporter, teleportee)); event.setCancelled(!pt.playerCanGoFromTo(fromWorld, toWorld, teleporter, teleportee));
if (event.isCancelled() && teleporter != null) { if (event.isCancelled() && teleporter != null) {
@ -200,13 +202,31 @@ public class MVPlayerListener implements Listener {
+ "' was DENIED ACCESS to '" + toWorld.getAlias() + "' was DENIED ACCESS to '" + toWorld.getAlias()
+ "' because '" + teleporter.getName() + "' because '" + teleporter.getName()
+ "' don't have: multiverse.access." + event.getTo().getWorld().getName()); + "' don't have: multiverse.access." + event.getTo().getWorld().getName());
} else { return;
this.stateSuccess(teleportee.getName(), toWorld.getAlias());
} }
} else { } else {
this.plugin.log(Level.FINE, "Player '" + teleportee.getName() this.plugin.log(Level.FINE, "Player '" + teleportee.getName()
+ "' was allowed to go to '" + toWorld.getAlias() + "' because enforceaccess is off."); + "' 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) { private void stateSuccess(String playerName, String worldName) {

View File

@ -258,6 +258,25 @@ public class PermissionTools {
return true; 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. * Checks to see if a player should bypass game mode restrictions.
* *