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

Commit 06cbfa5

Browse files
committed
save all signshop items to nbt
1 parent 805a25f commit 06cbfa5

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,10 +23,14 @@ public class SignShop {
2323
@PrimaryKey
2424
public UUID owner;
2525
public String yaml = "";
26+
private YamlConfiguration config = null;
2627

2728
@DataColumn("yaml")
2829
public String getYaml() {
29-
return Base64.getEncoder().encodeToString(this.yaml.getBytes());
30+
if (config == null) {
31+
return Base64.getEncoder().encodeToString(this.yaml.getBytes());
32+
}
33+
return Base64.getEncoder().encodeToString(this.config.saveToString().getBytes());
3034
}
3135

3236
public void setYaml(String yaml) {
@@ -46,15 +50,12 @@ public List<ShopItem> getItems(ShopMode mode) {
4650
}
4751

4852
public List<ShopItem> loadItems(String path) {
49-
YamlConfiguration configuration = new YamlConfiguration();
50-
try {
51-
configuration.loadFromString(this.yaml);
52-
} catch (InvalidConfigurationException e) {
53-
e.printStackTrace();
53+
if (config == null) {
54+
load();
5455
}
5556
ArrayList<ShopItem> list = new ArrayList<>();
56-
if (configuration.isConfigurationSection(path)) {
57-
ConfigurationSection section = configuration.getConfigurationSection(path);
57+
if (config.isConfigurationSection(path)) {
58+
ConfigurationSection section = config.getConfigurationSection(path);
5859
for (String k : section.getKeys(false)) {
5960
list.add(new ShopItem(section.getConfigurationSection(k)));
6061
}
@@ -63,20 +64,33 @@ public List<ShopItem> loadItems(String path) {
6364
}
6465

6566
public void saveItems(String path, List<ShopItem> list) {
66-
YamlConfiguration configuration = new YamlConfiguration();
67-
try {
68-
configuration.loadFromString(this.yaml);
69-
} catch (InvalidConfigurationException e) {
70-
e.printStackTrace();
67+
if (config == null) {
68+
load();
7169
}
72-
configuration.set(path, null);
73-
ConfigurationSection section = configuration.createSection(path);
70+
config.set(path, null);
71+
ConfigurationSection section = config.createSection(path);
7472
for (int i = 0; i < list.size(); i++) {
7573
ShopItem item = list.get(i);
7674
if (item.amount > 0 && item.getItemStack(1).getType() != Material.AIR) {
7775
list.get(i).save(section.createSection(String.valueOf(i)));
7876
}
7977
}
80-
this.yaml = configuration.saveToString();
78+
}
79+
80+
public void yamlToNBT() {
81+
if (config == null) {
82+
load();
83+
}
84+
setItems(getItems(ShopMode.BUY), ShopMode.BUY);
85+
setItems(getItems(ShopMode.SELL), ShopMode.SELL);
86+
}
87+
88+
private void load() {
89+
config = new YamlConfiguration();
90+
try {
91+
config.loadFromString(this.yaml);
92+
} catch (InvalidConfigurationException e) {
93+
e.printStackTrace();
94+
}
8195
}
8296
}

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)