Skip to content

Allow more versatile implementations of Polychat clients #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import club.moddedminecraft.polychat.client.clientbase.handlers.CommandMessageHandler;
import club.moddedminecraft.polychat.client.clientbase.handlers.PlayerStatusChangedMessageHandler;
import club.moddedminecraft.polychat.client.clientbase.handlers.ServerStatusMessageHandler;
import club.moddedminecraft.polychat.client.clientbase.logging.LogManager;
import club.moddedminecraft.polychat.client.clientbase.util.MuteStorage;
import club.moddedminecraft.polychat.core.common.YamlConfig;
import club.moddedminecraft.polychat.core.messagelibrary.PolychatProtobufMessageDispatcher;
Expand Down Expand Up @@ -53,9 +54,13 @@ public class PolychatClient {
* @param clientImpl the implementation of the client protocol
*/
public PolychatClient(ClientApiBase clientImpl) {
clientCallbacks = new ClientCallbacks(this);
this(clientImpl, null);
}

public PolychatClient(ClientApiBase clientImpl, YamlConfig customConfig) {
clientApi = clientImpl;
config = getConfig();
config = customConfig != null ? customConfig : getConfig();
clientCallbacks = new ClientCallbacks(this);
client = new Client(config.getOrDefault("server.address", "localhost"),
config.getOrDefault("server.port", 5005), config.getOrDefault("server.buffersize", 32768));
polychatProtobufMessageDispatcher = new PolychatProtobufMessageDispatcher();
Expand All @@ -79,6 +84,10 @@ public PolychatClient(ClientApiBase clientImpl) {
* @return client config
*/
public YamlConfig getConfig() {
if (config != null) {
return config;
}

try {
Path directory = clientApi.getConfigDirectory();
Path configPath = directory.resolve("polychat.yml");
Expand All @@ -90,8 +99,7 @@ public YamlConfig getConfig() {

return YamlConfig.fromFilesystem(configPath);
} catch (IOException e) {
System.err.println("Failed to load config!");
e.printStackTrace();
LogManager.LOGGER.error("Failed to load config!", e);
}
return YamlConfig.fromInMemoryString("");
}
Expand Down Expand Up @@ -141,7 +149,7 @@ public void update() {
try {
messages = client.poll();
} catch (IOException e) {
System.err.println("Failed to reconnect to Polychat server");
LogManager.LOGGER.warn("Failed to reconnect to Polychat server", e);
}

for (Message message : messages) {
Expand All @@ -162,8 +170,7 @@ public void sendMessage(com.google.protobuf.Message message) {
try {
client.sendMessage(messageBytes);
} catch (IOException e) {
System.err.println("Failed to send message!");
e.printStackTrace();
LogManager.LOGGER.warn("Failed to send message!", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class CommandMessageHandler {
public CommandMessageHandler(ClientApiBase clientApiBase, PolychatClient client) {
this.clientApiBase = clientApiBase;
this.client = client;
System.out.println("command message handler");
}

public int calculateParameters(String command) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package club.moddedminecraft.polychat.client.clientbase.logging;

public class DefaultLogger implements Logger {
@Override
public void error(String message) {
System.err.println(message);
}

@Override
public void error(String message, Throwable t) {
System.err.println(message);
t.printStackTrace();
}

@Override
public void warn(String message) {
error(message);
}

@Override
public void warn(String message, Throwable t) {
error(message, t);
}

@Override
public void info(String message) {
System.out.println(message);
}

@Override
public void info(String message, Throwable t) {
System.out.println(message);
t.printStackTrace(System.out);
}

@Override
public void debug(String message) {
info(message);
}

@Override
public void debug(String message, Throwable t) {
info(message, t);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package club.moddedminecraft.polychat.client.clientbase.logging;

public class LogManager {
public static Logger LOGGER = new DefaultLogger();

private LogManager() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package club.moddedminecraft.polychat.client.clientbase.logging;

public interface Logger {
void error(String message);
void error(String message, Throwable t);
void warn(String message);
void warn(String message, Throwable t);
void info(String message);
void info(String message, Throwable t);
void debug(String message);
void debug(String message, Throwable t);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package club.moddedminecraft.polychat.client.clientbase.util;

import club.moddedminecraft.polychat.client.clientbase.logging.LogManager;

import java.io.*;
import java.nio.file.Path;
import java.util.*;
Expand All @@ -19,14 +21,14 @@ private void loadMutelist() {
try {
Object unparsed = new ObjectInputStream(new FileInputStream(path.resolve(FILENAME).toFile())).readObject();
if (!(unparsed instanceof UUID[])) {
System.err.println("Failed to parse mutelist!");
LogManager.LOGGER.warn("Failed to parse mutelist!");
return;
}
UUID[] uuids = (UUID[]) unparsed;
List<UUID> tempList = Arrays.asList(uuids);
Collections.addAll(muteList, uuids);
} catch (IOException | ClassNotFoundException e) {
System.err.println("Failed to access mutelist!");
LogManager.LOGGER.warn("Failed to access mutelist!");
}
}

Expand Down Expand Up @@ -54,7 +56,7 @@ private void writeList(ArrayList<UUID> uuids) {
UUID[] uuidArray = uuids.toArray(new UUID[0]);
outputStream.writeObject(uuidArray);
} catch (IOException e) {
System.err.println("Failed to access mutelist");
LogManager.LOGGER.warn("Failed to access mutelist");
}
}

Expand Down