Skip to content

Instantly share code, notes, and snippets.

@test414EFF
Last active April 12, 2020 15:22
Show Gist options
  • Save test414EFF/6656433ce12c8f6e9912c4060393f52a to your computer and use it in GitHub Desktop.
Save test414EFF/6656433ce12c8f6e9912c4060393f52a to your computer and use it in GitHub Desktop.
package cc.modernity.moderneconomy.api;
import de.leonhard.storage.Json;
import de.leonhard.storage.internal.FlatFile;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class ShopStorage {
private final FlatFile shops = new Json("shops", "plugins/ModernEconomy/");
private final ItemStackSerializer serializer = new ItemStackSerializer();
private final Util utility = new Util();
public String getShopOwner(String name) {
return Bukkit.getOfflinePlayer(UUID.fromString(shops.getString(name + ".info.owner"))).getName();
}
public Map getShopItems(String name) {
return shops.getMap(name + ".items");
}
public String getShopName(Block block) {
return block.getLocation().getBlockX() + "," + block.getLocation().getBlockY() + "," + block.getLocation().getBlockZ();
}
public Location getShopLoc(Object name, World world) {
String[] shopLocationList = name.toString().split(",");
return new Location(world, Double.parseDouble(shopLocationList[0]), Double.parseDouble(shopLocationList[1]), Double.parseDouble(shopLocationList[2]));
}
public boolean shopExists(Block block) {
if(shops.contains(getShopName(block))) {
return true;
} else {
return false;
}
}
public void addItem(Player p, Object name, ItemStack item, BigDecimal price) {
if(!shops.contains(name.toString())) {
HashMap<String, HashMap> shop = new HashMap<>();
HashMap<String, String> items = new HashMap<>();
HashMap<String, Object> info = new HashMap<>();
items.put(serializer.itemToString(item), price.toString());
info.put("owner", utility.getUUID(p));
shop.put("items", items);
shop.put("info", info);
shops.set(name.toString(), shop);
utility.sendFormattedMessage("Economy", "info", "Please set your shop's bank account! This will be where all money goes when transactions are made.", p);
} else {
HashMap<String, HashMap> shop = new HashMap(shops.getMap(name.toString()));
HashMap<String, String> items = new HashMap<>(shop.get("items"));
items.put(serializer.itemToString(item), price.toString());
shop.put("items", items);
shops.set(name.toString(), shop);
utility.sendFormattedMessage("Economy", "success", "Successfully set your shop's bank account.", p);
}
}
public void setShopAccount(Player p, Object name, String accUUID) {
HashMap<String, HashMap> shop = new HashMap(shops.getMap(name.toString()));
HashMap<String, String> info = new HashMap<>(shop.get("info"));
info.put("shopAccount", accUUID);
shop.put("info", info);
shops.set(name.toString(), shop);
}
public String getShopAccount(Object name) {
HashMap<String, HashMap> shop = new HashMap(shops.getMap(name.toString()));
HashMap<String, String> info = new HashMap<>(shop.get("info"));
if(info.get("shopAccount") != null) {
return info.get("shopAccount");
} else {
return null;
}
}
public void deleteShop(String name) {
System.out.println("Deleting shop " + name);
if(shops.contains(name)) {
System.out.println("Shop exists!");
}
shops.remove(name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment