Skip to content

Commit

Permalink
Update Ethans API
Browse files Browse the repository at this point in the history
  • Loading branch information
zeruth committed Dec 24, 2023
1 parent 5d9b7a5 commit 4f1088a
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public WidgetQuery filter(Predicate<? super Widget> predicate) {
}

public WidgetQuery withAction(String action) {
widgets = widgets.stream().filter(widget -> widget.getActions() != null && Arrays.asList(widget.getActions()).contains(action)).collect(Collectors.toList());
widgets = widgets.stream().filter(widget -> widget.getActions() != null && Arrays.asList(widget.getActions()).contains(action)).collect(java.util.stream.Collectors.toList());
return this;
}

Expand All @@ -34,17 +34,17 @@ public boolean empty() {
}

public WidgetQuery hiddenState(boolean hidden) {
widgets = widgets.stream().filter(widget -> widget.isHidden() == hidden).collect(Collectors.toList());
widgets = widgets.stream().filter(widget -> widget.isHidden() == hidden).collect(java.util.stream.Collectors.toList());
return this;
}

public WidgetQuery withId(int id) {
widgets = widgets.stream().filter(widget -> widget.getId() == id).collect(Collectors.toList());
widgets = widgets.stream().filter(widget -> widget.getId() == id).collect(java.util.stream.Collectors.toList());
return this;
}

