diff --git a/src/comms/java/comms/BufferedConnection.java b/src/comms/java/comms/BufferedConnection.java new file mode 100644 index 00000000..13fef7f5 --- /dev/null +++ b/src/comms/java/comms/BufferedConnection.java @@ -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 . + */ + +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? + *

+ * Do you prefer just being able to get a list of all messages received since the last tick? + *

+ * If so, this class is for you! + * + * @author leijurv + */ +public class BufferedConnection implements IConnection { + private final IConnection wrapped; + private final LinkedBlockingQueue queue; + private volatile transient IOException thrownOnRead; + + public BufferedConnection(IConnection wrapped) { + this(wrapped, Integer.MAX_VALUE); // LinkedBlockingQueue accepts this as "no limit" + } + + public BufferedConnection(IConnection 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 receiveMessagesNonBlocking() throws IOException { + ArrayList 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; + } +} diff --git a/src/comms/java/comms/ConstructingDeserializer.java b/src/comms/java/comms/ConstructingDeserializer.java new file mode 100644 index 00000000..4048a7be --- /dev/null +++ b/src/comms/java/comms/ConstructingDeserializer.java @@ -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 . + */ + +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> 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) 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 klass) { + return (byte) MSGS.indexOf(klass); + } +} diff --git a/src/comms/java/comms/FilteredConnection.java b/src/comms/java/comms/FilteredConnection.java new file mode 100644 index 00000000..2d740fcd --- /dev/null +++ b/src/comms/java/comms/FilteredConnection.java @@ -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 . + */ + +package comms; + +import java.io.IOException; + +/** + * This is just a meme idk if this is actually useful + * + * @param + */ +public class FilteredConnection implements IConnection { + private final Class klass; + private final IConnection wrapped; + + public FilteredConnection(IConnection wrapped, Class 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(); + } +} diff --git a/src/comms/java/comms/HandlableMessage.java b/src/comms/java/comms/HandlableMessage.java new file mode 100644 index 00000000..dce1d3de --- /dev/null +++ b/src/comms/java/comms/HandlableMessage.java @@ -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 . + */ + +package comms; + +public interface HandlableMessage { + void handle(IMessageListener listener); +} diff --git a/src/comms/java/comms/IConnection.java b/src/comms/java/comms/IConnection.java new file mode 100644 index 00000000..05cef73d --- /dev/null +++ b/src/comms/java/comms/IConnection.java @@ -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 . + */ + +package comms; + +import java.io.IOException; + +public interface IConnection { + void sendMessage(T message) throws IOException; + + T receiveMessage() throws IOException; + + void close(); +} diff --git a/src/comms/java/comms/IMessageListener.java b/src/comms/java/comms/IMessageListener.java new file mode 100644 index 00000000..afed4cfe --- /dev/null +++ b/src/comms/java/comms/IMessageListener.java @@ -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 . + */ + +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 + } +} \ No newline at end of file diff --git a/src/comms/java/comms/MessageDeserializer.java b/src/comms/java/comms/MessageDeserializer.java new file mode 100644 index 00000000..ed18bf1a --- /dev/null +++ b/src/comms/java/comms/MessageDeserializer.java @@ -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 . + */ + +package comms; + +import java.io.DataInputStream; +import java.io.IOException; + +public interface MessageDeserializer { + SerializableMessage deserialize(DataInputStream in) throws IOException; +} diff --git a/src/comms/java/comms/Pipe.java b/src/comms/java/comms/Pipe.java new file mode 100644 index 00000000..a8cad44f --- /dev/null +++ b/src/comms/java/comms/Pipe.java @@ -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 . + */ + +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 { + private final LinkedBlockingQueue> AtoB; + private final LinkedBlockingQueue> 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 { + private final LinkedBlockingQueue> in; + private final LinkedBlockingQueue> out; + + private PipedConnection(LinkedBlockingQueue> in, LinkedBlockingQueue> 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 = 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) { + } + } + } +} diff --git a/src/comms/java/comms/SerializableMessage.java b/src/comms/java/comms/SerializableMessage.java new file mode 100644 index 00000000..90187446 --- /dev/null +++ b/src/comms/java/comms/SerializableMessage.java @@ -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 . + */ + +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()); + } +} diff --git a/src/comms/java/comms/SerializedConnection.java b/src/comms/java/comms/SerializedConnection.java new file mode 100644 index 00000000..9e8af84c --- /dev/null +++ b/src/comms/java/comms/SerializedConnection.java @@ -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 . + */ + +package comms; + +import java.io.*; + +public class SerializedConnection implements IConnection { + 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) { + } + } +} \ No newline at end of file diff --git a/src/comms/java/comms/SocketConnection.java b/src/comms/java/comms/SocketConnection.java new file mode 100644 index 00000000..e40d5a1a --- /dev/null +++ b/src/comms/java/comms/SocketConnection.java @@ -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 . + */ + +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()); + } +} diff --git a/src/comms/java/comms/downward/MessageChat.java b/src/comms/java/comms/downward/MessageChat.java new file mode 100644 index 00000000..35b63ee5 --- /dev/null +++ b/src/comms/java/comms/downward/MessageChat.java @@ -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 . + */ + +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); + } +} diff --git a/src/comms/java/comms/iMessage.java b/src/comms/java/comms/iMessage.java new file mode 100644 index 00000000..5a4ebd29 --- /dev/null +++ b/src/comms/java/comms/iMessage.java @@ -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 . + */ + +package comms; + +/** + * hell yeah + *

+ *

+ * dumb android users cant read this file + *

+ * + * @author leijurv + */ +public interface iMessage { +} diff --git a/src/comms/java/comms/upward/MessageStatus.java b/src/comms/java/comms/upward/MessageStatus.java new file mode 100644 index 00000000..fe0a6489 --- /dev/null +++ b/src/comms/java/comms/upward/MessageStatus.java @@ -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 . + */ + +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); + } +} diff --git a/src/main/java/baritone/Baritone.java b/src/main/java/baritone/Baritone.java index 42403e84..0b42e112 100755 --- a/src/main/java/baritone/Baritone.java +++ b/src/main/java/baritone/Baritone.java @@ -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 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 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; - } } diff --git a/src/main/java/baritone/behavior/ControllerBehavior.java b/src/main/java/baritone/behavior/ControllerBehavior.java new file mode 100644 index 00000000..25a128a1 --- /dev/null +++ b/src/main/java/baritone/behavior/ControllerBehavior.java @@ -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 . + */ + +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 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 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); + } +} diff --git a/src/main/java/baritone/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index 841349e4..03f1079f 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -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"); }