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