menu swapper: add custom shift-click object swap

This commit is contained in:
Adam
2022-06-21 15:19:19 -04:00
parent 9d69c84653
commit a9d89f058b

View File

@@ -102,6 +102,7 @@ public class MenuEntrySwapperPlugin extends Plugin
private static final String SHIFTCLICK_CONFIG_GROUP = "shiftclick";
private static final String ITEM_KEY_PREFIX = "item_";
private static final String OBJECT_KEY_PREFIX = "object_";
private static final String OBJECT_SHIFT_KEY_PREFIX = "object_shift_";
private static final String NPC_KEY_PREFIX = "npc_";
private static final String NPC_SHIFT_KEY_PREFIX = "npc_shift_";
private static final String WORN_ITEM_KEY_PREFIX = "wornitem_";
@@ -688,10 +689,14 @@ public class MenuEntrySwapperPlugin extends Plugin
final ObjectComposition composition = client.getObjectDefinition(entry.getIdentifier());
final String[] actions = composition.getActions();
final Integer swapConfig = getObjectSwapConfig(composition.getId());
final Integer swapConfig = getObjectSwapConfig(false, composition.getId());
final MenuAction currentAction = swapConfig != null ? OBJECT_MENU_TYPES.get(swapConfig) :
defaultAction(composition);
final Integer shiftSwapConfig = getObjectSwapConfig(true, composition.getId());
final MenuAction currentShiftAction = shiftSwapConfig != null ? OBJECT_MENU_TYPES.get(shiftSwapConfig) :
defaultAction(composition);
for (int actionIdx = 0; actionIdx < OBJECT_MENU_TYPES.size(); ++actionIdx)
{
if (Strings.isNullOrEmpty(actions[actionIdx]))
@@ -699,21 +704,61 @@ public class MenuEntrySwapperPlugin extends Plugin
continue;
}
final int menuIdx = actionIdx;
final MenuAction menuAction = OBJECT_MENU_TYPES.get(actionIdx);
if (currentAction == menuAction)
if (menuAction != currentAction)
{
continue;
client.createMenuEntry(idx)
.setOption("Swap left click " + actions[actionIdx])
.setTarget(entry.getTarget())
.setType(MenuAction.RUNELITE)
.onClick(objectConsumer(composition, actions, actionIdx, menuAction, false));
}
if (menuAction != currentShiftAction && menuAction != currentAction)
{
client.createMenuEntry(idx)
.setOption("Swap " + actions[actionIdx])
.setOption("Swap shift click " + actions[actionIdx])
.setTarget(entry.getTarget())
.setType(MenuAction.RUNELITE)
.onClick(objectConsumer(composition, actions, actionIdx, menuAction, true));
}
}
if (swapConfig != null || shiftSwapConfig != null)
{
// Reset
client.createMenuEntry(idx)
.setOption("Reset swap")
.setTarget(entry.getTarget())
.setType(MenuAction.RUNELITE)
.onClick(e ->
{
final String message = new ChatMessageBuilder()
.append("The default left click option for '").append(Text.removeTags(composition.getName())).append("' ")
.append("The default left and shift click options for '").append(Text.removeTags(composition.getName())).append("' ")
.append("have been reset.")
.build();
chatMessageManager.queue(QueuedMessage.builder()
.type(ChatMessageType.CONSOLE)
.runeLiteFormattedMessage(message)
.build());
log.debug("Unset object swap for {}", composition.getId());
unsetObjectSwapConfig(true, composition.getId());
unsetObjectSwapConfig(false, composition.getId());
});
}
}
}
}
private Consumer<MenuEntry> objectConsumer(ObjectComposition composition, String[] actions, int menuIdx,
MenuAction menuAction, boolean shift)
{
return e ->
{
final String message = new ChatMessageBuilder()
.append("The default ").append(shift ? "shift" : "left").append(" click option for '").append(Text.removeTags(composition.getName())).append("' ")
.append("has been set to '").append(actions[menuIdx]).append("'.")
.build();
@@ -724,19 +769,8 @@ public class MenuEntrySwapperPlugin extends Plugin
log.debug("Set object swap for {} to {}", composition.getId(), menuAction);
final MenuAction defaultAction = defaultAction(composition);
if (defaultAction != menuAction)
{
setObjectSwapConfig(composition.getId(), menuIdx);
}
else
{
unsetObjectSwapConfig(composition.getId());
}
});
}
}
}
setObjectSwapConfig(shift, composition.getId(), menuIdx);
};
}
private Consumer<MenuEntry> walkHereConsumer(boolean shift, NPCComposition composition)
@@ -1172,7 +1206,7 @@ public class MenuEntrySwapperPlugin extends Plugin
objectId = objectComposition.getId();
}
Integer customOption = getObjectSwapConfig(objectId);
Integer customOption = getObjectSwapConfig(shiftModifier(), objectId);
if (customOption != null)
{
MenuAction swapAction = OBJECT_MENU_TYPES.get(customOption);
@@ -1492,9 +1526,9 @@ public class MenuEntrySwapperPlugin extends Plugin
rebuildCustomizationMenus();
}
private Integer getObjectSwapConfig(int objectId)
private Integer getObjectSwapConfig(boolean shift, int objectId)
{
String config = configManager.getConfiguration(MenuEntrySwapperConfig.GROUP, OBJECT_KEY_PREFIX + objectId);
String config = configManager.getConfiguration(MenuEntrySwapperConfig.GROUP, (shift ? OBJECT_SHIFT_KEY_PREFIX : OBJECT_KEY_PREFIX) + objectId);
if (config == null || config.isEmpty())
{
return null;
@@ -1503,14 +1537,14 @@ public class MenuEntrySwapperPlugin extends Plugin
return Integer.parseInt(config);
}
private void setObjectSwapConfig(int objectId, int index)
private void setObjectSwapConfig(boolean shift, int objectId, int index)
{
configManager.setConfiguration(MenuEntrySwapperConfig.GROUP, OBJECT_KEY_PREFIX + objectId, index);
configManager.setConfiguration(MenuEntrySwapperConfig.GROUP, (shift ? OBJECT_SHIFT_KEY_PREFIX : OBJECT_KEY_PREFIX) + objectId, index);
}
private void unsetObjectSwapConfig(int objectId)
private void unsetObjectSwapConfig(boolean shift, int objectId)
{
configManager.unsetConfiguration(MenuEntrySwapperConfig.GROUP, OBJECT_KEY_PREFIX + objectId);
configManager.unsetConfiguration(MenuEntrySwapperConfig.GROUP, (shift ? OBJECT_SHIFT_KEY_PREFIX : OBJECT_KEY_PREFIX) + objectId);
}
private static MenuAction defaultAction(ObjectComposition objectComposition)