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 private final InventoryUtil inventoryUtil; 064 private final EventDispatcher eventDispatcher; 065 066 @Inject 067 public Music(final @Nullable InventoryUtil inventoryUtil, final @NonNull EventDispatcher eventDispatcher) { 068 this.inventoryUtil = inventoryUtil; 069 this.eventDispatcher = eventDispatcher; 070 } 071 072 @Override 073 public boolean onCommand(PlotPlayer<?> player, String[] args) { 074 Location location = player.getLocation(); 075 final Plot plot = location.getPlotAbs(); 076 if (plot == null) { 077 player.sendMessage(TranslatableCaption.of("errors.not_in_plot")); 078 return false; 079 } 080 if (!plot.hasOwner()) { 081 player.sendMessage(TranslatableCaption.of("info.plot_unowned")); 082 return false; 083 } 084 if (!plot.isAdded(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN_MUSIC_OTHER)) { 085 player.sendMessage( 086 TranslatableCaption.of("permission.no_permission"), 087 TagResolver.resolver( 088 "node", 089 Tag.inserting(Permission.PERMISSION_ADMIN_MUSIC_OTHER) 090 ) 091 ); 092 return true; 093 } 094 PlotInventory inv = new PlotInventory( 095 this.inventoryUtil, 096 player, 097 2, 098 TranslatableCaption.of("plotjukebox.jukebox_header").getComponent(player) 099 ) { 100 @Override 101 public boolean onClick(int index) { 102 PlotItemStack item = getItem(index); 103 if (item == null) { 104 return true; 105 } 106 if (item.getType() == ItemTypes.BEDROCK) { 107 PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class) 108 .createFlagInstance(item.getType()); 109 PlotFlagRemoveEvent event = eventDispatcher.callFlagRemove(plotFlag, plot); 110 if (event.getEventResult() == Result.DENY) { 111 getPlayer().sendMessage( 112 TranslatableCaption.of("events.event_denied"), 113 TagResolver.resolver("value", Tag.inserting(Component.text("Music removal"))) 114 ); 115 return true; 116 } 117 plot.removeFlag(event.getFlag()); 118 getPlayer().sendMessage( 119 TranslatableCaption.of("flag.flag_removed"), 120 TagResolver.builder() 121 .tag("flag", Tag.inserting(Component.text("music"))) 122 .tag("value", Tag.inserting(Component.text("music_disc"))) 123 .build() 124 ); 125 } else if (item.getName().toLowerCase(Locale.ENGLISH).contains("disc")) { 126 PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class) 127 .createFlagInstance(item.getType()); 128 PlotFlagAddEvent event = eventDispatcher.callFlagAdd(plotFlag, plot); 129 if (event.getEventResult() == Result.DENY) { 130 getPlayer().sendMessage( 131 TranslatableCaption.of("events.event_denied"), 132 TagResolver.resolver("value", Tag.inserting(Component.text("Music addition"))) 133 ); 134 return true; 135 } 136 plot.setFlag(event.getFlag()); 137 getPlayer().sendMessage( 138 TranslatableCaption.of("flag.flag_added"), 139 TagResolver.builder() 140 .tag("flag", Tag.inserting(Component.text("music"))) 141 .tag("value", Tag.inserting(Component.text(event.getFlag().getValue().toString()))) 142 .build() 143 ); 144 } else { 145 getPlayer().sendMessage(TranslatableCaption.of("flag.flag_not_added")); 146 } 147 return false; 148 } 149 }; 150 int index = 0; 151 152 for (final String disc : DISCS) { 153 final String name = String.format("<gold>%s</gold>", disc); 154 final String[] lore = {TranslatableCaption.of("plotjukebox.click_to_play").getComponent(player)}; 155 ItemType type = ItemTypes.get(disc); 156 if (type == null) { 157 continue; 158 } 159 final PlotItemStack item = new PlotItemStack(type, 1, name, lore); 160 if (inv.setItemChecked(index, item)) { 161 index++; 162 } 163 } 164 165 // Always add the cancel button 166 // if (player.getMeta("music") != null) { 167 String name = TranslatableCaption.of("plotjukebox.cancel_music").getComponent(player); 168 String[] lore = {TranslatableCaption.of("plotjukebox.reset_music").getComponent(player)}; 169 inv.setItem(index, new PlotItemStack("bedrock", 1, name, lore)); 170 // } 171 172 inv.openInventory(); 173 return true; 174 } 175 176}