Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.practice.nosql;
- import java.net.UnknownHostException;
- import net.practice.api.API;
- import net.practice.arenas.Arena;
- import org.bukkit.Location;
- import com.mongodb.BasicDBObject;
- import com.mongodb.DB;
- import com.mongodb.DBCollection;
- import com.mongodb.DBCursor;
- import com.mongodb.DBObject;
- import com.mongodb.MongoClient;
- import com.mongodb.MongoClientURI;
- import net.practice.Main;
- public class NoSQL {
- private DB database;
- private DBCollection collection;
- private String collectionName = "Arenas";
- @SuppressWarnings({ "resource", "deprecation" })
- public void setUp() throws UnknownHostException {
- MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
- database = mongoClient.getDB("FieldPractice");
- collection = database.getCollection(collectionName);
- }
- public void saveArena(Arena arena) {
- DBObject obj = new BasicDBObject("_id", arena.getId()).append("loc1", arena.getLocation1()).append("loc2",
- arena.getLocation2());
- collection.insert(obj);
- database.getMongo().close();
- }
- public void loadArena() {
- DBObject query = new BasicDBObject();
- DBCursor cursor = collection.find(query);
- int number = cursor.length();
- for (int i = 1; i <= number; i++) {
- DBCollection coll = database.getCollection(collectionName);
- DBObject searchById = new BasicDBObject("_id", i);
- DBObject found = coll.findOne(searchById);
- Arena arena;
- if (found != null) {
- arena = new Arena(i, new API().stringToLocation((String) found.get("loc1")), new API().stringToLocation((String) found.get("loc2")), true);
- Main.arena.add(arena);
- }
- }
- database.getMongo().close();
- }
- private void loadArena(Arena arena) {
- DBCollection coll = database.getCollection(collectionName);
- DBObject searchById = new BasicDBObject("_id", arena.getId());
- DBObject found = coll.findOne(searchById);
- if (found != null) {
- arena = new Arena(arena.getId(), new API().stringToLocation((String) found.get("loc1")), new API().stringToLocation((String) found.get("loc2")), true);
- Main.arena.add(arena);
- }
- }
- public void setLocation(Arena arena, Location loc, String nameOfLocation) {
- collection.update(new BasicDBObject("_id", arena.getId()),
- new BasicDBObject("$set", new BasicDBObject(nameOfLocation, new API().locationToString(loc))));
- loadArena(arena);
- database.getMongo().close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement