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.plot;
020
021import com.google.common.collect.ImmutableList;
022import com.plotsquared.core.location.BlockLoc;
023import com.plotsquared.core.location.Direction;
024import com.plotsquared.core.plot.comment.PlotComment;
025
026import java.util.ArrayList;
027import java.util.Collections;
028import java.util.HashMap;
029import java.util.List;
030import java.util.Map;
031import java.util.UUID;
032
033/**
034 * Generic settings class.
035 * - Does not keep a reference to a parent class
036 * - Direct changes here will not occur in the db (Use the parent plot object for that)
037 */
038public class PlotSettings {
039
040    /**
041     * Merged plots.
042     */
043    private boolean[] merged = new boolean[]{false, false, false, false};
044    /**
045     * Plot alias.
046     */
047    private String alias = "";
048    /**
049     * The ratings for a plot.
050     */
051    private HashMap<UUID, Integer> ratings;
052    /**
053     * Plot comments.
054     */
055    private List<PlotComment> comments = null;
056    /**
057     * Home Position.
058     */
059    private BlockLoc position;
060
061    /**
062     * <b>Check if the plot is merged in a direction</b><br> 0 = North<br> 1 = East<br> 2 = South<br> 3 = West<br>
063     *
064     * @param direction Direction to check
065     * @return boolean merged
066     */
067    public boolean getMerged(int direction) {
068        return this.merged[direction];
069    }
070
071    public Map<UUID, Integer> getRatings() {
072        if (this.ratings == null) {
073            this.ratings = new HashMap<>();
074        }
075        return this.ratings;
076    }
077
078    public void setRatings(HashMap<UUID, Integer> ratings) {
079        this.ratings = ratings;
080    }
081
082    public boolean setMerged(int direction, boolean merged) {
083        if (this.merged[direction] != merged) {
084            this.merged[direction] = merged;
085            return true;
086        }
087        return false;
088    }
089
090    public boolean setMerged(Direction direction, boolean merged) {
091        if (Direction.ALL == direction) {
092            throw new IllegalArgumentException("You cannot use Direction.ALL in this method!");
093        }
094        if (this.merged[direction.getIndex()] != merged) {
095            this.merged[direction.getIndex()] = merged;
096            return true;
097        }
098        return false;
099    }
100
101    public BlockLoc getPosition() {
102        if (this.position == null) {
103            return BlockLoc.MINY;
104        }
105        return this.position;
106    }
107
108    public void setPosition(BlockLoc position) {
109        if (position != null && position.getX() == 0 && position.getY() == 0
110                && position.getZ() == 0) {
111            position = null;
112        }
113        this.position = position;
114    }
115
116    @SuppressWarnings({"UnstableApiUsage"})
117    public List<PlotComment> getComments(String inbox) {
118        if (this.comments == null) {
119            return Collections.emptyList();
120        }
121
122        return this.comments.stream().filter(comment -> comment.inbox.equals(inbox))
123                .collect(ImmutableList.toImmutableList());
124    }
125
126    boolean removeComment(PlotComment comment) {
127        if (this.comments == null) {
128            return false;
129        }
130        return this.comments.remove(comment);
131    }
132
133    void removeComments(List<PlotComment> comments) {
134        comments.forEach(this::removeComment);
135    }
136
137    void addComment(PlotComment comment) {
138        if (this.comments == null) {
139            this.comments = new ArrayList<>();
140        }
141        this.comments.add(comment);
142    }
143
144    public boolean[] getMerged() {
145        return this.merged;
146    }
147
148    public void setMerged(boolean[] merged) {
149        this.merged = merged;
150    }
151
152    public String getAlias() {
153        return this.alias;
154    }
155
156    public void setAlias(String alias) {
157        this.alias = alias;
158    }
159
160    public void setComments(List<PlotComment> comments) {
161        this.comments = comments;
162    }
163
164}