diff --git a/src/comms/java/comms/BufferedConnection.java b/src/comms/java/comms/BufferedConnection.java index 13fef7f57..450010e70 100644 --- a/src/comms/java/comms/BufferedConnection.java +++ b/src/comms/java/comms/BufferedConnection.java @@ -32,16 +32,16 @@ import java.util.concurrent.LinkedBlockingQueue; * * @author leijurv */ -public class BufferedConnection implements IConnection { +public class BufferedConnection implements IConnection { private final IConnection wrapped; - private final LinkedBlockingQueue queue; + private final LinkedBlockingQueue queue; private volatile transient IOException thrownOnRead; - public BufferedConnection(IConnection wrapped) { + public BufferedConnection(IConnection wrapped) { this(wrapped, Integer.MAX_VALUE); // LinkedBlockingQueue accepts this as "no limit" } - public BufferedConnection(IConnection wrapped, int maxInternalQueueSize) { + public BufferedConnection(IConnection wrapped, int maxInternalQueueSize) { this.wrapped = wrapped; this.queue = new LinkedBlockingQueue<>(); this.thrownOnRead = null; @@ -59,12 +59,12 @@ public class BufferedConnection implements IConnection { } @Override - public void sendMessage(T message) throws IOException { + public void sendMessage(iMessage message) throws IOException { wrapped.sendMessage(message); } @Override - public T receiveMessage() { + public iMessage receiveMessage() { throw new UnsupportedOperationException("BufferedConnection can only be read from non-blockingly"); } @@ -74,8 +74,8 @@ public class BufferedConnection implements IConnection { thrownOnRead = new EOFException("Closed"); } - public List receiveMessagesNonBlocking() throws IOException { - ArrayList msgs = new ArrayList<>(); + 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); diff --git a/src/comms/java/comms/ConstructingDeserializer.java b/src/comms/java/comms/ConstructingDeserializer.java index 3879204a5..c6407976a 100644 --- a/src/comms/java/comms/ConstructingDeserializer.java +++ b/src/comms/java/comms/ConstructingDeserializer.java @@ -26,20 +26,20 @@ import java.util.List; public enum ConstructingDeserializer implements MessageDeserializer { INSTANCE; - private final List> MSGS; + 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()) { if (m.getName().equals("handle")) { - MSGS.add((Class) m.getParameterTypes()[0]); + MSGS.add((Class) m.getParameterTypes()[0]); } } } @Override - public SerializableMessage deserialize(DataInputStream in) throws IOException { + public iMessage deserialize(DataInputStream in) throws IOException { int type = ((int) in.readByte()) & 0xff; try { return MSGS.get(type).getConstructor(DataInputStream.class).newInstance(in); @@ -48,7 +48,7 @@ public enum ConstructingDeserializer implements MessageDeserializer { } } - public byte getHeader(Class klass) { + 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 deleted file mode 100644 index 2d740fcd2..000000000 --- a/src/comms/java/comms/FilteredConnection.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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 deleted file mode 100644 index dce1d3dec..000000000 --- a/src/comms/java/comms/HandlableMessage.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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 index 05cef73df..dc54beb08 100644 --- a/src/comms/java/comms/IConnection.java +++ b/src/comms/java/comms/IConnection.java @@ -19,10 +19,10 @@ package comms; import java.io.IOException; -public interface IConnection { - void sendMessage(T message) throws IOException; +public interface IConnection { + void sendMessage(iMessage message) throws IOException; - T receiveMessage() throws IOException; + iMessage receiveMessage() throws IOException; void close(); } diff --git a/src/comms/java/comms/MessageDeserializer.java b/src/comms/java/comms/MessageDeserializer.java index ed18bf1a4..aece828d4 100644 --- a/src/comms/java/comms/MessageDeserializer.java +++ b/src/comms/java/comms/MessageDeserializer.java @@ -21,5 +21,5 @@ import java.io.DataInputStream; import java.io.IOException; public interface MessageDeserializer { - SerializableMessage deserialize(DataInputStream in) throws IOException; + iMessage deserialize(DataInputStream in) throws IOException; } diff --git a/src/comms/java/comms/Pipe.java b/src/comms/java/comms/Pipe.java index a8cad44fa..c189af600 100644 --- a/src/comms/java/comms/Pipe.java +++ b/src/comms/java/comms/Pipe.java @@ -26,8 +26,8 @@ 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 LinkedBlockingQueue> AtoB; + private final LinkedBlockingQueue> BtoA; private final PipedConnection A; private final PipedConnection B; private volatile boolean closed; @@ -47,17 +47,17 @@ public class Pipe { return B; } - public class PipedConnection implements IConnection { - private final LinkedBlockingQueue> in; - private final LinkedBlockingQueue> out; + public class PipedConnection implements IConnection { + private final LinkedBlockingQueue> in; + private final LinkedBlockingQueue> out; - private PipedConnection(LinkedBlockingQueue> in, LinkedBlockingQueue> out) { + private PipedConnection(LinkedBlockingQueue> in, LinkedBlockingQueue> out) { this.in = in; this.out = out; } @Override - public void sendMessage(T message) throws IOException { + public void sendMessage(iMessage message) throws IOException { if (closed) { throw new EOFException("Closed"); } @@ -69,12 +69,12 @@ public class Pipe { } @Override - public T receiveMessage() throws IOException { + public iMessage receiveMessage() throws IOException { if (closed) { throw new EOFException("Closed"); } try { - Optional t = in.take(); + Optional t = in.take(); if (!t.isPresent()) { throw new EOFException("Closed"); } diff --git a/src/comms/java/comms/SerializableMessage.java b/src/comms/java/comms/SerializableMessage.java deleted file mode 100644 index 901874461..000000000 --- a/src/comms/java/comms/SerializableMessage.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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 index 9e8af84ca..0952854d2 100644 --- a/src/comms/java/comms/SerializedConnection.java +++ b/src/comms/java/comms/SerializedConnection.java @@ -19,7 +19,7 @@ package comms; import java.io.*; -public class SerializedConnection implements IConnection { +public class SerializedConnection implements IConnection { private final DataInputStream in; private final DataOutputStream out; private final MessageDeserializer deserializer; @@ -35,13 +35,13 @@ public class SerializedConnection implements IConnection { } @Override - public void sendMessage(SerializableMessage message) throws IOException { + public void sendMessage(iMessage message) throws IOException { message.writeHeader(out); message.write(out); } @Override - public SerializableMessage receiveMessage() throws IOException { + public iMessage receiveMessage() throws IOException { return deserializer.deserialize(in); } diff --git a/src/comms/java/comms/downward/MessageChat.java b/src/comms/java/comms/downward/MessageChat.java index 35b63ee5a..b04d2a315 100644 --- a/src/comms/java/comms/downward/MessageChat.java +++ b/src/comms/java/comms/downward/MessageChat.java @@ -17,15 +17,14 @@ package comms.downward; -import comms.HandlableMessage; import comms.IMessageListener; -import comms.SerializableMessage; +import comms.iMessage; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -public class MessageChat implements SerializableMessage, HandlableMessage { +public class MessageChat implements iMessage { public final String msg; diff --git a/src/comms/java/comms/iMessage.java b/src/comms/java/comms/iMessage.java index 5a4ebd293..67ec29651 100644 --- a/src/comms/java/comms/iMessage.java +++ b/src/comms/java/comms/iMessage.java @@ -17,6 +17,9 @@ package comms; +import java.io.DataOutputStream; +import java.io.IOException; + /** * hell yeah *

@@ -27,4 +30,15 @@ package comms; * @author leijurv */ public interface 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()); + } + + void handle(IMessageListener listener); } diff --git a/src/comms/java/comms/upward/MessageStatus.java b/src/comms/java/comms/upward/MessageStatus.java index fe0a64895..55a298280 100644 --- a/src/comms/java/comms/upward/MessageStatus.java +++ b/src/comms/java/comms/upward/MessageStatus.java @@ -17,15 +17,14 @@ package comms.upward; -import comms.HandlableMessage; import comms.IMessageListener; -import comms.SerializableMessage; +import comms.iMessage; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -public class MessageStatus implements SerializableMessage, HandlableMessage { +public class MessageStatus implements iMessage { public final double x; public final double y; diff --git a/src/main/java/baritone/behavior/ControllerBehavior.java b/src/main/java/baritone/behavior/ControllerBehavior.java index 25a128a10..dd8594df6 100644 --- a/src/main/java/baritone/behavior/ControllerBehavior.java +++ b/src/main/java/baritone/behavior/ControllerBehavior.java @@ -21,8 +21,11 @@ import baritone.Baritone; import baritone.api.event.events.ChatEvent; import baritone.api.event.events.TickEvent; import baritone.utils.Helper; -import comms.*; +import comms.BufferedConnection; +import comms.IConnection; +import comms.IMessageListener; import comms.downward.MessageChat; +import comms.iMessage; import comms.upward.MessageStatus; import java.io.IOException; @@ -33,7 +36,7 @@ public class ControllerBehavior extends Behavior implements IMessageListener { super(baritone); } - private BufferedConnection conn; + private BufferedConnection conn; @Override public void onTick(TickEvent event) { @@ -48,15 +51,15 @@ public class ControllerBehavior extends Behavior implements IMessageListener { return; } try { - List msgs = conn.receiveMessagesNonBlocking(); - msgs.forEach(msg -> ((HandlableMessage) msg).handle(this)); + List msgs = conn.receiveMessagesNonBlocking(); + msgs.forEach(msg -> msg.handle(this)); } catch (IOException e) { e.printStackTrace(); disconnect(); } } - public boolean trySend(SerializableMessage msg) { + public boolean trySend(iMessage msg) { if (conn == null) { return false; }