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