mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-07-03 16:49:10 +00:00
Merge branch 'development' into feature/nightly-client-support
This commit is contained in:
@@ -4,7 +4,6 @@ import org.parabot.core.Core;
|
||||
import org.parabot.core.desc.ServerDescription;
|
||||
import org.parabot.core.lib.Library;
|
||||
import org.parabot.core.lib.javafx.JavaFX;
|
||||
import org.parabot.core.lib.naga.Naga;
|
||||
import org.parabot.core.parsers.servers.ServerParser;
|
||||
import org.parabot.core.ui.components.VerboseLoader;
|
||||
import org.parabot.environment.api.utils.WebUtil;
|
||||
@@ -30,7 +29,6 @@ public class Environment {
|
||||
|
||||
LinkedList<Library> libs = new LinkedList<>();
|
||||
libs.add(new JavaFX());
|
||||
libs.add(new Naga());
|
||||
|
||||
for(Library lib : libs) {
|
||||
if (lib.requiresJar()) {
|
||||
|
||||
@@ -123,11 +123,6 @@ public class Script implements Runnable {
|
||||
this.state = STATE_STOPPED;
|
||||
context.setRunningScript(null);
|
||||
|
||||
if (context.getUlirathaClient() != null) {
|
||||
context.getUlirathaClient().disconnect();
|
||||
context.setUlirathaClient(null);
|
||||
}
|
||||
|
||||
BotUI.getInstance().toggleRun();
|
||||
Core.verbose("Done.");
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import org.parabot.core.ui.utils.UILog;
|
||||
import org.parabot.environment.api.utils.WebUtil;
|
||||
import org.parabot.environment.scripts.Script;
|
||||
import org.parabot.environment.scripts.loader.JavaScriptLoader;
|
||||
import org.parabot.environment.scripts.uliratha.UlirathaExecuter;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.lang.reflect.Constructor;
|
||||
@@ -77,9 +76,6 @@ public class BDNScriptsExecuter extends ScriptExecuter {
|
||||
script.setScriptID(this.id);
|
||||
super.finalize(tg, script);
|
||||
|
||||
if (manager.getAccount().getApi() != null) {
|
||||
new UlirathaExecuter(manager.getAccount().getApi()).start(this.id);
|
||||
}
|
||||
} catch (NoClassDefFoundError | ClassNotFoundException ignored) {
|
||||
UILog.log("Error", "Failed to load BDN script, error: [This server provider does not support this script]", JOptionPane.ERROR_MESSAGE);
|
||||
} catch (Throwable t) {
|
||||
|
||||
@@ -1,158 +0,0 @@
|
||||
package org.parabot.environment.scripts.uliratha;
|
||||
|
||||
import naga.ExceptionObserver;
|
||||
import naga.NIOService;
|
||||
import naga.NIOSocket;
|
||||
import naga.SocketObserver;
|
||||
import naga.packetreader.RegularPacketReader;
|
||||
import naga.packetwriter.RegularPacketWriter;
|
||||
import org.parabot.core.ui.Logger;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* @author JKetelaar
|
||||
*/
|
||||
|
||||
public class UlirathaClient extends Thread {
|
||||
|
||||
private String host;
|
||||
private int port;
|
||||
private NIOSocket socket;
|
||||
private boolean connected;
|
||||
private int scriptID;
|
||||
private String api;
|
||||
private boolean valid;
|
||||
|
||||
public UlirathaClient(String host, int port, int scriptID, String api) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.scriptID = scriptID;
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
connect();
|
||||
}
|
||||
|
||||
private void connect() {
|
||||
try {
|
||||
NIOService service = new NIOService();
|
||||
service.setExceptionObserver(new ExceptionObserver() {
|
||||
@Override
|
||||
public void notifyExceptionThrown(Throwable throwable) {
|
||||
throwable.printStackTrace();
|
||||
if (valid) {
|
||||
reconnect();
|
||||
connected = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
socket = service.openSocket(host, port);
|
||||
socket.setPacketReader(new RegularPacketReader(4, true));
|
||||
socket.setPacketWriter(new RegularPacketWriter(4, true));
|
||||
socket.listen(new SocketObserver() {
|
||||
public void connectionOpened(NIOSocket nioSocket) {
|
||||
try {
|
||||
sendObjects(nioSocket, new Object[]{76, scriptID, api});
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void packetReceived(NIOSocket socket, byte[] packet) {
|
||||
try {
|
||||
DataInputStream stream = new DataInputStream(new ByteArrayInputStream(packet));
|
||||
int packetID = stream.readInt();
|
||||
|
||||
switch (packetID){
|
||||
case 75:
|
||||
valid = stream.readBoolean();
|
||||
if (valid) {
|
||||
Logger.addMessage("We're connected with the Uliratha server!", false);
|
||||
connected = true;
|
||||
}else{
|
||||
socket.close();
|
||||
}
|
||||
break;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void packetSent(NIOSocket nioSocket, Object o) {
|
||||
|
||||
}
|
||||
|
||||
public void connectionBroken(NIOSocket nioSocket, Exception exception) {
|
||||
if (valid) {
|
||||
Logger.addMessage("We lost connection with the Uliratha server, reconnecting...", false);
|
||||
reconnect();
|
||||
connected = false;
|
||||
}else{
|
||||
Logger.addMessage("We're disconnected from the Uliratha server", false);
|
||||
}
|
||||
}
|
||||
});
|
||||
while (true) {
|
||||
service.selectBlocking();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
if (valid) {
|
||||
reconnect();
|
||||
connected = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void reconnect() {
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
connect();
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return connected;
|
||||
}
|
||||
|
||||
public void disconnect(){
|
||||
valid = false;
|
||||
socket.close();
|
||||
}
|
||||
|
||||
private void sendObjects(NIOSocket socket, Object[] objects) throws IOException {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
DataOutputStream dataStream = new DataOutputStream(stream);
|
||||
for (Object o : objects) {
|
||||
if (o instanceof String) {
|
||||
dataStream.writeUTF((String) o);
|
||||
} else if (o instanceof Integer) {
|
||||
dataStream.writeInt((Integer) o);
|
||||
} else if (o instanceof byte[]) {
|
||||
dataStream.write((byte[]) o);
|
||||
} else if (o instanceof Long) {
|
||||
dataStream.writeLong((Long) o);
|
||||
} else if (o instanceof Boolean) {
|
||||
dataStream.writeBoolean((Boolean) o);
|
||||
}
|
||||
}
|
||||
dataStream.flush();
|
||||
final byte[] content = stream.toByteArray();
|
||||
dataStream.close();
|
||||
socket.write(content);
|
||||
}
|
||||
|
||||
public void sendMessage(String message){
|
||||
try {
|
||||
sendObjects(socket, new Object[]{83, message});
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package org.parabot.environment.scripts.uliratha;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
import org.parabot.core.Context;
|
||||
import org.parabot.environment.api.utils.WebUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author JKetelaar
|
||||
*/
|
||||
public class UlirathaExecuter {
|
||||
|
||||
private String api;
|
||||
private static boolean isVip = true;
|
||||
|
||||
public UlirathaExecuter(String api){
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
public void start(int scriptID){
|
||||
if (UlirathaExecuter.isVip) {
|
||||
String vipUrl = "http://bdn.parabot.org/api/v2/user/" + api + "/vip";
|
||||
JSONParser parser = new JSONParser();
|
||||
try {
|
||||
JSONObject vipObject = (JSONObject) parser.parse(WebUtil.getReader(vipUrl));
|
||||
|
||||
boolean isVip = (boolean) vipObject.get("result");
|
||||
if (isVip) {
|
||||
String serverUrl = "http://bdn.parabot.org/api/v2/clients/server";
|
||||
JSONObject serverObject = (JSONObject) parser.parse(WebUtil.getReader(serverUrl));
|
||||
JSONObject detailsObject = (JSONObject) serverObject.get("result");
|
||||
String host = (String) detailsObject.get("host");
|
||||
long port = (long) detailsObject.get("port");
|
||||
|
||||
UlirathaClient client = new UlirathaClient(host, (int) port, scriptID, api);
|
||||
client.start();
|
||||
Context.getInstance().setUlirathaClient(client);
|
||||
}else{
|
||||
UlirathaExecuter.isVip = false;
|
||||
}
|
||||
} catch (IOException | ParseException | ClassCastException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user