Commits

James Crasta  committed a04afb4

Add in a way to limit mondochests per user.

  • Participants
  • Parent commits c46795b

Comments (0)

Files changed (7)

+Version 0.6.1: Not yet released.
+ * Allow setting maximum number of MondoChests per player.
+
 Version 0.6: 2012-08-22
  * Add ability to define the users who can access a MondoChest
  * Add in MCStats metrics.
         # How far away from a mondo chest the slave can be
         max_add_radius: 150
 
+    mondochests:
+        # Allows limiting how many MondoChests(masters)  a given user can make.
+        per_user: unlimited
+
 
 #######################
 ## Restacking feature
 name: MondoChest
 main: us.crast.mondochest.MondoChest
-version: 0.6
+version: 0.6.1-pre1
 author: Crast
 description: "Sort your items into appropriate chests"
 website: "http://dev.bukkit.org/server-mods/mondochest/"
             mondochest.admin.break_any: true
             mondochest.admin.add_any_slave: true
             mondochest.admin.reload: true
+            mondochest.admin.no_master_limit: true
     mondochest.create_master:
         description: Create a new MondoChest bank
         default: op
     mondochest.add_slave:
-        description: Manually add new MondoSlaves
+        description: Add MondoSlaves to a MondoChest bank
         default: op
     mondochest.use:
+        description: Use MondoChest sign
         default: true
     mondochest.find:
         description: "Find items in a mondochest set"
-        default: true
+        default: op
     mondochest.admin.break_any:
         description: "Break anybody's MondoChest signs"
         default: op
     mondochest.admin.reload:
         description: "Reload the MondoChest config"
         default: op
+    mondochest.admin.no_master_limit:
+        description: "Can make unlimited MondoChests"
+        default: op
 
 commands:
     mondochest:

File src/us/crast/mondochest/MondoConfig.java

 	public static int SLAVE_MAX_ADD_RADIUS = 150;
 	public static int FIND_MAX_RADIUS = 300;
 	public static int SLAVES_PER_MASTER = -1;
+	public static int MASTERS_PER_USER = -1;
 
 	public static Permission VAULT_PERMISSIONS = null;
 	public static boolean USE_COMMANDS = true;
 		SLAVE_MAX_ADD_RADIUS = getLimit(config, "limits.slaves.max_add_radius");
 		FIND_MAX_RADIUS = getLimit(config, "limits.find_max_radius");
 		SLAVES_PER_MASTER = getLimit(config, "limits.slaves.per_master");
+		MASTERS_PER_USER = getLimit(config, "limits.mondochests.per_user");
 		RESTACK_MASTER = config.getBoolean("restack_master");
 		RESTACK_SLAVES = config.getBoolean("restack_slaves");
 		PROTECTION_SIGNS = config.getBoolean("protection.signs");

File src/us/crast/mondochest/MondoConstants.java

 
 public final class MondoConstants {
 	public static final String APP_NAME = "MondoChest";
-	public static final String MONDOCHEST_VERSION = "0.6";
+	public static final String MONDOCHEST_VERSION = "0.6.1-pre1";
 	
 	public static final String MASTER_SIGN_NAME = "[MondoChest]";
 	public static final String SLAVE_SIGN_NAME = "[MondoSlave]";

File src/us/crast/mondochest/MondoListener.java

 	private PermissionChecker can_override_break;
 	private PermissionChecker can_override_add_slave;
     private PermissionChecker can_override_open;
+    private PermissionChecker can_override_master_limit;
 
 	
 	private final BankManager bankManager;
 				return new BasicMessage("No permissions to create new bank", Status.WARNING);
 			}
 			
+			// Deal with masters per user limit.
+			if (MondoConfig.MASTERS_PER_USER != -1 && !can_override_master_limit.check(player)) {
+			    if (numBanks(player) >= MondoConfig.MASTERS_PER_USER) {
+			        return new BasicMessage(Status.WARNING, "You have passed the limit of {RED}%d{WARNING} banks", MondoConfig.MASTERS_PER_USER);
+			    }
+			}
+			
 			bank = bankFromSign(sign, player.getName()); // propagates MondoMessage
 			bankManager.addBank(world.getName(), vec, bank);
 			bankManager.save(); // propagates MondoMessage
 	    }
 	}
 
-	private MessageWithStatus slaveClickedCommon(Block block, Sign sign, Player player, String noun, Material material) throws MondoMessage {
-		Location lastClicked = getLastClicked(player);
+    private MessageWithStatus slaveClickedCommon(Block block, Sign sign, Player player, String noun, Material material) throws MondoMessage {
+	    Location lastClicked = getLastClicked(player);
 		if (lastClicked == null) {
 			return new BasicMessage("To add slaves to a bank, click a master sign first", Status.USAGE);
 		}
         }
     }
     
+    /**
+     * Find out how many banks a player has owned by them.
+     * @return number of banks, zero if none are owned by this player.
+     */
+    private int numBanks(final Player player) {
+        int myBanks = 0;
+        for (BankSet bs: bankManager.listAllBanks()) {
+            if (player.getName().equalsIgnoreCase(bs.getOwner())) {
+                myBanks++;
+            }
+        }
+        return myBanks;
+    }
+    
     public void reloadConfig() {
         this.can_use = MondoSecurity.getChecker("mondochest.use");
         this.can_add_slave = MondoSecurity.getChecker("mondochest.add_slave");
         this.can_override_break = MondoSecurity.getChecker("mondochest.admin.break_any");
         this.can_override_open = MondoSecurity.getChecker("mondochest.admin.open_any");
         this.can_override_add_slave = MondoSecurity.getChecker("mondochest.admin.add_any_slave");
+        this.can_override_master_limit = MondoSecurity.getChecker("mondochest.admin.no_master_limit");
     }
 
 }

File src/us/crast/mondochest/persist/BankManager.java

 
 import java.io.File;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Set;
 
 import org.bukkit.Location;
 		changed.add(bank);
 	}
 	
+	public List<BankSet> listAllBanks() {
+	    List<BankSet> items = new ArrayList<BankSet>();
+	    for (Entry<String, Map<BlockVector, BankSet>> e: banks.entrySet()) {
+	        for (BankSet bs : e.getValue().values()) {
+	            items.add(bs);
+	        }
+	    }
+	    return items;
+	}
+	
 	private ConfigurationSection ensureSection(ConfigurationSection base, String name) {
 		ConfigurationSection section = base.getConfigurationSection(name);
 		if (section == null) {