Skip to content
This repository
Browse code

First commit of TownyPerms - Not tested saving/loading to sql or leaving

towns/nations yet (but should work).
Bug fix commits to follow.
  • Loading branch information...
commit 9860eb2239f6f9725ea28ed21261ec7a4cdf74ba 1 parent 3203146
ElgarL authored
6 .classpath
@@ -10,7 +10,11 @@
10 10 <classpathentry kind="lib" path="../lib/PermissionsEx.jar"/>
11 11 <classpathentry kind="lib" path="../lib/Questioner.jar"/>
12 12 <classpathentry kind="lib" path="../lib/Register.jar"/>
13   - <classpathentry kind="lib" path="../lib/bukkit.jar"/>
  13 + <classpathentry kind="lib" path="../lib/bukkit.jar">
  14 + <attributes>
  15 + <attribute name="javadoc_location" value="http://jd.bukkit.org/apidocs/"/>
  16 + </attributes>
  17 + </classpathentry>
14 18 <classpathentry kind="lib" path="../lib/citizensapi-2.0-SNAPSHOT.jar"/>
15 19 <classpathentry kind="lib" path="../lib/Vault.jar"/>
16 20 <classpathentry kind="output" path="bin"/>
3  build.xml
@@ -39,7 +39,8 @@
39 39 <fileset dir="./src" includes="english.yml"/>
40 40 <fileset dir="./src" includes="german.yml"/>
41 41 <fileset dir="./src" includes="plugin.yml"/>
42   - <fileset dir="./src" includes="spanish.yml"/>
  42 + <fileset dir="./src" includes="spanish.yml"/>
  43 + <fileset dir="./src" includes="townyperms.yml"/>
