/** 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 addInvite(String owner, String home, String target, Date expiry, String reason) { boolean inviteSet = false; ArrayList<InviteEntry> invites; if (this.inviteEntries.containsKey(owner.toLowerCase())) { invites = this.inviteEntries.get(owner.toLowerCase()); } else { invites = new ArrayList<InviteEntry>(); } for (int index = 0; index < invites.size(); index++) { InviteEntry thisInvite = invites.get(index); if (thisInvite.getInviteTarget().compareToIgnoreCase(target) == 0 && thisInvite.getInviteHome().compareToIgnoreCase(home) == 0) { thisInvite.setInviteSource(owner); thisInvite.setInviteHome(home); thisInvite.setInviteTarget(target); thisInvite.setInviteExpires(expiry); thisInvite.setInviteReason(reason); invites.set(index, thisInvite); inviteSet = true; } } if (!inviteSet) { InviteEntry invite = new InviteEntry(owner, home, target, expiry, reason); invites.add(invite); } this.inviteEntries.put(owner.toLowerCase(), invites); updateInviteExpiry(); saveInvites(); }
@Override public ArrayList<InviteEntry> listPlayerInvitesToMe(String target) { updateInviteExpiry(); ArrayList<InviteEntry> activeInvites = new ArrayList<InviteEntry>(); for (Entry<String, ArrayList<InviteEntry>> thisEntry : this.inviteEntries.entrySet()) { for (InviteEntry thisInvite : thisEntry.getValue()) { if (thisInvite.getInviteTarget().compareToIgnoreCase("*") == 0 || thisInvite.getInviteTarget().compareToIgnoreCase(target) == 0) { activeInvites.add(thisInvite); } } } return activeInvites; }
@Override public InviteEntry getInvite(String owner, String home, String target) { updateInviteExpiry(); if (this.inviteEntries.containsKey(owner.toLowerCase())) { ArrayList<InviteEntry> invites = this.inviteEntries.get(owner.toLowerCase()); for (InviteEntry thisInvite : invites) { if (thisInvite.getInviteHome().compareToIgnoreCase(home) == 0 && (thisInvite.getInviteTarget().compareToIgnoreCase("*") == 0 || thisInvite.getInviteTarget().compareToIgnoreCase(target) == 0)) { return thisInvite; } } } return null; }
@Override public void removeInvite(String owner, String home, String target) { if (this.inviteEntries.containsKey(owner.toLowerCase())) { ArrayList<InviteEntry> playerInviteList = this.inviteEntries.get(owner.toLowerCase()); ArrayList<InviteEntry> removeList = new ArrayList<InviteEntry>(); for (InviteEntry thisInvite : playerInviteList) { if (thisInvite.getInviteHome().compareToIgnoreCase(home) == 0 && thisInvite.getInviteTarget().compareToIgnoreCase(target) == 0) { removeList.add(thisInvite); } } playerInviteList.removeAll(removeList); this.inviteEntries.put(owner.toLowerCase(), playerInviteList); 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); } } }
@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(); }