Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Persistence + No More Singleton #44

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
28 changes: 27 additions & 1 deletion src/main/java/dansplugins/minifactions/MiniFactions.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dansplugins.minifactions;

import dansplugins.minifactions.api.MiniFactionsAPI;
import dansplugins.minifactions.api.data.handlers.FactionHandler;
import dansplugins.minifactions.api.data.handlers.PowerRecordHandler;
import dansplugins.minifactions.api.data.handlers.TerritoryHandler;
import dansplugins.minifactions.commands.DefaultCommand;
import dansplugins.minifactions.commands.HelpCommand;
Expand Down Expand Up @@ -44,6 +46,8 @@ public class MiniFactions extends PonderBukkitPlugin {
private final String pluginVersion = "v" + getDescription().getVersion();
private final CommandService commandService = new CommandService(getPonder());
private MiniFactionsAPI api;
private FactionHandler factionHandler;
private PowerRecordHandler powerRecordHandler;
private TerritoryHandler territoryHandler;

/**
Expand All @@ -64,6 +68,8 @@ public void onEnable() {
registerEventHandlers();
initializeCommandService();
api = new MiniFactionsAPI();
factionHandler = new FactionHandler();
powerRecordHandler = new PowerRecordHandler();
territoryHandler = new TerritoryHandler();
}

Expand All @@ -72,7 +78,9 @@ public void onEnable() {
*/
@Override
public void onDisable() {

factionHandler.save();
powerRecordHandler.save();
territoryHandler.save();
}

/**
Expand Down Expand Up @@ -131,6 +139,24 @@ public MiniFactionsAPI getMiniFactionsAPI() {
return api;
}

/**
* Method to obtain the FactionHandler.
*
* @return {@link #factionHandler}.
*/
public FactionHandler getFactionHandler() {
return factionHandler;
}

/**
* Method to obtain the PowerRecordHandler.
*
* @return {@link #powerRecordHandler}.
*/
public PowerRecordHandler getPowerRecordHandler() {
return powerRecordHandler;
}

/**
* Method to obtain the TerritoryHandler.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package dansplugins.minifactions.api.data.handlers;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;
import dansplugins.minifactions.MiniFactions;
import dansplugins.minifactions.api.data.JsonFaction;
import dansplugins.minifactions.api.definitions.core.Faction;
import dansplugins.minifactions.api.exceptions.FactionFileException;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
* @author Daniel McCoy Stephenson
* @since April 16th, 2022
*/
public class FactionHandler {

/**
* File linked to the faction data.
*/
private final File file;

/**
* Data from the file.
*/
private HashMap<?, ?> data = null;

/**
* Constructor to initialise {@link #file} and then populate {@link #data}.
*
* @see #load()
*/
public FactionHandler() {
file = new File(MiniFactions.getInstance().getDataFolder(), "factions.json");
try {
if (!file.exists()) {
if (!file.createNewFile()) {
System.out.println("Failed to load '" + file.getName() + "'!");
throw new FactionFileException("Unable to create new file.");
}
}
} catch (IOException exception) {
throw new FactionFileException("IOException experienced:\t" + exception.getMessage());
}
load();
}

/**
* Method to obtain a faction from its Id.
* <p>
* Due to the nature of Json and GSON specifically, unchecked is dampened
* we hard-cast the data to a 'String:String' data-set.
* </p>
*
* @param factionId to get a faction for.
* @return {@link Faction} if found, or {@code null} if it isn't.
*/
@SuppressWarnings("unchecked")
@Nullable
public Faction getFaction(@NotNull UUID factionId) {
if (!data.containsKey(factionId) && !data.containsKey(factionId.toString())) {
return null;
}

Object object = data.getOrDefault(factionId, null);
if (object == null) {
data.getOrDefault(factionId.toString(), null);
}
if (object == null) {
return null;
}

final Faction faction = new JsonFaction();
faction.fromJSON((Map<String, String>) object);

return faction;
}

/**
*
*/
public void save() {
// TODO: implement
}

/**
* Method to load the data for faction handling.
*/
private void load() {
try {
final FileInputStream fileInputStream = new FileInputStream(file);
final InputStreamReader inputStream = new InputStreamReader(fileInputStream);
final JsonReader reader = new JsonReader(inputStream);
final Gson gson = new GsonBuilder().setPrettyPrinting().create();
final Object jsonData = gson.fromJson(reader, HashMap.class);

if (!(jsonData instanceof HashMap)) {
return;
}

this.data = ((HashMap<?, ?>) jsonData);

// Close the Readers ♥
reader.close();
inputStream.close();
fileInputStream.close();
} catch (Exception ex) {
ex.printStackTrace();
this.data = new HashMap<>();
Copy link

Choose a reason for hiding this comment

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

Are you sure you don't want to just exit out if it fails to load? Or are you ok with what I am guessing is a brand new vanilla factions implementation, the issue being what happens after? does it just save over the old bits?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm it looks like as of right now if it fails, the data is essentially cleared? Yeah we might want to exit here.

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package dansplugins.minifactions.api.data.handlers;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;
import dansplugins.minifactions.MiniFactions;
import dansplugins.minifactions.api.data.JsonPowerRecord;
import dansplugins.minifactions.api.definitions.PowerRecord;
import dansplugins.minifactions.api.exceptions.PowerRecordFileException;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
* @author Daniel McCoy Stephenson
* @since April 16th, 2022
*/
public class PowerRecordHandler {

/**
* File linked to the power record data.
*/
private final File file;

/**
* Data from the file.
*/
private HashMap<?, ?> data = null;

/**
* Constructor to initialise {@link #file} and then populate {@link #data}.
*
* @see #load()
*/
public PowerRecordHandler() {
file = new File(MiniFactions.getInstance().getDataFolder(), "powerRecords.json");
try {
if (!file.exists()) {
if (!file.createNewFile()) {
System.out.println("Failed to load '" + file.getName() + "'!");
throw new PowerRecordFileException("Unable to create new file.");
}
}
} catch (IOException exception) {
throw new PowerRecordFileException("IOException experienced:\t" + exception.getMessage());
}
load();
}

/**
* Method to obtain a powerrecord from its Id.
* <p>
* Due to the nature of Json and GSON specifically, unchecked is dampened
* we hard-cast the data to a 'String:String' data-set.
* </p>
*
* @param powerRecordId to get a powerrecord for.
* @return {@link PowerRecord} if found, or {@code null} if it isn't.
*/
@SuppressWarnings("unchecked")
@Nullable
public PowerRecord getPowerRecord(@NotNull UUID powerRecordId) {
if (!data.containsKey(powerRecordId) && !data.containsKey(powerRecordId.toString())) {
return null;
}

Object object = data.getOrDefault(powerRecordId, null);
if (object == null) {
data.getOrDefault(powerRecordId.toString(), null);
}
if (object == null) {
return null;
}

final PowerRecord powerrecord = new JsonPowerRecord(powerRecordId);
powerrecord.fromJSON((Map<String, String>) object);

return powerrecord;
}

/**
*
*/
public void save() {
// TODO: implement
Copy link

Choose a reason for hiding this comment

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

Should probably implement this

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm probably over-complicating it, but I have had trouble wrapping my head around how to implement this method with the new system.

}

/**
* Method to load the data for powerrecord handling.
*/
private void load() {
try {
final FileInputStream fileInputStream = new FileInputStream(file);
final InputStreamReader inputStream = new InputStreamReader(fileInputStream);
final JsonReader reader = new JsonReader(inputStream);
final Gson gson = new GsonBuilder().setPrettyPrinting().create();
final Object jsonData = gson.fromJson(reader, HashMap.class);

if (!(jsonData instanceof HashMap)) {
return;
}

this.data = ((HashMap<?, ?>) jsonData);

// Close the Readers ♥
reader.close();
inputStream.close();
fileInputStream.close();
} catch (Exception ex) {
ex.printStackTrace();
this.data = new HashMap<>();
Copy link

Choose a reason for hiding this comment

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

Same as above

Copy link
Member Author

Choose a reason for hiding this comment

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

Answered above

}
}
}
Loading