43 44 <fileset dir="./src" includes="ToDo.txt"/>
44 45 </jar>
45 46 </target>
1  src/com/palmergames/bukkit/towny/Towny.java
@@ -85,6 +85,7 @@ public void onEnable() {
85 85 TownyFormatter.initialize(this);
86 86 TownyRegenAPI.initialize(this);
87 87 PlayerCacheUtil.initialize(this);
  88 + TownyPerms.initialize(this);
88 89
89 90 if (load()) {
90 91 // Setup bukkit command interfaces
2  src/com/palmergames/bukkit/towny/TownySettings.java
@@ -213,7 +213,7 @@ public static void loadLanguage(String filepath, String defaultRes) throws IOExc
213 213 String res = getString(ConfigNodes.LANGUAGE.getRoot(), defaultRes);
214 214 String fullPath = filepath + FileMgmt.fileSeparator() + res;
215 215
216   - File file = FileMgmt.unpackLanguageFile(fullPath, res, defaultRes);
  216 + File file = FileMgmt.unpackResourceFile(fullPath, res, defaultRes);
217 217 if (file != null) {
218 218
219 219 // read the (language).yml into memory
107 src/com/palmergames/bukkit/towny/command/NationCommand.java
@@ -31,6 +31,7 @@
31 31 import com.palmergames.bukkit.towny.object.Town;
32 32 import com.palmergames.bukkit.towny.object.TownyUniverse;
33 33 import com.palmergames.bukkit.towny.permissions.PermissionNodes;
  34 +import com.palmergames.bukkit.towny.permissions.TownyPerms;
34 35 import com.palmergames.bukkit.towny.questioner.JoinNationTask;
35 36 import com.palmergames.bukkit.towny.questioner.ResidentNationQuestionTask;
36 37 import com.palmergames.bukkit.util.ChatTools;
@@ -164,7 +165,9 @@ else if (split[0].equalsIgnoreCase("withdraw")) {
164 165 } else {
165 166 String[] newSplit = StringMgmt.remFirstArg(split);
166 167
167   - if (split[0].equalsIgnoreCase("king"))
  168 + if (split[0].equalsIgnoreCase("rank"))
  169 + nationRank(player, newSplit);
  170 + else if (split[0].equalsIgnoreCase("king"))
168 171 nationKing(player, newSplit);
169 172 else if (split[0].equalsIgnoreCase("add"))
170 173 nationAdd(player, newSplit);
@@ -200,6 +203,102 @@ else if (split[0].equalsIgnoreCase("online")) {
200 203 }
201 204 }
202 205 }
  206 +
  207 + public void nationRank(Player player, String[] split) {
  208 +
  209 + if (split.length == 0) {
  210 + //Help output.
  211 + player.sendMessage(ChatTools.formatTitle("/nation rank"));
  212 + player.sendMessage(ChatTools.formatCommand("", "/nation rank", "add/remove [resident] rank", ""));
  213 +
  214 + } else {
  215 +
  216 + Resident resident, target;
  217 + Town town = null;
  218 + Town targetTown = null;
  219 + String rank;
  220 +
  221 + /*
  222 + * Does the command have enough arguments?
  223 + */
  224 + if (split.length < 3) {
  225 + TownyMessaging.sendErrorMsg(player, "Eg: /town rank add/remove [resident] [rank]");
  226 + return;
  227 + }
  228 +
  229 + try {
  230 + resident = TownyUniverse.getDataSource().getResident(player.getName());
  231 + target = TownyUniverse.getDataSource().getResident(split[1]);
  232 + town = resident.getTown();
  233 + targetTown = target.getTown();
  234 +
  235 + if (town.getNation() != targetTown.getNation())
  236 + throw new TownyException("This resident is not a member of your Town!");
  237 +
  238 + } catch (TownyException x) {
  239 + TownyMessaging.sendErrorMsg(player, x.getMessage());
  240 + return;
  241 + }
  242 +
  243 + rank = split[2].toLowerCase();
  244 + /*
  245 + * Is this a known rank?
  246 + */
  247 + if (!TownyPerms.getNationRanks().contains(rank)) {
  248 + TownyMessaging.sendErrorMsg(player, "Unknown rank '" + rank + "'. Permissible ranks are :- " + StringMgmt.join(TownyPerms.getNationRanks(), ",") + ".");
  249 + return;
  250 + }
  251 + /*
  252 + * Only allow the player to assign ranks if they have the grant perm for it.
  253 + */
  254 + if (!TownyUniverse.getPermissionSource().has(player, "towny.nation.grant-" + rank)) {
  255 + TownyMessaging.sendErrorMsg(player, "You do not have permission to grant this rank.");
  256 + return;
  257 + }
  258 +
  259 + if (split[0].equalsIgnoreCase("add")) {
  260 + try {
  261 + if (target.addNationRank(rank)) {
  262 + TownyMessaging.sendMsg(target, "You have been granted the Nation rank of '" + rank + "'.");
  263 + TownyMessaging.sendMsg(player, "You have granted the Nation rank of '" + rank + "' to " + target.getName() + ".");
  264 + } else {
  265 + // Not in a nation or Rank doesn't exist
  266 + TownyMessaging.sendErrorMsg(player, "That resident isn't a member of a town!");
  267 + return;
  268 + }
  269 + } catch (AlreadyRegisteredException e) {
  270 + // Must already have this rank
  271 + TownyMessaging.sendMsg(player, target.getName() + " already holds this Nation rank.");
  272 + return;
  273 + }
  274 +
  275 + } else if (split[0].equalsIgnoreCase("remove")) {
  276 + try {
  277 + if (target.removeTownRank(rank)) {
  278 + TownyMessaging.sendMsg(target, "You have been demoted from the Nation rank of '" + rank + "'.");
  279 + TownyMessaging.sendMsg(player, "You have removed the Nation rank of '" + rank + "' from " + target.getName() + ".");
  280 + }
  281 + } catch (NotRegisteredException e) {
  282 + // Must already have this rank
  283 + TownyMessaging.sendMsg(player, target.getName() + " doesn't hold this Nation rank.");
  284 + return;
  285 + }
  286 +
  287 +
  288 + } else {
  289 + TownyMessaging.sendErrorMsg(player, String.format(TownySettings.getLangString("msg_err_invalid_property"), split[0]));
  290 + return;
  291 + }
  292 +
  293 + /*
  294 + * If we got here we have made a change
  295 + * Save the altered resident data.
  296 + */
  297 + TownyUniverse.getDataSource().saveResident(target);
  298 +
  299 + }
  300 +
  301 + }
203 302
204 303 private void nationWithdraw(Player player, int amount) {
205 304
@@ -632,7 +731,8 @@ public void nationAssistantsAdd(Player player, Nation nation, List<Resident> inv
632 731 ArrayList<Resident> remove = new ArrayList<Resident>();
633 732 for (Resident newMember : invited)
634 733 try {
635   - nation.addAssistant(newMember);
  734 + //nation.addAssistant(newMember);
  735 + newMember.addNationRank("assistant");
636 736 plugin.deleteCache(newMember.getName());
637 737 TownyUniverse.getDataSource().saveResident(newMember);
638 738 } catch (AlreadyRegisteredException e) {
@@ -686,7 +786,8 @@ public void nationAssistantsRemove(Player player, Resident resident, Nation nati
686 786 ArrayList<Resident> remove = new ArrayList<Resident>();
687 787 for (Resident member : kicking)
688 788 try {
689   - nation.removeAssistant(member);
  789 + //nation.removeAssistant(member);
  790 + member.removeNationRank("assistant");
690 791 plugin.deleteCache(member.getName());
691 792 TownyUniverse.getDataSource().saveResident(member);
692 793 TownyUniverse.getDataSource().saveNation(nation);
123 src/com/palmergames/bukkit/towny/command/TownCommand.java
@@ -11,6 +11,7 @@
11 11 import com.palmergames.bukkit.towny.exceptions.*;
12 12 import com.palmergames.bukkit.towny.object.*;
13 13 import com.palmergames.bukkit.towny.permissions.PermissionNodes;
  14 +import com.palmergames.bukkit.towny.permissions.TownyPerms;
14 15 import com.palmergames.bukkit.towny.questioner.JoinTownTask;
15 16 import com.palmergames.bukkit.towny.questioner.ResidentTownQuestionTask;
16 17 import com.palmergames.bukkit.towny.regen.PlotBlockData;
@@ -62,6 +63,7 @@
62 63 output.add(ChatTools.formatCommand("", "/town", "new [town]", TownySettings.getLangString("town_help_6")));
63 64 output.add(ChatTools.formatCommand(TownySettings.getLangString("admin_sing"), "/town", "new [town] " + TownySettings.getLangString("town_help_2"), TownySettings.getLangString("town_help_7")));
64 65 output.add(ChatTools.formatCommand(TownySettings.getLangString("res_sing"), "/town", "deposit [$]", ""));
  66 + output.add(ChatTools.formatCommand(TownySettings.getLangString("res_sing"), "/town", "rank add/remove [resident] [rank]", ""));
65 67 output.add(ChatTools.formatCommand(TownySettings.getLangString("mayor_sing"), "/town", "mayor ?", TownySettings.getLangString("town_help_8")));
66 68 output.add(ChatTools.formatCommand(TownySettings.getLangString("admin_sing"), "/town", "delete [town]", ""));
67 69 }
@@ -133,7 +135,9 @@ else if (split.length == 2)
133 135 } else {
134 136 String[] newSplit = StringMgmt.remFirstArg(split);
135 137
136   - if (split[0].equalsIgnoreCase("set"))
  138 + if (split[0].equalsIgnoreCase("rank"))
  139 + townRank(player, newSplit);
  140 + else if (split[0].equalsIgnoreCase("set"))
137 141 townSet(player, newSplit);
138 142 else if (split[0].equalsIgnoreCase("buy"))
139 143 townBuy(player, newSplit);
@@ -394,6 +398,100 @@ private void toggleTest(Player player, Town town, String split) throws TownyExce
394 398 }
395 399 }
396 400
  401 + public void townRank(Player player, String[] split) {
  402 +
  403 + if (split.length == 0) {
  404 + //Help output.
  405 + player.sendMessage(ChatTools.formatTitle("/town rank"));
  406 + player.sendMessage(ChatTools.formatCommand("", "/town rank", "add/remove [resident] rank", ""));
  407 +
  408 + } else {
  409 +
  410 + Resident resident, target;
  411 + Town town = null;
  412 + String rank;
  413 +
  414 + /*
  415 + * Does the command have enough arguments?
  416 + */
  417 + if (split.length < 3) {
  418 + TownyMessaging.sendErrorMsg(player, "Eg: /town rank add/remove [resident] [rank]");
  419 + return;
  420 + }
  421 +
  422 + try {
  423 + resident = TownyUniverse.getDataSource().getResident(player.getName());
  424 + target = TownyUniverse.getDataSource().getResident(split[1]);
  425 + town = resident.getTown();
  426 +
  427 + if (town != target.getTown())
  428 + throw new TownyException("This resident is not a member of your Town!");
  429 +
  430 + } catch (TownyException x) {
  431 + TownyMessaging.sendErrorMsg(player, x.getMessage());
  432 + return;
  433 + }
  434 +
  435 + rank = split[2].toLowerCase();
  436 + /*
  437 + * Is this a known rank?
  438 + */
  439 + if (!TownyPerms.getTownRanks().contains(rank)) {
  440 + TownyMessaging.sendErrorMsg(player, "Unknown rank '" + rank + "'. Permissible ranks are :- " + StringMgmt.join(TownyPerms.getTownRanks(), ",") + ".");
  441 + return;
  442 + }
  443 + /*
  444 + * Only allow the player to assign ranks if they have the grant perm for it.
  445 + */
  446 + if (!TownyUniverse.getPermissionSource().has(player, "towny.town.grant-" + rank)) {
  447 + TownyMessaging.sendErrorMsg(player, "You do not have permission to grant this rank.");
  448 + return;
  449 + }
  450 +
  451 + if (split[0].equalsIgnoreCase("add")) {
  452 + try {
  453 + if (target.addTownRank(rank)) {
  454 + TownyMessaging.sendMsg(target, "You have been granted the Town rank of '" + rank + "'.");
  455 + TownyMessaging.sendMsg(player, "You have granted the Town rank of '" + rank + "' to " + target.getName() + ".");
  456 + } else {
  457 + // Not in a town or Rank doesn't exist
  458 + TownyMessaging.sendErrorMsg(player, "That resident isn't a member of a town!");
  459 + return;
  460 + }
  461 + } catch (AlreadyRegisteredException e) {
  462 + // Must already have this rank
  463 + TownyMessaging.sendMsg(player, target.getName() + " already holds this Town rank.");
  464 + return;
  465 + }
  466 +
  467 + } else if (split[0].equalsIgnoreCase("remove")) {
  468 + try {
  469 + if (target.removeTownRank(rank)) {
  470 + TownyMessaging.sendMsg(target, "You have been demoted from the Town rank of '" + rank + "'.");
  471 + TownyMessaging.sendMsg(player, "You have removed the Town rank of '" + rank + "' from " + target.getName() + ".");
  472 + }
  473 + } catch (NotRegisteredException e) {
  474 + // Must already have this rank
  475 + TownyMessaging.sendMsg(player, target.getName() + " doesn't hold this Town rank.");
  476 + return;
  477 + }
  478 +
  479 +
  480 + } else {
  481 + TownyMessaging.sendErrorMsg(player, String.format(TownySettings.getLangString("msg_err_invalid_property"), split[0]));
  482 + return;
  483 + }
  484 +
  485 + /*
  486 + * If we got here we have made a change
  487 + * Save the altered resident data.
  488 + */
  489 + TownyUniverse.getDataSource().saveResident(target);
  490 +
  491 + }
  492 +
  493 + }
  494 +
397 495 public void townSet(Player player, String[] split) {
398 496
399 497 if (split.length == 0) {
@@ -1358,16 +1456,20 @@ public void townAssistantsAdd(Player player, Town town, List<Resident> invited)
1358 1456 //TODO: change variable names from townAdd copypasta
1359 1457 ArrayList<Resident> remove = new ArrayList<Resident>();
1360 1458 for (Resident newMember : invited)
1361   - try {
1362   - town.addAssistant(newMember);
1363   - plugin.deleteCache(newMember.getName());
1364   - TownyUniverse.getDataSource().saveResident(newMember);
1365   - } catch (AlreadyRegisteredException e) {
1366   - remove.add(newMember);
1367   - } catch (NotRegisteredException e) {
  1459 + if (town.hasResident(newMember)) {
  1460 + try {
  1461 + //town.addAssistant(newMember);
  1462 + newMember.addTownRank("assistant");
  1463 + plugin.deleteCache(newMember.getName());
  1464 + TownyUniverse.getDataSource().saveResident(newMember);
  1465 + } catch (AlreadyRegisteredException e) {
  1466 + remove.add(newMember);
  1467 + }
  1468 + } else {
1368 1469 remove.add(newMember);
1369   - TownyMessaging.sendErrorMsg(player, e.getMessage());
  1470 + TownyMessaging.sendErrorMsg(player, newMember.getName() + " doesn't belong to your town.");
1370 1471 }
  1472 +
1371 1473 for (Resident newMember : remove)
1372 1474 invited.remove(newMember);
1373 1475
@@ -1416,7 +1518,8 @@ public void townAssistantsRemove(Player player, Resident resident, Town town, Li
1416 1518
1417 1519 for (Resident member : toKick)
1418 1520 try {
1419   - town.removeAssistant(member);
  1521 + //town.removeAssistant(member);
  1522 + member.removeTownRank("assistant");
1420 1523 plugin.deleteCache(member.getName());
1421 1524 TownyUniverse.getDataSource().saveResident(member);
1422 1525 TownyUniverse.getDataSource().saveTown(town);
58 src/com/palmergames/bukkit/towny/db/TownyFlatFileSource.java
@@ -14,6 +14,7 @@
14 14 import java.nio.charset.Charset;
15 15 import java.text.SimpleDateFormat;
16 16 import java.util.ArrayList;
  17 +import java.util.Arrays;
17 18 import java.util.HashMap;
18 19 import java.util.List;
19 20 import java.util.Set;
@@ -464,6 +465,14 @@ public boolean loadResident(Resident resident) {
464 465 line = kvFile.get("town");
465 466 if (line != null)
466 467 resident.setTown(getTown(line));
  468 +
  469 + line = kvFile.get("town-ranks");
  470 + if (line != null)
  471 + resident.setTownRanks(new ArrayList<String>(Arrays.asList((line.split(",")))));
  472 +
  473 + line = kvFile.get("nation-ranks");
  474 + if (line != null)
  475 + resident.setNationRanks(new ArrayList<String>(Arrays.asList((line.split(",")))));
467 476
468 477 line = kvFile.get("friends");
469 478 if (line != null) {
@@ -522,17 +531,17 @@ public boolean loadTown(Town town) {
522 531 if (line != null)
523 532 town.setMayor(getResident(line));
524 533
525   - line = kvFile.get("assistants");
526   - if (line != null) {
527   - tokens = line.split(",");
528   - for (String token : tokens) {
529   - if (!token.isEmpty()) {
530   - Resident assistant = getResident(token);
531   - if ((assistant != null) && (town.hasResident(assistant)))
532   - town.addAssistant(assistant);
533   - }
534   - }
535   - }
  534 +// line = kvFile.get("assistants");
  535 +// if (line != null) {
  536 +// tokens = line.split(",");
  537 +// for (String token : tokens) {
  538 +// if (!token.isEmpty()) {
  539 +// Resident assistant = getResident(token);
  540 +// if ((assistant != null) && (town.hasResident(assistant)))
  541 +// town.addAssistant(assistant);
  542 +// }
  543 +// }
  544 +// }
536 545
537 546 town.setTownBoard(kvFile.get("townBoard"));
538 547
@@ -800,17 +809,17 @@ public boolean loadNation(Nation nation) {
800 809 if (line != null)
801 810 nation.setCapital(getTown(line));
802 811
803   - line = kvFile.get("assistants");
804   - if (line != null) {
805   - tokens = line.split(",");
806   - for (String token : tokens) {
807   - if (!token.isEmpty()) {
808   - Resident assistant = getResident(token);
809   - if (assistant != null)
810   - nation.addAssistant(assistant);
811   - }
812   - }
813   - }
  812 +// line = kvFile.get("assistants");
  813 +// if (line != null) {
  814 +// tokens = line.split(",");
  815 +// for (String token : tokens) {
  816 +// if (!token.isEmpty()) {
  817 +// Resident assistant = getResident(token);
  818 +// if (assistant != null)
  819 +// nation.addAssistant(assistant);
  820 +// }
  821 +// }
  822 +// }
814 823
815 824 line = kvFile.get("tag");
816 825 if (line != null)
@@ -1393,8 +1402,11 @@ public boolean saveResident(Resident resident) {
1393 1402 fout.write("title=" + resident.getTitle() + newLine);
1394 1403 // surname
1395 1404 fout.write("surname=" + resident.getSurname() + newLine);
1396   - if (resident.hasTown())
  1405 + if (resident.hasTown()) {
1397 1406 fout.write("town=" + resident.getTown().getName() + newLine);
  1407 + fout.write("town-ranks=" + StringMgmt.join(resident.getTownRanks(), ",") + newLine);
  1408 + fout.write("nation-ranks=" + StringMgmt.join(resident.getNationRanks(), ",") + newLine);
  1409 + }
1398 1410 // Friends
1399 1411 fout.write("friends=");
1400 1412 for (Resident friend : resident.getFriends())
36 src/com/palmergames/bukkit/towny/db/TownyHModFlatFileSource.java
@@ -202,15 +202,15 @@ public boolean loadTown(Town town) {
202 202 if (line != null)
203 203 town.setMayor(getResident(line));
204 204
205   - line = kvFile.get("assistants");
206   - if (line != null) {
207   - tokens = line.split(",");
208   - for (String token : tokens) {
209   - Resident assistant = getResident(token);
210   - if (assistant != null)
211   - town.addAssistant(assistant);
212   - }
213   - }
  205 +// line = kvFile.get("assistants");
  206 +// if (line != null) {
  207 +// tokens = line.split(",");
  208 +// for (String token : tokens) {
  209 +// Resident assistant = getResident(token);
  210 +// if (assistant != null)
  211 +// town.addAssistant(assistant);
  212 +// }
  213 +// }
214 214
215 215 town.setTownBoard(kvFile.get("townBoard"));
216 216
@@ -322,15 +322,15 @@ public boolean loadNation(Nation nation) {
322 322 line = kvFile.get("capital");
323 323 nation.setCapital(getTown(line));
324 324
325   - line = kvFile.get("assistants");
326   - if (line != null) {
327   - tokens = line.split(",");
328   - for (String token : tokens) {
329   - Resident assistant = getResident(token);
330   - if (assistant != null)
331   - nation.addAssistant(assistant);
332   - }
333   - }
  325 +// line = kvFile.get("assistants");
  326 +// if (line != null) {
  327 +// tokens = line.split(",");
  328 +// for (String token : tokens) {
  329 +// Resident assistant = getResident(token);
  330 +// if (assistant != null)
  331 +// nation.addAssistant(assistant);
  332 +// }
  333 +// }
334 334
335 335 line = kvFile.get("allies");
336 336 if (line != null) {
88 src/com/palmergames/bukkit/towny/db/TownySQLSource.java
@@ -178,6 +178,8 @@ public void initialize(Towny plugin, TownyUniverse universe) {
178 178 String resident_create = "CREATE TABLE IF NOT EXISTS " + tb_prefix + "RESIDENTS ("
179 179 + " `name` VARCHAR(16) NOT NULL,"
180 180 + "`town` mediumtext,"
  181 + + "`town-ranks` mediumtext,"
  182 + + "`nation-ranks` mediumtext,"
181 183 + "`lastOnline` BIGINT NOT NULL,"
182 184 + "`registered` BIGINT NOT NULL,"
183 185 + "`isNPC` bool NOT NULL DEFAULT '0',"
@@ -195,6 +197,28 @@ public void initialize(Towny plugin, TownyUniverse universe) {
195 197 } catch (SQLException ee) {
196 198 TownyMessaging.sendErrorMsg("Error Creating table RESIDENTS :" + ee.getMessage());
197 199 }
  200 +
  201 + /*
  202 + * Update the table structure for older databases.
  203 + */
  204 + String resident_update = "IF EXISTS( SELECT NULL FROM INFORMATION_SCHEMA.COLUMNS "
  205 + + "WHERE table_name = '" + tb_prefix + "RESIDENTS' "
  206 + + "AND table_schema = '" + db_name + "' "
  207 + + "AND column_name != 'town-ranks') THEN "
  208 + + "ALTER TABLE " + tb_prefix + "RESIDENTS (ADD "
  209 + + "`town-ranks` mediumtext,"
  210 + + "`nation-ranks` mediumtext"
  211 + + ");";
  212 +
  213 + try {
  214 + Statement s = cntx.createStatement();
  215 + s.executeUpdate(resident_update);
  216 + TownyMessaging.sendDebugMsg("Table RESIDENTS is updated!");
  217 + } catch (SQLException ee) {
  218 + TownyMessaging.sendErrorMsg("Error updating table RESIDENTS :" + ee.getMessage());
  219 + }
  220 +
  221 +
198 222
199 223 String nation_create = "CREATE TABLE IF NOT EXISTS " + tb_prefix + "NATIONS ("
200 224 + "`name` VARCHAR(32) NOT NULL,"
@@ -599,7 +623,19 @@ public boolean loadResident(Resident resident) {
599 623 String line = rs.getString("town");
600 624 if ((line != null) && (!line.isEmpty())) {
601 625 resident.setTown(getTown(line));
602   - TownyMessaging.sendDebugMsg("Resident " + resident.getName() + " set to Town " + rs.getString("town"));
  626 + TownyMessaging.sendDebugMsg("Resident " + resident.getName() + " set to Town " + line);
  627 + }
  628 +
  629 + line = rs.getString("town-rank");
  630 + if ((line != null) && (!line.isEmpty())) {
  631 + resident.setTownRanks(new ArrayList<String>(Arrays.asList((line.split(",")))));
  632 + TownyMessaging.sendDebugMsg("Resident " + resident.getName() + " set Town-ranks " + line);
  633 + }
  634 +
  635 + line = rs.getString("nation-rank");
  636 + if ((line != null) && (!line.isEmpty())) {
  637 + resident.setNationRanks(new ArrayList<String>(Arrays.asList((line.split(",")))));
  638 + TownyMessaging.sendDebugMsg("Resident " + resident.getName() + " set Nation-ranks " + line);
603 639 }
604 640
605 641 try {
@@ -651,7 +687,7 @@ public boolean loadTown(Town town) {
651 687
652 688 try {
653 689 Statement s = cntx.createStatement();
654   - ResultSet rs = s.executeQuery("SELECT " + "residents,mayor,assistants,townBoard,nation,tag,protectionStatus,bonus,purchased,plotPrice,hasUpkeep,taxpercent,taxes" + ",plotTax,commercialPlotPrice,commercialPlotTax,embassyPlotPrice,embassyPlotTax" + ",open,public,townBlocks,homeBlock,spawn,outpostSpawns" + " FROM " + tb_prefix + "TOWNS " + " WHERE name='" + town.getName() + "'");
  690 + ResultSet rs = s.executeQuery("SELECT " + "residents,mayor,townBoard,nation,tag,protectionStatus,bonus,purchased,plotPrice,hasUpkeep,taxpercent,taxes" + ",plotTax,commercialPlotPrice,commercialPlotTax,embassyPlotPrice,embassyPlotTax" + ",open,public,townBlocks,homeBlock,spawn,outpostSpawns" + " FROM " + tb_prefix + "TOWNS " + " WHERE name='" + town.getName() + "'");
655 691
656 692 while (rs.next()) {
657 693
@@ -667,17 +703,17 @@ public boolean loadTown(Town town) {
667 703 }
668 704 }
669 705 town.setMayor(getResident(rs.getString("mayor")));
670   - line = rs.getString("assistants");
671   - if (line != null) {
672   - tokens = line.split(",");
673   - for (String token : tokens) {
674   - if (!token.isEmpty()) {
675   - Resident assistant = getResident(token);
676   - if ((assistant != null) && (town.hasResident(assistant)))
677   - town.addAssistant(assistant);
678   - }
679   - }
680   - }
  706 +// line = rs.getString("assistants");
  707 +// if (line != null) {
  708 +// tokens = line.split(",");
  709 +// for (String token : tokens) {
  710 +// if (!token.isEmpty()) {
  711 +// Resident assistant = getResident(token);
  712 +// if ((assistant != null) && (town.hasResident(assistant)))
  713 +// town.addAssistant(assistant);
  714 +// }
  715 +// }
  716 +// }
681 717 town.setTownBoard(rs.getString("townBoard"));
682 718 line = rs.getString("tag");
683 719 if (line != null)
@@ -803,7 +839,7 @@ public boolean loadNation(Nation nation) {
803 839 return false;
804 840 try {
805 841 Statement s = cntx.createStatement();
806   - ResultSet rs = s.executeQuery("SELECT towns,capital,assistants,tag,allies,enemies,taxes,neutral FROM " + tb_prefix + "NATIONS WHERE name='" + nation.getName() + "'");
  842 + ResultSet rs = s.executeQuery("SELECT towns,capital,tag,allies,enemies,taxes,neutral FROM " + tb_prefix + "NATIONS WHERE name='" + nation.getName() + "'");
807 843
808 844 while (rs.next()) {
809 845 line = rs.getString("towns");
@@ -818,17 +854,17 @@ public boolean loadNation(Nation nation) {
818 854 }
819 855 }
820 856 nation.setCapital(getTown(rs.getString("capital")));
821   - line = rs.getString("assistants");
822   - if (line != null) {
823   - tokens = line.split(",");
824   - for (String token : tokens) {
825   - if (!token.isEmpty()) {
826   - Resident assistant = getResident(token);
827   - if (assistant != null)
828   - nation.addAssistant(assistant);
829   - }
830   - }
831   - }
  857 +// line = rs.getString("assistants");
  858 +// if (line != null) {
  859 +// tokens = line.split(",");
  860 +// for (String token : tokens) {
  861 +// if (!token.isEmpty()) {
  862 +// Resident assistant = getResident(token);
  863 +// if (assistant != null)
  864 +// nation.addAssistant(assistant);
  865 +// }
  866 +// }
  867 +// }
832 868
833 869 nation.setTag(rs.getString("tag"));
834 870
@@ -1231,6 +1267,8 @@ public synchronized boolean saveResident(Resident resident) {
1231 1267 res_hm.put("title", resident.getTitle());
1232 1268 res_hm.put("surname", resident.getSurname());
1233 1269 res_hm.put("town", resident.hasTown() ? resident.getTown().getName() : "");
  1270 + res_hm.put("town-ranks", resident.hasTown() ? StringMgmt.join(resident.getTownRanks(), ",") : "");
  1271 + res_hm.put("nation-ranks", resident.hasTown() ? StringMgmt.join(resident.getNationRanks(), ",") : "");
1234 1272 String fstr = "";
1235 1273 for (Resident friend : resident.getFriends())
1236 1274 fstr += friend.getName() + ",";
8 src/com/palmergames/bukkit/towny/listeners/TownyPlayerListener.java
@@ -10,6 +10,7 @@
10 10 import com.palmergames.bukkit.towny.object.*;
11 11 import com.palmergames.bukkit.towny.object.PlayerCache.TownBlockStatus;
12 12 import com.palmergames.bukkit.towny.permissions.PermissionNodes;
  13 +import com.palmergames.bukkit.towny.permissions.TownyPerms;
13 14 import com.palmergames.bukkit.towny.regen.TownyRegenAPI;
14 15 import com.palmergames.bukkit.towny.regen.block.BlockLocation;
15 16 import com.palmergames.bukkit.towny.utils.PlayerCacheUtil;
@@ -80,6 +81,7 @@ public void onPlayerQuit(PlayerQuitEvent event) {
80 81 }
81 82
82 83 plugin.deleteCache(event.getPlayer());
  84 + TownyPerms.removeAttachment(event.getPlayer().getName());
83 85 }
84 86
85 87 @EventHandler(priority = EventPriority.NORMAL)
@@ -305,6 +307,12 @@ public void onPlayerTeleport(PlayerTeleportEvent event) {
305 307 }
306 308 onPlayerMove(event);
307 309 }
  310 +
  311 + @EventHandler(priority = EventPriority.LOWEST)
  312 + public void onPlayerChangeWorld(PlayerChangedWorldEvent event) { // has changed worlds
  313 +
  314 + TownyPerms.assignPermissions(null, event.getPlayer());
  315 + }
308 316
309 317 public boolean onPlayerInteract(Player player, Block block, ItemStack item) {
310 318
96 src/com/palmergames/bukkit/towny/object/Nation.java
@@ -2,6 +2,7 @@
2 2
3 3 import com.palmergames.bukkit.towny.TownySettings;
4 4 import com.palmergames.bukkit.towny.exceptions.*;
  5 +import com.palmergames.bukkit.towny.permissions.TownyPerms;
5 6 import com.palmergames.bukkit.towny.war.flagwar.TownyWar;
6 7 import com.palmergames.bukkit.util.BukkitTools;
7 8 import com.palmergames.util.StringMgmt;
@@ -15,7 +16,7 @@
15 16
16 17 private static final String ECONOMY_ACCOUNT_PREFIX = "nation-";
17 18
18   - private List<Resident> assistants = new ArrayList<Resident>();
  19 + //private List<Resident> assistants = new ArrayList<Resident>();
19 20 private List<Town> towns = new ArrayList<Town>();
20 21 private List<Nation> allies = new ArrayList<Nation>();
21 22 private List<Nation> enemies = new ArrayList<Nation>();
@@ -175,25 +176,26 @@ else if (town.hasNation())
175 176 }
176 177 }
177 178
178   - public void addAssistant(Resident resident) throws AlreadyRegisteredException {
  179 +// public void addAssistant(Resident resident) throws AlreadyRegisteredException {
  180 +//
  181 +// if (hasAssistant(resident))
  182 +// throw new AlreadyRegisteredException();
  183 +// else
  184 +// getAssistants().add(resident);
  185 +// }
179 186
180   - if (hasAssistant(resident))
181   - throw new AlreadyRegisteredException();
182   - else
183   - getAssistants().add(resident);
184   - }
185   -
186   - public void removeAssistant(Resident resident) throws NotRegisteredException {
187   -
188   - if (!hasAssistant(resident))
189   - throw new NotRegisteredException();
190   - else
191   - assistants.remove(resident);
192   - }
  187 +// public void removeAssistant(Resident resident) throws NotRegisteredException {
  188 +//
  189 +// if (!hasAssistant(resident))
  190 +// throw new NotRegisteredException();
  191 +// else
  192 +// assistants.remove(resident);
  193 +// }
193 194
194 195 public void setCapital(Town capital) {
195 196
196 197 this.capital = capital;
  198 + TownyPerms.assignPermissions(capital.getMayor(), null);
197 199 }
198 200
199 201 public Town getCapital() {
@@ -230,13 +232,20 @@ public boolean setAliegeance(String type, Nation nation) {
230 232 return false;
231 233 }
232 234
233   - public void setAssistants(List<Resident> assistants) {
234   -
235   - this.assistants = assistants;
236   - }
237   -
  235 +// public void setAssistants(List<Resident> assistants) {
  236 +//
  237 +// this.assistants = assistants;
  238 +// }
  239 +//
238 240 public List<Resident> getAssistants() {
239 241
  242 + List<Resident> assistants = new ArrayList<Resident>();
  243 +
  244 + for (Town town: towns)
  245 + for (Resident assistant: town.getResidents()) {
  246 + if (assistant.hasNationRank("assistant"))
  247 + assistants.add(assistant);
  248 + }
240 249 return assistants;
241 250 }
242 251
@@ -303,7 +312,7 @@ public void removeTown(Town town) throws EmptyNationException, NotRegisteredExce
303 312
304 313 private void remove(Town town) {
305 314
306   - removeAssistantsIn(town);
  315 + //removeAssistantsIn(town);
307 316 try {
308 317 town.setNation(null);
309 318 } catch (AlreadyRegisteredException e) {
@@ -317,23 +326,23 @@ private void removeAllTowns() {
317 326 remove(town);
318 327 }
319 328
320   - public boolean hasAssistantIn(Town town) {
321   -
322   - for (Resident resident : town.getResidents())
323   - if (hasAssistant(resident))
324   - return true;
325   - return false;
326   - }
327   -
328   - private void removeAssistantsIn(Town town) {
329   -
330   - for (Resident resident : new ArrayList<Resident>(town.getResidents()))
331   - if (hasAssistant(resident))
332   - try {
333   - removeAssistant(resident);
334   - } catch (NotRegisteredException e) {
335   - }
336   - }
  329 +// public boolean hasAssistantIn(Town town) {
  330 +//
  331 +// for (Resident resident : town.getResidents())
  332 +// if (hasAssistant(resident))
  333 +// return true;
  334 +// return false;
  335 +// }
  336 +//
  337 +// private void removeAssistantsIn(Town town) {
  338 +//
  339 +// for (Resident resident : new ArrayList<Resident>(town.getResidents()))
  340 +// if (hasAssistant(resident))
  341 +// try {
  342 +// removeAssistant(resident);
  343 +// } catch (NotRegisteredException e) {
  344 +// }
  345 +// }
337 346
338 347 public void setTaxes(double taxes) {
339 348
@@ -356,7 +365,7 @@ public void clear() {
356 365 removeAllEnemies();
357 366 removeAllTowns();
358 367 capital = null;
359   - assistants.clear();
  368 + //assistants.clear();
360 369 }
361 370
362 371 public void setNeutral(boolean neutral) throws TownyException {
@@ -397,7 +406,7 @@ public boolean hasResident(Resident resident) {
397 406
398 407 public void withdrawFromBank(Resident resident, int amount) throws EconomyException, TownyException {
399 408
400   - if (!isKing(resident) && !hasAssistant(resident))
  409 + if (!isKing(resident))// && !hasAssistant(resident))
401 410 throw new TownyException(TownySettings.getLangString("msg_no_access_nation_bank"));
402 411
403 412 if (TownySettings.isUsingEconomy()) {
@@ -422,8 +431,11 @@ public void withdrawFromBank(Resident resident, int amount) throws EconomyExcept
422 431 List<String> out = new ArrayList<String>();
423 432 out.add(getTreeDepth(depth) + "Nation (" + getName() + ")");
424 433 out.add(getTreeDepth(depth + 1) + "Capital: " + getCapital().getName());
425   - if (getAssistants().size() > 0)
426   - out.add(getTreeDepth(depth + 1) + "Assistants (" + getAssistants().size() + "): " + Arrays.toString(getAssistants().toArray(new Resident[0])));
  434 +
  435 + List<Resident> assistants = getAssistants();
  436 +
  437 + if (assistants.size() > 0)
  438 + out.add(getTreeDepth(depth + 1) + "Assistants (" + assistants.size() + "): " + Arrays.toString(assistants.toArray(new Resident[0])));
427 439 if (getAllies().size() > 0)
428 440 out.add(getTreeDepth(depth + 1) + "Allies (" + getAllies().size() + "): " + Arrays.toString(getAllies().toArray(new Nation[0])));
429 441 if (getEnemies().size() > 0)
92 src/com/palmergames/bukkit/towny/object/Resident.java
@@ -5,6 +5,7 @@
5 5 import com.palmergames.bukkit.towny.exceptions.EmptyTownException;
6 6 import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
7 7 import com.palmergames.bukkit.towny.exceptions.TownyException;
  8 +import com.palmergames.bukkit.towny.permissions.TownyPerms;
8 9 import com.palmergames.bukkit.towny.regen.TownyRegenAPI;
9 10 import com.palmergames.bukkit.towny.tasks.SetDefaultModes;
10 11 import com.palmergames.bukkit.util.BukkitTools;
@@ -30,6 +31,9 @@
30 31 private double teleportCost;
31 32 private String chatFormattedName;
32 33 private List<String> modes = new ArrayList<String>();
  34 +
  35 + private List<String> townRanks = new ArrayList<String>();
  36 + private List<String> nationRanks = new ArrayList<String>();
33 37
34 38 public Resident(String name) {
35 39
@@ -136,15 +140,20 @@ public void setTown(Town town) throws AlreadyRegisteredException {
136 140 this.town = null;
137 141 setTitle("");
138 142 setSurname("");
  143 + updatePerms();
139 144 return;
140 145 }
  146 +
141 147 if (this.town == town)
142 148 return;
  149 +
143 150 if (hasTown())
144 151 throw new AlreadyRegisteredException();
  152 +
145 153 this.town = town;
146 154 setTitle("");
147 155 setSurname("");
  156 + updatePerms();
148 157 }
149 158
150 159 public void setFriends(List<Resident> newFriends) {
@@ -197,9 +206,16 @@ public void clear() throws EmptyTownException {
197 206 town.removeResident(this);
198 207 setTitle("");
199 208 setSurname("");
  209 + updatePerms();
200 210 } catch (NotRegisteredException e) {
201 211 }
202 212 }
  213 +
  214 + private void updatePerms() {
  215 + setTownRanks(new ArrayList<String>());
  216 + setNationRanks(new ArrayList<String>());
  217 + TownyPerms.assignPermissions(this, null);
  218 + }
203 219
204 220 public void setRegistered(long registered) {
205 221
@@ -361,6 +377,82 @@ public void clearModes() {
361 377 TownyMessaging.sendErrorMsg("Could not set default modes for " + getName() + ".");
362 378
363 379 }
  380 +
  381 +
  382 + public boolean addTownRank(String rank) throws AlreadyRegisteredException {
  383 +
  384 + if (this.hasTown() && TownyPerms.getTownRanks().contains(rank)) {
  385 + if (townRanks.contains(rank))
  386 + throw new AlreadyRegisteredException();
  387 +
  388 + townRanks.add(rank);
  389 + TownyPerms.assignPermissions(this, null);
  390 + return true;
  391 + }
  392 +
  393 + return false;
  394 + }
  395 +
  396 + public void setTownRanks(List<String> ranks) {
  397 + townRanks.addAll(ranks);
  398 + }
  399 +
  400 + public boolean hasTownRank(String rank) {
  401 + return townRanks.contains(rank.toLowerCase());
  402 + }
  403 +
  404 + public List<String> getTownRanks() {
  405 + return townRanks;
  406 + }
  407 +
  408 + public boolean removeTownRank(String rank) throws NotRegisteredException {
  409 +
  410 + if (townRanks.contains(rank)) {
  411 + townRanks.remove(rank);
  412 + TownyPerms.assignPermissions(this, null);
  413 + return true;
  414 + }
  415 +
  416 + throw new NotRegisteredException();
  417 + }
  418 +
  419 + public boolean addNationRank(String rank) throws AlreadyRegisteredException {
  420 +
  421 + if (this.hasNation() && TownyPerms.getNationRanks().contains(rank)) {
  422 + if (nationRanks.contains(rank))
  423 + throw new AlreadyRegisteredException();
  424 +
  425 + nationRanks.add(rank);
  426 + TownyPerms.assignPermissions(this, null);
  427 + return true;
  428 + }
  429 +
  430 + return false;
  431 + }
  432 +
  433 + public void setNationRanks(List<String> ranks) {
  434 + nationRanks.addAll(ranks);
  435 + }
  436 +
  437 + public boolean hasNationRank(String rank) {
  438 + return nationRanks.contains(rank.toLowerCase());
  439 + }
  440 +
  441 + public List<String> getNationRanks() {
  442 + return nationRanks;
  443 + }
  444 +
  445 + public boolean removeNationRank(String rank) throws NotRegisteredException {
  446 +
  447 + if (nationRanks.contains(rank)) {
  448 + nationRanks.remove(rank);
  449 + TownyPerms.assignPermissions(this, null);
  450 + return true;
  451 + }
  452 +
  453 + throw new NotRegisteredException();
  454 +
  455 + }
364 456
365 457 @Override
366 458 protected World getBukkitWorld() {
80 src/com/palmergames/bukkit/towny/object/Town.java
@@ -2,6 +2,7 @@
2 2
3 3 import com.palmergames.bukkit.towny.TownySettings;
4 4 import com.palmergames.bukkit.towny.exceptions.*;
  5 +import com.palmergames.bukkit.towny.permissions.TownyPerms;
5 6 import com.palmergames.bukkit.util.BukkitTools;
6 7 import com.palmergames.bukkit.wallgen.Wall;
7 8 import com.palmergames.bukkit.wallgen.WallSection;
@@ -20,7 +21,7 @@
20 21 private static final String ECONOMY_ACCOUNT_PREFIX = "town-";
21 22
22 23 private List<Resident> residents = new ArrayList<Resident>();
23   - private List<Resident> assistants = new ArrayList<Resident>();
  24 + //private List<Resident> assistants = new ArrayList<Resident>();
24 25 private List<Location> outpostSpawns = new ArrayList<Location>();
25 26 private Wall wall = new Wall();
26 27 private Resident mayor;
@@ -129,6 +130,7 @@ public void setNation(Nation nation) throws AlreadyRegisteredException {
129 130
130 131 if (nation == null) {
131 132 this.nation = null;
  133 + TownyPerms.updateTownPerms(this);
132 134 return;
133 135 }
134 136 if (this.nation == nation)
@@ -136,6 +138,7 @@ public void setNation(Nation nation) throws AlreadyRegisteredException {
136 138 if (hasNation())
137 139 throw new AlreadyRegisteredException();
138 140 this.nation = nation;
  141 + TownyPerms.updateTownPerms(this);
139 142 }
140 143
141 144 @Override
@@ -146,6 +149,12 @@ public void setNation(Nation nation) throws AlreadyRegisteredException {
146 149
147 150 public List<Resident> getAssistants() {
148 151
  152 + List<Resident> assistants = new ArrayList<Resident>();
  153 +
  154 + for (Resident assistant: residents) {
  155 + if (assistant.hasTownRank("assistant"))
  156 + assistants.add(assistant);
  157 + }
149 158 return assistants;
150 159 }
151 160
@@ -165,7 +174,7 @@ public boolean hasResident(Resident resident) {
165 174
166 175 public boolean hasAssistant(Resident resident) {
167 176
168   - return assistants.contains(resident);
  177 + return getAssistants().contains(resident);
169 178 }
170 179
171 180 public void addResident(Resident resident) throws AlreadyRegisteredException {
@@ -189,16 +198,16 @@ else if (resident.hasTown())
189 198 }
190 199 }
191 200
192   - public void addAssistant(Resident resident) throws AlreadyRegisteredException, NotRegisteredException {
193   -
194   - if (hasAssistant(resident))
195   - throw new AlreadyRegisteredException();
196   -
197   - if (!hasResident(resident))
198   - throw new NotRegisteredException(resident.getName() + " doesn't belong to your town.");
199   -
200   - assistants.add(resident);
201   - }
  201 +// public void addAssistant(Resident resident) throws AlreadyRegisteredException, NotRegisteredException {
  202 +//
  203 +// if (hasAssistant(resident))
  204 +// throw new AlreadyRegisteredException();
  205 +//
  206 +// if (!hasResident(resident))
  207 +// throw new NotRegisteredException(resident.getName() + " doesn't belong to your town.");
  208 +//
  209 +// assistants.add(resident);
  210 +// }
202 211
203 212 public boolean isMayor(Resident resident) {