public WidgetQuery withItemId(int itemId) {
widgets = widgets.stream().filter(widget -> widget.getItemId() == itemId).collect(Collectors.toList());
widgets = widgets.stream().filter(widget -> widget.getItemId() == itemId).collect(java.util.stream.Collectors.toList());
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,20 +213,19 @@ public static int pathLength(Player player) {

@SneakyThrows
public static HeadIcon getHeadIcon(NPC npc) {
Method getHeadIconArrayMethod = null;
Method getHeadIconMethod = null;
for (Method declaredMethod : npc.getComposition().getClass().getDeclaredMethods()) {
if (declaredMethod.getReturnType() == short[].class && declaredMethod.getParameterTypes().length == 0) {
getHeadIconArrayMethod = declaredMethod;
if (getHeadIconArrayMethod == null) {
continue;
}
getHeadIconArrayMethod.setAccessible(true);
short[] headIconArray = (short[]) getHeadIconArrayMethod.invoke(npc.getComposition());
getHeadIconArrayMethod.setAccessible(false);
if (headIconArray == null || headIconArray.length == 0) {
if (declaredMethod.getName().length() == 2 && declaredMethod.getReturnType() == short.class && declaredMethod.getParameterCount() == 1) {
getHeadIconMethod = declaredMethod;
getHeadIconMethod.setAccessible(true);
short headIcon = (short) getHeadIconMethod.invoke(npc.getComposition(), 0);
getHeadIconMethod.setAccessible(false);

if (headIcon == -1) {
continue;
}
return HeadIcon.values()[headIconArray[0]];

return HeadIcon.values()[headIcon];
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.InteractionApi;

import com.example.EthanApiPlugin.Collections.Bank;
import com.example.EthanApiPlugin.Collections.Widgets;
import com.example.EthanApiPlugin.EthanApiPlugin;
import com.example.Packets.MousePackets;
import com.example.Packets.WidgetPackets;
Expand All @@ -11,10 +12,20 @@

public class BankInteraction {
private static final int WITHDRAW_QUANTITY = 3960;
private static final int WITHDRAW_AS_VARBIT = 3958;
private static final int WITHDRAW_ITEM_MODE = 0;
private static final int WITHDRAW_NOTES_MODE = 1;
private static final int WITHDRAW_ITEM_MODE_WIDGET = 786454;
private static final int WITHDRAW_NOTE_MODE_WIDGET = 786456;

private static final String ITEM_MODE_ACTION = "Item";
private static final String NOTE_MODE_ACTION = "Note";

public static boolean useItem(String name, String... actions) {
return Bank.search().withName(name).first().flatMap(item ->
{
setWithdrawMode(EthanApiPlugin.getClient().getVarbitValue(WITHDRAW_AS_VARBIT));

MousePackets.queueClickPacket();
WidgetPackets.queueWidgetAction(item, actions);
return Optional.of(true);
Expand All @@ -24,6 +35,8 @@ public static boolean useItem(String name, String... actions) {
public static boolean useItem(int id, String... actions) {
return Bank.search().withId(id).first().flatMap(item ->
{
setWithdrawMode(EthanApiPlugin.getClient().getVarbitValue(WITHDRAW_AS_VARBIT));

MousePackets.queueClickPacket();
WidgetPackets.queueWidgetAction(item, actions);
return Optional.of(true);
Expand All @@ -33,13 +46,17 @@ public static boolean useItem(int id, String... actions) {
public static boolean useItem(Predicate<? super Widget> predicate, String... actions) {
return Bank.search().filter(predicate).first().flatMap(item ->
{
setWithdrawMode(EthanApiPlugin.getClient().getVarbitValue(WITHDRAW_AS_VARBIT));

MousePackets.queueClickPacket();
WidgetPackets.queueWidgetAction(item, actions);
return Optional.of(true);
}).orElse(false);
}

public static void withdrawX(Widget item, int amount) {
setWithdrawMode(EthanApiPlugin.getClient().getVarbitValue(WITHDRAW_AS_VARBIT));

if (EthanApiPlugin.getClient().getVarbitValue(WITHDRAW_QUANTITY) == amount) {
MousePackets.queueClickPacket();
WidgetPackets.queueWidgetActionPacket(5, item.getId(), item.getItemId(), item.getIndex());
Expand All @@ -55,6 +72,8 @@ public static void withdrawX(Widget item, int amount) {
public static boolean useItemIndex(int index, String... actions) {
return Bank.search().indexIs(index).first().flatMap(item ->
{
setWithdrawMode(EthanApiPlugin.getClient().getVarbitValue(WITHDRAW_AS_VARBIT));

MousePackets.queueClickPacket();
WidgetPackets.queueWidgetAction(item, actions);
return Optional.of(true);
Expand All @@ -65,8 +84,107 @@ public static boolean useItem(Widget item, String... actions) {
if (item == null) {
return false;
}

setWithdrawMode(EthanApiPlugin.getClient().getVarbitValue(WITHDRAW_AS_VARBIT));

MousePackets.queueClickPacket();
WidgetPackets.queueWidgetAction(item, actions);
return true;
}

public static boolean useItem(String name, boolean noted, String... actions) {
return Bank.search().withName(name).first().flatMap(item ->
{
setWithdrawMode(EthanApiPlugin.getClient().getVarbitValue(WITHDRAW_AS_VARBIT));

MousePackets.queueClickPacket();
WidgetPackets.queueWidgetAction(item, actions);
return Optional.of(true);
}).orElse(false);
}

public static boolean useItem(int id, boolean noted, String... actions) {
return Bank.search().withId(id).first().flatMap(item ->
{
setWithdrawMode(noted? WITHDRAW_NOTES_MODE : WITHDRAW_ITEM_MODE);

MousePackets.queueClickPacket();
WidgetPackets.queueWidgetAction(item, actions);
return Optional.of(true);
}).orElse(false);
}

public static boolean useItem(Predicate<? super Widget> predicate, boolean noted, String... actions) {
return Bank.search().filter(predicate).first().flatMap(item ->
{
setWithdrawMode(noted? WITHDRAW_NOTES_MODE : WITHDRAW_ITEM_MODE);

MousePackets.queueClickPacket();
WidgetPackets.queueWidgetAction(item, actions);
return Optional.of(true);
}).orElse(false);
}

public static void withdrawX(Widget item, int amount, boolean noted) {
setWithdrawMode(noted? WITHDRAW_NOTES_MODE : WITHDRAW_ITEM_MODE);

if (EthanApiPlugin.getClient().getVarbitValue(WITHDRAW_QUANTITY) == amount) {
MousePackets.queueClickPacket();
WidgetPackets.queueWidgetActionPacket(5, item.getId(), item.getItemId(), item.getIndex());
return;
}
BankInteraction.useItem(item, noted, "Withdraw-X");
EthanApiPlugin.getClient().setVarcStrValue(359, Integer.toString(amount));
EthanApiPlugin.getClient().setVarcIntValue(5, 7);
EthanApiPlugin.getClient().runScript(681);
EthanApiPlugin.getClient().setVarbit(WITHDRAW_QUANTITY, amount);
}

public static boolean useItemIndex(int index, boolean noted, String... actions) {
return Bank.search().indexIs(index).first().flatMap(item ->
{
setWithdrawMode(noted? WITHDRAW_NOTES_MODE : WITHDRAW_ITEM_MODE);

MousePackets.queueClickPacket();
WidgetPackets.queueWidgetAction(item, actions);
return Optional.of(true);
}).orElse(false);
}

public static boolean useItem(Widget item, boolean noted, String... actions) {
if (item == null) {
return false;
}

setWithdrawMode(noted? WITHDRAW_NOTES_MODE : WITHDRAW_ITEM_MODE);

MousePackets.queueClickPacket();
WidgetPackets.queueWidgetAction(item, actions);
return true;
}

public static boolean setWithdrawMode(int withdrawMode) {
int withdrawAsVarbitValue = EthanApiPlugin.getClient().getVarbitValue(WITHDRAW_AS_VARBIT);
Optional<Widget> itemWidget = Widgets.search().withId(WITHDRAW_ITEM_MODE_WIDGET).withAction(ITEM_MODE_ACTION).first();
Optional<Widget> noteWidget = Widgets.search().withId(WITHDRAW_NOTE_MODE_WIDGET).withAction(NOTE_MODE_ACTION).first();
if (itemWidget.isEmpty() || noteWidget.isEmpty()) {
return false;
}

if (withdrawMode == WITHDRAW_ITEM_MODE && withdrawAsVarbitValue != WITHDRAW_ITEM_MODE) {
MousePackets.queueClickPacket();
WidgetPackets.queueWidgetAction(itemWidget.get(), "Item");

return true;
}

if (withdrawMode == WITHDRAW_NOTES_MODE && withdrawAsVarbitValue != WITHDRAW_NOTES_MODE) {
MousePackets.queueClickPacket();
WidgetPackets.queueWidgetAction(noteWidget.get(), "Note");

return true;
}

return false;
}
}

0 comments on commit 4f1088a

Please sign in to comment.