Skip to content

Commit

Permalink
More refactoring, remove unnecesary interface method "getPluginDataMa…
Browse files Browse the repository at this point in the history
…nager"

remove unnecesary interface method from APi class "getPluginDataManager

More refactoring, code is now sub 700 lines!
  • Loading branch information
Burchard36 committed Nov 20, 2021
1 parent 5a19a35 commit 14ca0b8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 43 deletions.
9 changes: 0 additions & 9 deletions src/main/java/com/burchard36/Api.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
package com.burchard36;

import com.burchard36.json.PluginDataManager;

public interface Api {

/**
* In order for the Data Management side of this plugin to work, it needs to have access to an already running
* PluginDataManager
* @return running PluginDataManager instance
*/
PluginDataManager getPluginDataManager();

/**
* If the debug mode is true, when debug is called on the
* Logger#debug method a message gets logged, this also may effect other things
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/com/burchard36/ApiLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ public static Api getLib(final JavaPlugin plugin) {
} else return null;
}

@Override
public PluginDataManager getPluginDataManager() {
return this.manager;
}

@Override
public boolean isDebug() {
return getLib(this.plugin).isDebug();
Expand All @@ -100,4 +95,12 @@ public static String convert(final String message) {
public HologramManager getHologramManager() {
return this.hologramManager;
}

/**
* Returns an instance of the PluginDataManager
* @return insstance of PluginDataManager
*/
public PluginDataManager getPluginDataManager() {
return this.manager;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/burchard36/json/PluginDataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class PluginDataManager {

public PluginDataManager(final JavaPlugin plugin) {
this.mapper = new ObjectMapper();
this.jsonWriter = new PluginJsonWriter(plugin);
this.jsonWriter = new PluginJsonWriter(this.mapper);
}

/**
Expand Down
34 changes: 6 additions & 28 deletions src/main/java/com/burchard36/json/PluginJsonWriter.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,21 @@
package com.burchard36.json;

import com.burchard36.Api;
import com.burchard36.ApiLib;
import com.burchard36.Logger;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.*;

public class PluginJsonWriter {

private final JavaPlugin plugin;
private ObjectMapper mapper = null;

public PluginJsonWriter(final JavaPlugin plugin) {
this.plugin = plugin;
final Api apiLib = ApiLib.getLib(plugin);
if (apiLib != null) {
this.mapper = apiLib.getPluginDataManager().getMapper();
} else Logger.error("API :: ERROR! :: Error when grabbing moshi variables from Api implementing class," +
" as it does not implement Api!");
}
public record PluginJsonWriter(ObjectMapper mapper) {

public File createFile(final JsonDataFile config) {
final File file = config.getFile();
if (!file.exists()) {
try {
if (!file.getParentFile().exists())
if (file.getParentFile().mkdirs())
Logger.log("&aAPI :: Successfully created directories");
if (file.createNewFile()) this.mapper.writeValue(file, config);

if (!file.getParentFile().exists()) if (file.getParentFile().mkdirs()) {
Bukkit.getLogger().info("API :: Successfully created directories");
}

if (file.createNewFile()) {
this.mapper.writeValue(file, config);
}
} catch (IOException ex) {
ex.printStackTrace();
}
Expand All @@ -44,9 +25,7 @@ public File createFile(final JsonDataFile config) {

public void writeDataToFile(final JsonDataFile config) {
File file = config.getFile();
if (!file.exists()) {
file = this.createFile(config);
}
if (!file.exists()) file = this.createFile(config);

try {
this.mapper.writeValue(file, config);
Expand All @@ -67,5 +46,4 @@ public JsonDataFile getDataFromFile(final JsonDataFile config) {
return null;
}
}

}

0 comments on commit 14ca0b8

Please sign in to comment.