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.plotsquared.core.player.MetaDataAccess;
022import com.plotsquared.core.player.PlayerMetaDataKeys;
023import com.plotsquared.core.player.PlotPlayer;
024import com.plotsquared.core.util.InventoryUtil;
025import org.checkerframework.checker.nullness.qual.NonNull;
026
027public class PlotInventory {
028
029    private final PlotPlayer<?> player;
030    private final int lines;
031    private final PlotItemStack[] items;
032    private final InventoryUtil inventoryUtil;
033    private String title;
034    private boolean open = false;
035
036    public PlotInventory(
037            final @NonNull InventoryUtil inventoryUtil,
038            PlotPlayer<?> player, int lines, String name
039    ) {
040        this.lines = lines;
041        this.title = name == null ? "" : name;
042        this.player = player;
043        this.items = new PlotItemStack[lines * 9];
044        this.inventoryUtil = inventoryUtil;
045    }
046
047    public static boolean hasPlotInventoryOpen(final @NonNull PlotPlayer<?> plotPlayer) {
048        return getOpenPlotInventory(plotPlayer) != null;
049    }
050
051    public static PlotInventory getOpenPlotInventory(final @NonNull PlotPlayer<?> plotPlayer) {
052        try (final MetaDataAccess<PlotInventory> inventoryAccess = plotPlayer.accessTemporaryMetaData(
053                PlayerMetaDataKeys.TEMPORARY_INVENTORY)) {
054            return inventoryAccess.get().orElse(null);
055        }
056    }
057
058    public static void setPlotInventoryOpen(
059            final @NonNull PlotPlayer<?> plotPlayer,
060            final @NonNull PlotInventory plotInventory
061    ) {
062        try (final MetaDataAccess<PlotInventory> inventoryAccess = plotPlayer.accessTemporaryMetaData(
063                PlayerMetaDataKeys.TEMPORARY_INVENTORY)) {
064            inventoryAccess.set(plotInventory);
065        }
066    }
067
068    public static void removePlotInventoryOpen(final @NonNull PlotPlayer<?> plotPlayer) {
069        try (final MetaDataAccess<PlotInventory> inventoryAccess = plotPlayer.accessTemporaryMetaData(
070                PlayerMetaDataKeys.TEMPORARY_INVENTORY)) {
071            inventoryAccess.remove();
072        }
073    }
074
075    public boolean onClick(int index) {
076        return true;
077    }
078
079    public void openInventory() {
080        if (this.title == null) {
081            return;
082        }
083        if (!hasPlotInventoryOpen(getPlayer())) {
084            this.open = true;
085            setPlotInventoryOpen(getPlayer(), this);
086            this.inventoryUtil.open(this);
087        }
088    }
089
090    public void close() {
091        if (this.title == null) {
092            return;
093        }
094        removePlotInventoryOpen(getPlayer());
095        this.inventoryUtil.close(this);
096        this.open = false;
097    }
098
099    /**
100     * Put an item into this inventory
101     *
102     * @param index the index (= slot) where to place the item
103     * @param item  the item to place
104     * @see #setItemChecked(int, PlotItemStack)
105     */
106    public void setItem(int index, PlotItemStack item) {
107        setItemChecked(index, item);
108    }
109
110    /**
111     * Put an item into this inventory, while also checking the existence of the material in the current version
112     *
113     * @param index the index (= slot) where to place the item
114     * @param item  the item to place
115     * @return {@code true} if the item could be placed, otherwise {@code false}
116     * @see InventoryUtil#setItemChecked(PlotInventory, int, PlotItemStack)
117     * @since 6.5.0
118     */
119    public boolean setItemChecked(int index, PlotItemStack item) {
120        if (!this.inventoryUtil.setItemChecked(this, index, item)) {
121            return false;
122        }
123        this.items[index] = item;
124        return true;
125    }
126
127    public PlotItemStack getItem(int index) {
128        if ((index < 0) || (index >= this.items.length)) {
129            return null;
130        }
131        return this.items[index];
132    }
133
134    public PlotItemStack[] getItems() {
135        return this.items;
136    }
137
138    public String getTitle() {
139        return this.title;
140    }
141
142    public void setTitle(String title) {
143        if (title == null) {
144            return;
145        }
146        boolean tmp = this.open;
147        close();
148        this.title = title;
149        if (tmp) {
150            openInventory();
151        }
152    }
153
154    public boolean isOpen() {
155        return this.open;
156    }
157
158    public PlotPlayer<?> getPlayer() {
159        return player;
160    }
161
162    public int getLines() {
163        return lines;
164    }
165
166}