Friend notes plugin: Add documentation and increase code readability (#2714)

* Add javadocs to methods
* Rename 2 methods for clarity, and add Nullable tag
* Rename local vars for consistency / correctness
* Simplify setHoveredFriend to allow clearer variable names
* Finalize local vars and add KEY_PREFIX constant
* Tweak 2 log messages for clarity and crash-proofing
* Inline the comments added to generic callbacks
* Skip the tag removal on MenuEntryAdded targets
This commit is contained in:
SoyChai
2018-05-20 03:44:21 +10:00
committed by Tomas Slusny
parent 45c98b83b3
commit cea85de384
2 changed files with 56 additions and 37 deletions

View File

@@ -57,14 +57,14 @@ class FriendNoteOverlay extends Overlay
return null; return null;
} }
// Add a friend note tooltip to a hovered friend list entry
final HoveredFriend hovered = plugin.getHoveredFriend(); final HoveredFriend hovered = plugin.getHoveredFriend();
if (hovered == null)
{
return null;
}
final String content = hovered.getNote(); if (hovered != null) // Will always have a friend note if non-null
tooltipManager.add(new Tooltip(content)); {
final String content = hovered.getNote();
tooltipManager.add(new Tooltip(content));
}
return null; return null;
} }

View File

@@ -30,6 +30,7 @@ package net.runelite.client.plugins.friendnotes;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.ObjectArrays; import com.google.common.collect.ObjectArrays;
import com.google.common.eventbus.Subscribe; import com.google.common.eventbus.Subscribe;
import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import lombok.Getter; import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -58,6 +59,7 @@ public class FriendNotesPlugin extends Plugin
private static final int CHARACTER_LIMIT = 128; private static final int CHARACTER_LIMIT = 128;
private static final String KEY_PREFIX = "note_";
private static final String ADD_NOTE = "Add Note"; private static final String ADD_NOTE = "Add Note";
private static final String EDIT_NOTE = "Edit Note"; private static final String EDIT_NOTE = "Edit Note";
private static final String NOTE_PROMPT_FORMAT = "%s's Notes<br>" + private static final String NOTE_PROMPT_FORMAT = "%s's Notes<br>" +
@@ -84,49 +86,62 @@ public class FriendNotesPlugin extends Plugin
return overlay; return overlay;
} }
private void setFriendNote(String friend, String note) /**
* Set a friend note, or unset by passing a null/empty note.
*/
private void setFriendNote(String displayName, String note)
{ {
if (!Strings.isNullOrEmpty(note)) if (Strings.isNullOrEmpty(note))
{ {
configManager.setConfiguration(CONFIG_GROUP, "note_" + friend, note); configManager.unsetConfiguration(CONFIG_GROUP, KEY_PREFIX + displayName);
} }
else else
{ {
configManager.unsetConfiguration(CONFIG_GROUP, "note_" + friend); configManager.setConfiguration(CONFIG_GROUP, KEY_PREFIX + displayName, note);
} }
} }
private String getFriendNote(String friend) /**
* Get the friend note of a display name, or null if no friend note exists for it.
*/
@Nullable
private String getFriendNote(String displayName)
{ {
return configManager.getConfiguration(CONFIG_GROUP, "note_" + friend); return configManager.getConfiguration(CONFIG_GROUP, KEY_PREFIX + displayName);
} }
private void checkNameChange(String currentDisplayName, String previousDisplayName) /**
* Migrate a friend note to a new display name, and remove the previous one.
* If current name already has a note, or previous name had none, do nothing.
*/
private void migrateFriendNote(String currentDisplayName, String prevDisplayName)
{ {
String currentNote = getFriendNote(currentDisplayName); final String currentNote = getFriendNote(currentDisplayName);
if (currentNote == null) if (currentNote == null)
{ {
String prevNote = getFriendNote(previousDisplayName); final String prevNote = getFriendNote(prevDisplayName);
if (prevNote != null) if (prevNote != null)
{ {
setFriendNote(previousDisplayName, null); log.debug("Update friend's username: '{}' -> '{}'", prevDisplayName, currentDisplayName);
setFriendNote(prevDisplayName, null);
setFriendNote(currentDisplayName, prevNote); setFriendNote(currentDisplayName, prevNote);
log.debug("Updating friends username: '{}' '{}'", currentDisplayName, previousDisplayName);
} }
} }
} }
private void setCurrentFriend(String target) /**
* Set the currently hovered display name, if a friend note exists for it.
*/
private void setHoveredFriend(String displayName)
{ {
hoveredFriend = null; hoveredFriend = null;
if (!Strings.isNullOrEmpty(target)) if (!Strings.isNullOrEmpty(displayName))
{ {
target = Text.removeTags(target); final String note = getFriendNote(displayName);
String targetNote = getFriendNote(target); if (note != null)
if (targetNote != null)
{ {
hoveredFriend = new HoveredFriend(target, targetNote); hoveredFriend = new HoveredFriend(displayName, note);
} }
} }
} }
@@ -136,11 +151,13 @@ public class FriendNotesPlugin extends Plugin
{ {
final int groupId = WidgetInfo.TO_GROUP(event.getActionParam1()); final int groupId = WidgetInfo.TO_GROUP(event.getActionParam1());
// look for "Message" on friends list // Look for "Message" on friends list
if (groupId == WidgetInfo.FRIENDS_LIST.getGroupId() && event.getOption().equals("Message")) if (groupId == WidgetInfo.FRIENDS_LIST.getGroupId() && event.getOption().equals("Message"))
{ {
setCurrentFriend(event.getTarget()); // Assume the display name text is untagged
setHoveredFriend(event.getTarget());
// Build "Add Note" or "Edit Note" menu entry
final MenuEntry addNote = new MenuEntry(); final MenuEntry addNote = new MenuEntry();
addNote.setOption(hoveredFriend == null || hoveredFriend.getNote() == null ? ADD_NOTE : EDIT_NOTE); addNote.setOption(hoveredFriend == null || hoveredFriend.getNote() == null ? ADD_NOTE : EDIT_NOTE);
addNote.setType(MenuAction.RUNELITE.getId()); addNote.setType(MenuAction.RUNELITE.getId());
@@ -149,7 +166,7 @@ public class FriendNotesPlugin extends Plugin
addNote.setParam1(event.getActionParam1()); addNote.setParam1(event.getActionParam1());
// Add menu entry // Add menu entry
MenuEntry[] menuEntries = ObjectArrays.concat(client.getMenuEntries(), addNote); final MenuEntry[] menuEntries = ObjectArrays.concat(client.getMenuEntries(), addNote);
client.setMenuEntries(menuEntries); client.setMenuEntries(menuEntries);
} }
else if (hoveredFriend != null) else if (hoveredFriend != null)
@@ -170,12 +187,14 @@ public class FriendNotesPlugin extends Plugin
final String sanitizedTarget = Text.removeTags(event.getMenuTarget()); final String sanitizedTarget = Text.removeTags(event.getMenuTarget());
// Handle clicks on "Add Note" or "Edit Note"
if (event.getMenuOption().equals(ADD_NOTE) || event.getMenuOption().equals(EDIT_NOTE)) if (event.getMenuOption().equals(ADD_NOTE) || event.getMenuOption().equals(EDIT_NOTE))
{ {
event.consume(); event.consume();
String note = getFriendNote(sanitizedTarget); final String note = getFriendNote(sanitizedTarget);
// Open the chatbox input dialog
chatboxInputManager.openInputWindow(String.format(NOTE_PROMPT_FORMAT, sanitizedTarget, chatboxInputManager.openInputWindow(String.format(NOTE_PROMPT_FORMAT, sanitizedTarget,
CHARACTER_LIMIT), Strings.nullToEmpty(note), CHARACTER_LIMIT, (content) -> CHARACTER_LIMIT), Strings.nullToEmpty(note), CHARACTER_LIMIT, (content) ->
{ {
@@ -194,24 +213,24 @@ public class FriendNotesPlugin extends Plugin
} }
@Subscribe @Subscribe
public void onNameableNameChange(NameableNameChanged nameableNameChanged) public void onNameableNameChange(NameableNameChanged event)
{ {
Nameable nameable = nameableNameChanged.getNameable(); final Nameable nameable = event.getNameable();
if (nameable instanceof Friend) if (nameable instanceof Friend)
{ {
Friend friend = (Friend) nameable; // Migrate a friend's note to their new display name
String name = friend.getName(); final Friend friend = (Friend) nameable;
String prevName = friend.getPrevName(); migrateFriendNote(friend.getName(), friend.getPrevName());
checkNameChange(name, prevName);
} }
} }
@Subscribe @Subscribe
public void onRemoveFriend(RemovedFriend removedFriend) public void onRemoveFriend(RemovedFriend event)
{ {
String name = removedFriend.getName(); // Delete a friend's note if they are removed
log.debug("Removed friend: '{}'", name); final String displayName = event.getName();
setFriendNote(name, null); log.debug("Remove friend: '{}'", displayName);
setFriendNote(displayName, null);
} }
} }