Created
August 11, 2019 20:06
-
-
Save qverkk/bb60a15e37c42a3760f323625223b8ad to your computer and use it in GitHub Desktop.
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
package com.qverkk.free.combat.simplefighter.data; | |
import java.util.LinkedList; | |
public class FighterConfig { | |
private final LinkedList<String> npcNames = new LinkedList<>(); | |
private final LinkedList<String> itemNames = new LinkedList<>(); | |
public LinkedList<String> getNpcNames() { | |
return npcNames; | |
} | |
public LinkedList<String> getItemNames() { | |
return itemNames; | |
} | |
} |
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
package com.qverkk.free.combat.simplefighter; | |
import com.qverkk.free.combat.simplefighter.data.FighterConfig; | |
import com.qverkk.free.combat.simplefighter.tasks.*; | |
import org.rspeer.game.event.listener.RenderListener; | |
import org.rspeer.game.event.types.RenderEvent; | |
import org.rspeer.script.ScriptCategory; | |
import org.rspeer.script.ScriptMeta; | |
import org.rspeer.script.task.Task; | |
import org.rspeer.script.task.TaskScript; | |
import java.awt.*; | |
@ScriptMeta( | |
developer = "Qverkk", | |
name = "Simple fighter", | |
desc = "Fights any monster and loots. Will automatically eat below 40% health if you have food in inventory", | |
category = ScriptCategory.COMBAT, | |
version = 1.0 | |
) | |
public final class SimpleFighter extends TaskScript implements RenderListener { | |
private final FighterConfig config = new FighterConfig(); | |
private final Task[] tasks = new Task[]{ | |
new FighterUiTask(this), new Eating(), new Looting(this), new Attacking(this), new DelayTask() | |
}; | |
@Override | |
public boolean onStart() { | |
submit(tasks); | |
return true; | |
} | |
@Override | |
public void onState(State previous, State current) { | |
if (current == State.STOPPED) { | |
remove(tasks); | |
} | |
super.onState(previous, current); | |
} | |
public FighterConfig getConfig() { | |
return config; | |
} | |
@Override | |
public void notify(RenderEvent e) { | |
Graphics g = e.getSource(); | |
g.drawString("Current task: " + getStatus(), 20, 20); | |
} | |
private String getStatus() { | |
return getClass().getSimpleName() + (getCurrent() != null ? "[" + getCurrent().getClass().getSimpleName() + "]" : ""); | |
} | |
} |
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
package com.qverkk.free.combat.simplefighter.tasks; | |
import com.google.gson.Gson; | |
import com.google.gson.JsonObject; | |
import com.mashape.unirest.http.HttpResponse; | |
import com.mashape.unirest.http.Unirest; | |
import com.mashape.unirest.http.exceptions.UnirestException; | |
import com.qverkk.free.combat.simplefighter.SimpleFighter; | |
import org.rspeer.api.commons.Random; | |
import org.rspeer.game.adapter.scene.Npc; | |
import org.rspeer.game.adapter.scene.Player; | |
import org.rspeer.game.api.query.NpcQueryBuilder; | |
import org.rspeer.game.api.scene.Npcs; | |
import org.rspeer.game.api.scene.Players; | |
import org.rspeer.script.task.Task; | |
import java.util.HashMap; | |
import java.util.Map; | |
public final class Attacking extends Task { | |
private static final String link = "http://services.runescape.com/m=itemdb_rs/bestiary/beastData.json?beastid="; | |
private final Gson gson = new Gson(); | |
private SimpleFighter bot; | |
private Player local; | |
private Map<Integer, Integer> getDeathAnims = new HashMap<>(); | |
public Attacking(SimpleFighter simpleFighter) { | |
this.bot = simpleFighter; | |
} | |
@Override | |
public int execute() { | |
NpcQueryBuilder filter = Npcs.newQuery().filter(a -> bot.getConfig().getNpcNames().contains(a.getName().toLowerCase())); | |
Npc npc = filter.filter(a -> { | |
int deathId = getDeathAnimation(a.getId()); | |
return deathId == -1 ? a.getAnimation() == -1 : a.getAnimation() != getDeathAnimation(a.getId()); | |
}).actions("Attack").targeting(local).results().nearest(); | |
if (npc == null) { | |
npc = filter.filter(a -> { | |
int deathId = getDeathAnimation(a.getId()); | |
return deathId == -1 ? a.getAnimation() == -1 : a.getAnimation() != getDeathAnimation(a.getId()); | |
}).actions("Attack").targetless().results().nearest(); | |
} | |
if (npc != null && npc.interact("Attack")) { | |
return 300; | |
} | |
return Random.nextInt(300, 350); | |
} | |
private int getDeathAnimation(int npcId) { | |
if (getDeathAnims.containsKey(npcId)) { | |
return getDeathAnims.get(npcId); | |
} | |
try { | |
HttpResponse<String> config = Unirest.get(link + npcId).header("Content-type", "text/html").asString(); | |
if (config == null) { | |
throw new UnirestException("Config is null"); | |
} | |
JsonObject o = gson.fromJson(config.getBody(), JsonObject.class); | |
if (o == null) { | |
getDeathAnims.put(npcId, -1); | |
return -1; | |
} | |
JsonObject animations = o.getAsJsonObject("animations"); | |
int deathId = animations.get("death").getAsInt(); | |
getDeathAnims.put(npcId, deathId); | |
return deathId; | |
} catch (UnirestException e) { | |
getDeathAnims.put(npcId, -1); | |
return -1; | |
} | |
} | |
@Override | |
public boolean validate() { | |
local = Players.getLocal(); | |
return local != null && local.getTargetIndex() == -1; | |
} | |
} |
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
package com.qverkk.free.combat.simplefighter.tasks; | |
import org.rspeer.api.commons.Random; | |
import org.rspeer.script.task.Task; | |
public class DelayTask extends Task { | |
@Override | |
public int execute() { | |
return Random.nextInt(300, 400); | |
} | |
@Override | |
public boolean validate() { | |
return true; | |
} | |
} |
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
package com.qverkk.free.combat.simplefighter.tasks; | |
import org.rspeer.game.adapter.scene.Player; | |
import org.rspeer.game.api.component.Health; | |
import org.rspeer.game.api.component.Item; | |
import org.rspeer.game.api.component.tab.Backpack; | |
import org.rspeer.game.api.scene.Players; | |
import org.rspeer.script.task.Task; | |
/** | |
* @author Qverkk | |
* @project inuvation | |
* @date 8/9/2019 | |
**/ | |
public class Eating extends Task { | |
private Item eat; | |
@Override | |
public int execute() { | |
eat.interact("Eat"); | |
return 600; | |
} | |
@Override | |
public boolean validate() { | |
eat = Backpack.getFirst(a -> a.containsAction("Eat")); | |
final Player local = Players.getLocal(); | |
return Health.getCurrentPercent() < 40 && eat != null && local != null && local.getTargetIndex() == -1; | |
} | |
} |
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
package com.qverkk.free.combat.simplefighter.tasks; | |
import com.qverkk.free.combat.simplefighter.SimpleFighter; | |
import com.qverkk.free.combat.simplefighter.ui.FighterUi; | |
import org.rspeer.api.commons.Random; | |
import org.rspeer.script.task.Task; | |
import java.awt.*; | |
public class FighterUiTask extends Task { | |
private final FighterUi ui; | |
public FighterUiTask(SimpleFighter bot) { | |
ui = new FighterUi(bot); | |
EventQueue.invokeLater(() -> { | |
ui.build(); | |
ui.display(); | |
}); | |
} | |
@Override | |
public int execute() { | |
return Random.nextInt(1000, 2000); | |
} | |
@Override | |
public boolean validate() { | |
return !ui.isCompleted(); | |
} | |
} |
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
package com.qverkk.free.combat.simplefighter.tasks; | |
import com.qverkk.free.combat.simplefighter.SimpleFighter; | |
import org.rspeer.api.commons.Random; | |
import org.rspeer.api.commons.Time; | |
import org.rspeer.game.adapter.scene.GroundItem; | |
import org.rspeer.game.adapter.scene.Player; | |
import org.rspeer.game.api.component.AreaLoot; | |
import org.rspeer.game.api.position.Position; | |
import org.rspeer.game.api.scene.GroundItems; | |
import org.rspeer.game.api.scene.Players; | |
import org.rspeer.script.task.Task; | |
public final class Looting extends Task { | |
private SimpleFighter bot; | |
private GroundItem item; | |
private Player local; | |
public Looting(SimpleFighter simpleFighter) { | |
this.bot = simpleFighter; | |
} | |
@Override | |
public int execute() { | |
if (AreaLoot.isOpen() && AreaLoot.contains(item.getId()) && AreaLoot.take(item.getId())) { | |
return Random.nextInt(200, 350); | |
} else if (item.interact("Take")) { | |
Time.sleepUntil(() -> AreaLoot.isOpen() || getItemOn(item.getName(), item.getPosition()) == null, local::isMoving, Random.nextInt(2000, 3000)); | |
} | |
return Random.nextInt(300, 600); | |
} | |
private GroundItem getItemOn(String itemName, Position itemPosition) { | |
return GroundItems.newQuery().names(itemName).on(itemPosition).reachable().results().first(); | |
} | |
@Override | |
public boolean validate() { | |
item = GroundItems.newQuery().filter(a -> bot.getConfig().getItemNames().contains(a.getName().toLowerCase())).reachable().results().nearest(); | |
local = Players.getLocal(); | |
return item != null && local != null; | |
} | |
} |
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
package com.qverkk.free.combat.simplefighter.ui; | |
import com.qverkk.free.combat.simplefighter.SimpleFighter; | |
import net.miginfocom.swing.MigLayout; | |
import org.rspeer.ui.BotTitlePane; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
import java.util.Enumeration; | |
public final class FighterUi extends JFrame { | |
private boolean completed = false; | |
private SimpleFighter bot; | |
private TextField npcNameText; | |
private DefaultListModel<String> dlm; | |
public FighterUi(SimpleFighter bot) { | |
this.bot = bot; | |
} | |
public void build() { | |
setTitle("Simple fighter"); | |
setLayout(new MigLayout()); | |
JTabbedPane jTabbedPane = new JTabbedPane(); | |
// general options | |
JPanel generalSettings = new JPanel(); | |
generalSettings.setLayout(new MigLayout()); | |
npcNameText = new TextField(); | |
npcNameText.setPreferredSize(new Dimension(200, 40)); | |
Labelled<TextField> npcNameField = new Labelled<>("Npc name (split with a ;)", npcNameText); | |
npcNameField.add(generalSettings); | |
jTabbedPane.addTab("General", generalSettings); | |
// loot options | |
JPanel lootingSettings = new JPanel(); | |
lootingSettings.setLayout(new MigLayout()); | |
dlm = new DefaultListModel<>(); | |
JList<String> lootList = new JList<>(dlm); | |
lootList.setPreferredSize(new Dimension(350, 200)); | |
lootList.addMouseListener(new DeleteItemListener(dlm, lootList)); | |
lootingSettings.add(new JLabel("Double click on an item to remove it"), "wrap"); | |
lootingSettings.add(lootList, "wrap"); | |
JTextField itemName = new JTextField(); | |
itemName.addKeyListener(addItemOnEnter(dlm, itemName)); | |
lootingSettings.add(itemName, "growx,wrap"); | |
JButton addItem = new JButton("Add"); | |
addItem.addActionListener(new AddItemListener(dlm, itemName)); | |
lootingSettings.add(addItem, "growx,wrap"); | |
jTabbedPane.addTab("Looting", lootingSettings); | |
add(jTabbedPane, "wrap"); | |
Button startButton = new Button("Start"); | |
startButton.addActionListener(startButtonAction()); | |
add(startButton, "growx"); | |
BotTitlePane.decorate(this); | |
setLocationRelativeTo(null); | |
setResizable(false); | |
pack(); | |
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); | |
} | |
private ActionListener startButtonAction() { | |
return new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
System.out.println(bot == null); | |
bot.getConfig().getItemNames().clear(); | |
bot.getConfig().getNpcNames().clear(); | |
String npcNames = npcNameText.getText(); | |
System.out.println(npcNames == null); | |
if (npcNames.isEmpty()) { | |
return; | |
} | |
String[] npcNamesArray = npcNames.split(";"); | |
System.out.println(npcNamesArray == null); | |
if (npcNamesArray.length < 1) { | |
return; | |
} | |
for (String s : npcNamesArray) { | |
bot.getConfig().getNpcNames().add(s.toLowerCase()); | |
} | |
String s; | |
Enumeration<String> elements = dlm.elements(); | |
while (elements.hasMoreElements() && (s = elements.nextElement()) != null) { | |
if (s.isEmpty()) { | |
continue; | |
} | |
bot.getConfig().getItemNames().add(s.toLowerCase()); | |
} | |
System.out.println("Completed"); | |
completed = true; | |
} | |
}; | |
} | |
public boolean isCompleted() { | |
return completed; | |
} | |
private KeyListener addItemOnEnter(DefaultListModel<String> dlm, JTextField itemName) { | |
return new KeyListener() { | |
@Override | |
public void keyTyped(KeyEvent e) { | |
} | |
@Override | |
public void keyPressed(KeyEvent e) { | |
if (e.getKeyCode() != KeyEvent.VK_ENTER) { | |
return; | |
} | |
String text = itemName.getText(); | |
if (text.isEmpty() || dlm.contains(text)) { | |
return; | |
} | |
dlm.addElement(text); | |
itemName.setText(""); | |
} | |
@Override | |
public void keyReleased(KeyEvent e) { | |
} | |
}; | |
} | |
public void display() { | |
setVisible(true); | |
} | |
private class Labelled<T extends Component> { | |
private String text; | |
private T base; | |
public Labelled(String text, T base) { | |
this.text = text; | |
this.base = base; | |
} | |
private T add(JFrame frame) { | |
frame.add(new JLabel(text)); | |
frame.add(base, "wrap"); | |
return base; | |
} | |
private T add(JPanel frame) { | |
frame.add(new JLabel(text)); | |
frame.add(base, "wrap"); | |
return base; | |
} | |
} | |
private class AddItemListener implements ActionListener { | |
private DefaultListModel<String> dlm; | |
private JTextField itemName; | |
public AddItemListener(DefaultListModel<String> dlm, JTextField itemName) { | |
this.dlm = dlm; | |
this.itemName = itemName; | |
} | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
String text = itemName.getText(); | |
if (text.isEmpty() || dlm.contains(text)) { | |
return; | |
} | |
dlm.addElement(text); | |
itemName.setText(""); | |
} | |
} | |
private class DeleteItemListener implements MouseListener { | |
private DefaultListModel<String> dlm; | |
private JList<String> lootList; | |
public DeleteItemListener(DefaultListModel<String> dlm, JList<String> lootList) { | |
this.dlm = dlm; | |
this.lootList = lootList; | |
} | |
@Override | |
public void mouseClicked(MouseEvent e) { | |
if (e.getClickCount() != 2) { | |
return; | |
} | |
dlm.remove(lootList.getSelectedIndex()); | |
} | |
@Override | |
public void mousePressed(MouseEvent e) { | |
} | |
@Override | |
public void mouseReleased(MouseEvent e) { | |
} | |
@Override | |
public void mouseEntered(MouseEvent e) { | |
} | |
@Override | |
public void mouseExited(MouseEvent e) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment