Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON-based helper importing #1868

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/main/java/com/questhelper/QuestHelperPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ public class QuestHelperPlugin extends Plugin
@Inject
public SkillIconManager skillIconManager;

@Inject
public QuestHelperSharingManager questHelperSharingManager;

private QuestHelperPanel panel;

private NavigationButton navButton;
Expand Down Expand Up @@ -406,6 +409,10 @@ else if (developerMode && commandExecuted.getCommand().equals("qh-inv"))
}
System.out.println(inv);
}
else if (commandExecuted.getCommand().equals("import-quest"))
{
questHelperSharingManager.promptForImport();
}
}

@Subscribe(priority = 100)
Expand Down Expand Up @@ -506,10 +513,8 @@ private void scanAndInstantiate()
}
}

private void instantiate(QuestHelperQuest quest)
void instantiate(QuestHelper questHelper, String name)
{
QuestHelper questHelper = quest.getQuestHelper();

Module questModule = (Binder binder) ->
{
binder.bind(QuestHelper.class).toInstance(questHelper);
Expand All @@ -518,10 +523,17 @@ private void instantiate(QuestHelperQuest quest)
Injector questInjector = RuneLite.getInjector().createChildInjector(questModule);
injector.injectMembers(questHelper);
questHelper.setInjector(questInjector);
questHelper.setQuest(quest);
questHelper.setConfig(config);
questHelper.setQuestHelperPlugin(this);
questHelper.setName(name);

log.debug("Loaded quest helper {}", name);
}

log.debug("Loaded quest helper {}", quest.name());
private void instantiate(QuestHelperQuest quest)
{
QuestHelper questHelper = quest.getQuestHelper();
instantiate(questHelper, quest.name());
questHelper.setQuest(quest);
}
}
116 changes: 116 additions & 0 deletions src/main/java/com/questhelper/QuestHelperSharingManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (c) 2024, Zoinkwiz <https://www.github.com/Zoinkwiz
* Copyright (c) 2021, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.questhelper;

import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.questhelper.questimport.JsonToQuest;
import com.questhelper.managers.QuestManager;
import com.questhelper.questhelpers.QuestHelper;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.ChatMessageType;
import net.runelite.client.callback.ClientThread;
import net.runelite.client.chat.ChatMessageManager;
import net.runelite.client.chat.QueuedMessage;
import net.runelite.client.game.ItemManager;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import javax.inject.Inject;

