mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2025-04-14 16:15:58 +02:00
106 lines
3.6 KiB
Java
106 lines
3.6 KiB
Java
package net.citizensnpcs;
|
|
|
|
import java.io.File;
|
|
import java.sql.SQLException;
|
|
|
|
import net.citizensnpcs.Settings.Setting;
|
|
import net.citizensnpcs.api.npc.NPC;
|
|
import net.citizensnpcs.api.npc.NPCRegistry;
|
|
import net.citizensnpcs.api.util.DataKey;
|
|
import net.citizensnpcs.api.util.DatabaseStorage;
|
|
import net.citizensnpcs.api.util.NBTStorage;
|
|
import net.citizensnpcs.api.util.Storage;
|
|
import net.citizensnpcs.api.util.YamlStorage;
|
|
import net.citizensnpcs.npc.CitizensNPC;
|
|
import net.citizensnpcs.npc.CitizensNPCRegistry;
|
|
import net.citizensnpcs.util.Messages;
|
|
import net.citizensnpcs.util.Messaging;
|
|
|
|
import org.bukkit.entity.EntityType;
|
|
|
|
public class NPCDataStore {
|
|
private final Storage root;
|
|
|
|
private NPCDataStore(Storage saves) {
|
|
root = saves;
|
|
}
|
|
|
|
public void loadInto(CitizensNPCRegistry registry) {
|
|
int created = 0, spawned = 0;
|
|
for (DataKey key : root.getKey("npc").getIntegerSubKeys()) {
|
|
int id = Integer.parseInt(key.name());
|
|
if (!key.keyExists("name")) {
|
|
Messaging.logTr(Messages.LOAD_NAME_NOT_FOUND, id);
|
|
continue;
|
|
}
|
|
String unparsedEntityType = key.getString("traits.type", "PLAYER");
|
|
EntityType type = EntityType.fromName(unparsedEntityType);
|
|
if (type == null) {
|
|
try {
|
|
type = EntityType.valueOf(unparsedEntityType);
|
|
} catch (IllegalArgumentException ex) {
|
|
Messaging.logTr(Messages.LOAD_UNKNOWN_NPC_TYPE, unparsedEntityType);
|
|
continue;
|
|
}
|
|
}
|
|
NPC npc = registry.createNPC(type, id, key.getString("name"));
|
|
((CitizensNPC) npc).load(key);
|
|
|
|
created++;
|
|
if (npc.isSpawned())
|
|
spawned++;
|
|
}
|
|
Messaging.logTr(Messages.NUM_LOADED_NOTIFICATION, created, spawned);
|
|
}
|
|
|
|
public void remove(NPC npc) {
|
|
root.getKey("npc").removeKey(Integer.toString(npc.getId()));
|
|
}
|
|
|
|
public void saveToDisk() {
|
|
new Thread() {
|
|
@Override
|
|
public void run() {
|
|
root.save();
|
|
}
|
|
}.start();
|
|
}
|
|
|
|
public void saveToDiskImmediate() {
|
|
root.save();
|
|
}
|
|
|
|
public void store(NPC npc) {
|
|
((CitizensNPC) npc).save(root.getKey("npc." + npc.getId()));
|
|
}
|
|
|
|
public void storeAll(NPCRegistry registry) {
|
|
for (NPC npc : registry)
|
|
store(npc);
|
|
}
|
|
|
|
public static NPCDataStore create(File folder) {
|
|
Storage saves = null;
|
|
String type = Setting.STORAGE_TYPE.asString();
|
|
if (type.equalsIgnoreCase("db") || type.equalsIgnoreCase("database")) {
|
|
try {
|
|
saves = new DatabaseStorage(Setting.DATABASE_DRIVER.asString(),
|
|
Setting.DATABASE_URL.asString(), Setting.DATABASE_USERNAME.asString(),
|
|
Setting.DATABASE_PASSWORD.asString());
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
Messaging.logTr(Messages.DATABASE_CONNECTION_FAILED);
|
|
}
|
|
} else if (type.equalsIgnoreCase("nbt")) {
|
|
saves = new NBTStorage(folder + File.separator + Setting.STORAGE_FILE.asString(),
|
|
"Citizens NPC Storage");
|
|
}
|
|
if (saves == null)
|
|
saves = new YamlStorage(new File(folder, Setting.STORAGE_FILE.asString()), "Citizens NPC Storage");
|
|
if (!saves.load())
|
|
return null;
|
|
Messaging.logTr(Messages.SAVE_METHOD_SET_NOTIFICATION, saves.toString());
|
|
return new NPCDataStore(saves);
|
|
}
|
|
}
|