Java tutorial
/* * Themedbuidls, a custom mongo backed lot system for Minecraft * Copyright (C) meggawatts <http://mcme.co> * * This file is part of ThemedBuilds. * * ThemedBuilds is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * ThemedBuilds is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with ThemedBuilds. If not, see <http://www.gnu.org/licenses/>. */ package co.mcme.themedbuilds.database; import co.mcme.themedbuilds.ThemedBuildPlugin; import co.mcme.themedbuilds.database.Corner; import co.mcme.themedbuilds.database.Lot; import co.mcme.themedbuilds.database.Theme; import co.mcme.themedbuilds.database.statistics.PlayerDocument; import co.mcme.themedbuilds.database.statistics.PlayerReport; import co.mcme.themedbuilds.utilities.ThemedLogger; import co.mcme.util.jackson.serialization.OfflinePlayerGetter; import com.mongodb.BasicDBObject; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.QueryBuilder; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.UUID; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.logging.Level; import lombok.Getter; import org.bson.types.ObjectId; import org.bukkit.Location; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; public class DatabaseUtil { @Getter private static final HashMap<Corner, Lot> lotCache = new HashMap(); @Getter private static final HashMap<UUID, OfflinePlayer> playerCache = new HashMap(); private static final ExecutorService pool = Executors.newFixedThreadPool(3); public static OfflinePlayer getOfflinePlayer(UUID uuid) throws Exception { if (playerCache.containsKey(uuid)) { return playerCache.get(uuid); } Callable<OfflinePlayer> callable = new OfflinePlayerGetter(uuid); Future<OfflinePlayer> future = pool.submit(callable); try { OfflinePlayer res = future.get(); playerCache.put(uuid, res); return res; } catch (ExecutionException | InterruptedException ex) { ThemedLogger.getLog().log(Level.SEVERE, null, ex); return null; } } public static OfflinePlayer getOfflinePlayer(String name) throws Exception { Callable<OfflinePlayer> callable = new OfflinePlayerGetter(name); Future<OfflinePlayer> future = pool.submit(callable); try { OfflinePlayer res = future.get(); playerCache.put(res.getUniqueId(), res); return res; } catch (ExecutionException | InterruptedException ex) { ThemedLogger.getLog().log(Level.SEVERE, null, ex); return null; } } public static Theme getThemeByExactZCoordinate(int zcoord) { DBObject query = QueryBuilder.start("minZ").lessThanEquals(zcoord).and("maxZ").greaterThanEquals(zcoord) .get(); try { Theme theme = ThemedBuildPlugin.getJsonMapper().readValue( ThemedBuildPlugin.getMongoUtil().getThemeCollection().findOne(query).toString(), Theme.class); theme.fetchLots(); return theme; } catch (IOException ex) { ThemedLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex); } return null; } public static Theme getThemeByLocation(Location loc) { DBObject query = QueryBuilder.start("minZ").lessThanEquals(loc.getBlockZ()).and("maxZ") .greaterThanEquals(loc.getBlockZ()).get(); try { DBObject themeo = ThemedBuildPlugin.getMongoUtil().getThemeCollection().findOne(query); if (themeo != null) { Theme theme = ThemedBuildPlugin.getJsonMapper().readValue(themeo.toString(), Theme.class); theme.fetchLots(); return theme; } } catch (IOException ex) { ThemedLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex); return null; } return null; } public static Lot getLotByExactCornerCoordinate(int xcoord, int zcoord) { Corner lookup = new Corner(xcoord, zcoord); if (lotCache.containsKey(lookup)) { return lotCache.get(lookup); } try { Lot lot = ThemedBuildPlugin.getJsonMapper() .readValue( ThemedBuildPlugin.getMongoUtil().getLotCollection() .findOne(new BasicDBObject("corner", lookup.toDBObject())).toString(), Lot.class); lot.generateBounds(); lotCache.put(lot.getCorner(), lot); return lot; } catch (IOException ex) { ThemedLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex); } return null; } public static Lot getLotByCorner(Corner corner) { if (lotCache.containsKey(corner)) { return lotCache.get(corner); } try { Lot lot = ThemedBuildPlugin.getJsonMapper() .readValue( ThemedBuildPlugin.getMongoUtil().getLotCollection() .findOne(new BasicDBObject("corner", corner.toDBObject())).toString(), Lot.class); lot.generateBounds(); lotCache.put(lot.getCorner(), lot); return lot; } catch (IOException ex) { ThemedLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex); } return null; } public static Theme getThemeById(ObjectId id) { DBObject query = QueryBuilder.start("_id").is(id).get(); try { Theme theme = ThemedBuildPlugin.getJsonMapper().readValue( ThemedBuildPlugin.getMongoUtil().getThemeCollection().findOne(query).toString(), Theme.class); theme.fetchLots(); return theme; } catch (IOException ex) { ThemedLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex); } return null; } public static Theme getThemeByName(String name) { DBObject query = QueryBuilder.start("name").is(name).get(); try { DBCursor cursor = ThemedBuildPlugin.getMongoUtil().getThemeCollection().find(query); DBObject object = cursor.sort(new BasicDBObject("_id", -1)).limit(1).next(); Theme theme = ThemedBuildPlugin.getJsonMapper().readValue(object.toString(), Theme.class); theme.fetchLots(); return theme; } catch (IOException ex) { ThemedLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex); } return null; } public static Theme getCurrentTheme() { try { DBCursor cursor = ThemedBuildPlugin.getMongoUtil().getThemeCollection().find(); DBObject object = cursor.sort(new BasicDBObject("_id", -1)).limit(1).next(); Theme theme = ThemedBuildPlugin.getJsonMapper().readValue(object.toString(), Theme.class); theme.fetchLots(); return theme; } catch (IOException ex) { ThemedLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex); } return null; } public static Lot getLotById(ObjectId id) { try { DBObject result = ThemedBuildPlugin.getMongoUtil().getLotCollection() .findOne(new BasicDBObject("_id", id)); if (result == null) { return null; } Lot lot = ThemedBuildPlugin.getJsonMapper().readValue(result.toString(), Lot.class); lot.generateBounds(); return lot; } catch (IOException ex) { ThemedLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex); } return null; } public static Lot getNextLot(Player owner) { Theme current = ThemedBuildPlugin.getCurrentTheme(); int xindex = 0; if (current.getNumlots() > 0) { xindex = current.getLotsize() * current.getNumlots() + (current.getNumlots() * 6); } Corner corner = new Corner(xindex, current.getCorner().getZ()); Lot lot = new Lot(owner, current.getLotsize(), corner); lot.generateBounds(); return lot; } public static Lot getLot(String playerUUID) { DBObject query = QueryBuilder.start("owner").is(playerUUID).get(); try { DBCursor cursor = ThemedBuildPlugin.getMongoUtil().getLotCollection().find(query); DBObject object = cursor.sort(new BasicDBObject("_id", -1)).limit(1).next(); Lot lot = ThemedBuildPlugin.getJsonMapper().readValue(object.toString(), Lot.class); lot.generateBounds(); lotCache.put(lot.getCorner(), lot); return lot; } catch (IOException ex) { ThemedLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex); } return null; } public static Lot getLot(String playerUUID, ObjectId themeId) { DBObject query = QueryBuilder.start("owner").is(playerUUID).and("belongsToTheme").is(themeId).get(); try { DBCursor cursor = ThemedBuildPlugin.getMongoUtil().getLotCollection().find(query); DBObject object = cursor.sort(new BasicDBObject("_id", -1)).limit(1).next(); Lot lot = ThemedBuildPlugin.getJsonMapper().readValue(object.toString(), Lot.class); lot.generateBounds(); lotCache.put(lot.getCorner(), lot); return lot; } catch (IOException ex) { ThemedLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex); } return null; } public static void storeUpdatedTheme(Theme toStore) { DBCollection themecollection = ThemedBuildPlugin.getMongoUtil().getThemeCollection(); themecollection.update(new BasicDBObject("_id", toStore.get_id()), toStore.toDBObject(), true, false); } public static void storeLot(Lot toStore) { DBCollection lotcollection = ThemedBuildPlugin.getMongoUtil().getLotCollection(); lotcollection.update(new BasicDBObject("_id", toStore.get_id()), toStore.toDBObject(), true, false); DBCollection playercollection = ThemedBuildPlugin.getMongoUtil().getPlayerCollection(); playercollection.update(new BasicDBObject("uuid", toStore.getOwner().getUniqueId().toString()), new BasicDBObject("$push", new BasicDBObject("lots", toStore.get_id())), true, false); } public static PlayerReport getPlayerReport(OfflinePlayer p) { DBObject query = QueryBuilder.start("uuid").is(p.getUniqueId().toString()).get(); try { DBCursor cursor = ThemedBuildPlugin.getMongoUtil().getPlayerCollection().find(query); DBObject object = cursor.sort(new BasicDBObject("_id", -1)).limit(1).next(); PlayerDocument doc = ThemedBuildPlugin.getJsonMapper().readValue(object.toString(), PlayerDocument.class); PlayerReport report = new PlayerReport(p, doc.getLots(), doc.getLots().get(doc.getLots().size() - 1)); return report; } catch (IOException ex) { ThemedLogger.getLog().log(Level.SEVERE, ex.getMessage(), ex); } return null; } }