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.command;
020
021import com.google.inject.Inject;
022import com.plotsquared.core.configuration.caption.TranslatableCaption;
023import com.plotsquared.core.events.PlotFlagAddEvent;
024import com.plotsquared.core.events.PlotFlagRemoveEvent;
025import com.plotsquared.core.events.Result;
026import com.plotsquared.core.permissions.Permission;
027import com.plotsquared.core.player.PlotPlayer;
028import com.plotsquared.core.plot.Plot;
029import com.plotsquared.core.plot.PlotInventory;
030import com.plotsquared.core.plot.PlotItemStack;
031import com.plotsquared.core.plot.flag.PlotFlag;
032import com.plotsquared.core.plot.flag.implementations.MusicFlag;
033import com.plotsquared.core.util.EventDispatcher;
034import com.plotsquared.core.util.InventoryUtil;
035import com.sk89q.worldedit.world.item.ItemType;
036import com.sk89q.worldedit.world.item.ItemTypes;
037import net.kyori.adventure.text.Component;
038import net.kyori.adventure.text.minimessage.tag.Tag;
039import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
040import org.checkerframework.checker.nullness.qual.NonNull;
041
042import javax.annotation.Nullable;
043import java.util.Arrays;
044import java.util.Collection;
045import java.util.Locale;
046
047@CommandDeclaration(command = "music",
048        permission = "plots.music",
049        usage = "/plot music",
050        category = CommandCategory.APPEARANCE,
051        requiredType = RequiredType.PLAYER)
052public class Music extends SubCommand {
053
054    private static final Collection<String> DISCS = Arrays
055            .asList("music_disc_13", "music_disc_cat", "music_disc_blocks", "music_disc_chirp",
056                    "music_disc_far", "music_disc_mall", "music_disc_mellohi", "music_disc_stal",
057                    "music_disc_strad", "music_disc_ward", "music_disc_11", "music_disc_wait", "music_disc_otherside",
058                    "music_disc_pigstep", "music_disc_5", "music_disc_relic", "music_disc_creator",
059                    "music_disc_creator_music_box", "music_disc_precipice", "music_disc_tears", "music_disc_lava_chicken"
060            );
061
062    // make sure all discs and the bedrock ("cancel") fit into the inventory
063    private static final int INVENTORY_ROWS = (int) Math.ceil((DISCS.size() + 1) / 9.0);
064
065    private final InventoryUtil inventoryUtil;
066    private final EventDispatcher eventDispatcher;
067
068    @Inject
069    public Music(final @Nullable InventoryUtil inventoryUtil, final @NonNull EventDispatcher eventDispatcher) {
070        this.inventoryUtil = inventoryUtil;
071        this.eventDispatcher = eventDispatcher;
072    }
073
074    @Override
075    public boolean onCommand(PlotPlayer<?> player, String[] args) {
076        final Plot plot = player.getCurrentPlot();
077        if (plot == null) {
078            player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
079            return false;
080        }
081        if (!plot.hasOwner()) {
082            player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
083            return false;
084        }
085        if (!plot.isAdded(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN_MUSIC_OTHER)) {
086            player.sendMessage(
087                    TranslatableCaption.of("permission.no_permission"),
088                    TagResolver.resolver(
089                            "node",
090                            Tag.inserting(Permission.PERMISSION_ADMIN_MUSIC_OTHER)
091                    )
092            );
093            return true;
094        }
095        PlotInventory inv = new PlotInventory(
096                this.inventoryUtil,
097                player,
098                INVENTORY_ROWS,
099                TranslatableCaption.of("plotjukebox.jukebox_header").getComponent(player)
100        ) {
101            @Override
102            public boolean onClick(int index) {
103                PlotItemStack item = getItem(index);
104                if (item == null) {
105                    return true;
106                }
107                if (item.getType() == ItemTypes.BEDROCK) {
108                    PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class)
109                            .createFlagInstance(item.getType());
110                    PlotFlagRemoveEvent event = eventDispatcher.callFlagRemove(plotFlag, plot);
111                    if (event.getEventResult() == Result.DENY) {
112                        getPlayer().sendMessage(
113                                TranslatableCaption.of("events.event_denied"),
114                                TagResolver.resolver("value", Tag.inserting(Component.text("Music removal")))
115                        );
116                        return true;
117                    }
118                    plot.removeFlag(event.getFlag());
119                    getPlayer().sendMessage(
120                            TranslatableCaption.of("flag.flag_removed"),
121                            TagResolver.builder()
122                                    .tag("flag", Tag.inserting(Component.text("music")))
123                                    .tag("value", Tag.inserting(Component.text("music_disc")))
124                                    .build()
125                    );
126                } else if (item.getName().toLowerCase(Locale.ENGLISH).contains("disc")) {
127                    PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class)
128                            .createFlagInstance(item.getType());
129                    PlotFlagAddEvent event = eventDispatcher.callFlagAdd(plotFlag, plot);
130                    if (event.getEventResult() == Result.DENY) {
131                        getPlayer().sendMessage(
132                                TranslatableCaption.of("events.event_denied"),
133                                TagResolver.resolver("value", Tag.inserting(Component.text("Music addition")))
134                        );
135                        return true;
136                    }
137                    plot.setFlag(event.getFlag());
138                    getPlayer().sendMessage(
139                            TranslatableCaption.of("flag.flag_added"),
140                            TagResolver.builder()
141                                    .tag("flag", Tag.inserting(Component.text("music")))
142                                    .tag("value", Tag.inserting(Component.text(event.getFlag().getValue().toString())))
143                                    .build()
144                    );
145                } else {
146                    getPlayer().sendMessage(TranslatableCaption.of("flag.flag_not_added"));
147                }
148                return false;
149            }
150        };
151        int index = 0;
152
153        for (final String disc : DISCS) {
154            final String name = String.format("<gold>%s</gold>", disc);
155            final String[] lore = {TranslatableCaption.of("plotjukebox.click_to_play").getComponent(player)};
156            ItemType type = ItemTypes.get(disc);
157            if (type == null) {
158                continue;
159            }
160            final PlotItemStack item = new PlotItemStack(type, 1, name, lore);
161            if (inv.setItemChecked(index, item)) {
162                index++;
163            }
164        }
165
166        // Always add the cancel button
167        // if (player.getMeta("music") != null) {
168        String name = TranslatableCaption.of("plotjukebox.cancel_music").getComponent(player);
169        String[] lore = {TranslatableCaption.of("plotjukebox.reset_music").getComponent(player)};
170        inv.setItem(index, new PlotItemStack("bedrock", 1, name, lore));
171        // }
172
173        inv.openInventory();
174        return true;
175    }
176
177}