This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Persistence + No More Singleton #44
Draft
dmccoystephenson
wants to merge
5
commits into
main
Choose a base branch
from
persistence
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c8ce892
Created FactionHandler and PowerRecordHandler classes.
dmccoystephenson 5b26763
Merge pull request #64 from Dans-Plugins/main
dmccoystephenson 8abdb59
Data handler classes now implement data interfaces.
dmccoystephenson 041c664
The FactionHandler class is now being used to access faction data ins…
dmccoystephenson aa08114
Started the transition to a singleton-less project.
dmccoystephenson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/main/java/dansplugins/minifactions/api/data/handlers/FactionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>(); | ||
} | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
src/main/java/dansplugins/minifactions/api/data/handlers/PowerRecordHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably implement this There was a problem hiding this comment. Choose a reason for hiding this commentThe 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<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Answered above |
||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.