Skip to content
This repository was archived by the owner on Mar 8, 2022. It is now read-only.

Commit 57e1921

Browse files
committed
save all signshop items to nbt
1 parent 4676399 commit 57e1921

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

src/main/java/cat/nyaa/HamsterEcoHelper/Configuration.java

+2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ public class Configuration extends PluginConfigure {
101101
public long search_ench_cooldown_tick = 200;
102102
@Serializable
103103
public int database_version = 0;
104+
@Serializable
105+
public boolean signshop_yaml_to_nbt = true;
104106

105107

106108
public Map<String, Integer> signshop_sign_limit = new HashMap<>();

src/main/java/cat/nyaa/HamsterEcoHelper/database/SignShop.java

+30-16
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class SignShop {
2323
public UUID owner;
2424

2525
public String yaml = "";
26+
private YamlConfiguration config = null;
2627

2728
@Column(name = "id")
2829
@Id
@@ -36,7 +37,10 @@ public void setOwner(String owner) {
3637

3738
@Column(name = "yaml", columnDefinition = "MEDIUMTEXT")
3839
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());
4044
}
4145

4246
public void setYaml(String yaml) {
@@ -58,15 +62,12 @@ public List<ShopItem> getItems(ShopMode mode) {
5862
}
5963

6064
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();
6667
}
6768
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);
7071
for (String k : section.getKeys(false)) {
7172
list.add(new ShopItem(section.getConfigurationSection(k)));
7273
}
@@ -75,20 +76,33 @@ public List<ShopItem> loadItems(String path) {
7576
}
7677

7778
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();
8381
}
84-
configuration.set(path, null);
85-
ConfigurationSection section = configuration.createSection(path);
82+
config.set(path, null);
83+
ConfigurationSection section = config.createSection(path);
8684
for (int i = 0; i < list.size(); i++) {
8785
ShopItem item = list.get(i);
8886
if (item.amount > 0 && item.getItemStack(1).getType() != Material.AIR) {
8987
list.get(i).save(section.createSection(String.valueOf(i)));
9088
}
9189
}
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+
}
93107
}
94108
}

src/main/java/cat/nyaa/HamsterEcoHelper/signshop/SignShopManager.java

+9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ public class SignShopManager {
3131
public SignShopManager(HamsterEcoHelper pl) {
3232
plugin = pl;
3333
signLocations = plugin.database.getShopSigns();
34+
if (plugin.config.signshop_yaml_to_nbt) {
35+
List<SignShop> shops = plugin.database.getSignShops();
36+
for (SignShop s : shops) {
37+
plugin.logger.info("[signshop] UUID: " + s.owner.toString());
38+
s.yamlToNBT();
39+
plugin.database.setSignShop(s.owner, s);
40+
}
41+
plugin.config.signshop_yaml_to_nbt = false;
42+
}
3443
updateAttachedBlocks();
3544
}
3645

0 commit comments

Comments
 (0)