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

Commit

Permalink
save all signshop items to nbt
Browse files Browse the repository at this point in the history
  • Loading branch information
cyilin committed Nov 5, 2018
1 parent 805a25f commit 06cbfa5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/main/java/cat/nyaa/HamsterEcoHelper/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public class Configuration extends PluginConfigure {
public long search_ench_cooldown_tick = 200;
@Serializable
public int database_version = 0;
@Serializable
public boolean signshop_yaml_to_nbt = true;


public Map<String, Integer> signshop_sign_limit = new HashMap<>();
Expand Down
46 changes: 30 additions & 16 deletions src/main/java/cat/nyaa/HamsterEcoHelper/database/SignShop.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ public class SignShop {
@PrimaryKey
public UUID owner;
public String yaml = "";
private YamlConfiguration config = null;

@DataColumn("yaml")
public String getYaml() {
return Base64.getEncoder().encodeToString(this.yaml.getBytes());
if (config == null) {
return Base64.getEncoder().encodeToString(this.yaml.getBytes());
}
return Base64.getEncoder().encodeToString(this.config.saveToString().getBytes());
}

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

public List<ShopItem> loadItems(String path) {
YamlConfiguration configuration = new YamlConfiguration();
try {
configuration.loadFromString(this.yaml);
} catch (InvalidConfigurationException e) {
e.printStackTrace();
if (config == null) {
load();
}
ArrayList<ShopItem> list = new ArrayList<>();
if (configuration.isConfigurationSection(path)) {
ConfigurationSection section = configuration.getConfigurationSection(path);
if (config.isConfigurationSection(path)) {
ConfigurationSection section = config.getConfigurationSection(path);
for (String k : section.getKeys(false)) {
list.add(new ShopItem(section.getConfigurationSection(k)));
}
Expand All @@ -63,20 +64,33 @@ public List<ShopItem> loadItems(String path) {
}

public void saveItems(String path, List<ShopItem> list) {
YamlConfiguration configuration = new YamlConfiguration();
try {
configuration.loadFromString(this.yaml);
} catch (InvalidConfigurationException e) {
e.printStackTrace();
if (config == null) {
load();
}
configuration.set(path, null);
ConfigurationSection section = configuration.createSection(path);
config.set(path, null);
ConfigurationSection section = config.createSection(path);
for (int i = 0; i < list.size(); i++) {
ShopItem item = list.get(i);
if (item.amount > 0 && item.getItemStack(1).getType() != Material.AIR) {
list.get(i).save(section.createSection(String.valueOf(i)));
}
}
this.yaml = configuration.saveToString();
}

public void yamlToNBT() {
if (config == null) {
load();
}
setItems(getItems(ShopMode.BUY), ShopMode.BUY);
setItems(getItems(ShopMode.SELL), ShopMode.SELL);
}

private void load() {
config = new YamlConfiguration();
try {
config.loadFromString(this.yaml);
} catch (InvalidConfigurationException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public class SignShopManager {
public SignShopManager(HamsterEcoHelper pl) {
plugin = pl;
signLocations = plugin.database.getShopSigns();
if (plugin.config.signshop_yaml_to_nbt) {
List<SignShop> shops = plugin.database.getSignShops();
for (SignShop s : shops) {
plugin.logger.info("[signshop] UUID: " + s.owner.toString());
s.yamlToNBT();
plugin.database.setSignShop(s.owner, s);
}
plugin.config.signshop_yaml_to_nbt = false;
}
updateAttachedBlocks();
}

Expand Down

0 comments on commit 06cbfa5

Please sign in to comment.