generated from runelite/example-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add initial attempt at importable JSON quest helpers
- Loading branch information
Showing
13 changed files
with
585 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/main/java/com/questhelper/QuestHelperSharingManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* 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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/main/java/com/questhelper/questimport/JsonQuestHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.