From b7919a847d0d9c616b3c2d3cc4ed6950f40c2b3a Mon Sep 17 00:00:00 2001 From: Slqmy <90862990+Slqmy@users.noreply.github.com> Date: Tue, 13 Aug 2024 00:39:57 +0100 Subject: [PATCH] Store custom multiblocks in a JSON file --- .../AbstractCustomMultiblock.java | 8 ++ .../CustomMultiblockManager.java | 96 +++++++++++++++++++ .../StoredCustomMultiblock.java | 28 ++++++ .../StoredCustomMultiblocks.java | 15 +++ src/main/resources/multiblocks.json | 1 + 5 files changed, 148 insertions(+) create mode 100644 src/main/java/net/slqmy/template_paper_plugin/custom_multiblock/StoredCustomMultiblock.java create mode 100644 src/main/java/net/slqmy/template_paper_plugin/custom_multiblock/StoredCustomMultiblocks.java create mode 100644 src/main/resources/multiblocks.json diff --git a/src/main/java/net/slqmy/template_paper_plugin/custom_multiblock/AbstractCustomMultiblock.java b/src/main/java/net/slqmy/template_paper_plugin/custom_multiblock/AbstractCustomMultiblock.java index 290fad69..664721d0 100644 --- a/src/main/java/net/slqmy/template_paper_plugin/custom_multiblock/AbstractCustomMultiblock.java +++ b/src/main/java/net/slqmy/template_paper_plugin/custom_multiblock/AbstractCustomMultiblock.java @@ -17,6 +17,14 @@ public abstract class AbstractCustomMultiblock implements Listener { private final List> multiblocks = new ArrayList<>(); + public List> getMultiblocks() { + return multiblocks; + } + + public void addMultiblocks(List> addedMultiblocks) { + multiblocks.addAll(addedMultiblocks); + } + public AbstractCustomMultiblock(TemplatePaperPlugin plugin, CustomMultiblockManager customMultiblockManager, CustomMultiblock multiblockId) { this.plugin = plugin; diff --git a/src/main/java/net/slqmy/template_paper_plugin/custom_multiblock/CustomMultiblockManager.java b/src/main/java/net/slqmy/template_paper_plugin/custom_multiblock/CustomMultiblockManager.java index 40430f99..3a31259f 100644 --- a/src/main/java/net/slqmy/template_paper_plugin/custom_multiblock/CustomMultiblockManager.java +++ b/src/main/java/net/slqmy/template_paper_plugin/custom_multiblock/CustomMultiblockManager.java @@ -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; @@ -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 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) { @@ -30,4 +51,79 @@ public AbstractCustomMultiblock getAbstractCustomMultiblock(CustomMultiblock mul public void placeCustomMultiblock(CustomMultiblock multiblockId, Location location) { customMultiblockMap.get(multiblockId).getCustomMultiblock(location); } + + public List getAllMultiblocks() { + List allMultiblocks = new ArrayList<>(); + + for (Entry 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 multiblockInfo : customMultiblockMap.entrySet()) { + output.addCustomMultiblock(new StoredCustomMultiblock(multiblockInfo)); + } + + return output; + } } diff --git a/src/main/java/net/slqmy/template_paper_plugin/custom_multiblock/StoredCustomMultiblock.java b/src/main/java/net/slqmy/template_paper_plugin/custom_multiblock/StoredCustomMultiblock.java new file mode 100644 index 00000000..1b489912 --- /dev/null +++ b/src/main/java/net/slqmy/template_paper_plugin/custom_multiblock/StoredCustomMultiblock.java @@ -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> blockLocations; + private final CustomMultiblock multiblockId; + + public List> getBlockLocations() { + return blockLocations; + } + + public CustomMultiblock getMultiblockId() { + return multiblockId; + } + + public StoredCustomMultiblock(List> blockLocations, CustomMultiblock multiblockId) { + this.blockLocations = blockLocations; + this.multiblockId = multiblockId; + } + + public StoredCustomMultiblock(Entry multiblockInfo) { + this(multiblockInfo.getValue().getMultiblocks(), multiblockInfo.getKey()); + } +} diff --git a/src/main/java/net/slqmy/template_paper_plugin/custom_multiblock/StoredCustomMultiblocks.java b/src/main/java/net/slqmy/template_paper_plugin/custom_multiblock/StoredCustomMultiblocks.java new file mode 100644 index 00000000..89495db3 --- /dev/null +++ b/src/main/java/net/slqmy/template_paper_plugin/custom_multiblock/StoredCustomMultiblocks.java @@ -0,0 +1,15 @@ +package net.slqmy.template_paper_plugin.custom_multiblock; + +import java.util.List; + +public class StoredCustomMultiblocks { + private List storedCustomMultiblocks; + + public List getStoredCustomMultiblocks() { + return storedCustomMultiblocks; + } + + public void addCustomMultiblock(StoredCustomMultiblock addedCustomMultiblocks) { + storedCustomMultiblocks.add(addedCustomMultiblocks); + } +} diff --git a/src/main/resources/multiblocks.json b/src/main/resources/multiblocks.json new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/src/main/resources/multiblocks.json @@ -0,0 +1 @@ +[]