Skip to content
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

Optimizations and Code Refactoring #15

Open
wants to merge 4 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 @@ -13,11 +13,11 @@ public BukkitConfigLoader(final ConfigurationSection configFile) {

@Override
public boolean getBoolean(final String key) {
return this.configFile.getBoolean(key);
return configFile.getBoolean(key);
}

@Override
public String getString(final String key) {
return this.configFile.getString(key);
return configFile.getString(key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import app.shopmc.plugin.router.Socket;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.java_websocket.handshake.ServerHandshake;

Expand All @@ -19,12 +18,10 @@ public class BukkitShopMCPlugin extends JavaPlugin {

@Override
public void onEnable() {
// init config file
if (!new File(getDataFolder(), "config.yml").exists()) {
saveDefaultConfig();
}

// check if config is correct
try {
config = new Config(new BukkitConfigLoader(this.getConfig()));
} catch (EmptyConfigFieldException exception) {
Expand All @@ -33,11 +30,12 @@ public void onEnable() {
return;
}

Thread networkThread = new Thread(() -> {
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
socket = new Socket(config.key) {
@Override
public void onCommand(String command) {
Bukkit.getScheduler().runTask(BukkitShopMCPlugin.this, () -> Bukkit.dispatchCommand(getServer().getConsoleSender(), command));
Bukkit.getScheduler().runTask(BukkitShopMCPlugin.this, () ->
Bukkit.dispatchCommand(getServer().getConsoleSender(), command));
getLogger().info("Executed command: " + command);
}

Expand All @@ -49,16 +47,13 @@ public void onOpen(ServerHandshake serverHandshake) {
@Override
public void onClose(int code, String reason, boolean remote) {
getLogger().warning("Connection closed");
reconnectTask = new BukkitRunnable() {
@Override
public void run() {
if (!socket.isOpen()) {
socket.reconnect();
} else {
cancel();
}
reconnectTask = Bukkit.getScheduler().runTaskTimerAsynchronously(BukkitShopMCPlugin.this, () -> {
if (!socket.isOpen()) {
socket.reconnect();
} else {
reconnectTask.cancel();
}
}.runTaskLater(BukkitShopMCPlugin.this, 200);
}, 0, 200L);
}

@Override
Expand All @@ -69,7 +64,6 @@ public void onError(Exception ex) {
socket.connect();
socket.setConnectionLostTimeout(0);
});
networkThread.start();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public BungeeConfigLoader(final Configuration configFile) {

@Override
public boolean getBoolean(final String key) {
return this.configFile.getBoolean(key);
return configFile.getBoolean(key);
}

@Override
public String getString(final String key) {
return this.configFile.getString(key);
return configFile.getString(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ public void onEnable() {
final ResourceLoader<Configuration> resourceLoader = new BungeeResourceLoader(this.getClass(), this.getDataFolder());
try {
if (resourceLoader.saveDefault("config.yml")) {
this.getLogger().info("Default file config.yml has been saved, configure it and restart proxy");
getLogger().info("Default file config.yml has been saved, configure it and restart proxy");
return;
}

try {
final Configuration cfgFile = resourceLoader.load("config.yml");
config = new Config(new BungeeConfigLoader(cfgFile));
} catch (final EmptyConfigFieldException exception) {
this.getLogger().severe(exception.getMessage());
getLogger().severe(exception.getMessage());
return; // Stop further execution if config is not valid
}
} catch (final ResourceLoaderException exception) {
this.getLogger().severe("Reason: " + exception.getCause().getMessage());
getLogger().severe("Reason: " + exception.getCause().getMessage());
return; // Stop further execution if resource loading fails
}

socket = new Socket(config.key) {
Expand All @@ -52,7 +54,6 @@ public void onCommand(String command) {
getLogger().info("Executed command: " + command);
}


@Override
public void onClose(int code, String reason, boolean remote) {
getLogger().warning("Connection closed");
Expand Down
4 changes: 2 additions & 2 deletions common/src/main/java/app/shopmc/plugin/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ public class Config {

public Config(final ConfigLoader loader) throws EmptyConfigFieldException {
this.key = loader.getString("key");
this.checkValues();
checkValues();
}

private void checkValues() throws EmptyConfigFieldException {
if (this.key == null || this.key.isEmpty()) {
if (key == null || key.isEmpty()) {
throw new EmptyConfigFieldException("key");
}
}
Expand Down
38 changes: 22 additions & 16 deletions common/src/main/java/app/shopmc/plugin/resource/ResourceLoader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.shopmc.plugin.resource;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -18,29 +19,34 @@ protected ResourceLoader(final Class<?> loadingClass, final Path dataDirectory)
protected abstract T loadResource(final Path resourcePath) throws ResourceLoaderException;

public boolean saveDefault(final String resourceName) throws ResourceLoaderException {
if (!Files.exists(this.dataDirectory)) {
try {
Files.createDirectories(this.dataDirectory);
} catch (final Exception exception) {
throw new ResourceLoaderException(ResourceLoaderException.Reason.DIRECTORY_NOT_CREATED, exception);
try {
createDataDirectoryIfNotExists();
final Path resourcePath = this.dataDirectory.resolve(resourceName);

if (Files.notExists(resourcePath)) {
try (final InputStream in = this.loadingClass.getClassLoader().getResourceAsStream(resourceName)) {
Files.copy(Objects.requireNonNull(in), resourcePath);
return true;
}
}
} catch (final IOException exception) {
throw new ResourceLoaderException(ResourceLoaderException.Reason.DEFAULT_FILE_NOT_SAVED, exception);
}

final Path resourcePath = this.dataDirectory.resolve(resourceName);
if (!Files.exists(resourcePath)) {
try (final InputStream in = this.loadingClass.getClassLoader().getResourceAsStream(resourceName)) {
Files.copy(Objects.requireNonNull(in), resourcePath);
return true;
} catch (final Exception exception) {
throw new ResourceLoaderException(ResourceLoaderException.Reason.DEFAULT_FILE_NOT_SAVED, exception);
return false;
}

private void createDataDirectoryIfNotExists() throws ResourceLoaderException {
if (Files.notExists(this.dataDirectory)) {
try {
Files.createDirectories(this.dataDirectory);
} catch (final IOException exception) {
throw new ResourceLoaderException(ResourceLoaderException.Reason.DIRECTORY_NOT_CREATED, exception);
}
}

return false;
}

public T load(final String resourceName) throws ResourceLoaderException {
return this.loadResource(this.dataDirectory.resolve(resourceName));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ public String getMessage(final String fileName) {
return String.format(this.message, fileName);
}
}

}
6 changes: 4 additions & 2 deletions common/src/main/java/app/shopmc/plugin/router/Socket.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
import java.net.URI;

public abstract class Socket extends WebSocketClient {

public abstract void onCommand(String command);
public Socket(String key){

public Socket(String key) {
super(URI.create("wss://router.shopmc.app/" + key));
}

@Override
public void onMessage(String commands) {
for (String command : commands.split("\n")) {
Socket.this.onCommand(command);
onCommand(command);
}
}
}