forked from RepoMirrors/baritone
initial comms
This commit is contained in:
parent
46de72e28c
commit
f014e42aa4
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package comms;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
/**
|
||||
* Do you not like having a blocking "receiveMessage" thingy?
|
||||
* <p>
|
||||
* Do you prefer just being able to get a list of all messages received since the last tick?
|
||||
* <p>
|
||||
* If so, this class is for you!
|
||||
*
|
||||
* @author leijurv
|
||||
*/
|
||||
public class BufferedConnection<T extends iMessage> implements IConnection<T> {
|
||||
private final IConnection wrapped;
|
||||
private final LinkedBlockingQueue<T> queue;
|
||||
private volatile transient IOException thrownOnRead;
|
||||
|
||||
public BufferedConnection(IConnection<T> wrapped) {
|
||||
this(wrapped, Integer.MAX_VALUE); // LinkedBlockingQueue accepts this as "no limit"
|
||||
}
|
||||
|
||||
public BufferedConnection(IConnection<T> wrapped, int maxInternalQueueSize) {
|
||||
this.wrapped = wrapped;
|
||||
this.queue = new LinkedBlockingQueue<>();
|
||||
this.thrownOnRead = null;
|
||||
new Thread(() -> {
|
||||
try {
|
||||
while (thrownOnRead == null) {
|
||||
queue.put(wrapped.receiveMessage());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
thrownOnRead = e;
|
||||
} catch (InterruptedException e) {
|
||||
thrownOnRead = new IOException("Interrupted while enqueueing", e);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(T message) throws IOException {
|
||||
wrapped.sendMessage(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T receiveMessage() {
|
||||
throw new UnsupportedOperationException("BufferedConnection can only be read from non-blockingly");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
wrapped.close();
|
||||
thrownOnRead = new EOFException("Closed");
|
||||
}
|
||||
|
||||
public List<T> receiveMessagesNonBlocking() throws IOException {
|
||||
ArrayList<T> msgs = new ArrayList<>();
|
||||
queue.drainTo(msgs); // preserves order -- first message received will be first in this arraylist
|
||||
if (msgs.isEmpty() && thrownOnRead != null) {
|
||||
IOException up = new IOException("BufferedConnection wrapped", thrownOnRead);
|
||||
throw up;
|
||||
}
|
||||
return msgs;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package comms;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public enum ConstructingDeserializer implements MessageDeserializer {
|
||||
INSTANCE;
|
||||
private final List<Class<? extends SerializableMessage>> MSGS;
|
||||
|
||||
ConstructingDeserializer() {
|
||||
MSGS = new ArrayList<>();
|
||||
// imagine doing something in reflect but it's actually concise and you don't need to catch 42069 different exceptions. huh.
|
||||
for (Method m : IMessageListener.class.getDeclaredMethods()) {
|
||||
MSGS.add((Class<? extends SerializableMessage>) m.getParameterTypes()[0]);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public SerializableMessage deserialize(DataInputStream in) throws IOException {
|
||||
int type = ((int) in.readByte()) & 0xff;
|
||||
try {
|
||||
return MSGS.get(type).getConstructor(DataInputStream.class).newInstance(in);
|
||||
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException ex) {
|
||||
throw new IOException("Unknown message type " + type, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public byte getHeader(Class<? extends SerializableMessage> klass) {
|
||||
return (byte) MSGS.indexOf(klass);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package comms;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* This is just a meme idk if this is actually useful
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public class FilteredConnection<T extends iMessage> implements IConnection<T> {
|
||||
private final Class<T> klass;
|
||||
private final IConnection<? super iMessage> wrapped;
|
||||
|
||||
public FilteredConnection(IConnection<? super iMessage> wrapped, Class<T> klass) {
|
||||
this.klass = klass;
|
||||
this.wrapped = wrapped;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(T message) throws IOException {
|
||||
wrapped.sendMessage(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T receiveMessage() throws IOException {
|
||||
while (true) {
|
||||
iMessage msg = wrapped.receiveMessage();
|
||||
if (klass.isInstance(msg)) {
|
||||
return klass.cast(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
wrapped.close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package comms;
|
||||
|
||||
public interface HandlableMessage {
|
||||
void handle(IMessageListener listener);
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package comms;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface IConnection<T extends iMessage> {
|
||||
void sendMessage(T message) throws IOException;
|
||||
|
||||
T receiveMessage() throws IOException;
|
||||
|
||||
void close();
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package comms;
|
||||
|
||||
import comms.downward.MessageChat;
|
||||
import comms.upward.MessageStatus;
|
||||
|
||||
public interface IMessageListener {
|
||||
default void handle(MessageStatus message) {
|
||||
unhandled(message);
|
||||
}
|
||||
|
||||
default void handle(MessageChat message) {
|
||||
unhandled(message);
|
||||
}
|
||||
|
||||
default void unhandled(iMessage msg) {
|
||||
// can override this to throw UnsupportedOperationException, if you want to make sure you're handling everything
|
||||
// default is to silently ignore messages without handlers
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package comms;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public interface MessageDeserializer {
|
||||
SerializableMessage deserialize(DataInputStream in) throws IOException;
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package comms;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
/**
|
||||
* Do you want a socket to localhost without actually making a gross real socket to localhost?
|
||||
*/
|
||||
public class Pipe<T extends iMessage> {
|
||||
private final LinkedBlockingQueue<Optional<T>> AtoB;
|
||||
private final LinkedBlockingQueue<Optional<T>> BtoA;
|
||||
private final PipedConnection A;
|
||||
private final PipedConnection B;
|
||||
private volatile boolean closed;
|
||||
|
||||
public Pipe() {
|
||||
this.AtoB = new LinkedBlockingQueue<>();
|
||||
this.BtoA = new LinkedBlockingQueue<>();
|
||||
this.A = new PipedConnection(BtoA, AtoB);
|
||||
this.B = new PipedConnection(AtoB, BtoA);
|
||||
}
|
||||
|
||||
public PipedConnection getA() {
|
||||
return A;
|
||||
}
|
||||
|
||||
public PipedConnection getB() {
|
||||
return B;
|
||||
}
|
||||
|
||||
public class PipedConnection implements IConnection<T> {
|
||||
private final LinkedBlockingQueue<Optional<T>> in;
|
||||
private final LinkedBlockingQueue<Optional<T>> out;
|
||||
|
||||
private PipedConnection(LinkedBlockingQueue<Optional<T>> in, LinkedBlockingQueue<Optional<T>> out) {
|
||||
this.in = in;
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(T message) throws IOException {
|
||||
if (closed) {
|
||||
throw new EOFException("Closed");
|
||||
}
|
||||
try {
|
||||
out.put(Optional.of(message));
|
||||
} catch (InterruptedException e) {
|
||||
// this can never happen since the LinkedBlockingQueues are not constructed with a maximum capacity, see above
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public T receiveMessage() throws IOException {
|
||||
if (closed) {
|
||||
throw new EOFException("Closed");
|
||||
}
|
||||
try {
|
||||
Optional<T> t = in.take();
|
||||
if (!t.isPresent()) {
|
||||
throw new EOFException("Closed");
|
||||
}
|
||||
return t.get();
|
||||
} catch (InterruptedException e) {
|
||||
// again, cannot happen
|
||||
// but we have to throw something
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
closed = true;
|
||||
try {
|
||||
AtoB.put(Optional.empty()); // unstick threads
|
||||
BtoA.put(Optional.empty());
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package comms;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public interface SerializableMessage extends iMessage {
|
||||
void write(DataOutputStream out) throws IOException;
|
||||
|
||||
default void writeHeader(DataOutputStream out) throws IOException {
|
||||
out.writeByte(getHeader());
|
||||
}
|
||||
|
||||
default byte getHeader() {
|
||||
return ConstructingDeserializer.INSTANCE.getHeader(getClass());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package comms;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class SerializedConnection implements IConnection<SerializableMessage> {
|
||||
private final DataInputStream in;
|
||||
private final DataOutputStream out;
|
||||
private final MessageDeserializer deserializer;
|
||||
|
||||
public SerializedConnection(InputStream in, OutputStream out) {
|
||||
this(ConstructingDeserializer.INSTANCE, in, out);
|
||||
}
|
||||
|
||||
public SerializedConnection(MessageDeserializer d, InputStream in, OutputStream out) {
|
||||
this.in = new DataInputStream(in);
|
||||
this.out = new DataOutputStream(out);
|
||||
this.deserializer = d;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(SerializableMessage message) throws IOException {
|
||||
message.writeHeader(out);
|
||||
message.write(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SerializableMessage receiveMessage() throws IOException {
|
||||
return deserializer.deserialize(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
try {
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
try {
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package comms;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
|
||||
public class SocketConnection extends SerializedConnection {
|
||||
public SocketConnection(Socket s) throws IOException {
|
||||
super(s.getInputStream(), s.getOutputStream());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package comms.downward;
|
||||
|
||||
import comms.HandlableMessage;
|
||||
import comms.IMessageListener;
|
||||
import comms.SerializableMessage;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class MessageChat implements SerializableMessage, HandlableMessage {
|
||||
|
||||
public final String msg;
|
||||
|
||||
public MessageChat(DataInputStream in) throws IOException {
|
||||
this.msg = in.readUTF();
|
||||
}
|
||||
|
||||
public MessageChat(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(DataOutputStream out) throws IOException {
|
||||
out.writeUTF(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(IMessageListener listener) {
|
||||
listener.handle(this);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package comms;
|
||||
|
||||
/**
|
||||
* hell yeah
|
||||
* <p>
|
||||
* <p>
|
||||
* dumb android users cant read this file
|
||||
* <p>
|
||||
*
|
||||
* @author leijurv
|
||||
*/
|
||||
public interface iMessage {
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package comms.upward;
|
||||
|
||||
import comms.HandlableMessage;
|
||||
import comms.IMessageListener;
|
||||
import comms.SerializableMessage;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class MessageStatus implements SerializableMessage, HandlableMessage {
|
||||
|
||||
public final double x;
|
||||
public final double y;
|
||||
public final double z;
|
||||
|
||||
public MessageStatus(DataInputStream in) throws IOException {
|
||||
this.x = in.readDouble();
|
||||
this.y = in.readDouble();
|
||||
this.z = in.readDouble();
|
||||
}
|
||||
|
||||
public MessageStatus(double x, double y, double z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(DataOutputStream out) throws IOException {
|
||||
out.writeDouble(x);
|
||||
out.writeDouble(y);
|
||||
out.writeDouble(z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(IMessageListener listener) {
|
||||
listener.handle(this);
|
||||
}
|
||||
}
|
|
@ -22,10 +22,7 @@ import baritone.api.IBaritone;
|
|||
import baritone.api.Settings;
|
||||
import baritone.api.event.listener.IEventBus;
|
||||
import baritone.api.utils.IPlayerContext;
|
||||
import baritone.behavior.Behavior;
|
||||
import baritone.behavior.LookBehavior;
|
||||
import baritone.behavior.MemoryBehavior;
|
||||
import baritone.behavior.PathingBehavior;
|
||||
import baritone.behavior.*;
|
||||
import baritone.cache.WorldProvider;
|
||||
import baritone.event.GameEventHandler;
|
||||
import baritone.process.CustomGoalProcess;
|
||||
|
@ -77,6 +74,7 @@ public class Baritone implements IBaritone {
|
|||
private GameEventHandler gameEventHandler;
|
||||
|
||||
private List<Behavior> behaviors;
|
||||
private ControllerBehavior controllerBehavior;
|
||||
private PathingBehavior pathingBehavior;
|
||||
private LookBehavior lookBehavior;
|
||||
private MemoryBehavior memoryBehavior;
|
||||
|
@ -107,6 +105,7 @@ public class Baritone implements IBaritone {
|
|||
this.behaviors = new ArrayList<>();
|
||||
{
|
||||
// the Behavior constructor calls baritone.registerBehavior(this) so this populates the behaviors arraylist
|
||||
controllerBehavior = new ControllerBehavior(this);
|
||||
pathingBehavior = new PathingBehavior(this);
|
||||
lookBehavior = new LookBehavior(this);
|
||||
memoryBehavior = new MemoryBehavior(this);
|
||||
|
@ -131,10 +130,6 @@ public class Baritone implements IBaritone {
|
|||
this.initialized = true;
|
||||
}
|
||||
|
||||
public PathingControlManager getPathingControlManager() {
|
||||
return this.pathingControlManager;
|
||||
}
|
||||
|
||||
public List<Behavior> getBehaviors() {
|
||||
return this.behaviors;
|
||||
}
|
||||
|
@ -144,11 +139,19 @@ public class Baritone implements IBaritone {
|
|||
this.gameEventHandler.registerEventListener(behavior);
|
||||
}
|
||||
|
||||
public PathingControlManager getPathingControlManager() {
|
||||
return this.pathingControlManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputOverrideHandler getInputOverrideHandler() {
|
||||
return this.inputOverrideHandler;
|
||||
}
|
||||
|
||||
public ControllerBehavior getControllerBehavior() {
|
||||
return this.controllerBehavior;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CustomGoalProcess getCustomGoalProcess() { // Iffy
|
||||
return this.customGoalProcess;
|
||||
|
@ -160,18 +163,8 @@ public class Baritone implements IBaritone {
|
|||
}
|
||||
|
||||
@Override
|
||||
public IPlayerContext getPlayerContext() {
|
||||
return this.playerContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FollowProcess getFollowProcess() {
|
||||
return this.followProcess;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LookBehavior getLookBehavior() {
|
||||
return this.lookBehavior;
|
||||
public PathingBehavior getPathingBehavior() {
|
||||
return this.pathingBehavior;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -180,13 +173,13 @@ public class Baritone implements IBaritone {
|
|||
}
|
||||
|
||||
@Override
|
||||
public MineProcess getMineProcess() {
|
||||
return this.mineProcess;
|
||||
public IPlayerContext getPlayerContext() {
|
||||
return this.playerContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PathingBehavior getPathingBehavior() {
|
||||
return this.pathingBehavior;
|
||||
public FollowProcess getFollowProcess() {
|
||||
return this.followProcess;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -199,6 +192,20 @@ public class Baritone implements IBaritone {
|
|||
return this.gameEventHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LookBehavior getLookBehavior() {
|
||||
return this.lookBehavior;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MineProcess getMineProcess() {
|
||||
return this.mineProcess;
|
||||
}
|
||||
|
||||
public static Executor getExecutor() {
|
||||
return threadPool;
|
||||
}
|
||||
|
||||
public static Settings settings() {
|
||||
return BaritoneAPI.getSettings();
|
||||
}
|
||||
|
@ -206,8 +213,4 @@ public class Baritone implements IBaritone {
|
|||
public static File getDir() {
|
||||
return dir;
|
||||
}
|
||||
|
||||
public static Executor getExecutor() {
|
||||
return threadPool;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package baritone.behavior;
|
||||
|
||||
import baritone.Baritone;
|
||||
import baritone.api.event.events.ChatEvent;
|
||||
import baritone.api.event.events.TickEvent;
|
||||
import baritone.utils.Helper;
|
||||
import comms.*;
|
||||
import comms.downward.MessageChat;
|
||||
import comms.upward.MessageStatus;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class ControllerBehavior extends Behavior implements IMessageListener {
|
||||
public ControllerBehavior(Baritone baritone) {
|
||||
super(baritone);
|
||||
}
|
||||
|
||||
private BufferedConnection<SerializableMessage> conn;
|
||||
|
||||
@Override
|
||||
public void onTick(TickEvent event) {
|
||||
if (event.getType() == TickEvent.Type.OUT) {
|
||||
return;
|
||||
}
|
||||
trySend(new MessageStatus(ctx.player().posX, ctx.player().posY, ctx.player().posZ));
|
||||
}
|
||||
|
||||
private void readAndHandle() {
|
||||
if (conn == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
List<SerializableMessage> msgs = conn.receiveMessagesNonBlocking();
|
||||
msgs.forEach(msg -> ((HandlableMessage) msg).handle(this));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean trySend(SerializableMessage msg) {
|
||||
if (conn == null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
conn.sendMessage(msg);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
disconnect();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void connectTo(IConnection conn) {
|
||||
disconnect();
|
||||
if (conn instanceof BufferedConnection) {
|
||||
this.conn = (BufferedConnection) conn;
|
||||
} else {
|
||||
this.conn = new BufferedConnection(conn);
|
||||
}
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
if (conn != null) {
|
||||
conn.close();
|
||||
}
|
||||
conn = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(MessageChat msg) { // big brain
|
||||
ChatEvent event = new ChatEvent(ctx.player(), msg.msg);
|
||||
baritone.getGameEventHandler().onSendChatMessage(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unhandled(iMessage msg) {
|
||||
Helper.HELPER.logDebug("Unhandled message received by ControllerBehavior " + msg);
|
||||
}
|
||||
}
|
|
@ -31,6 +31,7 @@ import baritone.cache.Waypoint;
|
|||
import baritone.pathing.movement.Movement;
|
||||
import baritone.pathing.movement.Moves;
|
||||
import baritone.process.CustomGoalProcess;
|
||||
import comms.SocketConnection;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.client.multiplayer.ChunkProviderClient;
|
||||
import net.minecraft.entity.Entity;
|
||||
|
@ -38,6 +39,8 @@ import net.minecraft.entity.player.EntityPlayer;
|
|||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
@ -462,6 +465,23 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
|
|||
}
|
||||
return true;
|
||||
}
|
||||
if (msg.startsWith("connect")) {
|
||||
String dest = msg.substring(7).trim();
|
||||
String[] parts = dest.split(" ");
|
||||
if (parts.length != 2) {
|
||||
logDirect("Unable to parse");
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
Socket s = new Socket(parts[0], Integer.parseInt(parts[1]));
|
||||
SocketConnection conn = new SocketConnection(s);
|
||||
baritone.getControllerBehavior().connectTo(conn);
|
||||
logDirect("Created and attached socket connection");
|
||||
} catch (IOException | NumberFormatException e) {
|
||||
logDirect("Unable to connect " + e);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (msg.equals("damn")) {
|
||||
logDirect("daniel");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue