2024-03-04 13:46:36 +01:00
|
|
|
package net.citizensnpcs.trait.shop;
|
2022-12-03 14:32:58 +01:00
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
|
|
|
|
import net.citizensnpcs.api.persistence.Persist;
|
|
|
|
import net.citizensnpcs.api.persistence.PersistenceLoader;
|
2023-02-14 22:36:51 +01:00
|
|
|
import net.citizensnpcs.api.util.Messaging;
|
2022-12-03 14:32:58 +01:00
|
|
|
import net.citizensnpcs.api.util.Storage;
|
|
|
|
import net.citizensnpcs.api.util.YamlStorage;
|
|
|
|
import net.citizensnpcs.trait.ShopTrait.NPCShop;
|
|
|
|
|
|
|
|
public class StoredShops {
|
|
|
|
@Persist(value = "global", reify = true)
|
|
|
|
public Map<String, NPCShop> globalShops = Maps.newHashMap();
|
|
|
|
@Persist(value = "npc", reify = true)
|
|
|
|
public Map<String, NPCShop> npcShops = Maps.newHashMap();
|
|
|
|
private final Storage storage;
|
|
|
|
|
|
|
|
public StoredShops(YamlStorage storage) {
|
|
|
|
this.storage = storage;
|
|
|
|
}
|
|
|
|
|
2022-12-29 16:26:02 +01:00
|
|
|
public void deleteShop(NPCShop shop) {
|
2023-02-14 22:36:51 +01:00
|
|
|
if (Messaging.isDebugging()) {
|
|
|
|
Messaging.debug("Deleting shop", shop.getName());
|
|
|
|
}
|
2024-03-16 16:10:12 +01:00
|
|
|
if (npcShops.containsKey(shop.getName())) {
|
|
|
|
npcShops.remove(shop.getName());
|
2022-12-18 18:44:16 +01:00
|
|
|
} else {
|
2024-03-16 16:10:12 +01:00
|
|
|
globalShops.remove(shop.getName());
|
2022-12-18 18:44:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public NPCShop getGlobalShop(String name) {
|
2023-02-09 14:09:53 +01:00
|
|
|
return globalShops.computeIfAbsent(name, NPCShop::new);
|
2022-12-18 18:44:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public NPCShop getShop(String name) {
|
2023-11-05 13:58:37 +01:00
|
|
|
if (npcShops.containsKey(name))
|
2022-12-18 18:44:16 +01:00
|
|
|
return npcShops.get(name);
|
|
|
|
return getGlobalShop(name);
|
|
|
|
}
|
|
|
|
|
2022-12-03 14:32:58 +01:00
|
|
|
public void load() {
|
|
|
|
PersistenceLoader.load(this, storage.getKey(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean loadFromDisk() {
|
|
|
|
return storage.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void saveToDisk() {
|
|
|
|
storage.save();
|
|
|
|
}
|
2023-02-14 22:36:51 +01:00
|
|
|
|
|
|
|
public void storeShops() {
|
|
|
|
PersistenceLoader.save(this, storage.getKey(""));
|
|
|
|
}
|
2022-12-03 14:32:58 +01:00
|
|
|
}
|