Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 All @@ -21,12 +22,12 @@
public class PolychatClient {

private final ClientApiBase clientApi;
private final Client client;
private final PolychatProtobufMessageDispatcher polychatProtobufMessageDispatcher;
private final YamlConfig config;
private final String serverId;
private final ClientCallbacks clientCallbacks;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What made you strip the final from each of these?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed them because I moved common initialization code to it's own method. I was kinda lost how to properly do this, the new approach is better.

private final MuteStorage muteStorage;
private Client client;
private PolychatProtobufMessageDispatcher polychatProtobufMessageDispatcher;
private YamlConfig config;
private String serverId;
private ClientCallbacks clientCallbacks;
private MuteStorage muteStorage;
private long lastUpdate = 0;
private static final HashMap<Integer, Integer> colorHashMap = new HashMap<Integer, Integer>() {{
put(0, 0x000000);
Expand All @@ -53,8 +54,18 @@ public class PolychatClient {
* @param clientImpl the implementation of the client protocol
*/
public PolychatClient(ClientApiBase clientImpl) {
clientCallbacks = new ClientCallbacks(this);
clientApi = clientImpl;
initialize();
}

public PolychatClient(ClientApiBase clientImpl, YamlConfig customConfig) {
clientApi = clientImpl;
config = customConfig;
initialize();
}

private void initialize() {
clientCallbacks = new ClientCallbacks(this);
config = getConfig();
client = new Client(config.getOrDefault("server.address", "localhost"),
config.getOrDefault("server.port", 5005), config.getOrDefault("server.buffersize", 32768));
Expand All @@ -79,6 +90,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 +105,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 +155,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 +176,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