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: Create VarComparisonRequirement for Lumbridge Elite quests comp…
…lete check (#1766) * feat: Added VarComparisonRequirement This makes it easier to compare values between various varbits and varps. The initial usage will be for seeing if the player has all quests completed. * feat: Make use of var 1782 to check all quests completed This is used in Lumbridge Elite diary. * fix: Added missing header to VarType * fix: Update docs on VarComparisonRequirement
- Loading branch information
Showing
3 changed files
with
166 additions
and
37 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
114 changes: 114 additions & 0 deletions
114
src/main/java/com/questhelper/requirements/var/VarComparisonRequirement.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.requirements.var; | ||
|
||
import com.questhelper.questhelpers.QuestHelper; | ||
import com.questhelper.requirements.AbstractRequirement; | ||
import com.questhelper.requirements.util.Operation; | ||
import java.math.BigInteger; | ||
import java.util.Locale; | ||
import java.util.function.Predicate; | ||
import java.util.function.ToIntBiFunction; | ||
|
||
import com.questhelper.util.Utils; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.runelite.api.Client; | ||
import net.runelite.api.Varbits; | ||
import net.runelite.client.util.Text; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
/** | ||
* Checks if a player's varbit value is meets the required value as determined by the | ||
* {@link Operation} | ||
*/ | ||
@Getter | ||
@Slf4j | ||
public class VarComparisonRequirement extends AbstractRequirement | ||
{ | ||
private final VarType v1Type; | ||
private final int v1Id; | ||
|
||
private final VarType v2Type; | ||
private final int v2Id; | ||
private final Operation operation; | ||
private final String displayText; | ||
|
||
private boolean hasFiredWarning = false; | ||
|
||
/** | ||
* Compares the varbit/varp of a player to another varbit/varp of the player | ||
* {@link Operation}. | ||
* | ||
* @param v1Type the {@link VarType} to use for the first id | ||
* @param v1Id the {@link Varbits} or {@link net.runelite.api.annotations.Varp} id to use for the first id | ||
* @param v2Type the {@link VarType} to use for the second id | ||
* @param v2Id the {@link Varbits} or {@link net.runelite.api.annotations.Varp} id to use for the second id | ||
* @param operation the {@link Operation} to check with | ||
* @param displayText the display text | ||
*/ | ||
public VarComparisonRequirement(VarType v1Type, int v1Id, VarType v2Type, int v2Id, Operation operation, String displayText) | ||
{ | ||
this.v1Type = v1Type; | ||
this.v1Id = v1Id; | ||
this.v2Type = v2Type; | ||
this.v2Id = v2Id; | ||
this.operation = operation; | ||
this.displayText = displayText; | ||
shouldCountForFilter = true; | ||
} | ||
|
||
@Override | ||
public boolean check(Client client) | ||
{ | ||
try { | ||
int v1Value = v1Type.getValue(client, v1Id); | ||
int v2Value = v2Type.getValue(client, v2Id); | ||
|
||
return operation.check(v1Value, v2Value); | ||
} catch (IndexOutOfBoundsException e) { | ||
if (!hasFiredWarning) { | ||
var message = String.format("Error reading varbit %d or %d, please report this in the Quest Helper discord.", v1Id, v2Id); | ||
log.warn(message); | ||
Utils.addChatMessage(client, message); | ||
hasFiredWarning = true; | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public String getDisplayText() | ||
{ | ||
if (displayText != null) | ||
{ | ||
return displayText; | ||
} | ||
|
||
return v1Type.name() + " " + v1Id + " must be + " + operation.name().toLowerCase(Locale.ROOT) + " to " + v2Type.name() + " " + v2Id; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/questhelper/requirements/var/VarType.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,46 @@ | ||
/* | ||
* 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.requirements.var; | ||
|
||
import net.runelite.api.Client; | ||
|
||
import java.util.function.ToIntBiFunction; | ||
|
||
public enum VarType { | ||
VARBIT(Client::getVarbitValue), | ||
VARP(Client::getVarpValue); | ||
|
||
private final ToIntBiFunction<Client, Integer> getter; | ||
|
||
VarType(ToIntBiFunction<Client, Integer> getter) | ||
{ | ||
this.getter = getter; | ||
} | ||
|
||
public int getValue(Client client, int id) | ||
{ | ||
return getter.applyAsInt(client, id); | ||
} | ||
} |