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