/** Scans through the invites list, removing expired invites. */ private void updateInviteExpiry() { Date now = new Date(); // Remove expired invites. for (Entry<String, ArrayList<InviteEntry>> entry : this.inviteEntries.entrySet()) { ArrayList<InviteEntry> invites = entry.getValue(); ArrayList<InviteEntry> removeList = new ArrayList<InviteEntry>(); for (InviteEntry thisInvite : invites) { if (thisInvite.getInviteExpires() != null) { if (thisInvite.getInviteExpires().getTime() < now.getTime()) { removeList.add(thisInvite); } } } invites.removeAll(removeList); this.inviteEntries.put(entry.getKey(), invites); } // Remove empty users. ArrayList<String> removeList = new ArrayList<String>(); for (Entry<String, ArrayList<InviteEntry>> entry : this.inviteEntries.entrySet()) { ArrayList<InviteEntry> invites = entry.getValue(); if (invites.size() == 0) { removeList.add(entry.getKey()); } } for (String entry : removeList) { this.inviteEntries.remove(entry); } }
@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); } }