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;
}
// Add a friend note tooltip to a hovered friend list entry
final HoveredFriend hovered = plugin.getHoveredFriend();
if (hovered == null)
{
return null;
}
final String content = hovered.getNote();
tooltipManager.add(new Tooltip(content));
if (hovered != null) // Will always have a friend note if non-null
{
final String content = hovered.getNote();
tooltipManager.add(new Tooltip(content));
}
return null;
}

View File

@@ -30,6 +30,7 @@ package net.runelite.client.plugins.friendnotes;
import com.google.common.base.Strings;
import com.google.common.collect.ObjectArrays;
import com.google.common.eventbus.Subscribe;
import javax.annotation.Nullable;
import javax.inject.Inject;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
@@ -58,6 +59,7 @@ public class FriendNotesPlugin extends Plugin
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 EDIT_NOTE = "Edit Note";
private static final String NOTE_PROMPT_FORMAT = "%s's Notes<br>" +
@@ -84,49 +86,62 @@ public class FriendNotesPlugin extends Plugin
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
{
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)
{
String prevNote = getFriendNote(previousDisplayName);
final String prevNote = getFriendNote(prevDisplayName);
if (prevNote != null)
{
setFriendNote(previousDisplayName, null);
log.debug("Update friend's username: '{}' -> '{}'", prevDisplayName, currentDisplayName);
setFriendNote(prevDisplayName, null);
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;
if (!Strings.isNullOrEmpty(target))
if (!Strings.isNullOrEmpty(displayName))
{
target = Text.removeTags(target);
String targetNote = getFriendNote(target);
if (targetNote != null)
final String note = getFriendNote(displayName);
if (note != 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());
// look for "Message" on friends list
// Look for "Message" on friends list
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();
addNote.setOption(hoveredFriend == null || hoveredFriend.getNote() == null ? ADD_NOTE : EDIT_NOTE);
addNote.setType(MenuAction.RUNELITE.getId());
@@ -149,7 +166,7 @@ public class FriendNotesPlugin extends Plugin
addNote.setParam1(event.getActionParam1());
// Add menu entry
MenuEntry[] menuEntries = ObjectArrays.concat(client.getMenuEntries(), addNote);
final MenuEntry[] menuEntries = ObjectArrays.concat(client.getMenuEntries(), addNote);
client.setMenuEntries(menuEntries);
}
else if (hoveredFriend != null)
@@ -170,12 +187,14 @@ public class FriendNotesPlugin extends Plugin
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))
{
event.consume();
String note = getFriendNote(sanitizedTarget);
final String note = getFriendNote(sanitizedTarget);
// Open the chatbox input dialog
chatboxInputManager.openInputWindow(String.format(NOTE_PROMPT_FORMAT, sanitizedTarget,
CHARACTER_LIMIT), Strings.nullToEmpty(note), CHARACTER_LIMIT, (content) ->
{
@@ -194,24 +213,24 @@ public class FriendNotesPlugin extends Plugin
}
@Subscribe
public void onNameableNameChange(NameableNameChanged nameableNameChanged)
public void onNameableNameChange(NameableNameChanged event)
{
Nameable nameable = nameableNameChanged.getNameable();
final Nameable nameable = event.getNameable();
if (nameable instanceof Friend)
{
Friend friend = (Friend) nameable;
String name = friend.getName();
String prevName = friend.getPrevName();
checkNameChange(name, prevName);
// Migrate a friend's note to their new display name
final Friend friend = (Friend) nameable;
migrateFriendNote(friend.getName(), friend.getPrevName());
}
}
@Subscribe
public void onRemoveFriend(RemovedFriend removedFriend)
public void onRemoveFriend(RemovedFriend event)
{
String name = removedFriend.getName();
log.debug("Removed friend: '{}'", name);
setFriendNote(name, null);
// Delete a friend's note if they are removed
final String displayName = event.getName();
log.debug("Remove friend: '{}'", displayName);
setFriendNote(displayName, null);
}
}