Skip to content

Commit

Permalink
Store custom multiblocks in a JSON file
Browse files Browse the repository at this point in the history
  • Loading branch information
esotericenderman committed Aug 12, 2024
1 parent 865de2e commit b7919a8
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ public abstract class AbstractCustomMultiblock implements Listener {

private final List<List<BlockLocation>> multiblocks = new ArrayList<>();

public List<List<BlockLocation>> getMultiblocks() {
return multiblocks;
}

public void addMultiblocks(List<List<BlockLocation>> addedMultiblocks) {
multiblocks.addAll(addedMultiblocks);
}

public AbstractCustomMultiblock(TemplatePaperPlugin plugin, CustomMultiblockManager customMultiblockManager, CustomMultiblock multiblockId) {
this.plugin = plugin;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package net.slqmy.template_paper_plugin.custom_multiblock;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.bukkit.Location;

import com.google.gson.Gson;

import net.slqmy.template_paper_plugin.custom_multiblock.AbstractCustomMultiblock;
import net.slqmy.template_paper_plugin.custom_multiblock.CustomMultiblock;
import net.slqmy.template_paper_plugin.TemplatePaperPlugin;
Expand All @@ -13,10 +24,20 @@ public class CustomMultiblockManager {

private final TemplatePaperPlugin plugin;

private final String multiblocksFileExtension = ".json";
private final String multiblocksResourceFilePath = "multiblocks" + multiblocksFileExtension;
private final String multiblocksFilePath;
private final File multiblocksFile;

private final Map<CustomMultiblock, AbstractCustomMultiblock> customMultiblockMap = new HashMap<>();

public CustomMultiblockManager(TemplatePaperPlugin plugin) {
this.plugin = plugin;

multiblocksFilePath = plugin.getDataFolder() + File.separator + multiblocksResourceFilePath;
multiblocksFile = new File(multiblocksFilePath);

load();
}

public void addCustomMultiblock(CustomMultiblock multiblockId, AbstractCustomMultiblock abstractCustomMultiblock) {
Expand All @@ -30,4 +51,79 @@ public AbstractCustomMultiblock getAbstractCustomMultiblock(CustomMultiblock mul
public void placeCustomMultiblock(CustomMultiblock multiblockId, Location location) {
customMultiblockMap.get(multiblockId).getCustomMultiblock(location);
}

public List<StoredCustomMultiblock> getAllMultiblocks() {
List<StoredCustomMultiblock> allMultiblocks = new ArrayList<>();

for (Entry<CustomMultiblock, AbstractCustomMultiblock> multiblockInfo : customMultiblockMap.entrySet()) {
allMultiblocks.add(new StoredCustomMultiblock(multiblockInfo));
}

return allMultiblocks;
}

private void load() {
if (!multiblocksFile.exists()) {
return;
}

StoredCustomMultiblocks multiblocks;

Gson gson = new Gson();
try {
Reader reader = new FileReader(multiblocksFile);

multiblocks = gson.fromJson(reader, StoredCustomMultiblocks.class);

reader.close();
} catch (IOException exception) {
exception.printStackTrace();
return;
}

loadStoredCustomMultiblocks(multiblocks);
}

public void save() {
StoredCustomMultiblocks dataToSave = getStoredCustomMultiblocks();

if (dataToSave.getStoredCustomMultiblocks().size() == 0) {
return;
}

if (multiblocksFile.exists()) {
plugin.saveResource(multiblocksResourceFilePath, false);
}

Gson gson = new Gson();

try {
Writer writer = new FileWriter(multiblocksFile);

String json = gson.toJson(dataToSave);

writer.write(json);

writer.flush();
writer.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}

public void loadStoredCustomMultiblocks(StoredCustomMultiblocks multiblocks) {
for (StoredCustomMultiblock multiblock : multiblocks.getStoredCustomMultiblocks()) {
customMultiblockMap.get(multiblock.getMultiblockId()).addMultiblocks(multiblock.getBlockLocations());
}
}

public StoredCustomMultiblocks getStoredCustomMultiblocks() {
StoredCustomMultiblocks output = new StoredCustomMultiblocks();

for (Entry<CustomMultiblock, AbstractCustomMultiblock> multiblockInfo : customMultiblockMap.entrySet()) {
output.addCustomMultiblock(new StoredCustomMultiblock(multiblockInfo));
}

return output;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.slqmy.template_paper_plugin.custom_multiblock;

import java.util.List;
import java.util.Map.Entry;

import net.slqmy.template_paper_plugin.util.types.BlockLocation;

public class StoredCustomMultiblock {
private final List<List<BlockLocation>> blockLocations;
private final CustomMultiblock multiblockId;

public List<List<BlockLocation>> getBlockLocations() {
return blockLocations;
}

public CustomMultiblock getMultiblockId() {
return multiblockId;
}

public StoredCustomMultiblock(List<List<BlockLocation>> blockLocations, CustomMultiblock multiblockId) {
this.blockLocations = blockLocations;
this.multiblockId = multiblockId;
}

public StoredCustomMultiblock(Entry<CustomMultiblock, AbstractCustomMultiblock> multiblockInfo) {
this(multiblockInfo.getValue().getMultiblocks(), multiblockInfo.getKey());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.slqmy.template_paper_plugin.custom_multiblock;

import java.util.List;

public class StoredCustomMultiblocks {
private List<StoredCustomMultiblock> storedCustomMultiblocks;

public List<StoredCustomMultiblock> getStoredCustomMultiblocks() {
return storedCustomMultiblocks;
}

public void addCustomMultiblock(StoredCustomMultiblock addedCustomMultiblocks) {
storedCustomMultiblocks.add(addedCustomMultiblocks);
}
}
1 change: 1 addition & 0 deletions src/main/resources/multiblocks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]

0 comments on commit b7919a8

Please sign in to comment.