From 96b73f2d282538cddeb6891cb267c8f2dca5ffe8 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 24 Oct 2023 13:35:09 +0200 Subject: [PATCH] Persistency --- .../steamwar/inventory/SchematicSelector.java | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SchematicSelector.java b/SpigotCore_Main/src/de/steamwar/inventory/SchematicSelector.java index e5b54e0..b3e3e32 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SchematicSelector.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SchematicSelector.java @@ -19,6 +19,9 @@ package de.steamwar.inventory; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import de.steamwar.core.Core; import de.steamwar.sql.*; import lombok.*; @@ -374,6 +377,16 @@ public class SchematicSelector { if (!filterCache.containsKey(player)) { filterCache.put(player, new SelectorFilter[9]); + + String cfg = UserConfig.getConfig(user.getId(), "selector:filters"); + + if (cfg != null) { + JsonArray array = JsonParser.parseString(cfg).getAsJsonArray(); + for (int i = 0; i < array.size(); i++) { + JsonObject object = array.get(i).getAsJsonObject(); + filterCache.get(player)[i] = new SelectorFilter(object); + } + } } SelectorFilter[] filters = filterCache.get(player); @@ -381,7 +394,7 @@ public class SchematicSelector { for (int i = 0; i < filters.length; i++) { SelectorFilter filterCached = filters[i]; if (filterCached == null) { - inv.setItem(i + 9, new SWItem(Material.BARRIER, "SCHEM_SELECTOR_FILTER_EMPTY", click -> {})); + inv.setItem(i + 9, new SWItem(Material.BARRIER, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_EMPTY", player), click -> {})); } else { SWItem item = filterCached.toItemStack(player); item.setEnchanted(filterCached.equals(filter)); @@ -413,6 +426,14 @@ public class SchematicSelector { filters[0] = filter; filterCache.put(player, filters); + JsonArray array = new JsonArray(); + for (SelectorFilter f : filters) { + if (f != null) { + array.add(f.toJson()); + } + } + + UserConfig.updatePlayerConfig(user.getId(), "selector:filters", array.toString()); } injectable.onFilterApply(this); @@ -526,6 +547,13 @@ public class SchematicSelector { private final SchematicType type; private final Material item; + public SelectorFilter(JsonObject object) { + this.name = object.get("name").isJsonNull() ? null : object.get("name").getAsString(); + this.owner = object.get("owner").isJsonNull() ? null : object.get("owner").getAsInt(); + this.type = object.get("type").isJsonNull() ? null : SchematicType.fromDB(object.get("type").getAsString()); + this.item = object.get("item").isJsonNull() ? null : Material.valueOf(object.get("item").getAsString()); + } + public boolean matches(SchematicNode node) { boolean matches = name == null || node.getName().contains(name); @@ -594,6 +622,16 @@ public class SchematicSelector { return new SWItem(Material.BARRIER, Core.MESSAGE.parse("SCHEM_SELECTOR_FILTER_TITLE_EMPTY", player), Collections.emptyList(), false, click -> {}); } } + + public JsonObject toJson() { + JsonObject object = new JsonObject(); + object.addProperty("name", name); + object.addProperty("owner", owner); + object.addProperty("type", type == null?null:type.toDB()); + object.addProperty("item", item == null?null:item.name()); + + return object; + } } public enum PublicMode {