Raxen/src/main/java/me/unurled/raxen/config/TutorialConfig.java

99 lines
4.1 KiB
Java

package me.unurled.raxen.config;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import java.util.Map.Entry;
import java.util.UUID;
import me.unurled.raxen.Raxen;
import me.unurled.raxen.components.tutorial.Tutorial;
import me.unurled.raxen.manager.server.TutorialManager;
import me.unurled.raxen.utils.libs.MongoDB;
import org.bson.Document;
import org.bukkit.Location;
public class TutorialConfig {
private static Raxen main;
private MongoDB mongoDB;
private MongoCollection<Document> mongoCollection;
public TutorialConfig(Raxen main) {
this.main = main;
this.mongoDB = main.getManager().getLibsManager().getMongoDB();
this.mongoCollection = mongoDB.getMongoConfigs();
}
public void saveUsingMongoDB() {
TutorialManager manager = main.getManager().getTutorialManager();
Document doc = new Document();
Document loc = new Document();
for (Entry<UUID, Location> entry : manager.getLocations().entrySet()) {
Document location = new Document();
location.append("x", entry.getValue().getX());
location.append("y", entry.getValue().getY());
location.append("z", entry.getValue().getZ());
location.append("world", entry.getValue().getWorld().getName());
location.append("uuid", entry.getKey().toString());
loc.append("location", location);
}
doc.append("locations", loc);
Document tutorials = new Document();
for (Entry<UUID, Tutorial> entry : manager.getTutorials().entrySet()) {
Document tutorial = new Document();
tutorial.append("uuid", entry.getKey().toString());
tutorial.append("tutorial", entry.getValue().getStep());
tutorial.append("x", entry.getValue().getX());
tutorial.append("y", entry.getValue().getY());
tutorial.append("z", entry.getValue().getZ());
tutorial.append("world", entry.getValue().getWorld().getName());
tutorials.append(entry.getKey().toString(), tutorial);
}
doc.append("tutorials", tutorials);
Document find = mongoCollection.find(Filters.eq("tutorial", "tutorial")).first();
if (find == null) {
mongoCollection.insertOne(doc);
} else {
mongoCollection.replaceOne(find, doc);
}
}
public void loadUsingMongoDB() {
Document doc = mongoCollection.find(Filters.eq("tutorial", "tutorial")).first();
if (doc != null) {
TutorialManager manager = main.getManager().getTutorialManager();
Document loc = (Document) doc.get("locations");
for (Entry<String, Object> entry : loc.entrySet()) {
Document location = (Document) entry.getValue();
double x = location.getDouble("x");
double y = location.getDouble("y");
double z = location.getDouble("z");
String world = location.getString("world");
UUID uuid = UUID.fromString(location.getString("uuid"));
manager.getLocations()
.put(uuid, new Location(main.getServer().getWorld(world), x, y, z));
}
Document tutorials = (Document) doc.get("tutorials");
for (Entry<String, Object> entry : tutorials.entrySet()) {
Document tutorial = (Document) entry.getValue();
UUID uuid1 = UUID.fromString(tutorial.getString("uuid"));
double x = tutorial.getDouble("x");
double y = tutorial.getDouble("y");
double z = tutorial.getDouble("z");
String world = tutorial.getString("world");
int step = tutorial.getInteger("tutorial");
manager.getTutorials()
.put(uuid1, new Tutorial(step, x, y, z, main.getServer().getWorld(world)));
}
}
}
public void saveTutorialConfig() {
saveUsingMongoDB();
}
public void loadTutorialConfig() {
loadUsingMongoDB();
}
}