remove the useless stuff

This commit is contained in:
Leijurv
2018-11-18 11:35:54 -08:00
parent dfb49179c5
commit 9ad35dbf28
13 changed files with 54 additions and 149 deletions

View File

@@ -32,16 +32,16 @@ import java.util.concurrent.LinkedBlockingQueue;
*
* @author leijurv
*/
public class BufferedConnection<T extends iMessage> implements IConnection<T> {
public class BufferedConnection implements IConnection {
private final IConnection wrapped;
private final LinkedBlockingQueue<T> queue;
private final LinkedBlockingQueue<iMessage> queue;
private volatile transient IOException thrownOnRead;
public BufferedConnection(IConnection<T> wrapped) {
public BufferedConnection(IConnection wrapped) {
this(wrapped, Integer.MAX_VALUE); // LinkedBlockingQueue accepts this as "no limit"
}
public BufferedConnection(IConnection<T> 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<T extends iMessage> implements IConnection<T> {
}
@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<T extends iMessage> implements IConnection<T> {
thrownOnRead = new EOFException("Closed");
}
public List<T> receiveMessagesNonBlocking() throws IOException {
ArrayList<T> msgs = new ArrayList<>();
public List<iMessage> receiveMessagesNonBlocking() throws IOException {
ArrayList<iMessage> 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);

View File

@@ -26,20 +26,20 @@ import java.util.List;
public enum ConstructingDeserializer implements MessageDeserializer {
INSTANCE;
private final List<Class<? extends SerializableMessage>> MSGS;
private final List<Class<? extends iMessage>> 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<? extends SerializableMessage>) m.getParameterTypes()[0]);
MSGS.add((Class<? extends iMessage>) 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<? extends SerializableMessage> klass) {
public byte getHeader(Class<? extends iMessage> klass) {
return (byte) MSGS.indexOf(klass);
}
}

View File

@@ -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 <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();
}
}

View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
package comms;
public interface HandlableMessage {
void handle(IMessageListener listener);
}

View File

@@ -19,10 +19,10 @@ package comms;
import java.io.IOException;
public interface IConnection<T extends iMessage> {
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();
}

View File

@@ -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;
}

View File

@@ -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<T extends iMessage> {
private final LinkedBlockingQueue<Optional<T>> AtoB;
private final LinkedBlockingQueue<Optional<T>> BtoA;
private final LinkedBlockingQueue<Optional<iMessage>> AtoB;
private final LinkedBlockingQueue<Optional<iMessage>> BtoA;
private final PipedConnection A;
private final PipedConnection B;
private volatile boolean closed;
@@ -47,17 +47,17 @@ public class Pipe<T extends iMessage> {
return B;
}
public class PipedConnection implements IConnection<T> {
private final LinkedBlockingQueue<Optional<T>> in;
private final LinkedBlockingQueue<Optional<T>> out;
public class PipedConnection implements IConnection {
private final LinkedBlockingQueue<Optional<iMessage>> in;
private final LinkedBlockingQueue<Optional<iMessage>> out;
private PipedConnection(LinkedBlockingQueue<Optional<T>> in, LinkedBlockingQueue<Optional<T>> out) {
private PipedConnection(LinkedBlockingQueue<Optional<iMessage>> in, LinkedBlockingQueue<Optional<iMessage>> 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<T extends iMessage> {
}
@Override
public T receiveMessage() throws IOException {
public iMessage receiveMessage() throws IOException {
if (closed) {
throw new EOFException("Closed");
}
try {
Optional<T> t = in.take();
Optional<iMessage> t = in.take();
if (!t.isPresent()) {
throw new EOFException("Closed");
}

View File

@@ -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 <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());
}
}

View File

@@ -19,7 +19,7 @@ package comms;
import java.io.*;
public class SerializedConnection implements IConnection<SerializableMessage> {
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<SerializableMessage> {
}
@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);
}

View File

@@ -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;

View File

@@ -17,6 +17,9 @@
package comms;
import java.io.DataOutputStream;
import java.io.IOException;
/**
* hell yeah
* <p>
@@ -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);
}

View File

@@ -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;

View File

@@ -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<SerializableMessage> conn;
private BufferedConnection conn;
@Override
public void onTick(TickEvent event) {
@@ -48,15 +51,15 @@ public class ControllerBehavior extends Behavior implements IMessageListener {
return;
}
try {
List<SerializableMessage> msgs = conn.receiveMessagesNonBlocking();
msgs.forEach(msg -> ((HandlableMessage) msg).handle(this));
List<iMessage> 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;
}