@Slf4j
public class QuestHelperSharingManager
{
private final QuestHelperPlugin plugin;
private final QuestManager questManager;
private final ChatMessageManager chatMessageManager;
private final ClientThread clientThread;
private final ItemManager itemManager;
private final Gson gson;

@Inject
private QuestHelperSharingManager(QuestHelperPlugin plugin, ClientThread clientThread, QuestManager questManager, ChatMessageManager chatMessageManager,
ItemManager itemManager, Gson gson)
{
this.plugin = plugin;
this.clientThread = clientThread;
this.questManager = questManager;
this.chatMessageManager = chatMessageManager;
this.itemManager = itemManager;
this.gson = gson;
}

public void promptForImport()
{
final String clipboardText;
try
{
clipboardText = Toolkit.getDefaultToolkit()
.getSystemClipboard()
.getData(DataFlavor.stringFlavor)
.toString();
}
catch (IOException | UnsupportedFlavorException ex)
{
sendChatMessage("Unable to read system clipboard.");
log.warn("error reading clipboard", ex);
return;
}

log.debug("Clipboard contents: {}", clipboardText);
if (Strings.isNullOrEmpty(clipboardText))
{
sendChatMessage("You do not have any quest helper helpers copied in your clipboard.");
return;
}

try
{
JsonToQuest jsonToQuest = new JsonToQuest(itemManager);
QuestHelper questHelper = jsonToQuest.importQuestFromJson(gson, clipboardText);
clientThread.invokeLater(() -> {
plugin.instantiate(questHelper, "Imported Helper");
questManager.startUpQuest(questHelper);
});

}
catch (JsonSyntaxException e)
{
log.debug("Malformed JSON for clipboard import", e);
sendChatMessage("Invalid helper copied in your clipboard.");
}
}

private void sendChatMessage(final String message)
{
chatMessageManager.queue(QueuedMessage.builder()
.type(ChatMessageType.CONSOLE)
.runeLiteFormattedMessage(message)
.build());
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/questhelper/managers/QuestManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public void startUpBackgroundQuest(String questHelperName)
return;
}

if (selectedQuest != null && selectedQuest.getQuest().getName().equals(questHelperName))
if (selectedQuest != null && selectedQuest.getQuest() != null && selectedQuest.getQuest().getName().equals(questHelperName))
{
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/questhelper/panel/PanelDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class PanelDetails
private Requirement hideCondition;

@Getter
@Setter
private List<Requirement> requirements;

@Getter
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/questhelper/panel/QuestOverviewPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public void addQuest(QuestHelper quest, boolean isActive)

if (quest.getCurrentStep() != null)
{
questNameLabel.setText(quest.getQuest().getName());
questNameLabel.setText(quest.getName());
actionsContainer.setVisible(true);

if (quest.getConfigs() != null)
Expand Down Expand Up @@ -571,7 +571,7 @@ private void updateExternalResourcesPanel(QuestHelper quest)
externalResourcesList = Collections.singletonList(ExternalQuestResources.valueOf(quest.getQuest().name().toUpperCase()).getWikiURL());
} catch (Exception e)
{
externalResourcesList = Collections.singletonList("https://oldschool.runescape.wiki/w/" + StringUtils.lowerCase(URLEncoder.encode(quest.getQuest().name(), StandardCharsets.UTF_8)));
externalResourcesList = Collections.singletonList("https://oldschool.runescape.wiki/w/" + StringUtils.lowerCase(URLEncoder.encode(quest.getName(), StandardCharsets.UTF_8)));
}
JButton wikiBtn = new JButton();

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/questhelper/questhelpers/QuestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public abstract class QuestHelper implements Module, QuestDebugRenderer

private boolean hasInitialized;

@Setter
@Getter
private String name;

@Override
public void configure(Binder binder)
{
Expand Down
114 changes: 114 additions & 0 deletions src/main/java/com/questhelper/questimport/JsonQuestHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (c) 2024, Zoinkwiz <https://github.com/Zoinkwiz>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.questhelper.questimport;

import com.questhelper.panel.PanelDetails;
import com.questhelper.questhelpers.ComplexStateQuestHelper;
import com.questhelper.steps.ConditionalStep;
import com.questhelper.steps.DetailedQuestStep;
import com.questhelper.steps.QuestStep;
import java.util.ArrayList;
import java.util.List;

public class JsonQuestHelper extends ComplexStateQuestHelper
{
ConditionalStep steps;

public void setSteps(ConditionalStep conditionalStep)
{
this.steps = conditionalStep;
}

@Override
public QuestStep loadStep()
{
return steps;
}

@Override
protected void setupRequirements()
{

}

@Override
public List<PanelDetails> getPanels()
{
List<PanelDetails> allSteps = new ArrayList<>();

int i = 0;
QuestStep defaultStep = null;
for (var questStep : steps.getSteps())
{
if (i == 0)
{
defaultStep = questStep;
i++;
continue;
}

PanelDetails details = makePanelDetail(questStep, String.valueOf(i));
if (details == null) continue;
allSteps.add(details);
i++;
}
if (defaultStep != null)
{
PanelDetails details = makePanelDetail(defaultStep, "default");
if (details != null) allSteps.add(details);
}

return allSteps;
}

private PanelDetails makePanelDetail(QuestStep questStep, String panelText)
{
if (questStep == null) return null;
if (questStep.getText().get(0).equals("Default step.")) return null;
PanelDetails details = new PanelDetails("Step " + panelText);
if (questStep instanceof DetailedQuestStep)
{
var reqs = ((DetailedQuestStep) questStep).getRequirements();
if (reqs != null) details.setRequirements(reqs);
}
questStep.setLockable(true);
details.addSteps(questStep);
details.setLockingStep(questStep);

return details;
}

@Override
public boolean isCompleted()
{
return false;
}

@Override
public int getVar()
{
return 0;
}
}
Loading
Loading