001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.database;
020
021import com.plotsquared.core.plot.Plot;
022import com.plotsquared.core.plot.PlotArea;
023import com.plotsquared.core.plot.PlotCluster;
024import com.plotsquared.core.plot.PlotId;
025import com.plotsquared.core.plot.comment.PlotComment;
026import com.plotsquared.core.plot.flag.PlotFlag;
027import com.plotsquared.core.util.task.RunnableVal;
028
029import java.sql.ResultSet;
030import java.sql.ResultSetMetaData;
031import java.sql.SQLException;
032import java.util.HashMap;
033import java.util.List;
034import java.util.Map;
035import java.util.Set;
036import java.util.UUID;
037import java.util.concurrent.CompletableFuture;
038
039/**
040 * Database Functions
041 * - These functions do not update the local plot objects and only make changes to the DB
042 */
043public class DBFunc {
044
045    /**
046     * The "global" uuid.
047     */
048    // TODO: Use this instead. public static final UUID EVERYONE = UUID.fromString("4aa2aaa4-c06b-485c-bc58-186aa1780d9b");
049    public static final UUID EVERYONE = UUID.fromString("1-1-3-3-7");
050    public static final UUID SERVER = UUID.fromString("00000000-0000-0000-0000-000000000000");
051
052    /**
053     * Abstract Database Manager
054     */
055    public static AbstractDB dbManager;
056
057    public static void updateTables(int[] oldVersion) {
058        if (dbManager != null) {
059            dbManager.updateTables(oldVersion);
060        }
061    }
062
063    public static void addPersistentMeta(UUID uuid, String key, byte[] meta, boolean delete) {
064        if (dbManager != null) {
065            dbManager.addPersistentMeta(uuid, key, meta, delete);
066        }
067    }
068
069    public static void getPersistentMeta(UUID uuid, RunnableVal<Map<String, byte[]>> result) {
070        if (dbManager != null) {
071            dbManager.getPersistentMeta(uuid, result);
072        }
073    }
074
075    public static void removePersistentMeta(UUID uuid, String key) {
076        if (dbManager != null) {
077            dbManager.removePersistentMeta(uuid, key);
078        }
079    }
080
081    public static CompletableFuture<Boolean> swapPlots(Plot plot1, Plot plot2) {
082        if (dbManager != null) {
083            return dbManager.swapPlots(plot1, plot2);
084        }
085        return CompletableFuture.completedFuture(false);
086    }
087
088    public static boolean deleteTables() {
089        return dbManager != null && dbManager.deleteTables();
090    }
091
092    public static void movePlot(Plot originalPlot, Plot newPlot) {
093        if (originalPlot.temp == -1 || newPlot.temp == -1) {
094            return;
095        }
096        DBFunc.dbManager.movePlot(originalPlot, newPlot);
097    }
098
099    public static void validatePlots(Set<Plot> plots) {
100        if (dbManager == null) {
101            return;
102        }
103        DBFunc.dbManager.validateAllPlots(plots);
104    }
105
106
107    //TODO Consider Removal
108
109    /**
110     * Check if a {@link ResultSet} contains a column.
111     *
112     * @param resultSet
113     * @param name
114     * @return
115     */
116    @Deprecated
117    public static boolean hasColumn(ResultSet resultSet, String name) {
118        try {
119            ResultSetMetaData meta = resultSet.getMetaData();
120            int count = meta.getColumnCount();
121            for (int x = 1; x <= count; x++) {
122                if (name.equals(meta.getColumnName(x))) {
123                    return true;
124                }
125            }
126            return false;
127        } catch (SQLException ignored) {
128            return false;
129        }
130    }
131
132    /**
133     * Set the owner of a plot
134     *
135     * @param plot Plot Object
136     * @param uuid New Owner
137     */
138    public static void setOwner(Plot plot, UUID uuid) {
139        if (plot.temp == -1 || dbManager == null) {
140            return;
141        }
142        DBFunc.dbManager.setOwner(plot, uuid);
143    }
144
145    /**
146     * Create all settings + (trusted, denied, members)
147     *
148     * @param plots List containing all plot objects
149     */
150    public static void createPlotsAndData(List<Plot> plots, Runnable whenDone) {
151        if (dbManager == null) {
152            return;
153        }
154        DBFunc.dbManager.createPlotsAndData(plots, whenDone);
155    }
156
157    public static void createPlotSafe(
158            final Plot plot, final Runnable success,
159            final Runnable failure
160    ) {
161        if (dbManager == null) {
162            return;
163        }
164        DBFunc.dbManager.createPlotSafe(plot, success, failure);
165    }
166
167    /**
168     * Create a plot.
169     *
170     * @param plot Plot to create
171     */
172    public static void createPlotAndSettings(Plot plot, Runnable whenDone) {
173        if (plot.temp == -1 || dbManager == null) {
174            return;
175        }
176        DBFunc.dbManager.createPlotAndSettings(plot, whenDone);
177    }
178
179    /**
180     * Create tables.
181     *
182     * @throws Exception
183     */
184    public static void createTables() throws Exception {
185        if (dbManager == null) {
186            return;
187        }
188        DBFunc.dbManager.createTables();
189    }
190
191    /**
192     * Delete a plot.
193     *
194     * @param plot Plot to delete
195     */
196    public static void delete(Plot plot) {
197        if (plot.temp == -1 || dbManager == null) {
198            return;
199        }
200        DBFunc.dbManager.delete(plot);
201        plot.temp = -1;
202    }
203
204    /**
205     * Delete the ratings for a plot.
206     *
207     * @param plot
208     */
209    public static void deleteRatings(Plot plot) {
210        if (plot.temp == -1 || dbManager == null) {
211            return;
212        }
213        DBFunc.dbManager.deleteRatings(plot);
214    }
215
216    /**
217     * Delete the trusted list for a plot.
218     *
219     * @param plot
220     */
221    public static void deleteTrusted(Plot plot) {
222        if (plot.temp == -1 || dbManager == null) {
223            return;
224        }
225        DBFunc.dbManager.deleteHelpers(plot);
226    }
227
228    /**
229     * Delete the members list for a plot.
230     *
231     * @param plot
232     */
233    public static void deleteMembers(Plot plot) {
234        if (plot.temp == -1 || dbManager == null) {
235            return;
236        }
237        DBFunc.dbManager.deleteTrusted(plot);
238    }
239
240    /**
241     * Delete the denied list for a plot.
242     *
243     * @param plot
244     */
245    public static void deleteDenied(Plot plot) {
246        if (plot.temp == -1 || dbManager == null) {
247            return;
248        }
249        DBFunc.dbManager.deleteDenied(plot);
250    }
251
252    /**
253     * Delete the comments in a plot.
254     *
255     * @param plot
256     */
257    public static void deleteComments(Plot plot) {
258        if (plot.temp == -1 || dbManager == null) {
259            return;
260        }
261        DBFunc.dbManager.deleteComments(plot);
262    }
263
264    /**
265     * Deleting settings will
266     * 1) Delete any settings (flags and such) associated with the plot
267     * 2) Prevent any local changes to the plot from saving properly to the db
268     * <p>
269     * This shouldn't ever be needed
270     *
271     * @param plot
272     */
273    public static void deleteSettings(Plot plot) {
274        if (plot.temp == -1 || dbManager == null) {
275            return;
276        }
277        DBFunc.dbManager.deleteSettings(plot);
278    }
279
280    public static void delete(PlotCluster toDelete) {
281        if (dbManager == null) {
282            return;
283        }
284        DBFunc.dbManager.delete(toDelete);
285    }
286
287    /**
288     * Create plot settings.
289     *
290     * @param id   Plot ID
291     * @param plot Plot Object
292     */
293    public static void createPlotSettings(int id, Plot plot) {
294        if (plot.temp == -1 || dbManager == null) {
295            return;
296        }
297        DBFunc.dbManager.createPlotSettings(id, plot);
298    }
299
300    /**
301     * Get a plot id.
302     *
303     * @param plot Plot Object
304     * @return ID
305     */
306    public static int getId(Plot plot) {
307        if (dbManager == null) {
308            return 0;
309        }
310        return DBFunc.dbManager.getId(plot);
311    }
312
313    /**
314     * @return Plots
315     */
316    public static HashMap<String, HashMap<PlotId, Plot>> getPlots() {
317        if (dbManager == null) {
318            return new HashMap<>();
319        }
320        return DBFunc.dbManager.getPlots();
321    }
322
323    public static void setMerged(Plot plot, boolean[] merged) {
324        if (plot.temp == -1 || dbManager == null) {
325            return;
326        }
327        DBFunc.dbManager.setMerged(plot, merged);
328    }
329
330    public static void setFlag(Plot plot, PlotFlag<?, ?> flag) {
331        if (plot.temp == -1 || dbManager == null) {
332            return;
333        }
334        DBFunc.dbManager.setFlag(plot, flag);
335    }
336
337    public static void removeFlag(Plot plot, PlotFlag<?, ?> flag) {
338        if (plot.temp == -1 || dbManager == null) {
339            return;
340        }
341        DBFunc.dbManager.removeFlag(plot, flag);
342    }
343
344    /**
345     * @param plot
346     * @param alias
347     */
348    public static void setAlias(Plot plot, String alias) {
349        if (plot.temp == -1 || dbManager == null) {
350            return;
351        }
352        DBFunc.dbManager.setAlias(plot, alias);
353    }
354
355    public static void purgeIds(Set<Integer> uniqueIds) {
356        if (dbManager == null) {
357            return;
358        }
359        DBFunc.dbManager.purgeIds(uniqueIds);
360    }
361
362    public static void purge(PlotArea area, Set<PlotId> plotIds) {
363        if (dbManager == null) {
364            return;
365        }
366        DBFunc.dbManager.purge(area, plotIds);
367    }
368
369    /**
370     * @param plot
371     * @param position
372     */
373    public static void setPosition(Plot plot, String position) {
374        if (plot.temp == -1 || dbManager == null) {
375            return;
376        }
377        DBFunc.dbManager.setPosition(plot, position);
378    }
379
380    /**
381     * @param plot
382     * @param comment
383     */
384    public static void removeComment(Plot plot, PlotComment comment) {
385        if (plot.temp == -1 || dbManager == null) {
386            return;
387        }
388        DBFunc.dbManager.removeComment(plot, comment);
389    }
390
391    public static void clearInbox(Plot plot, String inbox) {
392        if (plot != null && plot.temp == -1 || dbManager == null) {
393            return;
394        }
395        DBFunc.dbManager.clearInbox(plot, inbox);
396    }
397
398    /**
399     * @param plot
400     * @param comment
401     */
402    public static void setComment(Plot plot, PlotComment comment) {
403        if (plot != null && plot.temp == -1 || dbManager == null) {
404            return;
405        }
406        DBFunc.dbManager.setComment(plot, comment);
407    }
408
409    /**
410     * @param plot
411     */
412    public static void getComments(
413            Plot plot, String inbox,
414            RunnableVal<List<PlotComment>> whenDone
415    ) {
416        if (plot != null && plot.temp == -1 || dbManager == null) {
417            return;
418        }
419        DBFunc.dbManager.getComments(plot, inbox, whenDone);
420    }
421
422    /**
423     * @param plot
424     * @param uuid
425     */
426    public static void removeTrusted(Plot plot, UUID uuid) {
427        if (plot.temp == -1 || dbManager == null) {
428            return;
429        }
430        DBFunc.dbManager.removeTrusted(plot, uuid);
431    }
432
433    /**
434     * @param cluster
435     * @param uuid
436     */
437    public static void removeHelper(PlotCluster cluster, UUID uuid) {
438        if (dbManager == null) {
439            return;
440        }
441        DBFunc.dbManager.removeHelper(cluster, uuid);
442    }
443
444    /**
445     * @param cluster
446     */
447    public static void createCluster(PlotCluster cluster) {
448        if (dbManager == null) {
449            return;
450        }
451        DBFunc.dbManager.createCluster(cluster);
452    }
453
454    /**
455     * @param current
456     * @param min
457     * @param max
458     */
459    public static void resizeCluster(PlotCluster current, PlotId min, PlotId max) {
460        if (dbManager == null) {
461            return;
462        }
463        DBFunc.dbManager.resizeCluster(current, min, max);
464    }
465
466    /**
467     * @param plot
468     * @param uuid
469     */
470    public static void removeMember(Plot plot, UUID uuid) {
471        if (plot.temp == -1 || dbManager == null) {
472            return;
473        }
474        DBFunc.dbManager.removeMember(plot, uuid);
475    }
476
477    /**
478     * @param cluster
479     * @param uuid
480     */
481    public static void removeInvited(PlotCluster cluster, UUID uuid) {
482        if (dbManager == null) {
483            return;
484        }
485        DBFunc.dbManager.removeInvited(cluster, uuid);
486    }
487
488    /**
489     * @param plot
490     * @param uuid
491     */
492    public static void setTrusted(Plot plot, UUID uuid) {
493        if (plot.temp == -1 || dbManager == null) {
494            return;
495        }
496        DBFunc.dbManager.setTrusted(plot, uuid);
497    }
498
499    public static void setHelper(PlotCluster cluster, UUID uuid) {
500        if (dbManager == null) {
501            return;
502        }
503        DBFunc.dbManager.setHelper(cluster, uuid);
504    }
505
506    /**
507     * @param plot
508     * @param uuid
509     */
510    public static void setMember(Plot plot, UUID uuid) {
511        if (plot.temp == -1 || dbManager == null) {
512            return;
513        }
514        DBFunc.dbManager.setMember(plot, uuid);
515    }
516
517    public static void setInvited(PlotCluster cluster, UUID uuid) {
518        if (dbManager == null) {
519            return;
520        }
521        DBFunc.dbManager.setInvited(cluster, uuid);
522    }
523
524    /**
525     * @param plot
526     * @param uuid
527     */
528    public static void removeDenied(Plot plot, UUID uuid) {
529        if (plot.temp == -1 || dbManager == null) {
530            return;
531        }
532        DBFunc.dbManager.removeDenied(plot, uuid);
533    }
534
535    /**
536     * @param plot
537     * @param uuid
538     */
539    public static void setDenied(Plot plot, UUID uuid) {
540        if (plot.temp == -1 || dbManager == null) {
541            return;
542        }
543        DBFunc.dbManager.setDenied(plot, uuid);
544    }
545
546    public static HashMap<UUID, Integer> getRatings(Plot plot) {
547        if (plot.temp == -1 || dbManager == null) {
548            return new HashMap<>(0);
549        }
550        return DBFunc.dbManager.getRatings(plot);
551    }
552
553    public static void setRating(Plot plot, UUID rater, int value) {
554        if (plot.temp == -1 || dbManager == null) {
555            return;
556        }
557        DBFunc.dbManager.setRating(plot, rater, value);
558    }
559
560    public static HashMap<String, Set<PlotCluster>> getClusters() {
561        if (dbManager == null) {
562            return new HashMap<>();
563        }
564        return DBFunc.dbManager.getClusters();
565    }
566
567    public static void setPosition(PlotCluster cluster, String position) {
568        if (dbManager == null) {
569            return;
570        }
571        DBFunc.dbManager.setPosition(cluster, position);
572    }
573
574    public static void replaceWorld(String oldWorld, String newWorld, PlotId min, PlotId max) {
575        if (dbManager == null) {
576            return;
577        }
578        DBFunc.dbManager.replaceWorld(oldWorld, newWorld, min, max);
579    }
580
581    /**
582     * Replace all occurrences of a uuid in the database with another one
583     *
584     * @param old
585     * @param now
586     */
587    public static void replaceUUID(UUID old, UUID now) {
588        if (dbManager == null) {
589            return;
590        }
591        DBFunc.dbManager.replaceUUID(old, now);
592    }
593
594    public static void close() {
595        if (dbManager != null) {
596            DBFunc.dbManager.close();
597        }
598    }
599
600}