001    import java.io.BufferedReader;
002    import java.io.BufferedWriter;
003    import java.io.File;
004    import java.io.FileReader;
005    import java.io.FileWriter;
006    import java.io.IOException;
007    import java.util.ArrayList;
008    import java.util.HashMap;
009    import java.util.Scanner;
010    import java.util.logging.Level;
011    
012    /**
013     * FlatFileSource.java - Accessing users, groups and such from flat files.
014     * 
015     * @author James
016     */
017    public class FlatFileSource extends DataSource {
018    
019        public void initialize() {
020            loadGroups();
021            loadKits();
022            loadHomes();
023            loadWarps();
024            loadItems();
025            // loadBanList();
026    
027            String location = etc.getInstance().getUsersLocation();
028            if (!new File(location).exists()) {
029                FileWriter writer = null;
030                try {
031                    writer = new FileWriter(location);
032                    writer.write("#Add your users here (When adding your entry DO NOT include #!)\r\n");
033                    writer.write("#The format is:\r\n");
034                    writer.write("#NAME:GROUPS:ADMIN/UNRESTRICTED:COLOR:COMMANDS:IPs\r\n");
035                    writer.write("#For administrative powers set admin/unrestricted to 2.\r\n");
036                    writer.write("#For no restrictions and ability to give out items set it to 1.\r\n");
037                    writer.write("#If you don't want the person to be able to build set it to -1.\r\n");
038                    writer.write("#Admin/unrestricted, color and commands are optional.\r\n");
039                    writer.write("#Examples:\r\n");
040                    writer.write("#Adminfoo:admins\r\n");
041                    writer.write("#Moderator39:mods:1:0:/unban\r\n");
042                    writer.write("#BobTheBuilder:vip:0:d\r\n");
043                } catch (Exception e) {
044                    log.log(Level.SEVERE, "Exception while creating " + location, e);
045                } finally {
046                    try {
047                        if (writer != null) {
048                            writer.close();
049                        }
050                    } catch (IOException e) {
051                        log.log(Level.SEVERE, "Exception while closing writer for " + location, e);
052                    }
053                }
054            }
055            location = etc.getInstance().getWhitelistLocation();
056            if (!new File(location).exists()) {
057                FileWriter writer = null;
058                try {
059                    writer = new FileWriter(location);
060                    writer.write("#Whitelist. Add your users here\r\n");
061                } catch (Exception e) {
062                    log.log(Level.SEVERE, "Exception while creating " + location, e);
063                } finally {
064                    try {
065                        if (writer != null) {
066                            writer.close();
067                        }
068                    } catch (IOException e) {
069                        log.log(Level.SEVERE, "Exception while closing writer for " + location, e);
070                    }
071                }
072            }
073        }
074    
075        public void loadGroups() {
076            String location = etc.getInstance().getGroupLocation();
077    
078            if (!new File(location).exists()) {
079                FileWriter writer = null;
080                try {
081                    writer = new FileWriter(location);
082                    writer.write("#Add your groups here (When adding your entry DO NOT include #!)\r\n");
083                    writer.write("#The format is:\r\n");
084                    writer.write("#NAME:COLOR:COMMANDS:INHERITEDGROUPS:ADMIN/UNRESTRICTED\r\n");
085                    writer.write("#For administrative powers set admin/unrestricted to 2.\r\n");
086                    writer.write("#For no restrictions and ability to give out items set it to 1.\r\n");
087                    writer.write("#If you don't want the group to be able to build set it to -1.\r\n");
088                    writer.write("#Inherited groups and admin/unrestricted are optional.\r\n");
089                    writer.write("#Examples:\r\n");
090                    writer.write("admins:c:*:mods:2\r\n");
091                    writer.write("mods:b:/ban,/kick,/item,/tp,/tphere,/s,/i,/give:vip:1\r\n");
092                    writer.write("vip:a::default\r\n");
093                    writer.write("default:f:/help,/sethome,/home,/spawn,/me,/msg,/kit,/playerlist,/warp,/motd,/compass,/tell,/m,/who:default\r\n");
094                } catch (Exception e) {
095                    log.log(Level.SEVERE, "Exception while creating " + location, e);
096                } finally {
097                    try {
098                        if (writer != null) {
099                            writer.close();
100                        }
101                    } catch (IOException e) {
102                        log.log(Level.SEVERE, "Exception while closing writer for " + location, e);
103                    }
104                }
105            }
106    
107            synchronized (groupLock) {
108                groups = new ArrayList<Group>();
109                try {
110                    Scanner scanner = new Scanner(new File(location));
111                    while (scanner.hasNextLine()) {
112                        String line = scanner.nextLine();
113                        if (line.startsWith("#") || line.equals("") || line.startsWith("")) {
114                            continue;
115                        }
116    
117                        String[] split = line.split(":");
118                        Group group = new Group();
119                        group.Name = split[0];
120                        group.Prefix = split[1];
121                        group.Commands = split[2].split(",");
122                        if (split.length >= 4) {
123                            group.InheritedGroups = split[3].split(",");
124                        }
125                        if (split.length >= 5) {
126                            if (split[4].equals("1")) {
127                                group.IgnoreRestrictions = true;
128                            } else if (split[4].equals("2")) {
129                                group.Administrator = true;
130                            } else if (split[4].equals("-1")) {
131                                group.CanModifyWorld = false;
132                            }
133                        }
134    
135                        // kind of a shitty way, but whatever.
136                        if (group.InheritedGroups != null) {
137                            if (group.InheritedGroups[0].equalsIgnoreCase(group.Name)) {
138                                group.InheritedGroups = new String[]{""};
139                                group.DefaultGroup = true;
140                            }
141                        }
142    
143                        groups.add(group);
144                    }
145                    scanner.close();
146                } catch (Exception e) {
147                    log.log(Level.SEVERE, "Exception while reading " + location + " (Are you sure you formatted it correctly?)", e);
148                }
149            }
150        }
151    
152        public void loadKits() {
153            String location = etc.getInstance().getKitsLocation();
154    
155            if (!new File(location).exists()) {
156                FileWriter writer = null;
157                try {
158                    writer = new FileWriter(location);
159                    writer.write("#Add your kits here. Example entry below (When adding your entry DO NOT include #!)\r\n");
160                    writer.write("#miningbasics:1,2,3,4:6000\r\n");
161                    writer.write("#The formats are (Find out more about groups in " + etc.getInstance().getUsersLocation() + ":\r\n");
162                    writer.write("#NAME:IDs:DELAY\r\n");
163                    writer.write("#NAME:IDs:DELAY:GROUP\r\n");
164                    writer.write("#6000 for delay is roughly 5 minutes.\r\n");
165                } catch (Exception e) {
166                    log.log(Level.SEVERE, "Exception while creating " + location, e);
167                } finally {
168                    try {
169                        if (writer != null) {
170                            writer.close();
171                        }
172                    } catch (IOException e) {
173                    }
174                }
175            }
176    
177            synchronized (kitLock) {
178                kits = new ArrayList<Kit>();
179                try {
180                    Scanner scanner = new Scanner(new File(location));
181                    while (scanner.hasNextLine()) {
182                        String line = scanner.nextLine();
183                        if (line.startsWith("#") || line.equals("")) // Skip it.
184                        {
185                            continue;
186                        }
187                        String[] split = line.split(":");
188                        String name = split[0];
189                        String[] ids = split[1].split(",");
190                        int delay = Integer.parseInt(split[2]);
191                        String group = "";
192                        if (split.length == 4) {
193                            group = split[3];
194                        }
195                        Kit kit = new Kit();
196                        kit.Name = name;
197                        kit.IDs = new HashMap<String, Integer>();
198                        for (String str : ids) {
199                            String id = "";
200                            int amount = 1;
201                            if (str.contains(" ")) {
202                                id = str.split(" ")[0];
203                                amount = Integer.parseInt(str.split(" ")[1]);
204                            } else {
205                                id = str;
206                            }
207                            kit.IDs.put(id, amount);
208                        }
209                        kit.Delay = delay;
210                        kit.Group = group;
211                        kits.add(kit);
212                    }
213                    scanner.close();
214                } catch (Exception e) {
215                    log.log(Level.SEVERE, "Exception while reading " + location, e);
216                }
217            }
218        }
219    
220        public void loadHomes() {
221            synchronized (homeLock) {
222                homes = new ArrayList<Warp>();
223                if (!etc.getInstance().canSaveHomes()) {
224                    return;
225                }
226    
227                String location = etc.getInstance().getHomeLocation();
228                if (new File(location).exists()) {
229                    try {
230                        Scanner scanner = new Scanner(new File(location));
231                        while (scanner.hasNextLine()) {
232                            String line = scanner.nextLine();
233                            if (line.startsWith("#") || line.equals("")) {
234                                continue;
235                            }
236                            String[] split = line.split(":");
237                            if (split.length < 4) {
238                                continue;
239                            }
240                            Location loc = new Location();
241                            loc.x = Double.parseDouble(split[1]);
242                            loc.y = Double.parseDouble(split[2]);
243                            loc.z = Double.parseDouble(split[3]);
244                            if (split.length >= 6) {
245                                loc.rotX = Float.parseFloat(split[4]);
246                                loc.rotY = Float.parseFloat(split[5]);
247                            }
248                            Warp home = new Warp();
249                            home.Name = split[0];
250                            home.Location = loc;
251                            if (split.length >= 7) {
252                                home.Group = split[6];
253                            } else {
254                                home.Group = "";
255                            }
256                            homes.add(home);
257                        }
258                        scanner.close();
259                    } catch (Exception e) {
260                        log.log(Level.SEVERE, "Exception while reading " + location, e);
261                    }
262                }
263            }
264        }
265    
266        public void loadWarps() {
267            synchronized (warpLock) {
268                warps = new ArrayList<Warp>();
269                String location = etc.getInstance().getWarpLocation();
270    
271                if (new File(location).exists()) {
272                    try {
273                        Scanner scanner = new Scanner(new File(location));
274                        while (scanner.hasNextLine()) {
275                            String line = scanner.nextLine();
276                            if (line.startsWith("#") || line.equals("")) {
277                                continue;
278                            }
279                            String[] split = line.split(":");
280                            if (split.length < 4) {
281                                continue;
282                            }
283    
284                            Location loc = new Location();
285                            loc.x = Double.parseDouble(split[1]);
286                            loc.y = Double.parseDouble(split[2]);
287                            loc.z = Double.parseDouble(split[3]);
288                            if (split.length == 6) {
289                                loc.rotX = Float.parseFloat(split[4]);
290                                loc.rotY = Float.parseFloat(split[5]);
291                            }
292                            Warp warp = new Warp();
293                            warp.Name = split[0];
294                            warp.Location = loc;
295                            if (split.length >= 7) {
296                                warp.Group = split[6];
297                            } else {
298                                warp.Group = "";
299                            }
300                            warps.add(warp);
301                        }
302                        scanner.close();
303                    } catch (Exception e) {
304                        log.log(Level.SEVERE, "Exception while reading " + location, e);
305                    }
306                }
307            }
308        }
309    
310        public void loadItems() {
311            String location = etc.getInstance().getItemLocation();
312    
313            if (!(new File(location).exists())) {
314                FileWriter writer = null;
315                try {
316                    writer = new FileWriter(location);
317                    writer.write("#Add your items in here (When adding your entry DO NOT include #!)\r\n");
318                    writer.write("#The format is:\r\n");
319                    writer.write("#NAME:ID\r\n");
320                    writer.write("#Default Items:\r\n");
321                    writer.write("air:0\r\n");
322                    writer.write("rock:1\r\n");
323                    writer.write("stone:1\r\n");
324                    writer.write("grass:2\r\n");
325                    writer.write("dirt:3\r\n");
326                    writer.write("cobblestone:4\r\n");
327                    writer.write("cobble:4\r\n");
328                    writer.write("wood:5\r\n");
329                    writer.write("sapling:6\r\n");
330                    writer.write("adminium:7\r\n");
331                    writer.write("bedrock:7\r\n");
332                    writer.write("water:8\r\n");
333                    writer.write("stillwater:9\r\n");
334                    writer.write("swater:9\r\n");
335                    writer.write("lava:10\r\n");
336                    writer.write("stilllava:11\r\n");
337                    writer.write("slava:11\r\n");
338                    writer.write("sand:12\r\n");
339                    writer.write("gravel:13\r\n");
340                    writer.write("goldore:14\r\n");
341                    writer.write("ironore:15\r\n");
342                    writer.write("coalore:16\r\n");
343                    writer.write("tree:17\r\n");
344                    writer.write("log:17\r\n");
345                    writer.write("leaves:18\r\n");
346                    writer.write("sponge:19\r\n");
347                    writer.write("glass:20\r\n");
348                    writer.write("cloth:35\r\n");
349                    writer.write("flower:37\r\n");
350                    writer.write("rose:38\r\n");
351                    writer.write("brownmushroom:39\r\n");
352                    writer.write("redmushroom:40\r\n");
353                    writer.write("gold:41\r\n");
354                    writer.write("goldblock:41\r\n");
355                    writer.write("iron:42\r\n");
356                    writer.write("ironblock:42\r\n");
357                    writer.write("doublestair:43\r\n");
358                    writer.write("stair:44\r\n");
359                    writer.write("step:44\r\n");
360                    writer.write("brickblock:45\r\n");
361                    writer.write("brickwall:45\r\n");
362                    writer.write("tnt:46\r\n");
363                    writer.write("bookshelf:47\r\n");
364                    writer.write("bookcase:47\r\n");
365                    writer.write("mossycobblestone:48\r\n");
366                    writer.write("mossy:48\r\n");
367                    writer.write("obsidian:49\r\n");
368                    writer.write("torch:50\r\n");
369                    writer.write("fire:51\r\n");
370                    writer.write("mobspawner:52\r\n");
371                    writer.write("woodstairs:53\r\n");
372                    writer.write("chest:54\r\n");
373                    writer.write("redstonedust:55\r\n");
374                    writer.write("redstonewire:55\r\n");
375                    writer.write("diamondore:56\r\n");
376                    writer.write("diamondblock:57\r\n");
377                    writer.write("workbench:58\r\n");
378                    writer.write("crop:59\r\n");
379                    writer.write("crops:59\r\n");
380                    writer.write("soil:60\r\n");
381                    writer.write("furnace:61\r\n");
382                    writer.write("litfurnace:62\r\n");
383                    writer.write("signblock:63\r\n");
384                    writer.write("wooddoorblock:64\r\n");
385                    writer.write("ladder:65\r\n");
386                    writer.write("rails:66\r\n");
387                    writer.write("rail:66\r\n");
388                    writer.write("track:66\r\n");
389                    writer.write("tracks:66\r\n");
390                    writer.write("cobblestonestairs:67\r\n");
391                    writer.write("stairs:67\r\n");
392                    writer.write("signblocktop:68\r\n");
393                    writer.write("wallsign:68\r\n");
394                    writer.write("lever:69\r\n");
395                    writer.write("rockplate:70\r\n");
396                    writer.write("stoneplate:70\r\n");
397                    writer.write("irondoorblock:71\r\n");
398                    writer.write("woodplate:72\r\n");
399                    writer.write("redstoneore:73\r\n");
400                    writer.write("redstoneorealt:74\r\n");
401                    writer.write("redstonetorchoff:75\r\n");
402                    writer.write("redstonetorchon:76\r\n");
403                    writer.write("button:77\r\n");
404                    writer.write("snow:78\r\n");
405                    writer.write("ice:79\r\n");
406                    writer.write("snowblock:80\r\n");
407                    writer.write("cactus:81\r\n");
408                    writer.write("clayblock:82\r\n");
409                    writer.write("reedblock:83\r\n");
410                    writer.write("jukebox:84\r\n");
411                    writer.write("fence:85\r\n");
412                    writer.write("pumpkin:86\r\n");
413                    writer.write("netherstone:87\r\n");
414                    writer.write("slowsand:88\r\n");
415                    writer.write("lightstone:89\r\n");
416                    writer.write("portal:90\r\n");
417                    writer.write("jackolantern:91\r\n");
418                    writer.write("jacko:91\r\n");
419                    writer.write("ironshovel:256\r\n");
420                    writer.write("ironspade:256\r\n");
421                    writer.write("ironpickaxe:257\r\n");
422                    writer.write("ironpick:257\r\n");
423                    writer.write("ironaxe:258\r\n");
424                    writer.write("flintandsteel:259\r\n");
425                    writer.write("lighter:259\r\n");
426                    writer.write("apple:260\r\n");
427                    writer.write("bow:261\r\n");
428                    writer.write("arrow:262\r\n");
429                    writer.write("coal:263\r\n");
430                    writer.write("diamond:264\r\n");
431                    writer.write("ironbar:265\r\n");
432                    writer.write("goldbar:266\r\n");
433                    writer.write("ironsword:267\r\n");
434                    writer.write("woodsword:268\r\n");
435                    writer.write("woodshovel:269\r\n");
436                    writer.write("woodspade:269\r\n");
437                    writer.write("woodpickaxe:270\r\n");
438                    writer.write("woodpick:270\r\n");
439                    writer.write("woodaxe:271\r\n");
440                    writer.write("stonesword:272\r\n");
441                    writer.write("stoneshovel:273\r\n");
442                    writer.write("stonespade:273\r\n");
443                    writer.write("stonepickaxe:274\r\n");
444                    writer.write("stonepick:274\r\n");
445                    writer.write("stoneaxe:275\r\n");
446                    writer.write("diamondsword:276\r\n");
447                    writer.write("diamondshovel:277\r\n");
448                    writer.write("diamondspade:277\r\n");
449                    writer.write("diamondpickaxe:278\r\n");
450                    writer.write("diamondpick:278\r\n");
451                    writer.write("diamondaxe:279\r\n");
452                    writer.write("stick:280\r\n");
453                    writer.write("bowl:281\r\n");
454                    writer.write("bowlwithsoup:282\r\n");
455                    writer.write("soupbowl:282\r\n");
456                    writer.write("soup:282\r\n");
457                    writer.write("goldsword:283\r\n");
458                    writer.write("goldshovel:284\r\n");
459                    writer.write("goldspade:284\r\n");
460                    writer.write("goldpickaxe:285\r\n");
461                    writer.write("goldpick:285\r\n");
462                    writer.write("goldaxe:286\r\n");
463                    writer.write("string:287\r\n");
464                    writer.write("feather:288\r\n");
465                    writer.write("gunpowder:289\r\n");
466                    writer.write("woodhoe:290\r\n");
467                    writer.write("stonehoe:291\r\n");
468                    writer.write("ironhoe:292\r\n");
469                    writer.write("diamondhoe:293\r\n");
470                    writer.write("goldhoe:294\r\n");
471                    writer.write("seeds:295\r\n");
472                    writer.write("wheat:296\r\n");
473                    writer.write("bread:297\r\n");
474                    writer.write("leatherhelmet:298\r\n");
475                    writer.write("leatherchestplate:299\r\n");
476                    writer.write("leatherpants:300\r\n");
477                    writer.write("leatherboots:301\r\n");
478                    writer.write("chainmailhelmet:302\r\n");
479                    writer.write("chainmailchestplate:303\r\n");
480                    writer.write("chainmailpants:304\r\n");
481                    writer.write("chainmailboots:305\r\n");
482                    writer.write("ironhelmet:306\r\n");
483                    writer.write("ironchestplate:307\r\n");
484                    writer.write("ironpants:308\r\n");
485                    writer.write("ironboots:309\r\n");
486                    writer.write("diamondhelmet:310\r\n");
487                    writer.write("diamondchestplate:311\r\n");
488                    writer.write("diamondpants:312\r\n");
489                    writer.write("diamondboots:313\r\n");
490                    writer.write("goldhelmet:314\r\n");
491                    writer.write("goldchestplate:315\r\n");
492                    writer.write("goldpants:316\r\n");
493                    writer.write("goldboots:317\r\n");
494                    writer.write("flint:318\r\n");
495                    writer.write("meat:319\r\n");
496                    writer.write("pork:319\r\n");
497                    writer.write("cookedmeat:320\r\n");
498                    writer.write("cookedpork:320\r\n");
499                    writer.write("painting:321\r\n");
500                    writer.write("paintings:321\r\n");
501                    writer.write("goldenapple:322\r\n");
502                    writer.write("sign:323\r\n");
503                    writer.write("wooddoor:324\r\n");
504                    writer.write("bucket:325\r\n");
505                    writer.write("waterbucket:326\r\n");
506                    writer.write("lavabucket:327\r\n");
507                    writer.write("minecart:328\r\n");
508                    writer.write("saddle:329\r\n");
509                    writer.write("irondoor:330\r\n");
510                    writer.write("redstonedust:331\r\n");
511                    writer.write("snowball:332\r\n");
512                    writer.write("boat:333\r\n");
513                    writer.write("leather:334\r\n");
514                    writer.write("milkbucket:335\r\n");
515                    writer.write("brick:336\r\n");
516                    writer.write("clay:337\r\n");
517                    writer.write("reed:338\r\n");
518                    writer.write("paper:339\r\n");
519                    writer.write("book:340\r\n");
520                    writer.write("slimeorb:341\r\n");
521                    writer.write("storageminecart:342\r\n");
522                    writer.write("poweredminecart:343\r\n");
523                    writer.write("egg:344\r\n");
524                    writer.write("compass:345\r\n");
525                    writer.write("fishingrod:346\r\n");
526                    writer.write("watch:347\r\n");
527                    writer.write("lightstonedust:348\r\n");
528                    writer.write("lightdust:348\r\n");
529                    writer.write("rawfish:349\r\n");
530                    writer.write("fish:349\r\n");
531                    writer.write("cookedfish:350\r\n");
532                    writer.write("goldrecord:2256\r\n");
533                    writer.write("greenrecord:2257\r\n");
534                } catch (Exception e) {
535                    log.log(Level.SEVERE, "Exception while creating " + location, e);
536                } finally {
537                    if (writer != null) {
538                        try {
539                            writer.close();
540                        } catch (IOException e) {
541                            log.log(Level.SEVERE, "Exception while closing writer for " + location, e);
542                        }
543                    }
544                }
545            }
546    
547            // This, for sure, now exists.
548            synchronized (itemLock) {
549                items = new HashMap<String, Integer>();
550                try {
551                    Scanner scanner = new Scanner(new File(location));
552                    while (scanner.hasNextLine()) {
553                        String line = scanner.nextLine();
554                        if (line.startsWith("#")) {
555                            continue;
556                        }
557                        if (line.equals("")) {
558                            continue;
559                        }
560                        String[] split = line.split(":");
561                        String name = split[0];
562    
563                        this.items.put(name, Integer.parseInt(split[1]));
564                    }
565                    scanner.close();
566                } catch (Exception e) {
567                    log.log(Level.SEVERE, "Exception while reading " + location + " (Are you sure you formatted it correctly?)", e);
568                }
569            }
570        }
571    
572        public void loadBanList() {
573            synchronized (banLock) {
574                bans = new ArrayList<Ban>();
575    
576                try {
577                    Scanner scanner = new Scanner(new File("banned-players.txt"));
578                    while (scanner.hasNextLine()) {
579                        String line = scanner.nextLine();
580                        if (line.startsWith("#") || line.equals("")) // Skip it.
581                        {
582                            continue;
583                        }
584                        String[] split = line.split(":");
585                        Ban ban = new Ban();
586                        if (split.length >= 1) {
587                            ban.setName(split[0]);
588                        }
589                        if (split.length == 4) {
590                            ban.setIp(split[1]);
591                            ban.setReason(split[2]);
592                            ban.setTimestamp(Integer.parseInt(split[3]));
593                        }
594                        bans.add(ban);
595                    }
596                    scanner.close();
597                } catch (Exception e) {
598                    log.log(Level.SEVERE, "Exception while reading banned-players.txt", e);
599                }
600    
601                try {
602                    Scanner scanner = new Scanner(new File("banned-ips.txt"));
603                    while (scanner.hasNextLine()) {
604                        String line = scanner.nextLine();
605                        if (line.startsWith("#") || line.equals("")) // Skip it.
606                        {
607                            continue;
608                        }
609                        String[] split = line.split(":");
610    
611                        Ban ban = new Ban();
612                        if (split.length >= 1) {
613                            ban.setIp(split[0]);
614                        }
615                        if (split.length == 4) {
616                            ban.setName(split[1]);
617                            ban.setReason(split[2]);
618                            ban.setTimestamp(Integer.parseInt(split[3]));
619                        }
620                        bans.add(ban);
621                    }
622                    scanner.close();
623                } catch (Exception e) {
624                    log.log(Level.SEVERE, "Exception while reading banned-ips.txt", e);
625                }
626            }
627        }
628    
629        // Users
630        public void addPlayer(Player player) {
631            String loc = etc.getInstance().getUsersLocation();
632    
633            try {
634                BufferedWriter bw = new BufferedWriter(new FileWriter(loc, true));
635                StringBuilder builder = new StringBuilder();
636                // #NAME:GROUPS:ADMIN/UNRESTRICTED:COLOR:COMMANDS
637                builder.append(player.getName());
638                builder.append(":");
639                builder.append(etc.combineSplit(0, player.getGroups(), ","));
640                builder.append(":");
641                if (player.getAdmin()) {
642                    builder.append("2");
643                } else if (player.ignoreRestrictions()) {
644                    builder.append("1");
645                } else if (!player.canModifyWorld()) {
646                    builder.append("-1");
647                } else {
648                    builder.append("0");
649                }
650                builder.append(":");
651                builder.append(player.getPrefix());
652                builder.append(":");
653                builder.append(etc.combineSplit(0, player.getCommands(), ","));
654                bw.append(builder.toString());
655                bw.newLine();
656                bw.close();
657            } catch (Exception ex) {
658                log.log(Level.SEVERE, "Exception while writing new user to " + loc, ex);
659            }
660        }
661    
662        public void modifyPlayer(Player player) {
663            String loc = etc.getInstance().getUsersLocation();
664    
665            try {
666                // Now to save...
667                BufferedReader reader = new BufferedReader(new FileReader(new File(loc)));
668                StringBuilder toWrite = new StringBuilder();
669                String line = "";
670                while ((line = reader.readLine()) != null) {
671                    if (!line.split(":")[0].equalsIgnoreCase(player.getName())) {
672                        toWrite.append(line).append("\r\n");
673                    } else {
674                        StringBuilder builder = new StringBuilder();
675                        builder.append(player.getName());
676                        builder.append(":");
677                        builder.append(etc.combineSplit(0, player.getGroups(), ","));
678                        builder.append(":");
679                        if (player.getAdmin()) {
680                            builder.append("2");
681                        } else if (player.ignoreRestrictions()) {
682                            builder.append("1");
683                        } else if (!player.canModifyWorld()) {
684                            builder.append("-1");
685                        } else {
686                            builder.append("0");
687                        }
688                        builder.append(":");
689                        builder.append(player.getPrefix());
690                        builder.append(":");
691                        builder.append(etc.combineSplit(0, player.getCommands(), ","));
692                        toWrite.append(builder.toString()).append("\r\n");
693                    }
694                }
695                reader.close();
696    
697                FileWriter writer = new FileWriter(loc);
698                writer.write(toWrite.toString());
699                writer.close();
700            } catch (Exception ex) {
701                log.log(Level.SEVERE, "Exception while editing user in " + loc, ex);
702            }
703        }
704    
705        public boolean doesPlayerExist(String player) {
706            String location = etc.getInstance().getUsersLocation();
707            try {
708                Scanner scanner = new Scanner(new File(location));
709                while (scanner.hasNextLine()) {
710                    String line = scanner.nextLine();
711                    if (line.startsWith("#") || line.equals("") || line.startsWith("")) {
712                        continue;
713                    }
714                    String[] split = line.split(":");
715                    if (!split[0].equalsIgnoreCase(player)) {
716                        continue;
717                    }
718                    return true;
719                }
720                scanner.close();
721            } catch (Exception e) {
722                log.log(Level.SEVERE, "Exception while reading " + location + " (Are you sure you formatted it correctly?)", e);
723            }
724            return false;
725        }
726    
727        public Player getPlayer(String name) {
728            Player player = new Player();
729            String location = etc.getInstance().getUsersLocation();
730    
731            try {
732                Scanner scanner = new Scanner(new File(location));
733                while (scanner.hasNextLine()) {
734                    String line = scanner.nextLine();
735                    if (line.startsWith("#") || line.equals("") || line.startsWith("")) {
736                        continue;
737                    }
738                    String[] split = line.split(":");
739                    if (!split[0].equalsIgnoreCase(name)) {
740                        continue;
741                    }
742    
743                    player.setGroups(split[1].split(","));
744    
745                    if (split.length >= 3) {
746                        if (split[2].equals("1")) {
747                            player.setIgnoreRestrictions(true);
748                        } else if (split[2].equals("2")) {
749                            player.setAdmin(true);
750                        } else if (split[2].equals("-1")) {
751                            player.setCanModifyWorld(false);
752                        }
753                    }
754    
755                    if (split.length >= 4) {
756                        player.setPrefix(split[3]);
757                    }
758                    if (split.length >= 5) {
759                        player.setCommands(split[4].split(","));
760                    }
761                    if (split.length >= 6) {
762                        player.setIps(split[5].split(","));
763                    }
764                }
765                scanner.close();
766            } catch (Exception e) {
767                log.log(Level.SEVERE, "Exception while reading " + location + " (Are you sure you formatted it correctly?)", e);
768            }
769            return player;
770        }
771    
772        // Groups
773        public void addGroup(Group group) {
774            throw new UnsupportedOperationException("Not supported yet.");
775        }
776    
777        public void modifyGroup(Group group) {
778            throw new UnsupportedOperationException("Not supported yet.");
779        }
780    
781        // Kits
782        public void addKit(Kit kit) {
783            throw new UnsupportedOperationException("Not supported yet.");
784        }
785    
786        public void modifyKit(Kit kit) {
787            throw new UnsupportedOperationException("Not supported yet.");
788        }
789    
790        // Homes
791        public void addHome(Warp home) {
792            String homeLoc = etc.getInstance().getHomeLocation();
793            try {
794                if (etc.getInstance().canSaveHomes()) {
795                    BufferedWriter bw = new BufferedWriter(new FileWriter(homeLoc, true));
796                    StringBuilder builder = new StringBuilder();
797                    builder.append(home.Name);
798                    builder.append(":");
799                    builder.append(home.Location.x);
800                    builder.append(":");
801                    builder.append(home.Location.y);
802                    builder.append(":");
803                    builder.append(home.Location.z);
804                    builder.append(":");
805                    builder.append(home.Location.rotX);
806                    builder.append(":");
807                    builder.append(home.Location.rotY);
808                    builder.append(":");
809                    builder.append(home.Group);
810                    bw.append(builder.toString());
811                    bw.newLine();
812                    bw.close();
813                }
814                synchronized (homeLock) {
815                    homes.add(home);
816                }
817            } catch (Exception e2) {
818                log.log(Level.SEVERE, "Exception while writing new user home to " + homeLoc, e2);
819            }
820        }
821    
822        public void changeHome(Warp home) {
823            synchronized (homeLock) {
824                Warp toRem = null;
825                for (Warp h : homes) {
826                    if (h.Name.equalsIgnoreCase(home.Name)) {
827                        toRem = h;
828                    }
829                }
830                if (toRem != null) {
831                    homes.remove(toRem);
832                }
833                homes.add(home);
834            }
835            FileWriter writer = null;
836            String homeLoc = etc.getInstance().getHomeLocation();
837            try {
838                // Now to save...
839                if (etc.getInstance().canSaveHomes()) {
840                    BufferedReader reader = new BufferedReader(new FileReader(new File(homeLoc)));
841                    StringBuilder toWrite = new StringBuilder();
842                    String line = "";
843                    while ((line = reader.readLine()) != null) {
844                        if (!line.split(":")[0].equalsIgnoreCase(home.Name)) {
845                            toWrite.append(line).append("\r\n");
846                        } else {
847                            StringBuilder builder = new StringBuilder();
848                            builder.append(home.Name);
849                            builder.append(":");
850                            builder.append(home.Location.x);
851                            builder.append(":");
852                            builder.append(home.Location.y);
853                            builder.append(":");
854                            builder.append(home.Location.z);
855                            builder.append(":");
856                            builder.append(home.Location.rotX);
857                            builder.append(":");
858                            builder.append(home.Location.rotY);
859                            builder.append(":");
860                            builder.append(home.Group);
861                            toWrite.append(builder.toString()).append("\r\n");
862                        }
863                    }
864                    reader.close();
865    
866                    writer = new FileWriter(homeLoc);
867                    writer.write(toWrite.toString());
868                    writer.close();
869                }
870            } catch (Exception e1) {
871                log.log(Level.SEVERE, "Exception while editing user home in " + homeLoc, e1);
872            } finally {
873                try {
874                    if (writer != null) {
875                        writer.close();
876                    }
877                } catch (IOException ex) {
878                }
879            }
880        }
881    
882        // Warps
883        public void addWarp(Warp warp) {
884            String warpLoc = etc.getInstance().getWarpLocation();
885            try {
886                BufferedWriter bw = new BufferedWriter(new FileWriter(warpLoc, true));
887                StringBuilder builder = new StringBuilder();
888                builder.append(warp.Name);
889                builder.append(":");
890                builder.append(warp.Location.x);
891                builder.append(":");
892                builder.append(warp.Location.y);
893                builder.append(":");
894                builder.append(warp.Location.z);
895                builder.append(":");
896                builder.append(warp.Location.rotX);
897                builder.append(":");
898                builder.append(warp.Location.rotY);
899                builder.append(":");
900                builder.append(warp.Group);
901                bw.append(builder.toString());
902                bw.newLine();
903                bw.close();
904                synchronized (warpLock) {
905                    warps.add(warp);
906                }
907            } catch (Exception e2) {
908                log.log(Level.SEVERE, "Exception while writing new warp to " + warpLoc, e2);
909            }
910        }
911    
912        public void changeWarp(Warp warp) {
913            synchronized (warpLock) {
914                Warp toRem = null;
915                for (Warp h : warps) {
916                    if (h.Name.equalsIgnoreCase(warp.Name)) {
917                        toRem = h;
918                    }
919                }
920                if (toRem != null) {
921                    warps.remove(toRem);
922                }
923                warps.add(warp);
924            }
925            FileWriter writer = null;
926            String warpLoc = etc.getInstance().getWarpLocation();
927            try {
928                // Now to save...
929                BufferedReader reader = new BufferedReader(new FileReader(new File(warpLoc)));
930                StringBuilder toWrite = new StringBuilder();
931                String line = "";
932                while ((line = reader.readLine()) != null) {
933                    if (!line.split(":")[0].equalsIgnoreCase(warp.Name)) {
934                        toWrite.append(line).append("\r\n");
935                    } else {
936                        StringBuilder builder = new StringBuilder();
937                        builder.append(warp.Name);
938                        builder.append(":");
939                        builder.append(warp.Location.x);
940                        builder.append(":");
941                        builder.append(warp.Location.y);
942                        builder.append(":");
943                        builder.append(warp.Location.z);
944                        builder.append(":");
945                        builder.append(warp.Location.rotX);
946                        builder.append(":");
947                        builder.append(warp.Location.rotY);
948                        builder.append(":");
949                        builder.append(warp.Group);
950                        toWrite.append(builder.toString()).append("\r\n");
951                    }
952                }
953                reader.close();
954    
955                writer = new FileWriter(warpLoc);
956                writer.write(toWrite.toString());
957                writer.close();
958            } catch (Exception e1) {
959                log.log(Level.SEVERE, "Exception while editing warp in " + warpLoc, e1);
960            } finally {
961                try {
962                    if (writer != null) {
963                        writer.close();
964                    }
965                } catch (IOException ex) {
966                }
967            }
968        }
969    
970        public void removeWarp(Warp warp) {
971            FileWriter writer = null;
972            String warpLoc = etc.getInstance().getWarpLocation();
973            try {
974                // Now to save...
975                BufferedReader reader = new BufferedReader(new FileReader(new File(warpLoc)));
976                StringBuilder toWrite = new StringBuilder();
977                String line = "";
978                while ((line = reader.readLine()) != null) {
979                    if (!line.split(":")[0].equalsIgnoreCase(warp.Name)) {
980                        toWrite.append(line).append("\r\n");
981                    }
982                }
983                reader.close();
984    
985                writer = new FileWriter(warpLoc);
986                writer.write(toWrite.toString());
987                writer.close();
988            } catch (Exception e1) {
989                log.log(Level.SEVERE, "Exception while delete warp from " + warpLoc, e1);
990            } finally {
991                try {
992                    if (writer != null) {
993                        writer.close();
994                    }
995                } catch (IOException ex) {
996                }
997            }
998    
999            synchronized (warpLock) {
1000                warps.remove(warp);
1001            }
1002        }
1003    
1004        // Whitelist
1005        public void addToWhitelist(String name) {
1006            if (isUserOnWhitelist(name)) {
1007                return;
1008            }
1009    
1010            BufferedWriter bw = null;
1011            String location = etc.getInstance().getWhitelistLocation();
1012            try {
1013                bw = new BufferedWriter(new FileWriter(location, true));
1014                bw.newLine();
1015                bw.append(name);
1016            } catch (Exception e2) {
1017                log.log(Level.SEVERE, "Exception while writing new user to " + location, e2);
1018            } finally {
1019                try {
1020                    if (bw != null) {
1021                        bw.close();
1022                    }
1023                } catch (IOException ex) {
1024                }
1025            }
1026        }
1027    
1028        public void removeFromWhitelist(String name) {
1029            if (!isUserOnWhitelist(name)) {
1030                return;
1031            }
1032    
1033            FileWriter writer = null;
1034            String location = etc.getInstance().getWhitelistLocation();
1035    
1036            try {
1037                // Now to save...
1038                BufferedReader reader = new BufferedReader(new FileReader(new File(location)));
1039                String line = "";
1040                StringBuilder toSave = new StringBuilder();
1041    
1042                while ((line = reader.readLine()) != null) {
1043                    if (!line.equalsIgnoreCase(name.toLowerCase())) {
1044                        toSave.append(line).append("\r\n");
1045                    }
1046                }
1047                reader.close();
1048    
1049                writer = new FileWriter(location);
1050                writer.write(toSave.toString());
1051            } catch (Exception e1) {
1052                log.log(Level.SEVERE, "Exception while removing player '" + name + "' from " + location, e1);
1053            } finally {
1054                try {
1055                    if (writer != null) {
1056                        writer.close();
1057                    }
1058                } catch (IOException ex) {
1059                }
1060            }
1061        }
1062    
1063        // Reservelist
1064        public void addToReserveList(String name) {
1065            if (isUserOnReserveList(name)) {
1066                return;
1067            }
1068            BufferedWriter bw = null;
1069            String location = etc.getInstance().getReservelistLocation();
1070            try {
1071                bw = new BufferedWriter(new FileWriter(location, true));
1072                bw.newLine();
1073                bw.append(name);
1074            } catch (Exception e2) {
1075                log.log(Level.SEVERE, "Exception while writing new user to " + location, e2);
1076            } finally {
1077                try {
1078                    if (bw != null) {
1079                        bw.close();
1080                    }
1081                } catch (IOException ex) {
1082                }
1083            }
1084        }
1085    
1086        public void removeFromReserveList(String name) {
1087            if (!isUserOnReserveList(name)) {
1088                return;
1089            }
1090    
1091            FileWriter writer = null;
1092            String location = etc.getInstance().getReservelistLocation();
1093    
1094            try {
1095                // Now to save...
1096                BufferedReader reader = new BufferedReader(new FileReader(new File(location)));
1097                String line = "";
1098                StringBuilder toSave = new StringBuilder();
1099    
1100                while ((line = reader.readLine()) != null) {
1101                    if (!line.equalsIgnoreCase(name.toLowerCase())) {
1102                        toSave.append(line).append("\r\n");
1103                    }
1104                }
1105                reader.close();
1106    
1107                writer = new FileWriter(location);
1108                writer.write(toSave.toString());
1109            } catch (Exception e1) {
1110                log.log(Level.SEVERE, "Exception while removing player '" + name + "' from " + location, e1);
1111            } finally {
1112                try {
1113                    if (writer != null) {
1114                        writer.close();
1115                    }
1116                } catch (IOException ex) {
1117                }
1118            }
1119        }
1120    
1121        public boolean isUserOnWhitelist(String user) {
1122            String location = etc.getInstance().getWhitelistLocation();
1123            Player player = getPlayer(user);
1124            try {
1125                Scanner scanner = new Scanner(new File(location));
1126                while (scanner.hasNextLine()) {
1127                    String line = scanner.nextLine();
1128                    if (line.startsWith("#") || line.equals("") || line.startsWith("")) {
1129                        continue;
1130                    }
1131                    if (line.startsWith("@") && player.isInGroup(line.substring(1)))
1132                        return true;
1133                    if (line.equalsIgnoreCase(user)) {
1134                        return true;
1135                    }
1136                }
1137                scanner.close();
1138            } catch (Exception e) {
1139                log.log(Level.SEVERE, "Exception while reading " + location, e);
1140            }
1141            return false;
1142        }
1143    
1144        public boolean isUserOnReserveList(String user) {
1145            String location = etc.getInstance().getReservelistLocation();
1146            Player player = getPlayer(user);
1147            try {
1148                Scanner scanner = new Scanner(new File(location));
1149                while (scanner.hasNextLine()) {
1150                    String line = scanner.nextLine();
1151                    if (line.startsWith("#") || line.equals("") || line.startsWith("")) {
1152                        continue;
1153                    }
1154                    if (line.startsWith("@") && player.isInGroup(line.substring(1)))
1155                        return true;
1156                    if (line.equalsIgnoreCase(user)) {
1157                        return true;
1158                    }
1159                }
1160                scanner.close();
1161            } catch (Exception e) {
1162                log.log(Level.SEVERE, "Exception while reading " + location, e);
1163            }
1164            return false;
1165        }
1166    
1167        public void modifyBan(Ban ban) {
1168            throw new UnsupportedOperationException("Not supported yet.");
1169        }
1170    }