@Override public void importInvites(ArrayList<InviteEntry> invites, boolean overwrite) { ArrayList<InviteEntry> playerInvites; updateInviteExpiry(); for (InviteEntry thisEntry : invites) { // Get the ArrayList of invites for this player if (this.inviteEntries.containsKey(thisEntry.getInviteSource().toLowerCase())) { playerInvites = this.inviteEntries.get(thisEntry.getInviteSource().toLowerCase()); } else { playerInvites = new ArrayList<InviteEntry>(); } boolean inviteFound = false; for (int index = 0; index < playerInvites.size(); index++) { InviteEntry thisInvite = playerInvites.get(index); if (thisInvite.getInviteHome().compareToIgnoreCase(thisEntry.getInviteHome()) == 0 && thisInvite.getInviteTarget().compareToIgnoreCase(thisEntry.getInviteTarget()) == 0) { // An existing home was found. if (overwrite) { thisInvite.setInviteSource(thisEntry.getInviteSource()); thisInvite.setInviteHome(thisEntry.getInviteHome()); thisInvite.setInviteTarget(thisEntry.getInviteTarget()); thisInvite.setInviteExpires(thisEntry.getInviteExpires()); thisInvite.setInviteReason(thisEntry.getInviteReason()); playerInvites.set(index, thisInvite); } inviteFound = true; } } if (!inviteFound) { // No existing location found. Create new entry. InviteEntry newInvite = new InviteEntry( thisEntry.getInviteSource(), thisEntry.getInviteHome(), thisEntry.getInviteTarget(), thisEntry.getInviteExpires(), thisEntry.getInviteReason()); playerInvites.add(newInvite); } // Replace the ArrayList in the homes HashMap this.inviteEntries.remove(thisEntry.getInviteSource().toLowerCase()); this.inviteEntries.put(thisEntry.getInviteSource().toLowerCase(), playerInvites); } // Save updateInviteExpiry(); saveInvites(); }
/** Save invites list to file. Clears the saveRequired flag. */ private void saveInvites() { try { FileWriter fstream = new FileWriter(this.invitesFile); BufferedWriter writer = new BufferedWriter(fstream); writer.write("# Stores user home invites." + Util.newLine()); writer.write("# <owner>;<home>;<target>;[<expiry>];[<reason>]" + Util.newLine()); writer.write(Util.newLine()); String owner; String home; String target; String expiry; String reason; for (Entry<String, ArrayList<InviteEntry>> entry : this.inviteEntries.entrySet()) { for (InviteEntry thisInvite : entry.getValue()) { owner = thisInvite.getInviteSource(); home = thisInvite.getInviteHome(); target = thisInvite.getInviteTarget(); expiry = ""; if (thisInvite.getInviteExpires() != null) expiry = Long.toString(thisInvite.getInviteExpires().getTime()); reason = ""; if (thisInvite.getInviteReason() != null && thisInvite.getInviteReason().length() > 0) reason = thisInvite.getInviteReason(); writer.write( owner + ";" + home + ";" + target + ";" + expiry + ";" + reason + Util.newLine()); } } writer.close(); } catch (Exception e) { Messaging.logSevere("Could not write the invites file.", this.plugin); } }
/** Load the invites list from file. */ private void loadInvites() { this.clearInvites(); // Create homes file if not exist if (!invitesFile.exists()) { try { FileWriter fstream = new FileWriter(this.invitesFile); BufferedWriter out = new BufferedWriter(fstream); out.write("# Stores user home invites." + Util.newLine()); out.write("# <owner>;<home>;<target>;[<expiry>];[<reason>]" + Util.newLine()); out.write(Util.newLine()); out.close(); } catch (Exception e) { Messaging.logSevere("Could not write the deafult invites file.", this.plugin); return; } } else { try { FileReader fstream = new FileReader(this.invitesFile); BufferedReader reader = new BufferedReader(fstream); String line = reader.readLine().trim(); this.clearInvites(); while (line != null) { if (!line.startsWith("#") && line.length() > 0) { InviteEntry thisInvite; thisInvite = parseInviteLine(line); if (thisInvite != null) { ArrayList<InviteEntry> inviteList; // Find HashMap entry for player if (!this.inviteEntries.containsKey(thisInvite.getInviteSource().toLowerCase())) { inviteList = new ArrayList<InviteEntry>(); } else { // Player not exist. Create dummy entry. inviteList = this.inviteEntries.get(thisInvite.getInviteSource().toLowerCase()); } // Don't save if this is a duplicate entry. boolean save = true; for (InviteEntry invite : inviteList) { if (invite.getInviteSource().compareToIgnoreCase(thisInvite.getInviteSource()) == 0) { save = false; } } if (save) { inviteList.add(thisInvite); } this.inviteEntries.put(thisInvite.getInviteSource().toLowerCase(), inviteList); } } line = reader.readLine(); } reader.close(); } catch (Exception e) { Messaging.logSevere("Could not read the invite list.", this.plugin); } } }