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    /**
083     * @deprecated Unused internally. Scheduled for removal in next major release. Use {@link PlotSettings#setMerged(Direction, boolean)}
084     */
085    @Deprecated(forRemoval = true, since = "6.11.1")
086    public boolean setMerged(int direction, boolean merged) {
087        if (this.merged[direction] != merged) {
088            this.merged[direction] = merged;
089            return true;
090        }
091        return false;
092    }
093
094    public boolean setMerged(Direction direction, boolean merged) {
095        if (Direction.ALL == direction) {
096            throw new IllegalArgumentException("You cannot use Direction.ALL in this method!");
097        }
098        if (this.merged[direction.getIndex()] != merged) {
099            this.merged[direction.getIndex()] = merged;
100            return true;
101        }
102        return false;
103    }
104
105    public BlockLoc getPosition() {
106        if (this.position == null) {
107            return BlockLoc.MINY;
108        }
109        return this.position;
110    }
111
112    public void setPosition(BlockLoc position) {
113        if (position != null && position.getX() == 0 && position.getY() == 0
114                && position.getZ() == 0) {
115            position = null;
116        }
117        this.position = position;
118    }
119
120    @SuppressWarnings({"UnstableApiUsage"})
121    public List<PlotComment> getComments(String inbox) {
122        if (this.comments == null) {
123            return Collections.emptyList();
124        }
125
126        return this.comments.stream().filter(comment -> comment.inbox.equals(inbox))
127                .collect(ImmutableList.toImmutableList());
128    }
129
130    boolean removeComment(PlotComment comment) {
131        if (this.comments == null) {
132            return false;
133        }
134        return this.comments.remove(comment);
135    }
136
137    void removeComments(List<PlotComment> comments) {
138        comments.forEach(this::removeComment);
139    }
140
141    void addComment(PlotComment comment) {
142        if (this.comments == null) {
143            this.comments = new ArrayList<>();
144        }
145        this.comments.add(comment);
146    }
147
148    public boolean[] getMerged() {
149        return this.merged;
150    }
151
152    public void setMerged(boolean[] merged) {
153        this.merged = merged;
154    }
155
156    public String getAlias() {
157        return this.alias;
158    }
159
160    public void setAlias(String alias) {
161        this.alias = alias;
162    }
163
164    public void setComments(List<PlotComment> comments) {
165        this.comments = comments;
166    }
167
168}