Skip to content

Commit

Permalink
Refractor loading and saving code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mystiflow committed Dec 11, 2019
1 parent 57818a4 commit b70fe0c
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 44 deletions.
59 changes: 30 additions & 29 deletions bungee/src/main/java/io/mystiflow/catalogue/CataloguePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;
import io.mystiflow.catalogue.api.Action;
import io.mystiflow.catalogue.api.Catalogue;
import io.mystiflow.catalogue.api.Delay;
Expand All @@ -15,63 +14,65 @@
import net.md_5.bungee.api.plugin.Plugin;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class CataloguePlugin extends Plugin {

@Getter
private static CataloguePlugin plugin;
@Getter
private Gson gson;
@Getter
private Catalogue catalogue;
private File catalogueFile;

@Override
public void onEnable() {
CataloguePlugin.plugin = this;

gson = new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapter(Message.class, new MessageAdapter())
.registerTypeAdapter(Action.class, new ActionAdapter())
.registerTypeAdapter(Delay.class, new DelayAdapter())
.create();

reloadCatalogue();

getProxy().getPluginManager().registerCommand(this, new CatalogueCommand(this));
}
if (!getDataFolder().exists()) {
getDataFolder().mkdir();
}

public void reloadCatalogue() {
if (getDataFolder().exists() || !getDataFolder().mkdir()) {
File file = new File(getDataFolder(), "messages.json");
if (!file.exists()) {
try (InputStream in = getResourceAsStream(file.getName())) {
Files.copy(in, file.toPath());
} catch (IOException ex) {
ex.printStackTrace();
}
}
catalogueFile = new File(getDataFolder(), "messages.json");

try (InputStreamReader reader = new InputStreamReader(new FileInputStream(file));
JsonReader jsonReader = new JsonReader(reader)) {
catalogue = gson.fromJson(jsonReader, Catalogue.class);
// Copy file from JAR to plugin folder
if (!catalogueFile.exists()) {
try (InputStream in = getResourceAsStream(catalogueFile.getName())) {
Files.copy(in, catalogueFile.toPath());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

public void saveCatalogue() {
File file = new File(getDataFolder(), "messages.json");
try {
try (Writer writer = new FileWriter(file)) {
gson.toJson(catalogue, writer);
}
catalogue = Catalogue.load(catalogueFile);
} catch (IOException ex) {
ex.printStackTrace();
}

getProxy().getPluginManager().registerCommand(this, new CatalogueCommand(this));
}

@Override
public void onDisable() {
CataloguePlugin.plugin = null;
}

public void reloadCatalogue() throws IOException {
catalogue = Catalogue.load(catalogueFile);
}

public void saveCatalogue() throws IOException {
catalogue.save(catalogueFile);
}
}
7 changes: 1 addition & 6 deletions bungee/src/main/java/io/mystiflow/catalogue/api/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,14 @@ public enum Type {
COMMAND
}

public Type getType() {
return Type.valueOf(typeString);
}

/**
* The action to perform
*/
private final String action;
/**
* The type of action
*/
@SerializedName("type")
private final String typeString;
private final Type type;
/**
* The amount of times to execute this action
*/
Expand Down
21 changes: 21 additions & 0 deletions bungee/src/main/java/io/mystiflow/catalogue/api/Catalogue.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package io.mystiflow.catalogue.api;

import com.google.gson.stream.JsonReader;
import io.mystiflow.catalogue.CataloguePlugin;
import lombok.Builder;
import lombok.Data;
import lombok.Singular;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.List;
import java.util.Optional;

Expand All @@ -23,4 +31,17 @@ public class Catalogue {
public Optional<Message> getMessage(String name) {
return messages.stream().filter(message -> message.getName().equalsIgnoreCase(name)).findAny();
}

public static Catalogue load(File file) throws IOException {
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(file));
JsonReader jsonReader = new JsonReader(reader)) {
return CataloguePlugin.getPlugin().getGson().fromJson(jsonReader, Catalogue.class);
}
}

public void save(File file) throws IOException {
try (Writer writer = new FileWriter(file)) {
CataloguePlugin.getPlugin().getGson().toJson(this, writer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import com.google.common.base.Joiner;
import io.mystiflow.catalogue.CataloguePlugin;
import io.mystiflow.catalogue.api.Action;
import io.mystiflow.catalogue.api.Catalogue;
import io.mystiflow.catalogue.api.Delay;
import io.mystiflow.catalogue.api.Message;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;

import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -38,7 +40,7 @@ public void execute(CommandSender sender, String[] args) {
return;
}

executeMessage(sender, optionalMessage.get());
execute(sender, optionalMessage.get());
return;
}

Expand All @@ -51,17 +53,31 @@ public void execute(CommandSender sender, String[] args) {
return;
}

if (args[0].equalsIgnoreCase("save")) {
try {
plugin.saveCatalogue();
sender.sendMessage(ChatColor.YELLOW + "Saved catalogue to config");
} catch (IOException ex) {
ex.printStackTrace();
}
return;
}

if (args[0].equalsIgnoreCase("reload")) {
plugin.reloadCatalogue();
sender.sendMessage(ChatColor.YELLOW + "Reloaded catalogue from config");
try {
plugin.reloadCatalogue();
sender.sendMessage(ChatColor.YELLOW + "Reloaded catalogue to config");
} catch (IOException ex) {
ex.printStackTrace();
}
return;
}
}

sender.sendMessage(ChatColor.YELLOW + "/" + getName() + " <execute|list|reload>");
sender.sendMessage(ChatColor.YELLOW + "/" + getName() + " <execute|list|reload|save>");
}

private void executeMessage(CommandSender sender, Message message) {
private void execute(CommandSender sender, Message message) {
List<Action> actions = message.getActions();
for (Action action : actions) {
Runnable runnable = () -> {
Expand All @@ -73,7 +89,7 @@ private void executeMessage(CommandSender sender, Message message) {
plugin.getCatalogue().getMessage(action.getAction()).ifPresent(message1 ->
{
for (int i = 0; i < action.getIterations(); i++) {
executeMessage(sender, message1);
execute(sender, message1);
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Action deserialize(JsonElement element, Type type, JsonDeserializationCon

Action action = new Action(
object.get("action").getAsString(),
object.get("type").getAsString(),
Action.Type.valueOf(object.get("type").getAsString()),
object.get("iterations").getAsInt()
);
if (object.has("delay")) {
Expand All @@ -34,7 +34,7 @@ public Action deserialize(JsonElement element, Type type, JsonDeserializationCon
public JsonElement serialize(Action action, Type type, JsonSerializationContext context) {
JsonObject object = new JsonObject();
object.addProperty("action", action.getAction());
object.addProperty("type", action.getTypeString());
object.addProperty("type", action.getType().name());
object.addProperty("iterations", action.getIterations());
if (action.getDelay() != null) {
object.add("delay", context.serialize(action.getDelay(), Delay.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import java.lang.reflect.Type;

public class DelayAdapter implements JsonSerializer<Delay>, JsonDeserializer<Delay> {
public class DelayAdapter implements JsonSerializer<Delay>, JsonDeserializer<Delay> {

@Override
public Delay deserialize(JsonElement element, Type type, JsonDeserializationContext context)
Expand Down
Empty file.

0 comments on commit b70fe0c

Please sign in to comment.