@@ -23,6 +23,7 @@ public class SignShop {
23
23
public UUID owner ;
24
24
25
25
public String yaml = "" ;
26
+ private YamlConfiguration config = null ;
26
27
27
28
@ Column (name = "id" )
28
29
@ Id
@@ -36,7 +37,10 @@ public void setOwner(String owner) {
36
37
37
38
@ Column (name = "yaml" , columnDefinition = "MEDIUMTEXT" )
38
39
public String getYaml () {
39
- return Base64 .getEncoder ().encodeToString (this .yaml .getBytes ());
40
+ if (config == null ) {
41
+ return Base64 .getEncoder ().encodeToString (this .yaml .getBytes ());
42
+ }
43
+ return Base64 .getEncoder ().encodeToString (this .config .saveToString ().getBytes ());
40
44
}
41
45
42
46
public void setYaml (String yaml ) {
@@ -58,15 +62,12 @@ public List<ShopItem> getItems(ShopMode mode) {
58
62
}
59
63
60
64
public List <ShopItem > loadItems (String path ) {
61
- YamlConfiguration configuration = new YamlConfiguration ();
62
- try {
63
- configuration .loadFromString (this .yaml );
64
- } catch (InvalidConfigurationException e ) {
65
- e .printStackTrace ();
65
+ if (config == null ) {
66
+ load ();
66
67
}
67
68
ArrayList <ShopItem > list = new ArrayList <>();
68
- if (configuration .isConfigurationSection (path )) {
69
- ConfigurationSection section = configuration .getConfigurationSection (path );
69
+ if (config .isConfigurationSection (path )) {
70
+ ConfigurationSection section = config .getConfigurationSection (path );
70
71
for (String k : section .getKeys (false )) {
71
72
list .add (new ShopItem (section .getConfigurationSection (k )));
72
73
}
@@ -75,20 +76,33 @@ public List<ShopItem> loadItems(String path) {
75
76
}
76
77
77
78
public void saveItems (String path , List <ShopItem > list ) {
78
- YamlConfiguration configuration = new YamlConfiguration ();
79
- try {
80
- configuration .loadFromString (this .yaml );
81
- } catch (InvalidConfigurationException e ) {
82
- e .printStackTrace ();
79
+ if (config == null ) {
80
+ load ();
83
81
}
84
- configuration .set (path , null );
85
- ConfigurationSection section = configuration .createSection (path );
82
+ config .set (path , null );
83
+ ConfigurationSection section = config .createSection (path );
86
84
for (int i = 0 ; i < list .size (); i ++) {
87
85
ShopItem item = list .get (i );
88
86
if (item .amount > 0 && item .getItemStack (1 ).getType () != Material .AIR ) {
89
87
list .get (i ).save (section .createSection (String .valueOf (i )));
90
88
}
91
89
}
92
- this .yaml = configuration .saveToString ();
90
+ }
91
+
92
+ public void yamlToNBT () {
93
+ if (config == null ) {
94
+ load ();
95
+ }
96
+ setItems (getItems (ShopMode .BUY ), ShopMode .BUY );
97
+ setItems (getItems (ShopMode .SELL ), ShopMode .SELL );
98
+ }
99
+
100
+ private void load () {
101
+ config = new YamlConfiguration ();
102
+ try {
103
+ config .loadFromString (this .yaml );
104
+ } catch (InvalidConfigurationException e ) {
105
+ e .printStackTrace ();
106
+ }
93
107
}
94
108
}
0 commit comments