@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(); }
@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 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); } }